check if the client is still there before sending a message (fix unhandled ValueError)
This commit is contained in:
parent
91b7d14958
commit
2b938f9278
15
punchd.nim
15
punchd.nim
|
@ -52,6 +52,12 @@ const PunchdSocket = "/tmp/punchd.socket"
|
|||
proc handleSigint() {.noconv.} =
|
||||
raise newException(Sigint, "received SIGINT")
|
||||
|
||||
proc sendToClient(unixSock: AsyncSocket, msg: string,
|
||||
cmsgs: seq[ControlMessage] = @[]) {.async.} =
|
||||
if not unixSock.isClosed():
|
||||
let unixFd = unixSock.getFd.AsyncFD
|
||||
await unixFd.asyncSendMsg(msg, cmsgs)
|
||||
|
||||
proc handleRequest(punchd: Punchd, line: string,
|
||||
unixSock: AsyncSocket) {.async.} =
|
||||
var id: string
|
||||
|
@ -68,7 +74,7 @@ proc handleRequest(punchd: Punchd, line: string,
|
|||
let content = @["tcp-syni-accept", $req.srcIp, req.srcPorts.join(","),
|
||||
$req.dstIp, req.dstPorts.join(","),
|
||||
seqNumbers.join(",")].join("|")
|
||||
await unixSock.send(&"progress|{id}|{content}\n")
|
||||
await sendToClient(unixSock, &"progress|{id}|{content}\n")
|
||||
sock = await punchd.tcpSyniCP.connect(req.srcPorts[0], req.dstIp,
|
||||
req.dstPorts, progress)
|
||||
|
||||
|
@ -83,7 +89,7 @@ proc handleRequest(punchd: Punchd, line: string,
|
|||
echo "progress!"
|
||||
let content = @["tcp-nutss-respond", $req.srcIp, req.srcPorts.join(","),
|
||||
$req.dstIp, req.dstPorts.join(",")].join("|")
|
||||
await unixSock.send(&"progress|{id}|{content}\n")
|
||||
await sendToClient(unixSock, &"progress|{id}|{content}\n")
|
||||
sock = await punchd.tcpNutssInitiator.initiate(req.srcPorts[0], req.dstIp,
|
||||
req.dstPorts, progress)
|
||||
|
||||
|
@ -95,12 +101,11 @@ proc handleRequest(punchd: Punchd, line: string,
|
|||
else:
|
||||
raise newException(ValueError, "invalid request")
|
||||
|
||||
let unixFd = unixSock.getFd.AsyncFD
|
||||
await unixFd.asyncSendMsg(&"ok|{id}\n", @[fromFd(sock.getFd.AsyncFD)])
|
||||
await sendToClient(unixSock, &"ok|{id}\n", @[fromFd(sock.getFd.AsyncFD)])
|
||||
sock.close()
|
||||
|
||||
except PunchHoleError as e:
|
||||
await unixSock.send(&"error|{id}|{e.msg}\n")
|
||||
await sendToClient(unixSock, &"error|{id}|{e.msg}\n")
|
||||
except ValueError:
|
||||
unixSock.close
|
||||
|
||||
|
|
Loading…
Reference in New Issue