mirror of https://github.com/r4sas/Niflheim-api
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
4.0 KiB
137 lines
4.0 KiB
7 years ago
|
#server for collecting DHT info
|
||
7 years ago
|
|
||
|
import json
|
||
6 years ago
|
import psycopg2
|
||
6 years ago
|
import time
|
||
7 years ago
|
import os
|
||
|
import socket
|
||
|
import ipaddress
|
||
|
import thread
|
||
6 years ago
|
import sys
|
||
7 years ago
|
|
||
|
SERVER = ""
|
||
6 years ago
|
DB_PASSWORD = "password"
|
||
|
DB_USER = "yggindex"
|
||
|
DB_NAME = "yggindex"
|
||
|
DB_HOST = "localhost"
|
||
7 years ago
|
|
||
6 years ago
|
#To setup tables in the database on first run please use:
|
||
|
#python vserv.py gentables
|
||
|
#This will generate all the tables needed
|
||
|
|
||
|
def create_tables():
|
||
|
try:
|
||
|
dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD)
|
||
|
cur = dbconn.cursor()
|
||
|
|
||
|
cur.execute('''CREATE TABLE yggindex(ipv6 varchar UNIQUE,\
|
||
|
coords varchar, unixtstamp varchar)''')
|
||
|
cur.execute('''CREATE TABLE timeseries(max varchar,\
|
||
|
unixtstamp varchar)''')
|
||
|
cur.execute('''CREATE TABLE contrib(ipv6 varchar UNIQUE,\
|
||
|
unixtstamp varchar)''')
|
||
|
|
||
|
dbconn.commit()
|
||
|
cur.close()
|
||
|
dbconn.close()
|
||
|
except:
|
||
|
print "somethings up, check you created the database correctly"
|
||
|
|
||
|
#check if tables need to be generated or not
|
||
|
if len(sys.argv) > 1:
|
||
|
if sys.argv[1] == "gentables":
|
||
|
create_tables()
|
||
7 years ago
|
|
||
|
def check_coords(coords):
|
||
|
coord_len = coords.replace(' ', '').replace('[', '').replace(']', '')
|
||
|
digits_found = [x for x in coord_len if x.isdigit()]
|
||
7 years ago
|
|
||
7 years ago
|
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
|
||
|
|
||
|
|
||
6 years ago
|
def insert_new_entry(ipv6, coords):
|
||
|
try:
|
||
|
dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD)
|
||
|
cur = dbconn.cursor()
|
||
7 years ago
|
|
||
6 years ago
|
cur.execute('''INSERT INTO yggindex(ipv6, coords, unixtstamp)\
|
||
|
VALUES(''' + "'" + ipv6 + "'," + "'" + coords + "'," + str(int(time.time())) + ''')\
|
||
|
ON CONFLICT (ipv6) DO UPDATE\
|
||
|
SET unixtstamp = ''' + "'" + str(int(time.time())) + "'," +''' \
|
||
|
coords = ''' + "'" + coords + "'" + ''';''')
|
||
7 years ago
|
|
||
6 years ago
|
dbconn.commit()
|
||
6 years ago
|
cur.close()
|
||
6 years ago
|
dbconn.close()
|
||
6 years ago
|
except:
|
||
|
cur.close()
|
||
|
dbconn.close()
|
||
|
|
||
7 years ago
|
|
||
6 years ago
|
def contrib_entry(ipv6):
|
||
6 years ago
|
try:
|
||
6 years ago
|
dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD)
|
||
|
cur = dbconn.cursor()
|
||
|
cur.execute('''INSERT INTO contrib(ipv6, unixtstamp)\
|
||
|
VALUES(''' + "'" + ipv6 + "'," + str(int(time.time())) + ''')\
|
||
|
ON CONFLICT (ipv6) DO UPDATE\
|
||
|
SET unixtstamp = ''' + "'" + str(int(time.time())) + "'" + ''';''')
|
||
6 years ago
|
dbconn.commit()
|
||
6 years ago
|
cur.close()
|
||
|
dbconn.close()
|
||
|
except:
|
||
|
cur.close()
|
||
6 years ago
|
dbconn.close()
|
||
6 years ago
|
|
||
7 years ago
|
|
||
6 years ago
|
def error_check_insert_into_db(dht, switchpeers, ipv6):
|
||
6 years ago
|
try:
|
||
|
if dht.get("status") == "success":
|
||
|
for x, y in dht["response"]["dht"].iteritems():
|
||
|
if valid_ipv6_check(x) and check_coords(y["coords"]):
|
||
|
insert_new_entry(x, y["coords"])
|
||
|
|
||
|
if dht.get("status") == "success":
|
||
|
for x in switchpeers["response"]["switchpeers"].iteritems():
|
||
|
if valid_ipv6_check(x[1]["ip"]) and check_coords(x[1]["coords"]):
|
||
|
insert_new_entry(x[1]["ip"], x[1]["coords"])
|
||
|
|
||
|
contrib_entry(ipv6)
|
||
|
except:
|
||
|
print"error in json file, aborting"
|
||
7 years ago
|
|
||
7 years ago
|
|
||
6 years ago
|
def proccess_incoming_data(datty, ipv6):
|
||
|
print str(time.time()) + " " + str(ipv6)
|
||
7 years ago
|
try:
|
||
6 years ago
|
ddata = datty.recv(18128)
|
||
|
data = json.loads(ddata.decode())
|
||
|
error_check_insert_into_db(data["dhtpack"], data["switchpack"], ipv6)
|
||
7 years ago
|
except:
|
||
|
print "ignoring, data was not json"
|
||
|
|
||
7 years ago
|
|
||
|
conn = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
||
7 years ago
|
conn.bind((SERVER, 45671))
|
||
7 years ago
|
conn.listen(30)
|
||
7 years ago
|
|
||
|
while True:
|
||
|
try:
|
||
7 years ago
|
dataraw, addr = conn.accept()
|
||
6 years ago
|
thread.start_new_thread(proccess_incoming_data, (dataraw, addr[0]))
|
||
7 years ago
|
except Exception:
|
||
7 years ago
|
print "bloop"
|