frontend/udp: generate private key when empty

This commit is contained in:
Jimmy Zelinskie
2016-11-28 19:49:53 -05:00
parent a48b9a50c3
commit 6deebdd6d4
2 changed files with 15 additions and 1 deletions
+15
View File
@@ -6,6 +6,7 @@ import (
"bytes"
"context"
"encoding/binary"
"math/rand"
"net"
"sync"
"time"
@@ -18,6 +19,8 @@ import (
"github.com/chihaya/chihaya/frontend/udp/bytepool"
)
var allowedGeneratedPrivateKeyRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
func init() {
prometheus.MustRegister(promResponseDurationMilliseconds)
}
@@ -68,6 +71,18 @@ type Frontend struct {
// NewFrontend allocates a new instance of a Frontend.
func NewFrontend(logic frontend.TrackerLogic, cfg Config) *Frontend {
// Generate a private key if one isn't provided by the user.
if cfg.PrivateKey == "" {
rand.Seed(time.Now().UnixNano())
pkeyRunes := make([]rune, 64)
for i := range pkeyRunes {
pkeyRunes[i] = allowedGeneratedPrivateKeyRunes[rand.Intn(len(allowedGeneratedPrivateKeyRunes))]
}
cfg.PrivateKey = string(pkeyRunes)
log.Warn("UDP private key was not provided, using generated key: ", cfg.PrivateKey)
}
return &Frontend{
closing: make(chan struct{}),
logic: logic,