1
0
mirror of https://github.com/r4sas/Niflheim-api synced 2025-01-27 07:04:44 +00:00
yggdrasil-api/vserv.py

118 lines
3.4 KiB
Python
Raw Normal View History

2018-07-01 02:15:34 +02:00
#server for collecting DHT info
2018-07-01 02:09:16 +02:00
import json
import time
2018-07-01 02:09:16 +02:00
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 02:15:34 +02:00
2018-07-01 02:09:16 +02: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:
2018-11-13 12:10:54 +01:00
dbconn = sqlite3.connect(db_path + 'yggindex.db')
c = dbconn.cursor()
c.execute('''create table yggindex(ipv6 varchar(45) UNIQUE, coords varchar(50),\
dt datetime default (strftime('%s','now')))''')
c.execute('''create table timeseries(max varchar(45),\
dt datetime default (strftime('%s','now')))''')
c.execute('''create table contrib(ipv6 varchar(45) UNIQUE,\
2018-07-08 16:01:07 +02:00
ut unixtime default (strftime('%s','now')))''')
2018-11-13 12:10:54 +01:00
dbconn.commit()
2018-07-01 02:09:16 +02:00
except Error as e:
2018-07-02 22:01:47 +02:00
print(e)
2018-07-01 02:09:16 +02:00
finally:
2018-11-13 12:10:54 +01:00
dbconn.close()
2018-07-01 02:09:16 +02:00
else:
2018-07-02 22:01:47 +02:00
print("found database will not create a new one")
2018-07-01 02:09:16 +02:00
2018-07-02 22:32:29 +02:00
def insert_new_entry(db_path, ipv6, coords):
2018-07-01 02:09:16 +02:00
try:
2018-11-13 12:10:54 +01:00
dbconn = sqlite3.connect(db_path + "yggindex.db")
dbconn.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords) VALUES(?, ?)''',\
2018-07-08 16:01:07 +02:00
(ipv6, coords))
2018-11-13 12:10:54 +01:00
dbconn.commit()
dbconn.close()
2018-07-01 02:09:16 +02:00
except Error as e:
2018-07-01 02:15:34 +02:00
print e
2018-07-01 02:09:16 +02:00
def contrib_entry(db_path, ipv6):
try:
2018-11-13 12:10:54 +01:00
dbconn = sqlite3.connect(db_path + "yggindex.db")
dbconn.execute('''INSERT OR REPLACE INTO contrib(ipv6) VALUES(''' + "'"\
+ ipv6 + "'" + ''')''')
2018-11-13 12:10:54 +01:00
dbconn.commit()
dbconn.close()
except Error as e:
print e
2018-07-01 02:09:16 +02:00
2018-11-13 12:10:54 +01:00
def error_check_insert_into_db(dht, switchpeers, ipv6):
2018-07-01 02:09:16 +02:00
try:
2018-07-01 02:15:34 +02:00
if dht.get("status") == "success":
for x, y in dht["response"]["dht"].iteritems():
2018-07-01 02:09:16 +02:00
if valid_ipv6_check(x) and check_coords(y["coords"]):
2018-07-08 16:01:07 +02:00
insert_new_entry(DB_PATH, x, y["coords"])
2018-07-01 02:09:16 +02:00
2018-07-01 02:15:34 +02:00
if dht.get("status") == "success":
2018-07-01 02:09:16 +02:00
for x in switchpeers["response"]["switchpeers"].iteritems():
if valid_ipv6_check(x[1]["ip"]) and check_coords(x[1]["coords"]):
2018-07-08 16:01:07 +02:00
insert_new_entry(DB_PATH, x[1]["ip"], x[1]["coords"])
2018-11-13 12:10:54 +01:00
contrib_entry(DB_PATH, ipv6)
2018-07-01 02:09:16 +02:00
except:
2018-07-01 02:15:34 +02:00
print"error in json file, aborting"
2018-07-01 02:09:16 +02:00
2018-07-02 22:01:47 +02:00
2018-11-13 12:10:54 +01:00
def proccess_incoming_data(datty, ipv6):
print str(time.time()) + " " + str(ipv6)
2018-07-01 02:09:16 +02:00
try:
2018-11-13 12:10:54 +01:00
ddata = datty.recv(1024 * 20)
data = json.loads(ddata.decode())
error_check_insert_into_db(data["dhtpack"], data["switchpack"], ipv6)
2018-07-01 02:09:16 +02:00
except:
print "ignoring, data was not json"
2018-07-02 22:01:47 +02:00
2018-07-01 02:09:16 +02:00
isdatabase(DB_PATH)
2018-07-02 22:01:47 +02:00
conn = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
2018-07-01 02:09:16 +02:00
conn.bind((SERVER, 45671))
2018-07-02 22:01:47 +02:00
conn.listen(30)
2018-07-01 02:09:16 +02:00
while True:
try:
2018-07-02 22:01:47 +02:00
dataraw, addr = conn.accept()
2018-11-13 12:10:54 +01:00
thread.start_new_thread(proccess_incoming_data, (dataraw, addr[0]))
2018-07-08 16:01:07 +02:00
except Exception:
2018-07-01 02:09:16 +02:00
print "bloop"