From b1738ac96479715ea4df11759b3bddafce27d145 Mon Sep 17 00:00:00 2001 From: erinn Date: Tue, 9 Oct 2018 08:54:34 -0700 Subject: [PATCH] simplify port number generation --- listener.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/listener.go b/listener.go index 2ced657..399beae 100644 --- a/listener.go +++ b/listener.go @@ -13,8 +13,7 @@ import ( "net" "strconv" "golang.org/x/crypto/sha3" - badrand "math/rand" - "encoding/binary" + "encoding/binary" "crypto" ) @@ -80,13 +79,12 @@ func (c *Conn) RecoverListener(config *NewOnionConfig, onion string, vports ...u seedbytes := make([]byte, 2) goodrand.Read(seedbytes) port = int(binary.LittleEndian.Uint16(seedbytes)) - if port < 1024 { // because why not ^ea - port = 1024 + int(seedbytes[0]) + int(seedbytes[1]) - } } else { // generate a deterministic port for resumptive programs seedbytes := sha3.New224().Sum([]byte(onion)) - seed := badrand.NewSource(int64(binary.LittleEndian.Uint64(seedbytes[:8]))) - port = badrand.New(seed).Intn(64511) + 1024 + port = int(seedbytes[0]) + (int(seedbytes[1]) << 8) + } + if port < 1024 { // this is not uniformly random, but we don't need it to be + port += 1024 } var loopbackAddr = "127.0.0.1:" + strconv.Itoa(port)