cleanup on SIGINT
This commit is contained in:
parent
1dc7cf6676
commit
e23457a647
34
punchd.nim
34
punchd.nim
|
@ -11,6 +11,12 @@ from nativesockets import setSockOptInt
|
||||||
const PunchdSocket = "/tmp/punchd.socket"
|
const PunchdSocket = "/tmp/punchd.socket"
|
||||||
|
|
||||||
type
|
type
|
||||||
|
Punchd = ref object
|
||||||
|
unixSocket: AsyncSocket
|
||||||
|
punchers: seq[TcpSyniPuncher]
|
||||||
|
|
||||||
|
Sigint = object of CatchableError
|
||||||
|
|
||||||
# Requests
|
# Requests
|
||||||
TcpSyniConnect = object
|
TcpSyniConnect = object
|
||||||
srcIp: IpAddress
|
srcIp: IpAddress
|
||||||
|
@ -25,12 +31,11 @@ type
|
||||||
srcPorts: array[3, Port]
|
srcPorts: array[3, Port]
|
||||||
seqNums: seq[uint32]
|
seqNums: seq[uint32]
|
||||||
|
|
||||||
Sigint = object of CatchableError
|
|
||||||
|
|
||||||
proc handleSigint() {.noconv.} =
|
proc handleSigint() {.noconv.} =
|
||||||
raise newException(Sigint, "received SIGINT")
|
raise newException(Sigint, "received SIGINT")
|
||||||
|
|
||||||
proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} =
|
proc handleRequest(punchd: Punchd, line: string,
|
||||||
|
unixSock: AsyncSocket) {.async.} =
|
||||||
var id: string
|
var id: string
|
||||||
var sock: AsyncSocket
|
var sock: AsyncSocket
|
||||||
var puncher: TcpSyniPuncher
|
var puncher: TcpSyniPuncher
|
||||||
|
@ -48,12 +53,14 @@ proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} =
|
||||||
seqNumbers.join(",")].join("|")
|
seqNumbers.join(",")].join("|")
|
||||||
await unixSock.send(&"progress|{id}|{content}\n")
|
await unixSock.send(&"progress|{id}|{content}\n")
|
||||||
puncher = initPuncher(req.srcPorts[0], req.dstIp, req.dstPorts)
|
puncher = initPuncher(req.srcPorts[0], req.dstIp, req.dstPorts)
|
||||||
|
punchd.punchers.add(puncher)
|
||||||
sock = await puncher.connect(handleSeqNumbers)
|
sock = await puncher.connect(handleSeqNumbers)
|
||||||
|
|
||||||
of "tcp-syni-accept":
|
of "tcp-syni-accept":
|
||||||
let req = parseMessage[TcpSyniAccept](args[2])
|
let req = parseMessage[TcpSyniAccept](args[2])
|
||||||
puncher = initPuncher(req.srcPorts[0], req.dstIp, req.dstPorts,
|
puncher = initPuncher(req.srcPorts[0], req.dstIp, req.dstPorts,
|
||||||
req.seqNums)
|
req.seqNums)
|
||||||
|
punchd.punchers.add(puncher)
|
||||||
sock = await puncher.accept()
|
sock = await puncher.accept()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -67,35 +74,36 @@ proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} =
|
||||||
except ValueError:
|
except ValueError:
|
||||||
unixSock.close
|
unixSock.close
|
||||||
|
|
||||||
proc handleRequests(userSock: AsyncSocket) {.async.} =
|
proc handleRequests(punchd: Punchd, userSock: AsyncSocket) {.async.} =
|
||||||
while true:
|
while true:
|
||||||
if userSock.isClosed:
|
if userSock.isClosed:
|
||||||
break
|
break
|
||||||
let line = await userSock.recvLine(maxLength = 400)
|
let line = await userSock.recvLine(maxLength = 400)
|
||||||
if line.len == 0:
|
if line.len == 0:
|
||||||
break
|
break
|
||||||
asyncCheck handleRequest(line, userSock)
|
asyncCheck punchd.handleRequest(line, userSock)
|
||||||
|
|
||||||
proc handleUsers(sock: AsyncSocket) {.async.} =
|
proc handleUsers(punchd: Punchd) {.async.} =
|
||||||
while true:
|
while true:
|
||||||
let user = await sock.accept()
|
let user = await punchd.unixSocket.accept()
|
||||||
asyncCheck handleRequests(user)
|
asyncCheck punchd.handleRequests(user)
|
||||||
|
|
||||||
proc main() =
|
proc main() =
|
||||||
setControlCHook(handleSigint)
|
setControlCHook(handleSigint)
|
||||||
removeFile(PunchdSocket)
|
removeFile(PunchdSocket)
|
||||||
let unixSocket = newAsyncSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP)
|
let punchd = Punchd(unixSocket: newAsyncSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP))
|
||||||
unixSocket.bindUnix(PunchdSocket)
|
punchd.unixSocket.bindUnix(PunchdSocket)
|
||||||
setFilePermissions(PunchdSocket,
|
setFilePermissions(PunchdSocket,
|
||||||
{fpUserRead, fpUserWrite, fpGroupRead, fpGroupWrite,
|
{fpUserRead, fpUserWrite, fpGroupRead, fpGroupWrite,
|
||||||
fpOthersRead, fpOthersWrite})
|
fpOthersRead, fpOthersWrite})
|
||||||
unixSocket.listen()
|
punchd.unixSocket.listen()
|
||||||
asyncCheck handleUsers(unixSocket)
|
asyncCheck handleUsers(punchd)
|
||||||
try:
|
try:
|
||||||
runForever()
|
runForever()
|
||||||
|
|
||||||
except Sigint:
|
except Sigint:
|
||||||
# FIXME: need to cleaup the punchers (i.e. delete firewall rules)
|
for puncher in punchd.punchers:
|
||||||
|
waitFor puncher.cleanup()
|
||||||
removeFile(PunchdSocket)
|
removeFile(PunchdSocket)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|
Loading…
Reference in New Issue