2020-07-07 19:39:28 +02:00
|
|
|
import asyncdispatch, asyncnet, os, strformat, strutils
|
|
|
|
from nativesockets import Domain, SockType, Protocol
|
|
|
|
from net import IpAddress, Port, `$`
|
|
|
|
import asyncutils
|
|
|
|
import message
|
|
|
|
import tcp_syni
|
|
|
|
|
|
|
|
from strutils import format, join
|
|
|
|
from nativesockets import setSockOptInt
|
|
|
|
|
2020-07-28 00:17:45 +02:00
|
|
|
const PunchdSocket = "/tmp/punchd.socket"
|
|
|
|
|
2020-07-07 19:39:28 +02:00
|
|
|
type
|
2020-08-22 12:34:12 +02:00
|
|
|
Punchd = ref object
|
|
|
|
unixSocket: AsyncSocket
|
2020-10-02 17:12:29 +02:00
|
|
|
tcpSyniPuncher: TcpSyniPuncher
|
2020-08-22 12:34:12 +02:00
|
|
|
|
|
|
|
Sigint = object of CatchableError
|
|
|
|
|
2020-07-07 19:39:28 +02:00
|
|
|
# Requests
|
|
|
|
TcpSyniConnect = object
|
|
|
|
srcIp: IpAddress
|
2020-08-26 21:33:13 +02:00
|
|
|
srcPorts: seq[Port]
|
2020-07-07 19:39:28 +02:00
|
|
|
dstIp: IpAddress
|
2020-08-26 21:33:13 +02:00
|
|
|
dstPorts: seq[Port]
|
2020-07-07 19:39:28 +02:00
|
|
|
|
|
|
|
TcpSyniAccept = object
|
|
|
|
dstIp: IpAddress
|
2020-08-26 21:33:13 +02:00
|
|
|
dstPorts: seq[Port]
|
2020-07-07 19:39:28 +02:00
|
|
|
srcIp: IpAddress
|
2020-08-26 21:33:13 +02:00
|
|
|
srcPorts: seq[Port]
|
2020-07-21 11:10:56 +02:00
|
|
|
seqNums: seq[uint32]
|
2020-07-07 19:39:28 +02:00
|
|
|
|
2020-07-28 00:17:45 +02:00
|
|
|
proc handleSigint() {.noconv.} =
|
|
|
|
raise newException(Sigint, "received SIGINT")
|
|
|
|
|
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:
|
|
|
|
let args = line.parseArgs(3)
|
|
|
|
id = args[1]
|
|
|
|
|
|
|
|
case args[0]:
|
|
|
|
of "tcp-syni-connect":
|
|
|
|
let req = parseMessage[TcpSyniConnect](args[2])
|
|
|
|
proc handleSeqNumbers(seqNumbers: seq[uint32]) {.async.} =
|
2020-07-21 11:10:56 +02:00
|
|
|
echo "progress! seqNumbers: ", seqNumbers
|
2020-07-07 19:39:28 +02:00
|
|
|
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")
|
2020-10-02 17:12:29 +02:00
|
|
|
sock = await punchd.tcpSyniPuncher.connect(req.srcPorts[0], req.dstIp,
|
|
|
|
req.dstPorts, handleSeqNumbers)
|
|
|
|
|
2020-07-07 19:39:28 +02:00
|
|
|
of "tcp-syni-accept":
|
|
|
|
let req = parseMessage[TcpSyniAccept](args[2])
|
2020-10-02 17:12:29 +02:00
|
|
|
sock = await punchd.tcpSyniPuncher.accept(req.srcPorts[0], req.dstIp,
|
|
|
|
req.dstPorts, req.seqNums)
|
2020-07-07 19:39:28 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "invalid request")
|
2020-08-21 23:12:45 +02:00
|
|
|
|
2020-07-07 19:39:28 +02:00
|
|
|
let unixFd = unixSock.getFd.AsyncFD
|
|
|
|
await unixFd.asyncSendMsg(&"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:
|
|
|
|
await unixSock.send(&"error|{id}|{e.msg}\n")
|
|
|
|
except ValueError:
|
|
|
|
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-02 17:12:29 +02:00
|
|
|
let punchd = Punchd(unixSocket: unixSocket, tcpSyniPuncher: initPuncher())
|
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-02 17:12:29 +02:00
|
|
|
waitFor punchd.tcpSyniPuncher.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()
|