diff --git a/monkey/monkey_island/cc/resources/ip_addresses.py b/monkey/monkey_island/cc/resources/ip_addresses.py index 3ba7d6123..8bd6a8b97 100644 --- a/monkey/monkey_island/cc/resources/ip_addresses.py +++ b/monkey/monkey_island/cc/resources/ip_addresses.py @@ -1,3 +1,4 @@ +from ipaddress import IPv4Address from typing import Sequence from monkey_island.cc.resources.AbstractResource import AbstractResource @@ -11,8 +12,8 @@ class IPAddresses(AbstractResource): urls = ["/api/island/ip-addresses"] - def __init__(self, ip_addresses: Sequence[str]): - self._ips = ip_addresses + def __init__(self, ip_addresses: Sequence[IPv4Address]): + self._ips = list(map(str, ip_addresses)) @jwt_required def get(self) -> Sequence[str]: diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index f488eff2e..e7771a332 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -2,6 +2,7 @@ import atexit import json import logging import sys +from ipaddress import IPv4Address from pathlib import Path from threading import Thread from typing import Optional, Sequence, Tuple @@ -23,7 +24,7 @@ if str(MONKEY_ISLAND_DIR_BASE_PATH) not in sys.path: sys.path.insert(0, MONKEY_ISLAND_DIR_BASE_PATH) from common import DIContainer # noqa: E402 -from common.network.network_utils import get_my_ip_addresses_legacy # noqa: E402 +from common.network.network_utils import get_my_ip_addresses # noqa: E402 from common.version import get_version # noqa: E402 from monkey_island.cc.app import init_app # noqa: E402 from monkey_island.cc.arg_parser import IslandCmdArgs # noqa: E402 @@ -100,10 +101,10 @@ def _configure_logging(config_options): setup_logging(config_options.data_dir, config_options.log_level) -def _collect_system_info() -> Tuple[Sequence[str], Deployment, Version]: +def _collect_system_info() -> Tuple[Sequence[IPv4Address], Deployment, Version]: deployment = _get_deployment() version = Version(get_version(), deployment) - return (get_my_ip_addresses_legacy(), deployment, version) + return (get_my_ip_addresses(), deployment, version) def _get_deployment() -> Deployment: @@ -122,11 +123,11 @@ def _get_deployment() -> Deployment: def _initialize_di_container( - ip_addresses: Sequence[str], version: Version, data_dir: Path + ip_addresses: Sequence[IPv4Address], version: Version, data_dir: Path ) -> DIContainer: container = DIContainer() - container.register_convention(Sequence[str], "ip_addresses", ip_addresses) + container.register_convention(Sequence[IPv4Address], "ip_addresses", ip_addresses) container.register_instance(Version, version) container.register_convention(Path, "data_dir", data_dir) container.register_convention(Path, "island_log_file_path", get_log_file_path(data_dir)) @@ -168,7 +169,7 @@ def _connect_to_mongodb(mongo_db_process: Optional[MongoDbProcess]): def _start_island_server( - ip_addresses: Sequence[str], + ip_addresses: Sequence[IPv4Address], should_setup_only: bool, config_options: IslandConfigOptions, container: DIContainer, @@ -217,14 +218,14 @@ def _get_wsgi_server_logger() -> logging.Logger: return wsgi_server_logger -def _log_init_info(ip_addresses: Sequence[str]): +def _log_init_info(ip_addresses: Sequence[IPv4Address]): logger.info("Monkey Island Server is running!") logger.info(f"version: {get_version()}") _log_web_interface_access_urls(ip_addresses) -def _log_web_interface_access_urls(ip_addresses: Sequence[str]): +def _log_web_interface_access_urls(ip_addresses: Sequence[IPv4Address]): web_interface_urls = ", ".join([f"https://{ip}:{ISLAND_PORT}" for ip in ip_addresses]) logger.info( "To access the web interface, navigate to one of the the following URLs using your " diff --git a/monkey/monkey_island/cc/services/run_local_monkey.py b/monkey/monkey_island/cc/services/run_local_monkey.py index 36c348b85..6455399e3 100644 --- a/monkey/monkey_island/cc/services/run_local_monkey.py +++ b/monkey/monkey_island/cc/services/run_local_monkey.py @@ -2,6 +2,7 @@ import logging import platform import stat import subprocess +from ipaddress import IPv4Address from pathlib import Path from shutil import copyfileobj from typing import Sequence @@ -19,7 +20,7 @@ class LocalMonkeyRunService: self, data_dir: Path, agent_binary_repository: IAgentBinaryRepository, - ip_addresses: Sequence[str], + ip_addresses: Sequence[IPv4Address], ): self._data_dir = data_dir self._agent_binary_repository = agent_binary_repository