diff --git a/lib/nntp/tiny/server.py b/lib/nntp/tiny/server.py index 2c0687c..d910cd5 100644 --- a/lib/nntp/tiny/server.py +++ b/lib/nntp/tiny/server.py @@ -70,6 +70,28 @@ 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): if not self.config.has_section('listen'): raise ConfigSectionException('listen') @@ -83,10 +105,13 @@ class Server(): listeners = list() for host in hosts: - if host.find(':') < 0: + if Server._is_ipv6(host): + listeners.append(self.listen(host, port, socket.AF_INET6)) + elif Server._is_ipv4(host): listeners.append(self.listen(host, port, socket.AF_INET)) else: - listeners.append(self.listen(host, port, socket.AF_INET6)) + for af in (socket.AF_INET, socket.AF_INET6): + listeners.append(self.listen(host, port, af)) if len(listeners) == 0: raise ConfigException('No listener hosts specified')