Medusa
Medusa copied to clipboard
Local IP address resolution fails on macOS
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.
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.
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.