1
0
mirror of https://github.com/r4sas/Niflheim-api synced 2025-01-13 00:17:57 +00:00
yggdrasil-api/vserv.py

104 lines
2.8 KiB
Python
Raw Normal View History

2018-07-01 00:15:34 +00:00
#server for collecting DHT info
2018-07-01 00:09:16 +00:00
import json
import time
import sqlite3
from sqlite3 import Error
import os
import socket
#ipaddress needs to be installed its not part of the standard lib
import ipaddress
import thread
SERVER = ""
DB_PATH = "vservdb/"
def check_coords(coords):
coord_len = coords.replace(' ', '').replace('[', '').replace(']', '')
digits_found = [x for x in coord_len if x.isdigit()]
2018-07-01 00:15:34 +00:00
2018-07-01 00:09:16 +00:00
if len(digits_found) == len(coord_len):
crus = True
else:
crus = False
return crus
def valid_ipv6_check(ipv6add):
try:
if ipaddress.IPv6Address(unicode(ipv6add)):
addr = True
except:
addr = False
return addr
def isdatabase(db_path):
if not os.path.exists(db_path + "yggindex.db"):
if not os.path.exists(db_path):
os.makedirs(db_path)
try:
conn = sqlite3.connect(db_path + 'yggindex.db')
c = conn.cursor()
2018-07-01 23:23:56 +00:00
c.execute('''create table yggindex(ipv6 varchar(45) UNIQUE, coords varchar(50),\
time timestamp default (strftime('%s', 'now')))''')
2018-07-01 00:09:16 +00:00
conn.commit()
except Error as e:
2018-07-02 20:01:47 +00:00
print(e)
2018-07-01 00:09:16 +00:00
finally:
conn.close()
else:
2018-07-02 20:01:47 +00:00
print("found database will not create a new one")
2018-07-01 00:09:16 +00:00
2018-07-02 20:32:29 +00:00
def insert_new_entry(db_path, ipv6, coords):
2018-07-01 00:09:16 +00:00
try:
conn = sqlite3.connect(db_path + "yggindex.db")
c = conn.cursor()
2018-07-02 20:32:29 +00:00
c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords) VALUES(?, ?)''',\
(ipv6, coords))
2018-07-01 00:09:16 +00:00
conn.commit()
conn.close()
except Error as e:
2018-07-01 00:15:34 +00:00
print e
2018-07-01 00:09:16 +00:00
def error_check_insert_into_db(dht, switchpeers):
try:
2018-07-01 00:15:34 +00:00
if dht.get("status") == "success":
for x, y in dht["response"]["dht"].iteritems():
2018-07-01 00:09:16 +00:00
if valid_ipv6_check(x) and check_coords(y["coords"]):
insert_new_entry(DB_PATH, x, y["coords"], int(time.time()))
2018-07-01 00:15:34 +00:00
if dht.get("status") == "success":
2018-07-01 00:09:16 +00:00
for x in switchpeers["response"]["switchpeers"].iteritems():
if valid_ipv6_check(x[1]["ip"]) and check_coords(x[1]["coords"]):
insert_new_entry(DB_PATH, x[1]["ip"], x[1]["coords"], int(time.time()))
except:
2018-07-01 00:15:34 +00:00
print"error in json file, aborting"
2018-07-01 00:09:16 +00:00
2018-07-02 20:01:47 +00:00
2018-07-01 00:09:16 +00:00
def proccess_incoming_data(datty, addr):
print addr
try:
2018-07-02 20:01:47 +00:00
ddata = datty.recv(1024 * 20)
data = json.loads(ddata.decode())
2018-07-01 00:09:16 +00:00
error_check_insert_into_db(data["dhtpack"], data["switchpack"])
except:
print "ignoring, data was not json"
2018-07-02 20:01:47 +00:00
2018-07-01 00:09:16 +00:00
isdatabase(DB_PATH)
2018-07-02 20:01:47 +00:00
conn = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
2018-07-01 00:09:16 +00:00
conn.bind((SERVER, 45671))
2018-07-02 20:01:47 +00:00
conn.listen(30)
2018-07-01 00:09:16 +00:00
while True:
try:
2018-07-02 20:01:47 +00:00
dataraw, addr = conn.accept()
2018-07-01 00:09:16 +00:00
thread.start_new_thread(proccess_incoming_data, (dataraw, addr))
except:
print "bloop"