Agent: Simplify scan_target_generator.py

The responsibility of type-hints are not to implement logic. Implementing logic via type-hints diminishes readability, because it forces you to cross-reference a class instead of just exposing the logic where it's used
This commit is contained in:
vakarisz 2022-09-27 11:33:30 +03:00
parent 311c294033
commit 182a566087
1 changed files with 5 additions and 11 deletions

View File

@ -4,19 +4,12 @@ import socket
from ipaddress import IPv4Interface
from typing import Dict, Iterable, List, Optional, Sequence
from typing_extensions import Protocol, runtime_checkable
from common.network.network_range import InvalidNetworkRangeError, NetworkRange
from infection_monkey.network import NetworkAddress
logger = logging.getLogger(__name__)
@runtime_checkable
class HasDomain(Protocol):
domain_name: str
def compile_scan_target_list(
local_network_interfaces: Sequence[IPv4Interface],
ranges_to_scan: Sequence[str],
@ -56,10 +49,11 @@ def _remove_redundant_targets(targets: Sequence[NetworkAddress]) -> List[Network
def _range_to_addresses(range_obj: NetworkRange) -> List[NetworkAddress]:
addresses = []
for address in range_obj:
if isinstance(range_obj, HasDomain):
addresses.append(NetworkAddress(address, range_obj.domain_name))
else:
addresses.append(NetworkAddress(address, None))
try:
domain = range_obj.domain_name # type: ignore
except AttributeError:
domain = None
addresses.append(NetworkAddress(address, domain))
return addresses