diff --git a/aiocraft/client.py b/aiocraft/client.py index 805b03f..83e45c2 100644 --- a/aiocraft/client.py +++ b/aiocraft/client.py @@ -10,6 +10,8 @@ from time import time from typing import Dict, List, Callable, Type, Optional, Tuple, AsyncIterator, Any, Set +import dns.resolver + from .dispatcher import Dispatcher from .mc.packet import Packet from .mc.auth import AuthInterface, AuthException, MojangAuthenticator, MicrosoftAuthenticator @@ -41,24 +43,28 @@ class MinecraftClient: server:str, authenticator:AuthInterface, online_mode:bool = True, + force_port:int = 0, + resolve_srv:bool = True, ): - super().__init__() - if ":" in server: - _host, _port = server.split(":", 1) - host = _host.strip() - port = int(_port) - else: - host = server.strip() - port = 25565 - + self.logger = LOGGER.getChild(f"on({server})") self.online_mode = online_mode self.authenticator = authenticator self._authenticated = False - - self.dispatcher = Dispatcher().set_host(host, port) self._processing = False - self.logger = LOGGER.getChild(f"on({server})") + host = server + port = force_port or 25565 + + if resolve_srv: + try: + answ = dns.resolver.resolve(f"_minecraft._tcp.{server}", "SRV") + # TODO can we just use the 1st record? + host = str(answ[0].target).rstrip('.') + port = answ[0].port + except dns.resolver.exception.DNSException: # TODO is this the right thing to catch? + self.logger.warning("Failed resolving SRV record for '%s'", server) + + self.dispatcher = Dispatcher().set_host(host, port) @property def connected(self) -> bool: diff --git a/pyproject.toml b/pyproject.toml index e7e28bb..5cbfbb3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,8 @@ dependencies = [ "pynbt", "cryptography", "aiohttp", - "termcolor" + "termcolor", + "dnspython" ] # [project.optional-dependencies]