From 99e30961efe6cb72ff93861a9f83b2bd85e27e22 Mon Sep 17 00:00:00 2001 From: "Lawrence, Rendall" Date: Wed, 30 Nov 2022 23:52:28 +0300 Subject: [PATCH] (minor) convert UDP private key to bytea --- frontend/udp/connection_id.go | 4 ++-- frontend/udp/frontend.go | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/frontend/udp/connection_id.go b/frontend/udp/connection_id.go index 006dbda..6892477 100644 --- a/frontend/udp/connection_id.go +++ b/frontend/udp/connection_id.go @@ -61,11 +61,11 @@ type ConnectionIDGenerator struct { } // NewConnectionIDGenerator creates a new connection ID generator. -func NewConnectionIDGenerator(key string, maxClockSkew time.Duration) *ConnectionIDGenerator { +func NewConnectionIDGenerator(key []byte, maxClockSkew time.Duration) *ConnectionIDGenerator { return &ConnectionIDGenerator{ mac: hmac.New(func() hash.Hash { return xxhash.New() - }, []byte(key)), + }, key), connID: make([]byte, connIDLen), buff: make([]byte, buffLen), scratch: make([]byte, scratchLen), diff --git a/frontend/udp/frontend.go b/frontend/udp/frontend.go index 43873fe..6ee2360 100644 --- a/frontend/udp/frontend.go +++ b/frontend/udp/frontend.go @@ -27,6 +27,7 @@ import ( const ( // Name - registered name of the frontend Name = "udp" + defaultKeyLen = 32 maxAllowedClockSkew = 30 * time.Second defaultMaxClockSkew = 10 * time.Second ) @@ -57,7 +58,7 @@ func (cfg Config) Validate() (validCfg Config) { // Generate a private key if one isn't provided by the user. if cfg.PrivateKey == "" { - pkeyRunes := make([]rune, 64) + pkeyRunes := make([]rune, defaultKeyLen) for i := range pkeyRunes { pkeyRunes[i] = allowedGeneratedPrivateKeyRunes[rand.Intn(len(allowedGeneratedPrivateKeyRunes))] } @@ -70,6 +71,7 @@ func (cfg Config) Validate() (validCfg Config) { Msg("falling back to default configuration") } + // ABS sb := cfg.MaxClockSkew >> 63 validCfg.MaxClockSkew = (cfg.MaxClockSkew ^ sb) + (sb & 1) @@ -108,6 +110,7 @@ func NewFrontend(c conf.MapConfig, logic *middleware.Logic) (frontend.Frontend, return nil, err } cfg = cfg.Validate() + pKey := []byte(cfg.PrivateKey) f := &udpFE{ sockets: make([]*net.UDPConn, cfg.Workers), @@ -117,7 +120,7 @@ func NewFrontend(c conf.MapConfig, logic *middleware.Logic) (frontend.Frontend, ParseOptions: cfg.ParseOptions, genPool: &sync.Pool{ New: func() any { - return NewConnectionIDGenerator(cfg.PrivateKey, cfg.MaxClockSkew) + return NewConnectionIDGenerator(pKey, cfg.MaxClockSkew) }, }, }