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