use random source port for initiating connection too

This commit is contained in:
Christian Ulrich 2020-09-25 21:05:57 +02:00
parent bf2fcc1602
commit 53a9d0fc24
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 6 additions and 5 deletions

View File

@ -178,6 +178,7 @@ proc initServerConnection(serverHostname: string, serverPort: Port,
sock.bindAddr(probingPort)
except OSError as e:
if failCount == 3:
echo "raising error"
raise e
failCount.inc
await sleepAsync(100)
@ -210,15 +211,15 @@ proc runApp(serverHostname: string, serverPort: Port, peerId: string,
punchdConn.inConnections = newFutureStream[AsyncSocket]("runApp")
await punchdConn.sock.connectUnix("/tmp/punchd.socket")
asyncCheck handlePunchdMessages(punchdConn)
let srcPort = rand(Port(1024) .. Port.high)
if otherPeerId.len == 0:
# register and wait for connections
let acceptPort = rand(Port(1024) .. Port.high)
echo &"init server connection, probing port: {acceptPort}"
echo &"init server connection, probing port: {srcPort}"
let serverConn = await initServerConnection(serverHostname, serverPort,
acceptPort)
srcPort)
asyncCheck handleServerMessages(serverConn)
asyncCheck handlePeerNotifications(serverConn, punchdConn, peerId)
let myPorts = (@[acceptPort] & serverConn.probedPorts).join(",")
let myPorts = (@[srcPort] & serverConn.probedPorts).join(",")
let req = &"{peerId}|{serverConn.probedIp}|{myPorts}"
echo "registering: ", req
discard await serverConn.sendRequest("register", req)
@ -234,7 +235,7 @@ proc runApp(serverHostname: string, serverPort: Port, peerId: string,
else:
# initiate a new connection
var serverConn = await initServerConnection(serverHostname, serverPort,
Port(1234))
srcPort)
asyncCheck handleServerMessages(serverConn)
let sock = await punchHole(punchdConn, serverConn, peerId, otherPeerId)
echo "connected!"