From e9134bb43f753eb1d34dc34d7a8236fae51f7668 Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Thu, 19 Dec 2024 13:56:05 +0000 Subject: [PATCH] Improve get_ip function --- kasa/transports/sslaestransport.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/kasa/transports/sslaestransport.py b/kasa/transports/sslaestransport.py index 7bb47871..15c2bc01 100644 --- a/kasa/transports/sslaestransport.py +++ b/kasa/transports/sslaestransport.py @@ -197,16 +197,27 @@ class SslAesTransport(BaseTransport): ) return self._ssl_context + async def _get_host_ip(self) -> str: + def get_ip() -> str: + # From https://stackoverflow.com/a/28950776 + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.settimeout(0) + try: + # doesn't even have to be reachable + s.connect(("10.254.254.254", 1)) + ip = s.getsockname()[0] + except Exception: + ip = "127.0.0.1" + finally: + s.close() + return ip + + loop = asyncio.get_running_loop() + return await loop.run_in_executor(None, get_ip) + async def _get_headers(self) -> dict: if not self._headers: - loop = asyncio.get_event_loop() - adrrinfo = await loop.getaddrinfo( - socket.gethostname(), 0, type=socket.SOCK_DGRAM, family=socket.AF_INET - ) - # getaddrinfo returns a list of 5 tuples with the following structure: - # (family, type, proto, canonname, sockaddr) - # where sockaddr is 2 tuple (ip, port). - this_ip = adrrinfo[0][4][0] + this_ip = await self._get_host_ip() self._headers = { **self.COMMON_HEADERS, "Referer": f"https://{this_ip}",