Move functions for testing for IPv4, IPv6 addresses to nntp.tiny.host

This commit is contained in:
XANTRONIX Development 2024-12-04 16:30:19 -05:00
parent b0cedd492f
commit 36d048713f
2 changed files with 25 additions and 24 deletions

22
lib/nntp/tiny/host.py Normal file
View file

@ -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

View file

@ -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):