Medusa icon indicating copy to clipboard operation
Medusa copied to clipboard

Local IP address resolution fails on macOS

Open sysophost opened this issue 2 years ago • 2 comments

The agent uses socket.gethostbyname() to retrieve the IP address of the host by looking up the hostname, returned from socket.gethostname(), against either the local resolver or the hosts file.

On macOS the device hostname is not present in /etc/hosts by default, so looking up the hostname fails, causing the agent to exit.

sysophost avatar Jul 20 '22 00:07 sysophost

A potential workaround would be:

def getIpAddress(self):
    ip_address = ""
    try:
        ip_address = socket.gethostbyname(socket.gethostname())
    except socket.gaierror:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect((re.sub("https?://", "", self.agent_config["Server"]), int(self.agent_config["Port"])))
        ip_address = s.getsockname()[0]
    finally:
        return ip_address

Then call this from the checkIn() function with "ip": self.getIpAddress().

This also requires importing re.

I haven't tested this with other transport methods, so the protocol removal using re.sub() would likely need some work to accommodate this.

sysophost avatar Jul 20 '22 01:07 sysophost

I've implemented this fix here but haven't raised a PR as I haven't applied the fix for Py2 or tested any other platforms/transports.

sysophost avatar Jul 25 '22 00:07 sysophost