use dynamic length of destination ports and captured sequence numbers (3 for now)

This commit is contained in:
Christian Ulrich 2020-07-21 11:10:56 +02:00
parent 301ca6c81d
commit 187b232162
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
4 changed files with 19 additions and 8 deletions

View File

@ -44,7 +44,7 @@ type
srcPorts: array[3, Port] srcPorts: array[3, Port]
dstIp: IpAddress dstIp: IpAddress
dstPorts: array[3, Port] dstPorts: array[3, Port]
seqNumbers: array[10, uint32] seqNumbers: seq[uint32]
# Exceptions # Exceptions
PunchdError = object of ValueError # FIXME: not used yet PunchdError = object of ValueError # FIXME: not used yet

View File

@ -25,6 +25,15 @@ proc parseField[S, T](input: string, output: var array[S, T]) =
for i in 0 .. S.high: for i in 0 .. S.high:
parseField(parts[i], output[i]) parseField(parts[i], output[i])
proc parseField[T](input: string, output: var seq[T]) =
let parts = input.split(",")
if parts.len < 1:
raise newException(ValueError, "Sequence is empty")
output = newSeq[T](parts.len)
echo "before for loop"
for i in 0 .. parts.len - 1:
parseField(parts[i], output[i])
proc parseField[T: tuple | object](input: string, output: var T) = proc parseField[T: tuple | object](input: string, output: var T) =
var fieldCount = 0 var fieldCount = 0
for _ in output.fields: for _ in output.fields:

View File

@ -21,7 +21,7 @@ type
dstPorts: array[3, Port] dstPorts: array[3, Port]
srcIp: IpAddress srcIp: IpAddress
srcPorts: array[3, Port] srcPorts: array[3, Port]
seqNums: array[10, uint32] # FIXME: this should be a seq seqNums: seq[uint32]
proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} = proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} =
var id: string var id: string
@ -35,6 +35,7 @@ proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} =
of "tcp-syni-connect": of "tcp-syni-connect":
let req = parseMessage[TcpSyniConnect](args[2]) let req = parseMessage[TcpSyniConnect](args[2])
proc handleSeqNumbers(seqNumbers: seq[uint32]) {.async.} = proc handleSeqNumbers(seqNumbers: seq[uint32]) {.async.} =
echo "progress! seqNumbers: ", seqNumbers
let content = @["tcp-syni-accept", $req.srcIp, req.srcPorts.join(","), let content = @["tcp-syni-accept", $req.srcIp, req.srcPorts.join(","),
$req.dstIp, req.dstPorts.join(","), $req.dstIp, req.dstPorts.join(","),
seqNumbers.join(",")].join("|") seqNumbers.join(",")].join("|")
@ -45,7 +46,7 @@ proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} =
of "tcp-syni-accept": of "tcp-syni-accept":
let req = parseMessage[TcpSyniAccept](args[2]) let req = parseMessage[TcpSyniAccept](args[2])
puncher = await initPuncher(req.srcPorts[0], req.dstIp, req.dstPorts, puncher = await initPuncher(req.srcPorts[0], req.dstIp, req.dstPorts,
@(req.seqNums)) - req.seqNums)
sock = await puncher.accept() sock = await puncher.accept()
else: else:

View File

@ -14,7 +14,7 @@ type
srcIp: IpAddress srcIp: IpAddress
srcPort: Port srcPort: Port
dstIp: IpAddress dstIp: IpAddress
dstPorts: array[10, Port] dstPorts: seq[Port]
seqNums: seq[uint32] seqNums: seq[uint32]
PunchProgressCb* = proc (seqNums: seq[uint32]) {.async.} PunchProgressCb* = proc (seqNums: seq[uint32]) {.async.}
@ -105,10 +105,11 @@ proc initPuncher*(srcPort: Port, dstIp: IpAddress, dstPorts: array[3, Port],
seqNums: seq[uint32] = @[]): Future[TcpSyniPuncher] {.async.} = seqNums: seq[uint32] = @[]): Future[TcpSyniPuncher] {.async.} =
let localIp = getPrimaryIPAddr(dstIp) let localIp = getPrimaryIPAddr(dstIp)
# TODO: do real port prediction # TODO: do real port prediction
var predictedDstPorts: array[10, Port] var predictedDstPorts = newSeq[Port](3)
let basePort = min(dstPorts[1].uint16, uint16.high - 9) let basePort = min(dstPorts[1].uint16,
for i in 0.uint16 .. 9.uint16: uint16.high - (predictedDstPorts.len - 1).uint16)
predictedDstPorts[i] = Port(basePort + i) for i in 0 .. predictedDstPorts.len - 1:
predictedDstPorts[i] = Port(basePort + i.uint16)
result = TcpSyniPuncher(srcIp: localIp, srcPort: srcPort, dstIp: dstIp, result = TcpSyniPuncher(srcIp: localIp, srcPort: srcPort, dstIp: dstIp,
dstPorts: predictedDstPorts, seqNums: seqNums) dstPorts: predictedDstPorts, seqNums: seqNums)
for dstPort in result.dstPorts: for dstPort in result.dstPorts: