diff --git a/monkey/infection_monkey/control.py b/monkey/infection_monkey/control.py index 58ab43fa6..d1b935899 100644 --- a/monkey/infection_monkey/control.py +++ b/monkey/infection_monkey/control.py @@ -9,6 +9,7 @@ from urllib3 import disable_warnings from common.common_consts.timeouts import LONG_REQUEST_TIMEOUT, MEDIUM_REQUEST_TIMEOUT from common.network.network_utils import get_my_ip_addresses from infection_monkey.config import GUID +from infection_monkey.island_api_client import HTTPIslandAPIClient from infection_monkey.network.info import get_host_subnets from infection_monkey.utils import agent_process @@ -78,13 +79,7 @@ class ControlClient: return try: telemetry = {"monkey_guid": GUID, "log": json.dumps(log)} - requests.post( # noqa: DUO123 - "https://%s/api/log" % (self.server_address,), - data=json.dumps(telemetry), - headers={"content-type": "application/json"}, - verify=False, - timeout=MEDIUM_REQUEST_TIMEOUT, - ) + HTTPIslandAPIClient(self.server_address).send_log(json.dumps(telemetry)) except Exception as exc: logger.warning(f"Error connecting to control server {self.server_address}: {exc}") diff --git a/monkey/infection_monkey/island_api_client/http_island_api_client.py b/monkey/infection_monkey/island_api_client/http_island_api_client.py index d9f9b1d9e..ce5e4338b 100644 --- a/monkey/infection_monkey/island_api_client/http_island_api_client.py +++ b/monkey/infection_monkey/island_api_client/http_island_api_client.py @@ -21,9 +21,20 @@ class HTTPIslandAPIClient(IIslandAPIClient): verify=False, timeout=MEDIUM_REQUEST_TIMEOUT, ) + self._island_server = island_server except requests.exceptions.ConnectionError as err: raise IslandAPIConnectionError(err) except TimeoutError as err: raise IslandAPITimeoutError(err) except Exception as err: raise IslandAPIError(err) + + # TODO: set server address as object property when init is called in find_server and pass + # object around? won't need to pass island server and create object in every function + def send_log(self, data: str): + requests.post( # noqa: DUO123 + "https://%s/api/log" % (self._island_server,), + json=data, + verify=False, + timeout=MEDIUM_REQUEST_TIMEOUT, + )