diff --git a/lib/nntp/tiny/host.py b/lib/nntp/tiny/host.py new file mode 100644 index 0000000..29eb2c7 --- /dev/null +++ b/lib/nntp/tiny/host.py @@ -0,0 +1,22 @@ +class Host(): + @staticmethod + def is_ipv6(value: str): + return value.find(':') >= 0 + + @staticmethod + def is_ipv4(value: str): + parts = value.split('.') + + if len(parts) > 4 or len(parts) == 0: + return False + + for part in parts: + if not part.isdecimal(): + return False + + num = int(part) + + if num > 255: + return False + + return True diff --git a/lib/nntp/tiny/server.py b/lib/nntp/tiny/server.py index 38669de..b2fc21d 100644 --- a/lib/nntp/tiny/server.py +++ b/lib/nntp/tiny/server.py @@ -7,6 +7,7 @@ import ssl from nntp.tiny.config import Config, ConfigException from nntp.tiny.db import Database +from nntp.tiny.host import Host from nntp.tiny.newsgroup import Newsgroup from nntp.tiny.session import Session @@ -67,28 +68,6 @@ class Server(): thread = threading.Thread(target=spawn) thread.start() - @staticmethod - def _is_ipv6(value: str): - return value.find(':') >= 0 - - @staticmethod - def _is_ipv4(value: str): - parts = value.split('.') - - if len(parts) > 4 or len(parts) == 0: - return False - - for part in parts: - if not part.isdecimal(): - return False - - num = int(part) - - if num > 255: - return False - - return True - def run(self): hosts = re.split(r'\s*,\s*', self.config.get('listen', 'host')) port = int(self.config.get('listen', 'port')) @@ -96,9 +75,9 @@ class Server(): listeners = list() for host in hosts: - if Server._is_ipv6(host): + if Host.is_ipv6(host): listeners.append(self.listen(host, port, socket.AF_INET6)) - elif Server._is_ipv4(host): + elif Host.is_ipv4(host): listeners.append(self.listen(host, port, socket.AF_INET)) else: for af in (socket.AF_INET, socket.AF_INET6):