move port prediction into dedicated module

This commit is contained in:
Christian Ulrich 2020-10-14 18:40:58 +02:00
parent a0125d7d80
commit 9147194427
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
4 changed files with 13 additions and 8 deletions

11
port_prediction.nim Normal file
View File

@ -0,0 +1,11 @@
from net import Port
proc predictPortRange*(dstPorts: seq[Port]): seq[Port] =
# TODO: do real port prediction
result = newSeq[Port](1)
let basePort = min(dstPorts[1].uint16,
uint16.high - (result.len - 1).uint16)
for i in 0 .. result.len - 1:
result[i] = Port(basePort + i.uint16)

View File

@ -42,14 +42,6 @@ proc injectTcpPacket*(rawFd: AsyncFD, ipPacket: IpPacket) {.async.} =
except OSError as e:
raise newException(PunchHoleError, e.msg)
proc predictPortRange*(dstPorts: seq[Port]): seq[Port] =
# TODO: do real port prediction
result = newSeq[Port](1)
let basePort = min(dstPorts[1].uint16,
uint16.high - (result.len - 1).uint16)
for i in 0 .. result.len - 1:
result[i] = Port(basePort + i.uint16)
proc makeFirewallRule(srcIp: IpAddress, srcPort: Port,
dstIp: IpAddress, dstPort: Port): string =
# FIXME: use & instead of fmt?

View File

@ -3,6 +3,7 @@ from net import IpAddress, Port, `$`, `==`, parseIpAddress
from random import randomize, rand
from sequtils import any
import ip_packet
import port_prediction
import puncher
import raw_socket
import utils

View File

@ -3,6 +3,7 @@ from net import IpAddress, Port, `$`, `==`
from nativesockets import setSockOptInt
import ip_packet
import network_interface
import port_prediction
import puncher
import raw_socket
import utils