diff --git a/mcstatus/scripts/address_tools.py b/mcstatus/scripts/address_tools.py index dd9b00b..138560f 100644 --- a/mcstatus/scripts/address_tools.py +++ b/mcstatus/scripts/address_tools.py @@ -1,52 +1,15 @@ import socket +from urllib.parse import urlparse +from ipaddress import ip_address def ip_type(address): try: - socket.inet_pton(socket.AF_INET, address) - except AttributeError: - try: - socket.inet_aton(address) - except socket.error: - return False - return 4 if address.count('.') == 3 else None - except socket.error: - try: - socket.inet_pton(socket.AF_INET6, address) - return 6 - except socket.error: - return None - return 4 + return ip_address(address).version + except ValueError: + return None def parse_address(address): - parts = address.split(":") - if len(parts) == 1: - return (address, None) - elif len(parts) == 2: - try: - return (parts[0], int(parts[1])) - except: - raise ValueError("Invalid address '%s'" % address) - elif len(parts) < 10: - tmp = address - port = None - - if len(parts[0]) and len(parts[-2]) and "[" == parts[0][0] and "]" == parts[-2][-1] : - if not parts[-1].isdigit(): - raise ValueError("Invalid address '%s'" % address) - - port = int(parts[-1]) - parts[0] = parts[0][1:] - parts[-2] = parts[-2][:-1] - tmp = ':'.join(parts[0:-1]) - else: - tmp = tmp.replace("[", "").replace("]", "") - - if not ip_type(tmp) == 6: - raise ValueError("Invalid address '%s'" % address) - else: - return (tmp, port) - parts[0] = parts[0][1:] - parts[-2] = parts[0][:-1] - - else: - raise ValueError("Invalid address '%s'" % address) \ No newline at end of file + tmp = urlparse("//"+address) + if not tmp.hostname: + raise ValueError("Invalid address '%s'" % address) + return (tmp.hostname, tmp.port) \ No newline at end of file