Skip to content

Commit

Permalink
ipv6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
fuktommy committed Feb 2, 2024
1 parent 6504b4c commit c328ba9
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions shingetsu/node.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Saku Node and NodeList.
"""
#
# Copyright (c) 2005-2023 shinGETsu Project.
# Copyright (c) 2005-2024 shinGETsu Project.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -124,15 +124,19 @@ class Node:
"""One unit for P2P."""

nodestr = None
isv6 = False

def __init__(self, nodestr=None, host="", port="", path=""):
if nodestr is not None:
nodestr = nodestr.strip()
if not re.search(r"\d+/[^: ]+$", nodestr):
raise NodeError('bad format')
self.nodestr = nodestr.replace('+', '/')
elif ':' in host:
self.nodestr = '[%s]:%d%s' % (host, port, re.sub(r"\+", "/", path))
else:
self.nodestr = host + ":" + str(port) + re.sub(r"\+", "/", path)
self.nodestr = '%s:%d%s' % (host, port, re.sub(r"\+", "/", path))
self.isv6 = self.nodestr.startswith('[')

def __str__(self):
return self.nodestr
Expand Down Expand Up @@ -250,6 +254,12 @@ def sync(self):
def random(self):
return random.choice(self)

def filterv4(self):
return [i for i in self if not i.isv6]

def filterv6(self):
return [i for i in self if i.isv6]

def append(self, node):
if node_allow().check(str(node)):
pass
Expand Down Expand Up @@ -295,7 +305,25 @@ def myself(self):
res = n.talk('ping')
lines = iter(res)
buf = (next(lines), next(lines))
if buf[0].strip() == 'PONG':
if buf[0].strip() == 'PONG' and ':' not in buf[1]:
return Node(host=buf[1].strip(), port=port, path=path)
except StopIteration:
sys.stderr.write('/ping %s: error\n' % n)
return None

def myself6(self):
"""Who am I. (IPv6)"""
port = config.port
path = config.server
dnsname = config.dnsname
if dnsname != "":
return Node(host=dnsname, port=port, path=path)
for n in self:
try:
res = n.talk('ping')
lines = iter(res)
buf = (next(lines), next(lines))
if buf[0].strip() == 'PONG' and ':' in buf[1]:
return Node(host=buf[1].strip(), port=port, path=path)
except StopIteration:
sys.stderr.write('/ping %s: error\n' % n)
Expand Down Expand Up @@ -340,6 +368,9 @@ def init(self):
myself = self.myself()
if myself and (myself in self):
self.remove(myself)
myself6 = self.myself6()
if myself6 and (myself6 in self):
self.remove(myself6)
if len(self) == 0:
return

Expand All @@ -366,8 +397,12 @@ def init(self):
self.remove(inode)
elif len(self) <= 1:
sys.stderr.write("Warning: Few linked nodes.\n")
while len(self) > config.nodes:
n = self.random()
while len(self.filterv4()) > config.nodes:
n = random.choice(self.filterv4())
n.bye()
self.remove(n)
while len(self.filterv6()) > config.nodes:
n = random.choice(self.filterv6())
n.bye()
self.remove(n)

Expand Down

0 comments on commit c328ba9

Please sign in to comment.