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

View file

@ -25,7 +25,6 @@ config:
addr: 0.0.0.0:6881
allow_ip_spoofing: true
max_clock_skew: 10s
private_key: "/K8AybyuKkob03fZJb+3Fu87eVcBNWsvVyDDxGd3wfvVGix10j6iGDEyS/ELPvQIIZvUPl2PnyfnpG6g1YJ/sw=="
storage:
gc_interval: 14m
peer_lifetime: 15m

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,