2020-10-24 18:44:37 +02:00
|
|
|
import asyncdispatch, asyncnet, strformat, options
|
|
|
|
from nativesockets import Protocol
|
2020-10-23 01:15:37 +02:00
|
|
|
from net import IpAddress, Port, `$`, `==`
|
2020-10-12 21:31:55 +02:00
|
|
|
from sequtils import any
|
|
|
|
import asyncutils
|
|
|
|
|
|
|
|
type
|
2020-10-23 01:15:37 +02:00
|
|
|
Attempt* = ref object of RootObj
|
2020-10-24 18:44:37 +02:00
|
|
|
## A hole punching attempt.
|
|
|
|
##
|
|
|
|
## It is created by parsing the arguments of a request in either
|
|
|
|
## ``parseInitiateRequest`` or ``parseRespondRequest`` and must not be
|
|
|
|
## modified afterwards. By inclduing ``some(acceptFuture)``, the puncher
|
|
|
|
## tells the caller to accept connections at the local IP address and
|
|
|
|
## port ``srcPort`` before calling ``initiate``. The desired transport
|
|
|
|
## protocol can be obtained by calling ``getProcotol``. The puncher expects
|
|
|
|
## the caller to complete the future when a connections from
|
|
|
|
## ``dstIp``:``dstPort`` has been accepted.
|
2020-10-25 10:46:29 +01:00
|
|
|
protocol*: Protocol
|
2020-10-23 01:15:37 +02:00
|
|
|
srcIp*: IpAddress
|
|
|
|
srcPort*: Port
|
|
|
|
dstIp*: IpAddress
|
|
|
|
dstPorts*: seq[Port]
|
2020-10-24 18:44:37 +02:00
|
|
|
acceptFuture*: Option[Future[AsyncSocket]]
|
2020-10-12 21:31:55 +02:00
|
|
|
|
2020-10-23 01:15:37 +02:00
|
|
|
Puncher* = ref object of RootObj
|
2020-10-24 18:44:37 +02:00
|
|
|
## A hole puncher.
|
2020-10-12 21:31:55 +02:00
|
|
|
|
|
|
|
PunchHoleError* = object of ValueError
|
2020-10-24 18:44:37 +02:00
|
|
|
attempt: Attempt
|
|
|
|
## An exception indicating that the contained ``attempt`` has failed.
|
2020-10-12 21:31:55 +02:00
|
|
|
|
2020-10-23 01:15:37 +02:00
|
|
|
PunchProgressCb* = proc(extraArgs: string) {.async.}
|
2020-10-24 18:44:37 +02:00
|
|
|
## A callback to allow a puncher report progress.
|
|
|
|
##
|
|
|
|
## When called a status message of type ``progress``, including the given
|
|
|
|
## ``extraArgs`` has to be sent to the application.
|
2020-10-23 01:15:37 +02:00
|
|
|
|
2020-10-12 21:31:55 +02:00
|
|
|
const Timeout* = 3000
|
|
|
|
|
2020-10-24 18:44:37 +02:00
|
|
|
proc `==`*(a, b: Attempt): bool =
|
|
|
|
## ``==`` for hole punching attempts.
|
|
|
|
##
|
2020-10-25 10:46:29 +01:00
|
|
|
## Two hole punching attempts are considered equal if their ``protocol`` is
|
|
|
|
## the same, ``srcIp``, ``srcPort`` and ``dstIp`` are equal and their
|
|
|
|
## ``dstPorts`` overlap.
|
|
|
|
a.protocol == b.protocol and a.srcIp == b.srcIp and a.srcPort == b.srcPort and
|
|
|
|
a.dstIp == b.dstIp and a.dstPorts.any(proc (p: Port): bool = p in b.dstPorts)
|
2020-10-12 21:31:55 +02:00
|
|
|
|
2020-10-24 18:44:37 +02:00
|
|
|
method cleanup*(attempt: Attempt): Future[void] {.base, async.} =
|
|
|
|
## Cleans up when an attempt finished (either successful or not).
|
|
|
|
##
|
|
|
|
## Does nothing. Override for custom attempt types.
|
|
|
|
discard
|
2020-10-12 21:31:55 +02:00
|
|
|
|
2020-10-24 18:44:37 +02:00
|
|
|
method parseInitiateRequest*(puncher: Puncher, args: string): Attempt {.base.} =
|
|
|
|
## Creates a new hole punching attempt by parsing arguments of an ``initiate``
|
|
|
|
## request.
|
|
|
|
##
|
|
|
|
## ``args`` has to be the arguments after the ``TECHNIQUE`` field, i.e.
|
|
|
|
## ``IP_FROM|PORTS_FROM|IP_TO|PORTS_TO``. My throw a ``ValueError`` if
|
|
|
|
## ``args`` is invalid.
|
|
|
|
raise newException(CatchableError, "Method without implementation override")
|
|
|
|
|
|
|
|
method parseRespondRequest*(puncher: Puncher, args: string): Attempt {.base.} =
|
|
|
|
## Creates a new hole punching attempt by parsing arguments of a ``respond``
|
|
|
|
## request.
|
|
|
|
##
|
|
|
|
## ``args`` has to be the arguments after the ``TECHNIQUE`` field, i.e.
|
|
|
|
## ``IP_FROM|PORTS_FROM|IP_TO|PORTS_TO|EXTRA_ARGS``. May throw a
|
|
|
|
## ``ValueError`` if ``args`` is invalid.
|
|
|
|
raise newException(CatchableError, "Method without implementation override")
|
2020-10-23 01:15:37 +02:00
|
|
|
|
2020-10-24 18:44:37 +02:00
|
|
|
method initiate*(puncher: Puncher, attempt: Attempt, progress: PunchProgressCb):
|
2020-10-23 01:15:37 +02:00
|
|
|
Future[AsyncSocket] {.base, async.} =
|
2020-10-24 18:44:37 +02:00
|
|
|
## Initiate a hole punching attempt.
|
|
|
|
##
|
|
|
|
## ``attempt`` has to be obtained by calling ``parseInitiateRequest``.
|
|
|
|
## ``progress`` will be called when the attempt has been initiated and a
|
|
|
|
## status message of type ``progress`` has to be sent to the application. The
|
|
|
|
## returned future contains a connected socket and will fail with a
|
|
|
|
## ``PunchHoleError`` if the hole punching fails.
|
2020-10-23 01:15:37 +02:00
|
|
|
block: # workaround for https://github.com/nim-lang/Nim/issues/12530
|
|
|
|
raise newException(CatchableError, "Method without implementation override")
|
|
|
|
|
2020-10-24 18:44:37 +02:00
|
|
|
method respond*(puncher: Puncher, attempt: Attempt):
|
2020-10-23 01:15:37 +02:00
|
|
|
Future[AsyncSocket] {.base, async.} =
|
2020-10-24 18:44:37 +02:00
|
|
|
## Respond to a hole punching attempt initiated by another peer.
|
|
|
|
##
|
|
|
|
## ``attempt has to be obtained by calling ``parseRespondRequest``. The
|
|
|
|
## returned future contains a connected socket and will fail with as
|
|
|
|
## ``PunchHoleError`` if the hole punching fails.
|
2020-10-23 01:15:37 +02:00
|
|
|
block: # workaround for https://github.com/nim-lang/Nim/issues/12530
|
|
|
|
raise newException(CatchableError, "Method without implementation override")
|
|
|
|
|
2020-10-25 10:34:25 +01:00
|
|
|
# FIXME move firewall procs to dedicated module
|
2020-10-12 21:31:55 +02:00
|
|
|
proc makeFirewallRule(srcIp: IpAddress, srcPort: Port,
|
|
|
|
dstIp: IpAddress, dstPort: Port): string =
|
2020-10-23 01:21:19 +02:00
|
|
|
result = &"""-w \
|
2020-10-12 21:31:55 +02:00
|
|
|
-d {srcIp} \
|
|
|
|
-p icmp \
|
|
|
|
--icmp-type time-exceeded \
|
|
|
|
-m conntrack \
|
|
|
|
--ctstate RELATED \
|
|
|
|
--ctproto tcp \
|
|
|
|
--ctorigsrc {srcIp} \
|
|
|
|
--ctorigsrcport {srcPort.int} \
|
|
|
|
--ctorigdst {dstIp} \
|
|
|
|
--ctorigdstport {dstPort.int} \
|
|
|
|
-j DROP"""
|
|
|
|
|
|
|
|
proc iptablesInsert(chain: string, rule: string) {.async.} =
|
2020-10-23 01:21:19 +02:00
|
|
|
let firewall_cmd = &"iptables -I {chain} {rule}"
|
2020-10-12 21:31:55 +02:00
|
|
|
discard await asyncExecCmd(firewall_cmd)
|
|
|
|
|
|
|
|
proc iptablesDelete(chain: string, rule: string) {.async.} =
|
2020-10-23 01:21:19 +02:00
|
|
|
let firewall_cmd = &"iptables -D {chain} {rule}"
|
2020-10-12 21:31:55 +02:00
|
|
|
discard await asyncExecCmd(firewall_cmd)
|
|
|
|
|
|
|
|
proc addFirewallRules*(attempt: Attempt) {.async.} =
|
|
|
|
for dstPort in attempt.dstPorts:
|
|
|
|
let rule = makeFirewallRule(attempt.srcIp, attempt.srcPort,
|
|
|
|
attempt.dstIp, dstPort)
|
|
|
|
try:
|
|
|
|
await iptablesInsert("INPUT", rule)
|
|
|
|
except OSError as e:
|
|
|
|
echo "cannot add firewall rule: ", e.msg
|
|
|
|
raise newException(PunchHoleError, e.msg)
|
|
|
|
|
|
|
|
proc deleteFirewallRules*(attempt: Attempt) {.async.} =
|
|
|
|
for dstPort in attempt.dstPorts:
|
|
|
|
let rule = makeFirewallRule(attempt.srcIp, attempt.srcPort,
|
|
|
|
attempt.dstIp, dstPort)
|
|
|
|
try:
|
|
|
|
await iptablesDelete("INPUT", rule)
|
|
|
|
except OSError:
|
|
|
|
# At least we tried
|
|
|
|
discard
|