2020-07-07 19:39:28 +02:00
|
|
|
import asyncdispatch, asyncnet, os, strformat, strutils
|
|
|
|
from nativesockets import Domain, SockType, Protocol
|
|
|
|
import asyncutils
|
|
|
|
import message
|
2020-10-23 01:15:37 +02:00
|
|
|
import tables
|
2020-10-22 00:22:11 +02:00
|
|
|
import tcp_syni_initiator
|
|
|
|
import tcp_syni_responder
|
2020-10-14 20:52:20 +02:00
|
|
|
import tcp_nutss_initiator
|
|
|
|
import tcp_nutss_responder
|
2020-07-07 19:39:28 +02:00
|
|
|
|
|
|
|
from strutils import format, join
|
|
|
|
from nativesockets import setSockOptInt
|
|
|
|
|
|
|
|
type
|
2020-08-22 12:34:12 +02:00
|
|
|
Punchd = ref object
|
|
|
|
unixSocket: AsyncSocket
|
2020-10-23 01:15:37 +02:00
|
|
|
initiators: Table[string, Initiator]
|
|
|
|
responders: Table[string, Responder]
|
2020-08-22 12:34:12 +02:00
|
|
|
|
|
|
|
Sigint = object of CatchableError
|
|
|
|
|
2020-10-12 21:31:55 +02:00
|
|
|
const PunchdSocket = "/tmp/punchd.socket"
|
|
|
|
|
2020-07-28 00:17:45 +02:00
|
|
|
proc handleSigint() {.noconv.} =
|
|
|
|
raise newException(Sigint, "received SIGINT")
|
|
|
|
|
2020-10-16 00:18:11 +02:00
|
|
|
proc sendToClient(unixSock: AsyncSocket, msg: string,
|
|
|
|
cmsgs: seq[ControlMessage] = @[]) {.async.} =
|
|
|
|
if not unixSock.isClosed():
|
|
|
|
let unixFd = unixSock.getFd.AsyncFD
|
|
|
|
await unixFd.asyncSendMsg(msg, cmsgs)
|
|
|
|
|
2020-08-22 12:34:12 +02:00
|
|
|
proc handleRequest(punchd: Punchd, line: string,
|
|
|
|
unixSock: AsyncSocket) {.async.} =
|
2020-07-07 19:39:28 +02:00
|
|
|
var id: string
|
|
|
|
var sock: AsyncSocket
|
|
|
|
try:
|
2020-10-22 00:22:11 +02:00
|
|
|
let args = line.parseArgs(4)
|
2020-07-07 19:39:28 +02:00
|
|
|
id = args[1]
|
2020-10-22 00:22:11 +02:00
|
|
|
|
2020-07-07 19:39:28 +02:00
|
|
|
case args[0]:
|
2020-10-22 00:22:11 +02:00
|
|
|
of "initiate":
|
2020-10-23 01:15:37 +02:00
|
|
|
proc progress(extraArgs: string) {.async.} =
|
|
|
|
let msg = &"progress|{id}|{args[2]}|{args[3]}|{extraArgs}\n"
|
|
|
|
await sendToClient(unixSock, msg)
|
|
|
|
sock = await punchd.initiators[args[2]].initiate(args[3], progress)
|
2020-10-22 00:22:11 +02:00
|
|
|
|
|
|
|
of "respond":
|
2020-10-23 01:15:37 +02:00
|
|
|
sock = await punchd.responders[args[2]].respond(args[3])
|
2020-10-22 00:22:11 +02:00
|
|
|
|
2020-07-07 19:39:28 +02:00
|
|
|
else:
|
|
|
|
raise newException(ValueError, "invalid request")
|
2020-08-21 23:12:45 +02:00
|
|
|
|
2020-10-16 00:18:11 +02:00
|
|
|
await sendToClient(unixSock, &"ok|{id}\n", @[fromFd(sock.getFd.AsyncFD)])
|
2020-10-07 00:31:04 +02:00
|
|
|
sock.close()
|
2020-07-07 19:39:28 +02:00
|
|
|
|
|
|
|
except PunchHoleError as e:
|
2020-10-16 00:18:11 +02:00
|
|
|
await sendToClient(unixSock, &"error|{id}|{e.msg}\n")
|
2020-10-23 01:15:37 +02:00
|
|
|
except KeyError, ValueError:
|
2020-07-07 19:39:28 +02:00
|
|
|
unixSock.close
|
|
|
|
|
2020-08-22 12:34:12 +02:00
|
|
|
proc handleRequests(punchd: Punchd, userSock: AsyncSocket) {.async.} =
|
2020-07-07 19:39:28 +02:00
|
|
|
while true:
|
2020-07-27 23:51:16 +02:00
|
|
|
if userSock.isClosed:
|
|
|
|
break
|
2020-07-07 19:39:28 +02:00
|
|
|
let line = await userSock.recvLine(maxLength = 400)
|
|
|
|
if line.len == 0:
|
2020-10-10 11:14:31 +02:00
|
|
|
userSock.close()
|
2020-07-07 19:39:28 +02:00
|
|
|
break
|
2020-08-22 12:34:12 +02:00
|
|
|
asyncCheck punchd.handleRequest(line, userSock)
|
2020-07-07 19:39:28 +02:00
|
|
|
|
2020-08-22 12:34:12 +02:00
|
|
|
proc handleUsers(punchd: Punchd) {.async.} =
|
2020-07-07 19:39:28 +02:00
|
|
|
while true:
|
2020-08-22 12:34:12 +02:00
|
|
|
let user = await punchd.unixSocket.accept()
|
|
|
|
asyncCheck punchd.handleRequests(user)
|
2020-07-07 19:39:28 +02:00
|
|
|
|
|
|
|
proc main() =
|
2020-07-28 00:17:45 +02:00
|
|
|
setControlCHook(handleSigint)
|
|
|
|
removeFile(PunchdSocket)
|
2020-10-02 17:12:29 +02:00
|
|
|
let unixSocket = newAsyncSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP)
|
|
|
|
unixSocket.bindUnix(PunchdSocket)
|
|
|
|
unixSocket.listen()
|
2020-07-28 00:18:27 +02:00
|
|
|
setFilePermissions(PunchdSocket,
|
|
|
|
{fpUserRead, fpUserWrite, fpGroupRead, fpGroupWrite,
|
|
|
|
fpOthersRead, fpOthersWrite})
|
2020-10-23 01:15:37 +02:00
|
|
|
let punchd = Punchd(unixSocket: unixSocket)
|
|
|
|
punchd.initiators["tcp-syni"] = initTcpSyniInitiator()
|
|
|
|
punchd.initiators["tcp-nutss"] = initTcpNutssInitiator()
|
|
|
|
punchd.responders["tcp-syni"] = initTcpSyniResponder()
|
|
|
|
punchd.responders["tcp-nutss"] = initTcpNutssResponder()
|
2020-08-22 12:34:12 +02:00
|
|
|
asyncCheck handleUsers(punchd)
|
2020-07-28 00:17:45 +02:00
|
|
|
try:
|
|
|
|
runForever()
|
|
|
|
|
|
|
|
except Sigint:
|
2020-10-23 01:15:37 +02:00
|
|
|
for i in punchd.initiators.values:
|
|
|
|
waitFor i.cleanup()
|
|
|
|
for r in punchd.responders.values:
|
|
|
|
waitFor r.cleanup()
|
2020-08-22 13:11:55 +02:00
|
|
|
punchd.unixSocket.close()
|
2020-07-28 00:17:45 +02:00
|
|
|
removeFile(PunchdSocket)
|
2020-07-07 19:39:28 +02:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
main()
|