try random ports for randokm port mapping
This commit is contained in:
parent
ee3ab4564d
commit
9b3196d8a0
|
@ -1,5 +1,6 @@
|
|||
import algorithm
|
||||
import net
|
||||
import random
|
||||
import sequtils
|
||||
import unittest
|
||||
|
||||
|
@ -82,15 +83,17 @@ proc predictPortRange*(localPort: Port, probedPorts: seq[Port]): seq[Port] =
|
|||
result.add(Port(maxPort.subtractOffset(i)))
|
||||
return
|
||||
# assume symmetric NAT with random port mapping
|
||||
let portRange = maxPort - minPort
|
||||
let first = if portRange > RandomPortCount:
|
||||
minPort
|
||||
randomize()
|
||||
let first = if minPort >= 1024 + 5000:
|
||||
minPort - 5000
|
||||
else:
|
||||
let notCovered = RandomPortCount - portRange
|
||||
max(minPort - notCovered shr 1, 1024)
|
||||
let last = first + RandomPortCount
|
||||
for i in first .. last:
|
||||
result.add(Port(i))
|
||||
1024
|
||||
let last = if maxPort <= uint16.high - 5000:
|
||||
maxPort + 5000
|
||||
else:
|
||||
uint16.high
|
||||
for _ in 1 .. RandomPortCount:
|
||||
result.add(Port(rand(first .. last)))
|
||||
|
||||
suite "port prediction tests":
|
||||
test "single port":
|
||||
|
@ -153,13 +156,6 @@ suite "port prediction tests":
|
|||
let predicted = predictPortRange(Port(1234), @[Port(1039), Port(1030)])
|
||||
check(predicted == @[Port(65533)])
|
||||
|
||||
test "random mapping, distance > RandomPortCount":
|
||||
test "random mapping":
|
||||
let predicted = predictPortRange(Port(1234), @[Port(3546), Port(7624)])
|
||||
check(predicted == toSeq(countup(3546'u16, 3546'u16 + RandomPortCount)).map(toPort))
|
||||
|
||||
test "random mapping, distance < RandomPortCount":
|
||||
let centerPort = 30000'u16
|
||||
let minPort = centerPort - RandomPortCount.uint16 shr 1 + 1
|
||||
let maxPort = centerPort + RandomPortCount.uint16 shr 1 - 1
|
||||
let predicted = predictPortRange(Port(centerPort), @[Port(minPort), Port(maxPort)])
|
||||
check(predicted == toSeq(countup(minPort - 1, maxPort + 1)).map(toPort))
|
||||
check(predicted.len == RandomPortCount)
|
||||
|
|
Loading…
Reference in New Issue