simplify port number generation

This commit is contained in:
erinn 2018-10-09 08:54:34 -07:00
parent 23e043ad93
commit b1738ac964
1 changed files with 5 additions and 7 deletions

View File

@ -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)