Skip to content

Commit

Permalink
Use getaddrinfo instead of resolving the A record during query
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkjt2000 committed Mar 27, 2022
1 parent 3cfcc34 commit 4fbe334
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
17 changes: 12 additions & 5 deletions mcstatus/protocol/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,22 @@ def __del__(self):
pass


class NoConnectionsPossibleError(Exception):
pass


class UDPSocketConnection(Connection):
def __init__(self, addr: Address, timeout: float = 3):
Connection.__init__(self)
self.addr = addr
self.socket = socket.socket(
socket.AF_INET if ip_type(addr[0]) == 4 else socket.AF_INET6,
socket.SOCK_DGRAM,
)
self.socket.settimeout(timeout)
possible_connections = socket.getaddrinfo(addr.host, addr.port, type=socket.SOCK_DGRAM)
for conn in possible_connections:
self.socket = socket.socket(*conn[0:2])
self.socket.settimeout(timeout)
break
else:
raise NoConnectionsPossibleError
self.socket.connect(conn[-1])

def flush(self) -> bytearray:
raise NotImplementedError("UDPSocketConnection does not support flush()")
Expand Down
7 changes: 2 additions & 5 deletions mcstatus/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ def query(self) -> QueryResponse:
:return: Query status information in a `QueryResponse` instance.
:rtype: QueryResponse
"""
ip = str(self.address.resolve_ip())
return self._retry_query(Address(ip, self.address.port))
return self._retry_query(self.address)

@retry(tries=3)
def _retry_query(self, addr: Address) -> QueryResponse:
Expand All @@ -162,9 +161,7 @@ async def async_query(self) -> QueryResponse:
:return: Query status information in a `QueryResponse` instance.
:rtype: QueryResponse
"""
ip = str(self.address.async_resolve_ip())
return await self._retry_async_query(Address(ip, self.port))
return await self._retry_async_query(Address(ip, self.address.port))
return await self._retry_async_query(self.address)

@retry(tries=3)
async def _retry_async_query(self, address: Address) -> QueryResponse:
Expand Down

0 comments on commit 4fbe334

Please sign in to comment.