Skip to content

Commit

Permalink
Ensure the IP is valid on server init
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsDrike committed Jan 12, 2022
1 parent 9ec29ee commit da4770a
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions mcstatus/server.py
@@ -1,3 +1,5 @@
import re

from mcstatus.pinger import PingResponse, ServerPinger, AsyncServerPinger
from mcstatus.protocol.connection import (
TCPSocketConnection,
Expand All @@ -13,6 +15,17 @@
__all__ = ["MinecraftServer", "MinecraftBedrockServer"]


def ensure_valid_ip(host: object, port: object):
if not isinstance(host, str):
raise TypeError(f"Host must be a string address, got {type(host)} ({host!r})")
if not isinstance(port, int):
raise TypeError(f"Port must be an integer port number, got {type(port)} ({port})")
if port >= 36635 or port < 0:
raise ValueError(f"Port must be within the allowed range (0-2^16), got {port}")
if not re.fullmatch(r"(\w+\.)*\w+", host):
raise ValueError(f"Invalid host address, {host!r} (doesn't match the required pattern)")


class MinecraftServer:
"""Base class for a Minecraft Java Edition server.
Expand All @@ -24,6 +37,7 @@ class MinecraftServer:
"""

def __init__(self, host: str, port: int = 25565, timeout: float = 3):
ensure_valid_ip(host, port)
self.host = host
self.port = port
self.timeout = timeout
Expand Down Expand Up @@ -212,6 +226,7 @@ class MinecraftBedrockServer:
"""

def __init__(self, host: str, port: int = 19132, timeout: float = 3):
ensure_valid_ip(host, port)
self.host = host
self.port = port
self.timeout = timeout
Expand Down

0 comments on commit da4770a

Please sign in to comment.