don't hard-code port; rename probed -> public

This commit is contained in:
Christian Ulrich 2020-09-25 22:59:53 +02:00
parent 53a9d0fc24
commit dce5115c5c
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
2 changed files with 11 additions and 9 deletions

View File

@ -23,8 +23,8 @@ type
sock: AsyncSocket sock: AsyncSocket
outMessages: TableRef[string, Future[string]] outMessages: TableRef[string, Future[string]]
peerNotifications: FutureStream[string] peerNotifications: FutureStream[string]
probedIp: IpAddress publicIp: IpAddress
probedPorts: seq[Port] publicPorts: seq[Port]
# Punchd messages # Punchd messages
ProgressTcpSyniConnect* = object ProgressTcpSyniConnect* = object
@ -162,16 +162,17 @@ proc punchHole(punchdConn: PunchdConnection, serverConn: ServerConnection,
future.fail(e) future.fail(e)
except ValueError as e: except ValueError as e:
future.fail(e) future.fail(e)
let myPorts = (@[Port(1234)] & serverConn.probedPorts).join(",") let myPorts = serverConn.publicPorts.join(",")
let peerPorts = peerInfo.ports.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) let pResp = await punchdConn.sendRequest("tcp-syni-connect", req, progressCb)
result = pResp.sock result = pResp.sock
proc initServerConnection(serverHostname: string, serverPort: Port, proc initServerConnection(serverHostname: string, serverPort: Port,
probingPort: Port): Future[ServerConnection] {.async.} = probingPort: Port): Future[ServerConnection] {.async.} =
result.publicPorts.add(probingPort)
var failCount = 0 var failCount = 0
while result.probedPorts.len < 2: while result.publicPorts.len < 3:
# FIXME: error handling # FIXME: error handling
let sock = newAsyncSocket() let sock = newAsyncSocket()
try: try:
@ -192,8 +193,8 @@ proc initServerConnection(serverHostname: string, serverPort: Port,
assert(args[1] == $id) assert(args[1] == $id)
let endpoint = parseMessage[OkGetEndpoint](args[2]) let endpoint = parseMessage[OkGetEndpoint](args[2])
echo "endpoint: ", endpoint echo "endpoint: ", endpoint
result.probedIp = endpoint.ip result.publicIp = endpoint.ip
result.probedPorts.add(endpoint.port) result.publicPorts.add(endpoint.port)
let emptyLine = await sock.recvLine(maxLength = 400) let emptyLine = await sock.recvLine(maxLength = 400)
assert(emptyLine.len == 0) assert(emptyLine.len == 0)
sock.close() sock.close()
@ -219,8 +220,8 @@ proc runApp(serverHostname: string, serverPort: Port, peerId: string,
srcPort) srcPort)
asyncCheck handleServerMessages(serverConn) asyncCheck handleServerMessages(serverConn)
asyncCheck handlePeerNotifications(serverConn, punchdConn, peerId) asyncCheck handlePeerNotifications(serverConn, punchdConn, peerId)
let myPorts = (@[srcPort] & serverConn.probedPorts).join(",") let myPorts = serverConn.publicPorts.join(",")
let req = &"{peerId}|{serverConn.probedIp}|{myPorts}" let req = &"{peerId}|{serverConn.publicIp}|{myPorts}"
echo "registering: ", req echo "registering: ", req
discard await serverConn.sendRequest("register", req) discard await serverConn.sendRequest("register", req)
while true: while true:

View File

@ -134,6 +134,7 @@ proc doConnect(srcIp: IpAddress, srcPort: Port, dstIp: IpAddress, dstPort: Port,
let sock = newAsyncSocket() let sock = newAsyncSocket()
sock.setSockOpt(OptReuseAddr, true) sock.setSockOpt(OptReuseAddr, true)
sock.getFd.setSockOptInt(IPPROTO_IP, IP_TTL, 2) sock.getFd.setSockOptInt(IPPROTO_IP, IP_TTL, 2)
echo &"doConnect {srcIp}:{srcPort} -> {dstIp}:{dstPort}"
sock.bindAddr(srcPort, $srcIp) sock.bindAddr(srcPort, $srcIp)
try: try:
await sock.connect($dstIp, dstPort) await sock.connect($dstIp, dstPort)