|
@@ -1,4 +1,4 @@
|
|
|
-import random
|
|
|
+import random, copy
|
|
|
|
|
|
# information taken from https://www.cymru.com/jtk/misc/ephemeralports.html
|
|
|
class PortRanges:
|
|
@@ -16,31 +16,28 @@ class PortRanges:
|
|
|
WINDOWS_VISTA = DYNAMIC_PORTS
|
|
|
WINDOWS_XP = range(1024, 5001)
|
|
|
|
|
|
+# This class uses classes instead of functions so deepcloning works
|
|
|
class PortSelectionStrategy:
|
|
|
- @staticmethod
|
|
|
- def sequential():
|
|
|
- counter = -1
|
|
|
+ class sequential:
|
|
|
+ def __init__(self):
|
|
|
+ self.counter = -1
|
|
|
|
|
|
# that function will always return a one higher counter than before,
|
|
|
# restarting from the start once it reached the highest value
|
|
|
- def select_port(port_range):
|
|
|
- global counter
|
|
|
- if counter == -1:
|
|
|
- counter = port_range.start
|
|
|
+ def __call__(port_range):
|
|
|
+ if self.counter == -1:
|
|
|
+ self.counter = port_range.start
|
|
|
|
|
|
port = counter
|
|
|
|
|
|
- counter += 1
|
|
|
- if counter == port_range.stop:
|
|
|
- counter = port_range.start
|
|
|
+ self.counter += 1
|
|
|
+ if self.counter == port_range.stop:
|
|
|
+ self.counter = port_range.start
|
|
|
|
|
|
return port
|
|
|
-
|
|
|
- return select_port
|
|
|
-
|
|
|
- @staticmethod
|
|
|
- def random(port_range):
|
|
|
- return random.randrange(port_range.start, port_range.stop)
|
|
|
+ class random:
|
|
|
+ def __call__(port_range):
|
|
|
+ return random.randrange(port_range.start, port_range.stop)
|
|
|
|
|
|
class PortSelector:
|
|
|
def __init__(self, port_range, select_function):
|
|
@@ -75,6 +72,9 @@ class PortSelector:
|
|
|
|
|
|
def clear(self):
|
|
|
self.generated = []
|
|
|
+
|
|
|
+ def clone(self):
|
|
|
+ return copy.deepcopy(self)
|
|
|
|
|
|
class ProtocolPortSelector:
|
|
|
def __init__(self, port_range, select_tcp, select_udp = None):
|
|
@@ -99,6 +99,16 @@ class ProtocolPortSelector:
|
|
|
def is_port_in_use_udp(self, port):
|
|
|
return self.udp.is_port_in_use(port)
|
|
|
|
|
|
+ def clone(self):
|
|
|
+ class Tmp: pass
|
|
|
+ clone = Tmp()
|
|
|
+ clone.__class__ = type(self)
|
|
|
+
|
|
|
+ clone.udp = self.udp.clone()
|
|
|
+ clone.tcp = self.tcp.clone()
|
|
|
+
|
|
|
+ return clone
|
|
|
+
|
|
|
def __getattr__(self, attr):
|
|
|
val = getattr(self.tcp, attr)
|
|
|
|