Skip to content

Commit

Permalink
check my ip address and init when changed
Browse files Browse the repository at this point in the history
  • Loading branch information
fuktommy committed Feb 9, 2025
1 parent 5701af3 commit c376997
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 65 deletions.
4 changes: 2 additions & 2 deletions doc/supervisor.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ directory = /var/local/run/saku
user = shingetsu

autorestart = true
stdout_logfile = NONE
stderr_logfile = NONE
stdout_logfile = /var/local/log/saku/stdout.log
stderr_logfile = /var/local/log/saku/stderr.log
5 changes: 3 additions & 2 deletions file/message-en.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# Message file for English
# Copyright (c) 2005-2015 shinGETsu Project.
# Copyright (c) 2005 shinGETsu Project.
#

lang<>en
Expand Down Expand Up @@ -104,7 +104,8 @@ known_nodes<>Known Nodes
files<>BBSes
records<>Articles
cache_size<>Cache Size
self_node<>Self node
self_node_ipv4<>Self node (IPv4)
self_node_ipv6<>Self node (IPv6)

# misc
limit<>limit
Expand Down
5 changes: 3 additions & 2 deletions file/message-ja.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# Message file for Japanese
# Copyright (c) 2005-2015 shinGETsu Project.
# Copyright (c) 2005 shinGETsu Project.
#

lang<>ja
Expand Down Expand Up @@ -106,7 +106,8 @@ known_nodes<>探索ノード
files<>掲示板の数
records<>書き込みの数
cache_size<>キャッシュサイズ
self_node<>自分自身のノード
self_node_ipv4<>自分自身のノード(IPv4)
self_node_ipv6<>自分自身のノード(IPv6)

