respondd_poller up

master
Niklas Yann Wettengel 3 years ago
parent 7bee75f9e5
commit aa15d8285c

@ -35,6 +35,7 @@ with open("/etc/respondd_poller.json", "r") as f:
wg = WireGuard() wg = WireGuard()
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
last_request = dict() last_request = dict()
last_http_request = dict()
last_response = dict() last_response = dict()
def get_wg_peers(): def get_wg_peers():
@ -51,6 +52,9 @@ def inflate(data):
return inflated.decode() return inflated.decode()
def cleanup(): def cleanup():
global last_request
global last_http_request
global last_response
while True: while True:
time.sleep(60) time.sleep(60)
old = time.monotonic() - 360 old = time.monotonic() - 360
@ -62,6 +66,7 @@ def cleanup():
for ip in ips: for ip in ips:
del last_response[ip] del last_response[ip]
del last_request[ip] del last_request[ip]
del last_http_request[ip]
def recv(): def recv():
global sock global sock
@ -81,8 +86,10 @@ def send(ip):
def get_http_nodeinfo(ip): def get_http_nodeinfo(ip):
global last_request global last_request
now = time.monotonic() global last_http_request
global last_response
try: try:
print("get_http_nodeinfo", ip)
status = requests.get('http://[' + str(ip) + ']/cgi-bin/status') status = requests.get('http://[' + str(ip) + ']/cgi-bin/status')
except: except:
return return
@ -104,12 +111,18 @@ def get_http_nodeinfo(ip):
for address in data["network"]["addresses"]: for address in data["network"]["addresses"]:
if ipaddress.IPv6Network(address).subnet_of(prefix): if ipaddress.IPv6Network(address).subnet_of(prefix):
node_ip = ipaddress.IPv6Address(address) node_ip = ipaddress.IPv6Address(address)
now = time.monotonic()
if node_ip not in last_request: if node_ip not in last_request:
last_request[node_ip] = now last_request[node_ip] = now
last_response[node_ip] = now last_response[node_ip] = now
if node_ip not in last_http_request or now - last_http_request[node_ip] > 300:
last_http_request[node_ip] = now
get_http_nodeinfo(node_ip)
def scan_wg_peers(): def scan_wg_peers():
global last_request global last_request
global last_http_request
global last_response
while True: while True:
print("scanning wg peers") print("scanning wg peers")
request_threads = [] request_threads = []
@ -118,6 +131,7 @@ def scan_wg_peers():
ip = ipaddress.IPv6Address(str(net.network_address) + "1") ip = ipaddress.IPv6Address(str(net.network_address) + "1")
if ip not in last_request: if ip not in last_request:
last_request[ip] = now last_request[ip] = now
last_http_request[ip] = now
last_response[ip] = now last_response[ip] = now
request_thread = threading.Thread(target=get_http_nodeinfo, args=(ip,)) request_thread = threading.Thread(target=get_http_nodeinfo, args=(ip,))
request_thread.start() request_thread.start()
@ -139,8 +153,8 @@ scan_thread.start()
last_wg_time = 0 last_wg_time = 0
while True: while True:
now = time.monotonic()
for ip in last_request: for ip in last_request:
now = time.monotonic()
if now - last_request[ip] > 15: if now - last_request[ip] > 15:
last_request[ip] = now last_request[ip] = now
send(ip) send(ip)

Loading…
Cancel
Save