From dce5115c5c3e3f8b22b0a5aa553aa39bbc3bd39a Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Fri, 25 Sep 2020 22:59:53 +0200 Subject: [PATCH] don't hard-code port; rename probed -> public --- examples/app/app.nim | 19 ++++++++++--------- tcp_syni.nim | 1 + 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/examples/app/app.nim b/examples/app/app.nim index 1673b90..41f8f24 100644 --- a/examples/app/app.nim +++ b/examples/app/app.nim @@ -23,8 +23,8 @@ type sock: AsyncSocket outMessages: TableRef[string, Future[string]] peerNotifications: FutureStream[string] - probedIp: IpAddress - probedPorts: seq[Port] + publicIp: IpAddress + publicPorts: seq[Port] # Punchd messages ProgressTcpSyniConnect* = object @@ -162,16 +162,17 @@ proc punchHole(punchdConn: PunchdConnection, serverConn: ServerConnection, future.fail(e) except ValueError as e: future.fail(e) - let myPorts = (@[Port(1234)] & serverConn.probedPorts).join(",") + let myPorts = serverConn.publicPorts.join(",") let peerPorts = peerInfo.ports.join(",") - let req = &"{serverConn.probedIp}|{myPorts}|{peerInfo.ip}|{peerPorts}" + let req = &"{serverConn.publicIp}|{myPorts}|{peerInfo.ip}|{peerPorts}" let pResp = await punchdConn.sendRequest("tcp-syni-connect", req, progressCb) result = pResp.sock proc initServerConnection(serverHostname: string, serverPort: Port, probingPort: Port): Future[ServerConnection] {.async.} = + result.publicPorts.add(probingPort) var failCount = 0 - while result.probedPorts.len < 2: + while result.publicPorts.len < 3: # FIXME: error handling let sock = newAsyncSocket() try: @@ -192,8 +193,8 @@ proc initServerConnection(serverHostname: string, serverPort: Port, assert(args[1] == $id) let endpoint = parseMessage[OkGetEndpoint](args[2]) echo "endpoint: ", endpoint - result.probedIp = endpoint.ip - result.probedPorts.add(endpoint.port) + result.publicIp = endpoint.ip + result.publicPorts.add(endpoint.port) let emptyLine = await sock.recvLine(maxLength = 400) assert(emptyLine.len == 0) sock.close() @@ -219,8 +220,8 @@ proc runApp(serverHostname: string, serverPort: Port, peerId: string, srcPort) asyncCheck handleServerMessages(serverConn) asyncCheck handlePeerNotifications(serverConn, punchdConn, peerId) - let myPorts = (@[srcPort] & serverConn.probedPorts).join(",") - let req = &"{peerId}|{serverConn.probedIp}|{myPorts}" + let myPorts = serverConn.publicPorts.join(",") + let req = &"{peerId}|{serverConn.publicIp}|{myPorts}" echo "registering: ", req discard await serverConn.sendRequest("register", req) while true: diff --git a/tcp_syni.nim b/tcp_syni.nim index aa68ec2..ab80994 100644 --- a/tcp_syni.nim +++ b/tcp_syni.nim @@ -134,6 +134,7 @@ proc doConnect(srcIp: IpAddress, srcPort: Port, dstIp: IpAddress, dstPort: Port, let sock = newAsyncSocket() sock.setSockOpt(OptReuseAddr, true) sock.getFd.setSockOptInt(IPPROTO_IP, IP_TTL, 2) + echo &"doConnect {srcIp}:{srcPort} -> {dstIp}:{dstPort}" sock.bindAddr(srcPort, $srcIp) try: await sock.connect($dstIp, dstPort)