check if the client is still there before sending a message (fix unhandled ValueError)

master
Christian Ulrich 2020-10-16 00:18:11 +02:00
parent 91b7d14958
commit 2b938f9278
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 10 additions and 5 deletions

View File

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