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
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:

View File

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