# misc
limit<>最大
Expand Down
5 changes: 3 additions & 2 deletions shingetsu/admin_cgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,15 @@ def print_status(self):
for cache in cachelist:
records += len(cache)
size += cache.size
myself = nodelist.myself()
myself4, myself6 = nodelist.myself()
status = (('linked_nodes', len(nodelist)),
('known_nodes', len(searchlist)),
('files', len(cachelist)),
('records', records),
('cache_size', '%.1f%s' % (size/1024/1024,
self.message['mb'])),
('self_node', myself))
('self_node_ipv4', myself4),
('self_node_ipv6', myself6))
node_status = (('linked_nodes', nodelist),
('known_nodes', searchlist))
var = {
Expand Down
17 changes: 8 additions & 9 deletions shingetsu/cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Cache of Saku BBS.
"""
#
# Copyright (c) 2005-2023 shinGETsu Project.
# Copyright (c) 2005 shinGETsu Project.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -764,18 +764,17 @@ def remove_records(self, now, limit):

self.sync_status()

def search(self, searchlist=None, myself=None):
def search(self, searchlist=None, myself4=None, myself6=None):
"""Search node from network and get records."""
self.standby_directories()
if searchlist is None:
searchlist = SearchList()
if not myself:
if not myself4 or not myself6:
nodelist = NodeList()
myself = nodelist.myself()
myself4, myself6 = nodelist.myself()
lookuptable = LookupTable()
node = searchlist.search(self,
myself = myself,
nodes = lookuptable.get(self.datfile, []))
nodes = lookuptable.get(self.datfile, [])
node = searchlist.search(self, myself4=myself4, myself6=myself6, nodes=nodes)
if node is not None:
nodelist = NodeList()
if node not in nodelist:
Expand Down Expand Up @@ -857,7 +856,7 @@ def getall(self, timelimit=0):
now = int(time())
random.shuffle(self)
nodelist = NodeList()
myself = nodelist.myself()
myself4, myself6 = nodelist.myself()
searchlist = SearchList()
for cache in self:
if int(time()) > timelimit:
Expand All @@ -866,7 +865,7 @@ def getall(self, timelimit=0):
elif not cache.exists():
pass
else:
cache.search(searchlist=searchlist, myself=myself)
cache.search(searchlist=searchlist, myself4=myself4, myself6=myself6)
cache.size = 0
cache.count = 0
cache.velocity = 0
Expand Down
4 changes: 1 addition & 3 deletions shingetsu/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Saku Configuration.
"""
#
# Copyright (c) 2005-2024 shinGETsu Project.
# Copyright (c) 2005 shinGETsu Project.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -165,8 +165,6 @@ def _get_version():
search_depth = 30 # Search node size
tiedfile_cache_size = 30

broadcast = "../tool/broadcast.py" # Broadcast script path

rss = "1" # RSS version; must be "1"
language = "en" # Language code (see RFC3066)

Expand Down
23 changes: 20 additions & 3 deletions shingetsu/crond.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''Cron daemon running in another thread for client.cgi.
'''
#
# Copyright (c) 2005-2023 shinGETsu Project.
# Copyright (c) 2005 shinGETsu Project.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -42,6 +42,10 @@
from .util import opentext


_myself4 = None
_myself6 = None


class Crond(Thread):
gc_counter = {}

Expand Down Expand Up @@ -150,15 +154,28 @@ def run(self):
status = Status.get_instance()
self.do_update()

global _myself4, _myself6
nodelist = NodeList()
myself4, myself6 = nodelist.myself()
print("myself: %s, %s" % (myself4, myself6))
if not _myself4 and not _myself6:
_myself4 = myself4
_myself6 = myself6

if len(nodelist) == 0:
self.do_init()
nodelist = NodeList()
if nodelist:
self.do_sync()
status = Status.get_instance()

if (int(time.time()) - status["init"]
elif myself4 != _myself4 or myself6 != _myself6:
print("changed myself: %s -> %s, %s -> %s" % (_myself4, myself4, _myself6, myself6))
_myself4 = myself4
_myself6 = myself6
self.do_init()
nodelist = NodeList()
status = Status.get_instance()
elif (int(time.time()) - status["init"]
>= config.init_cycle * len(nodelist)):
self.do_init()
status = Status.get_instance()
Expand Down
101 changes: 59 additions & 42 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-2024 shinGETsu Project.
# Copyright (c) 2005 shinGETsu Project.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -80,21 +80,29 @@ def node_deny():


class Broadcast(threading.Thread):
def __init__(self, msg, cache):
def __init__(self, msg4, msg6, cache):
threading.Thread.__init__(self)
self.msg = msg
self.msg4 = msg4
self.msg6 = msg6
self.cache = cache

def run(self):
nodelist = NodeList()
for node in self.cache.node:
if (node in nodelist) or node.ping():
node.talk(self.msg)
if self.msg6 and node.isv6:
node.talk(self.msg6)
elif self.msg4:
node.talk(self.msg4)
else:
self.cache.node.remove(node)
self.cache.node.sync()
for node in nodelist:
if node not in self.cache.node:
if self.msg6 and node.isv6:
node.talk(self.msg6)
elif self.msg4:
node.talk(self.msg4)
node.talk(self.msg)


Expand Down Expand Up @@ -299,35 +307,33 @@ def myself(self):
path = config.server
dnsname = config.dnsname
if dnsname != "":
return Node(host=dnsname, port=port, path=path)
n = Node(host=dnsname, port=port, path=path)
return n, n
addr4 = {}
addr6 = {}
for n in self:
try:
res = n.talk('ping')
lines = iter(res)
buf = (next(lines), next(lines))
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)
return None
if buf[0].strip() != 'PONG':
continue
addr = buf[1].strip()
if ':' in buf[1]:
addr6[addr] = addr6.get(addr, 0) + 1
else:
addr4[addr] = addr4.get(addr, 0) + 1
except Exception as err:
sys.stderr.write('/ping %s: error: %s\n' % (n, err))
node4 = None
node6 = None
if addr4:
addr = sorted(addr4.keys(), key=addr4.get)[0]
node4 = Node(host=addr, port=port, path=path)
if addr6:
addr = sorted(addr6.keys(), key=addr6.get)[0]
node6 = Node(host=addr, port=port, path=path)
return node4, node6

def pingall(self):
"""Ping all nodes."""
Expand Down Expand Up @@ -365,10 +371,9 @@ def init(self):
if inode.ping():
self.join(inode)
break
myself = self.myself()
if myself and (myself in self):
self.remove(myself)
myself6 = self.myself6()
myself4, myself6 = self.myself()
if myself4 and (myself4 in self):
self.remove(myself4)
if myself6 and (myself6 in self):
self.remove(myself6)
if len(self) == 0:
Expand Down Expand Up @@ -408,7 +413,6 @@ def init(self):

def rejoin(self, searchlist):
"""Copy node from searchlist to nodelist."""
myself = self.myself()
do_join = False
for n in searchlist:
if n in self:
Expand All @@ -433,17 +437,28 @@ def tell_update(self, cache, stamp="", id="", node=None):
If node is None, node is myself.
"""
if node:
tellstr = node.toxstring()
tellstr4 = node.toxstring()
tellstr6 = tellstr4
elif config.dnsname:
tellstr = self.myself().toxstring()
myself4, myself6 = self.myself()
tellstr4 = None
tellstr6 = None
if myself4:
tellstr4 = myself4.toxstring()
if myself6:
tellstr6 = myself6.toxstring()
else:
tellstr = ":" + \
str(config.port) + \
config.server.replace("/", "+")
tellstr4 = ":" + str(config.port) + config.server.replace("/", "+")
tellstr6 = tellstr4

arg = "/".join(("", "update", cache.datfile, str(stamp), id, tellstr))
tellstr4 = None
tellstr6 = None
if tellstr4:
msg4 = "/".join(("", "update", cache.datfile, str(stamp), id, tellstr4))
if tellstr6:
msg6 = "/".join(("", "update", cache.datfile, str(stamp), id, tellstr6))

broadcast = Broadcast(arg, cache)
broadcast = Broadcast(msg4, msg6, cache)
broadcast.start()

# End of NodeList
Expand All @@ -462,7 +477,7 @@ def join(self, node):
if node not in self:
self.append(node)

def search(self, cache=None, myself=None, nodes=None):
def search(self, cache=None, myself4=None, myself6=None, nodes=None):
"""Search node which has the file."""
nodelist = NodeList()
random.shuffle(self)
Expand All @@ -476,7 +491,9 @@ def search(self, cache=None, myself=None, nodes=None):
else:
target = self
for n in target:
if myself and (n == myself):
if myself4 and (n == myself4):
continue
elif myself6 and (n == myself6):
continue
elif (not node_allow().check(str(n))) and \
node_deny().check(str(n)):
Expand Down

0 comments on commit c376997

Please sign in to comment.