2019-07-05 14:26:25 +00:00
|
|
|
from core.prototypes.AbstractAddressGenerator import AbstractAddressGenerator
|
|
|
|
from core.prototypes.AbstractModuleClass import internal
|
2019-03-23 22:02:55 +00:00
|
|
|
from threading import RLock
|
2019-07-05 14:26:25 +00:00
|
|
|
import ipaddress
|
|
|
|
from types import GeneratorType
|
2019-03-23 22:02:55 +00:00
|
|
|
|
|
|
|
class IpGenerator(AbstractAddressGenerator):
|
|
|
|
|
2019-07-05 14:26:25 +00:00
|
|
|
def set_parsed_fields(self, ips : 'ipv4_ranges', ports : 'ports') -> None:
|
|
|
|
self.ips = ips
|
2019-03-23 22:02:55 +00:00
|
|
|
self.ports = ports
|
|
|
|
self.lock = RLock()
|
2019-07-05 14:26:25 +00:00
|
|
|
|
|
|
|
@internal
|
2019-03-23 22:02:55 +00:00
|
|
|
def get_next_port_number(self, previous_port):
|
|
|
|
return (self.ports.index(previous_port) + 1) % len(self.ports)
|
|
|
|
|
2019-07-05 14:26:25 +00:00
|
|
|
def get_next_address(self, previous_address : 'ipv4', previous_port :
|
|
|
|
'port') -> {'ipv4', 'port'}:
|
|
|
|
result = dict()
|
2019-03-23 22:02:55 +00:00
|
|
|
with self.lock:
|
|
|
|
portnum = 0
|
|
|
|
next_ip = None
|
|
|
|
if previous_address:
|
2019-07-05 14:26:25 +00:00
|
|
|
next_ip = previous_address
|
|
|
|
port = previous_port
|
2019-03-23 22:02:55 +00:00
|
|
|
portnum = self.get_next_port_number(port)
|
|
|
|
if (portnum == 0):
|
2019-07-05 14:26:25 +00:00
|
|
|
next_ip = None
|
2019-03-23 22:02:55 +00:00
|
|
|
try:
|
2019-07-05 14:26:25 +00:00
|
|
|
while not next_ip:
|
|
|
|
if isinstance(self.ips[0], ipaddress.IPv4Address):
|
|
|
|
next_ip = self.ips.pop(0)
|
|
|
|
else:
|
|
|
|
if not isinstance(self.ips[0], GeneratorType):
|
|
|
|
self.ips[0] = self.ips[0].hosts()
|
|
|
|
try:
|
|
|
|
next_ip = next(self.ips[0])
|
|
|
|
except StopIteration:
|
|
|
|
self.ips.pop(0)
|
|
|
|
except IndexError:
|
2019-03-23 22:02:55 +00:00
|
|
|
return None
|
|
|
|
result["ipv4"] = next_ip
|
|
|
|
result["port"] = self.ports[portnum]
|
|
|
|
return result
|
2019-07-05 14:26:25 +00:00
|
|
|
def get_all_addresses(self) -> {'ipv4_ranges', 'ports'}:
|
|
|
|
result = dict()
|
|
|
|
result['ipv4_ranges'] = self.ips
|
|
|
|
result['ports'] = ports
|
|
|
|
return result
|