Added automatic SRV check for host and port
This commit is contained in:
parent
70e89fc4a9
commit
7ffc69d8c2
2 changed files with 20 additions and 13 deletions
|
@ -10,6 +10,8 @@ from time import time
|
||||||
|
|
||||||
from typing import Dict, List, Callable, Type, Optional, Tuple, AsyncIterator, Any, Set
|
from typing import Dict, List, Callable, Type, Optional, Tuple, AsyncIterator, Any, Set
|
||||||
|
|
||||||
|
import dns.resolver
|
||||||
|
|
||||||
from .dispatcher import Dispatcher
|
from .dispatcher import Dispatcher
|
||||||
from .mc.packet import Packet
|
from .mc.packet import Packet
|
||||||
from .mc.auth import AuthInterface, AuthException, MojangAuthenticator, MicrosoftAuthenticator
|
from .mc.auth import AuthInterface, AuthException, MojangAuthenticator, MicrosoftAuthenticator
|
||||||
|
@ -41,24 +43,28 @@ class MinecraftClient:
|
||||||
server:str,
|
server:str,
|
||||||
authenticator:AuthInterface,
|
authenticator:AuthInterface,
|
||||||
online_mode:bool = True,
|
online_mode:bool = True,
|
||||||
|
force_port:int = 0,
|
||||||
|
resolve_srv:bool = True,
|
||||||
):
|
):
|
||||||
super().__init__()
|
self.logger = LOGGER.getChild(f"on({server})")
|
||||||
if ":" in server:
|
|
||||||
_host, _port = server.split(":", 1)
|
|
||||||
host = _host.strip()
|
|
||||||
port = int(_port)
|
|
||||||
else:
|
|
||||||
host = server.strip()
|
|
||||||
port = 25565
|
|
||||||
|
|
||||||
self.online_mode = online_mode
|
self.online_mode = online_mode
|
||||||
self.authenticator = authenticator
|
self.authenticator = authenticator
|
||||||
self._authenticated = False
|
self._authenticated = False
|
||||||
|
|
||||||
self.dispatcher = Dispatcher().set_host(host, port)
|
|
||||||
self._processing = False
|
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
|
@property
|
||||||
def connected(self) -> bool:
|
def connected(self) -> bool:
|
||||||
|
|
|
@ -35,7 +35,8 @@ dependencies = [
|
||||||
"pynbt",
|
"pynbt",
|
||||||
"cryptography",
|
"cryptography",
|
||||||
"aiohttp",
|
"aiohttp",
|
||||||
"termcolor"
|
"termcolor",
|
||||||
|
"dnspython"
|
||||||
]
|
]
|
||||||
|
|
||||||
# [project.optional-dependencies]
|
# [project.optional-dependencies]
|
||||||
|
|
Loading…
Reference in a new issue