From 35a27860d63ff5d85bb608488c2472d2b0e946f1 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Sat, 30 Jun 2018 23:03:42 +0200 Subject: [PATCH 01/36] Create send-view.py --- send-view.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 send-view.py diff --git a/send-view.py b/send-view.py new file mode 100644 index 0000000..2efd08f --- /dev/null +++ b/send-view.py @@ -0,0 +1,40 @@ +import socket +import json + +GETDHT = '{"request":"getDHT", "keepalive":true}' +GETSWITCHPEERS = '{"request":"getSwitchPeers"}' +SERVER = "y.yakamo.org" + + +def send_view_to_server(tosend): + if tosend: + attempts = 3 + while attempts: + try: + conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + conn.sendto(tosend, (SERVER, 45671)) + break + except: + attempts -= 1 + + +def collect_dht_getswitchpeers(): + try: + ygg = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + ygg.connect(('localhost', 9001)) + + ygg.send(GETDHT) + dhtdata = json.loads(ygg.recv(1024 * 15)) + + ygg.send(GETSWITCHPEERS) + switchdata = json.loads(ygg.recv(1024 * 15)) + + temp_dict = {} + temp_dict["dhtpack"] = dhtdata + temp_dict["switchpack"] = switchdata + + return json.dumps(temp_dict).encode() + except: + return None + +send_view_to_server(collect_dht_getswitchpeers()) From 29a8ca1a3935299bbb19bd8466c0e8d644d60977 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Sat, 30 Jun 2018 23:07:05 +0200 Subject: [PATCH 02/36] send-view addition to readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index ae88b41..7ac3232 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # ygg-node-db server to collect DHT views from yggdrasil nodes and store in a data base + + +# send-view.py + +Very simple to use just add in crontab and run once an hour. From c8d4f561bba6445e498e4b515e7220ceff911dcd Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Sat, 30 Jun 2018 23:07:17 +0200 Subject: [PATCH 03/36] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7ac3232..d844c1c 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,6 @@ server to collect DHT views from yggdrasil nodes and store in a data base -# send-view.py +### send-view.py Very simple to use just add in crontab and run once an hour. From 6bbf2ae5aed9721af9ae0d3c4437e37fc5644e67 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Sun, 1 Jul 2018 02:03:26 +0200 Subject: [PATCH 04/36] add missing 6 in AF_INET --- send-view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/send-view.py b/send-view.py index 2efd08f..4d1347f 100644 --- a/send-view.py +++ b/send-view.py @@ -11,7 +11,7 @@ def send_view_to_server(tosend): attempts = 3 while attempts: try: - conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + conn = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) conn.sendto(tosend, (SERVER, 45671)) break except: From 0a688f49ebc9d22cbf0ea7b2974eb8e9c5eed3af Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Sun, 1 Jul 2018 02:09:16 +0200 Subject: [PATCH 05/36] first messy draft --- vserv.py | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 vserv.py diff --git a/vserv.py b/vserv.py new file mode 100644 index 0000000..22f3faa --- /dev/null +++ b/vserv.py @@ -0,0 +1,101 @@ +#server for collecting views of the network + +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()] + + if len(digits_found) == len(coord_len): + crus = True + else: + crus = False + return crus + + +def valid_ipv6_check(ipv6add): + import ipaddress + 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() + c.execute('create table yggindex(ipv6 varchar(45) UNIQUE, coords varchar(50),\ + utimestamp varchar(40))') + conn.commit() + # c.commit() + except Error as e: + print(e) + finally: + conn.close() + else: + print("found database will not create a new one") + + +def insert_new_entry(db_path, ipv6, coords, utimestamp): + try: + conn = sqlite3.connect(db_path + "yggindex.db") + c = conn.cursor() + c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords, utimestamp) VALUES(?, ?, ?)''',\ + (ipv6, coords, utimestamp)) + conn.commit() + conn.close() + except Error as e: + print(e) + + +def error_check_insert_into_db(dht, switchpeers): + try: + if "success" == dht.get("status"): + for x,y in dht["response"]["dht"].iteritems(): + if valid_ipv6_check(x) and check_coords(y["coords"]): + insert_new_entry(DB_PATH, x, y["coords"], int(time.time())) + + if "success" == dht.get("status"): + 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: + print("error in json file, aborting") + +def proccess_incoming_data(datty, addr): + print addr + try: + data = json.loads(datty.decode()) + error_check_insert_into_db(data["dhtpack"], data["switchpack"]) + except: + print "ignoring, data was not json" + +isdatabase(DB_PATH) + +conn = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) +conn.bind((SERVER, 45671)) + +while True: + try: + dataraw, addr = conn.recvfrom(1024 * 20) + thread.start_new_thread(proccess_incoming_data, (dataraw, addr)) + except: + print "bloop" From 9188280a7bd3642526a01b98d04e7c5f178f9179 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Sun, 1 Jul 2018 02:15:34 +0200 Subject: [PATCH 06/36] Update vserv.py --- vserv.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/vserv.py b/vserv.py index 22f3faa..b776000 100644 --- a/vserv.py +++ b/vserv.py @@ -1,4 +1,4 @@ -#server for collecting views of the network +#server for collecting DHT info import json import time @@ -17,7 +17,7 @@ DB_PATH = "vservdb/" def check_coords(coords): coord_len = coords.replace(' ', '').replace('[', '').replace(']', '') digits_found = [x for x in coord_len if x.isdigit()] - + if len(digits_found) == len(coord_len): crus = True else: @@ -26,7 +26,6 @@ def check_coords(coords): def valid_ipv6_check(ipv6add): - import ipaddress try: if ipaddress.IPv6Address(unicode(ipv6add)): addr = True @@ -47,11 +46,11 @@ def isdatabase(db_path): conn.commit() # c.commit() except Error as e: - print(e) + print e finally: conn.close() else: - print("found database will not create a new one") + print"found database will not create a new one" def insert_new_entry(db_path, ipv6, coords, utimestamp): @@ -59,26 +58,26 @@ def insert_new_entry(db_path, ipv6, coords, utimestamp): conn = sqlite3.connect(db_path + "yggindex.db") c = conn.cursor() c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords, utimestamp) VALUES(?, ?, ?)''',\ - (ipv6, coords, utimestamp)) + (ipv6, coords, utimestamp)) conn.commit() conn.close() except Error as e: - print(e) + print e def error_check_insert_into_db(dht, switchpeers): try: - if "success" == dht.get("status"): - for x,y in dht["response"]["dht"].iteritems(): + 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(DB_PATH, x, y["coords"], int(time.time())) - if "success" == dht.get("status"): + 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(DB_PATH, x[1]["ip"], x[1]["coords"], int(time.time())) except: - print("error in json file, aborting") + print"error in json file, aborting" def proccess_incoming_data(datty, addr): print addr From 19f2fb47ee1b2867161bce0e7a8b034c5487de0f Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Sun, 1 Jul 2018 21:48:31 +0200 Subject: [PATCH 07/36] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index d844c1c..6387b80 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,9 @@ server to collect DHT views from yggdrasil nodes and store in a data base ### send-view.py Very simple to use just add in crontab and run once an hour. + + +## Todo + +add Triggers to database for self cleaning of old entrys +add postgress function for alternative use From 085381fdc845c13a8dc7db955115bf87a8b318b1 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 2 Jul 2018 01:23:56 +0200 Subject: [PATCH 08/36] Update vserv.py --- vserv.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/vserv.py b/vserv.py index b776000..229fa20 100644 --- a/vserv.py +++ b/vserv.py @@ -41,10 +41,9 @@ def isdatabase(db_path): try: conn = sqlite3.connect(db_path + 'yggindex.db') c = conn.cursor() - c.execute('create table yggindex(ipv6 varchar(45) UNIQUE, coords varchar(50),\ - utimestamp varchar(40))') + c.execute('''create table yggindex(ipv6 varchar(45) UNIQUE, coords varchar(50),\ + time timestamp default (strftime('%s', 'now')))''') conn.commit() - # c.commit() except Error as e: print e finally: From be6b4c9c433d13263d5bf6eec7f15ba03a1c246e Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 2 Jul 2018 17:00:36 +0200 Subject: [PATCH 09/36] Update vserv.py --- vserv.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vserv.py b/vserv.py index 229fa20..6244498 100644 --- a/vserv.py +++ b/vserv.py @@ -56,8 +56,8 @@ def insert_new_entry(db_path, ipv6, coords, utimestamp): try: conn = sqlite3.connect(db_path + "yggindex.db") c = conn.cursor() - c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords, utimestamp) VALUES(?, ?, ?)''',\ - (ipv6, coords, utimestamp)) + c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords) VALUES(?, ?)''',\ + (ipv6, coords)) conn.commit() conn.close() except Error as e: From ba9a2d2fa8247928da1a7a657c688a2e78509cb2 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 2 Jul 2018 21:18:39 +0200 Subject: [PATCH 10/36] changed to TCP --- send-view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/send-view.py b/send-view.py index 4d1347f..7335e39 100644 --- a/send-view.py +++ b/send-view.py @@ -11,7 +11,7 @@ def send_view_to_server(tosend): attempts = 3 while attempts: try: - conn = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) + conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.sendto(tosend, (SERVER, 45671)) break except: From 36648fcc50ab8c0a513201734bd46882fd6675b5 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 2 Jul 2018 21:19:24 +0200 Subject: [PATCH 11/36] changed to TCP --- vserv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vserv.py b/vserv.py index 6244498..f26cc1d 100644 --- a/vserv.py +++ b/vserv.py @@ -88,7 +88,7 @@ def proccess_incoming_data(datty, addr): isdatabase(DB_PATH) -conn = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) +conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.bind((SERVER, 45671)) while True: From a212fed4d9183fc78f348e032a3da62ad73c577c Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 2 Jul 2018 21:35:00 +0200 Subject: [PATCH 12/36] changed to TCP with fixes --- send-view.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/send-view.py b/send-view.py index 7335e39..085c72e 100644 --- a/send-view.py +++ b/send-view.py @@ -11,8 +11,11 @@ def send_view_to_server(tosend): attempts = 3 while attempts: try: - conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - conn.sendto(tosend, (SERVER, 45671)) + conn = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) + conn.connect((SERVER, 45671)) + conn.send(tosend) + conn.close() + print "sent ok" break except: attempts -= 1 From e8f47f2c3d60dec7ce4f6a28d5bf5cc9cbe1753c Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 2 Jul 2018 22:01:47 +0200 Subject: [PATCH 13/36] changed to TCP --- vserv.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/vserv.py b/vserv.py index f26cc1d..78d2cda 100644 --- a/vserv.py +++ b/vserv.py @@ -45,19 +45,19 @@ def isdatabase(db_path): time timestamp default (strftime('%s', 'now')))''') conn.commit() except Error as e: - print e + print(e) finally: conn.close() else: - print"found database will not create a new one" + print("found database will not create a new one") def insert_new_entry(db_path, ipv6, coords, utimestamp): try: conn = sqlite3.connect(db_path + "yggindex.db") c = conn.cursor() - c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords) VALUES(?, ?)''',\ - (ipv6, coords)) + c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords, utimestamp) VALUES(?, ?, ?)''',\ + (ipv6, coords, utimestamp)) conn.commit() conn.close() except Error as e: @@ -78,22 +78,26 @@ def error_check_insert_into_db(dht, switchpeers): except: print"error in json file, aborting" + def proccess_incoming_data(datty, addr): print addr try: - data = json.loads(datty.decode()) + ddata = datty.recv(1024 * 20) + data = json.loads(ddata.decode()) error_check_insert_into_db(data["dhtpack"], data["switchpack"]) except: print "ignoring, data was not json" + isdatabase(DB_PATH) -conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +conn = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) conn.bind((SERVER, 45671)) +conn.listen(30) while True: try: - dataraw, addr = conn.recvfrom(1024 * 20) + dataraw, addr = conn.accept() thread.start_new_thread(proccess_incoming_data, (dataraw, addr)) except: print "bloop" From 1ee7764df0fad7d3148fa7dd05cc84df553cb862 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 2 Jul 2018 22:32:29 +0200 Subject: [PATCH 14/36] Update vserv.py --- vserv.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vserv.py b/vserv.py index 78d2cda..432922d 100644 --- a/vserv.py +++ b/vserv.py @@ -52,12 +52,12 @@ def isdatabase(db_path): print("found database will not create a new one") -def insert_new_entry(db_path, ipv6, coords, utimestamp): +def insert_new_entry(db_path, ipv6, coords): try: conn = sqlite3.connect(db_path + "yggindex.db") c = conn.cursor() - c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords, utimestamp) VALUES(?, ?, ?)''',\ - (ipv6, coords, utimestamp)) + c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords) VALUES(?, ?)''',\ + (ipv6, coords)) conn.commit() conn.close() except Error as e: From 798a6107324a9bb12b2c6db5b38b59d2c0933818 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 2 Jul 2018 23:03:21 +0200 Subject: [PATCH 15/36] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6387b80..2367004 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,4 @@ Very simple to use just add in crontab and run once an hour. add Triggers to database for self cleaning of old entrys add postgress function for alternative use +create an alternative to send-view to collect from multiple nodes and send(rare use case as admin api isnt secure to leave open) From 533b6ee20b5aef335c51b9947e68375d8bea94d8 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 00:38:12 +0200 Subject: [PATCH 16/36] option for external admin api --- send-view.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/send-view.py b/send-view.py index 085c72e..7586194 100644 --- a/send-view.py +++ b/send-view.py @@ -1,10 +1,17 @@ import socket import json +import sys GETDHT = '{"request":"getDHT", "keepalive":true}' GETSWITCHPEERS = '{"request":"getSwitchPeers"}' SERVER = "y.yakamo.org" +#gives the option to get data from an external server instead and send that +#if no options given it will default to localhost instead +if len(sys.argv) == 4: + host_port = (sys.argv[1], sys.argv[2]) +elif len(sys.argv) == 1: + host_port = ('localhost', 9001) def send_view_to_server(tosend): if tosend: @@ -21,10 +28,10 @@ def send_view_to_server(tosend): attempts -= 1 -def collect_dht_getswitchpeers(): +def collect_dht_getswitchpeers(serport): try: ygg = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ygg.connect(('localhost', 9001)) + ygg.connect(host_port) ygg.send(GETDHT) dhtdata = json.loads(ygg.recv(1024 * 15)) From 07a4b5c12550982037091b52eb6566475ed8fb37 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 00:39:26 +0200 Subject: [PATCH 17/36] Update send-view.py --- send-view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/send-view.py b/send-view.py index 7586194..83db663 100644 --- a/send-view.py +++ b/send-view.py @@ -10,7 +10,7 @@ SERVER = "y.yakamo.org" #if no options given it will default to localhost instead if len(sys.argv) == 4: host_port = (sys.argv[1], sys.argv[2]) -elif len(sys.argv) == 1: +else: host_port = ('localhost', 9001) def send_view_to_server(tosend): From 367245fd9169c3dba951efc5300bbb9a410a9373 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 00:41:03 +0200 Subject: [PATCH 18/36] fix host_port in def --- send-view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/send-view.py b/send-view.py index 83db663..5bd0a2b 100644 --- a/send-view.py +++ b/send-view.py @@ -47,4 +47,4 @@ def collect_dht_getswitchpeers(serport): except: return None -send_view_to_server(collect_dht_getswitchpeers()) +send_view_to_server(collect_dht_getswitchpeers(host_port)) From 32976b2638ad5cdf479f682d5e4bfc3bdf280a78 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 01:43:35 +0200 Subject: [PATCH 19/36] Update send-view.py --- send-view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/send-view.py b/send-view.py index 5bd0a2b..87c80f1 100644 --- a/send-view.py +++ b/send-view.py @@ -8,7 +8,7 @@ SERVER = "y.yakamo.org" #gives the option to get data from an external server instead and send that #if no options given it will default to localhost instead -if len(sys.argv) == 4: +if len(sys.argv) == 3: host_port = (sys.argv[1], sys.argv[2]) else: host_port = ('localhost', 9001) From 01f7e09ce8fb47de0b814283188514021ef37d65 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 01:48:56 +0200 Subject: [PATCH 20/36] Update send-view.py --- send-view.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/send-view.py b/send-view.py index 87c80f1..af95e1b 100644 --- a/send-view.py +++ b/send-view.py @@ -8,11 +8,12 @@ SERVER = "y.yakamo.org" #gives the option to get data from an external server instead and send that #if no options given it will default to localhost instead -if len(sys.argv) == 3: - host_port = (sys.argv[1], sys.argv[2]) -else: - host_port = ('localhost', 9001) +#if len(sys.argv) == 3: +# host_port = (sys.argv[1], sys.argv[2]) +#else: +# host_port = ('localhost', 9001) +host_port = ('localhost', 9001) def send_view_to_server(tosend): if tosend: attempts = 3 From f7992b180e82293acb7d6df77e32d255f5168dbd Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 01:58:48 +0200 Subject: [PATCH 21/36] turn sys.argv[2] into int had to change the accepted len of sys.argv to 3 instead of 4 as this was too much --- send-view.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/send-view.py b/send-view.py index af95e1b..c0338ff 100644 --- a/send-view.py +++ b/send-view.py @@ -8,10 +8,10 @@ SERVER = "y.yakamo.org" #gives the option to get data from an external server instead and send that #if no options given it will default to localhost instead -#if len(sys.argv) == 3: -# host_port = (sys.argv[1], sys.argv[2]) -#else: -# host_port = ('localhost', 9001) +if len(sys.argv) == 3: + host_port = (sys.argv[1], int(sys.argv[2])) +else: + host_port = ('localhost', 9001) host_port = ('localhost', 9001) def send_view_to_server(tosend): From b77d91a898f4b950ac53bb622983564338bc7658 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 01:59:15 +0200 Subject: [PATCH 22/36] remove extra host_port test --- send-view.py | 1 - 1 file changed, 1 deletion(-) diff --git a/send-view.py b/send-view.py index c0338ff..ff9da9c 100644 --- a/send-view.py +++ b/send-view.py @@ -13,7 +13,6 @@ if len(sys.argv) == 3: else: host_port = ('localhost', 9001) -host_port = ('localhost', 9001) def send_view_to_server(tosend): if tosend: attempts = 3 From ccee13dadffedaa96c88a58554f49fe48e04ffa0 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 02:20:25 +0200 Subject: [PATCH 23/36] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 2367004..243ec1a 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,12 @@ server to collect DHT views from yggdrasil nodes and store in a data base ### send-view.py Very simple to use just add in crontab and run once an hour. +If you want to get access an external Admin API on another server: + +send-view.py ipv4 port + +__example__ +send-view.py 192.168.1.100 9001 ## Todo From 03e92d592984d38b2974e53f54cd4cd7c8a1353f Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 02:21:03 +0200 Subject: [PATCH 24/36] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 243ec1a..19326d8 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ server to collect DHT views from yggdrasil nodes and store in a data base ### send-view.py Very simple to use just add in crontab and run once an hour. -If you want to get access an external Admin API on another server: +If you want to get access to an external Admin API on another server: send-view.py ipv4 port From 7270820bb9ef72c813bcafa8cb0ebaa94617feba Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 02:21:44 +0200 Subject: [PATCH 25/36] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 19326d8..ad1c0ee 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,3 @@ send-view.py 192.168.1.100 9001 add Triggers to database for self cleaning of old entrys add postgress function for alternative use -create an alternative to send-view to collect from multiple nodes and send(rare use case as admin api isnt secure to leave open) From 81a630179a61c7fe392df3e6e29f1bc9c5d4cf46 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 16:26:27 +0200 Subject: [PATCH 26/36] Update vserv.py --- vserv.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vserv.py b/vserv.py index 432922d..1d235f5 100644 --- a/vserv.py +++ b/vserv.py @@ -41,8 +41,8 @@ def isdatabase(db_path): try: conn = sqlite3.connect(db_path + 'yggindex.db') c = conn.cursor() - c.execute('''create table yggindex(ipv6 varchar(45) UNIQUE, coords varchar(50),\ - time timestamp default (strftime('%s', 'now')))''') + c.execute('create table yggindex(ipv6 varchar(45) UNIQUE, coords varchar(50),\ + utimestamp int(40))') conn.commit() except Error as e: print(e) @@ -56,8 +56,8 @@ def insert_new_entry(db_path, ipv6, coords): try: conn = sqlite3.connect(db_path + "yggindex.db") c = conn.cursor() - c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords) VALUES(?, ?)''',\ - (ipv6, coords)) + c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords, utimestamp) VALUES(?, ?, ?)''',\ + (ipv6, coords, utimestamp)) conn.commit() conn.close() except Error as e: From 558287c63278f98a307dafbc23192b1adeb322cb Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 17:52:45 +0200 Subject: [PATCH 27/36] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ad1c0ee..ffb28bd 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,8 @@ server to collect DHT views from yggdrasil nodes and store in a data base ### send-view.py Very simple to use just add in crontab and run once an hour. -If you want to get access to an external Admin API on another server: + +If you want to get access to an external Admin API on another server: send-view.py ipv4 port From 9a2354205fae2270f85113c5803277e0ab66ad98 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Tue, 3 Jul 2018 17:54:42 +0200 Subject: [PATCH 28/36] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index ffb28bd..3fedd04 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,9 @@ server to collect DHT views from yggdrasil nodes and store in a data base Very simple to use just add in crontab and run once an hour. +__Access External Admin API:__ If you want to get access to an external Admin API on another server: -send-view.py ipv4 port - __example__ send-view.py 192.168.1.100 9001 From f8fbea016ff711c95703166302df297e55de0959 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Sun, 8 Jul 2018 16:01:07 +0200 Subject: [PATCH 29/36] Update vserv.py --- vserv.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/vserv.py b/vserv.py index 1d235f5..48442c1 100644 --- a/vserv.py +++ b/vserv.py @@ -41,8 +41,8 @@ def isdatabase(db_path): try: conn = sqlite3.connect(db_path + 'yggindex.db') c = conn.cursor() - c.execute('create table yggindex(ipv6 varchar(45) UNIQUE, coords varchar(50),\ - utimestamp int(40))') + c.execute('''create table yggindex(ipv6 varchar(45) UNIQUE, coords varchar(50), + ut unixtime default (strftime('%s','now')))''') conn.commit() except Error as e: print(e) @@ -56,8 +56,8 @@ def insert_new_entry(db_path, ipv6, coords): try: conn = sqlite3.connect(db_path + "yggindex.db") c = conn.cursor() - c.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords, utimestamp) VALUES(?, ?, ?)''',\ - (ipv6, coords, utimestamp)) + conn.execute('''INSERT OR REPLACE INTO yggindex(ipv6, coords) VALUES(?, ?)''',\ + (ipv6, coords)) conn.commit() conn.close() except Error as e: @@ -69,12 +69,12 @@ def error_check_insert_into_db(dht, switchpeers): 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(DB_PATH, x, y["coords"], int(time.time())) + insert_new_entry(DB_PATH, 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(DB_PATH, x[1]["ip"], x[1]["coords"], int(time.time())) + insert_new_entry(DB_PATH, x[1]["ip"], x[1]["coords"]) except: print"error in json file, aborting" @@ -99,5 +99,5 @@ while True: try: dataraw, addr = conn.accept() thread.start_new_thread(proccess_incoming_data, (dataraw, addr)) - except: + except Exception: print "bloop" From 6c8aeddc5f6d7a338bcb918503c0f767ca24eec5 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Sun, 8 Jul 2018 16:04:08 +0200 Subject: [PATCH 30/36] removed module time no longer needed --- vserv.py | 1 - 1 file changed, 1 deletion(-) diff --git a/vserv.py b/vserv.py index 48442c1..3a3a521 100644 --- a/vserv.py +++ b/vserv.py @@ -1,7 +1,6 @@ #server for collecting DHT info import json -import time import sqlite3 from sqlite3 import Error import os From 360e60469904daf9bde5a6b6515606de44ff516a Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Sun, 8 Jul 2018 22:51:27 +0200 Subject: [PATCH 31/36] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3fedd04..eefe213 100644 --- a/README.md +++ b/README.md @@ -16,4 +16,5 @@ send-view.py 192.168.1.100 9001 ## Todo add Triggers to database for self cleaning of old entrys +add rate limiting for sends and requests add postgress function for alternative use From b7e820f220c5d24a6a32c20659da926655a0432e Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Sun, 8 Jul 2018 23:30:41 +0200 Subject: [PATCH 32/36] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index eefe213..518c82f 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ send-view.py 192.168.1.100 9001 ## Todo -add Triggers to database for self cleaning of old entrys +__seperate out request for the api:__ +- current/ for active nodes +- old/ anything older than 4hrs +- all/ new and old together + add rate limiting for sends and requests add postgress function for alternative use From e670c8fcd3025e6cecd6c0a8e7a9850d93496694 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 9 Jul 2018 15:47:21 +0200 Subject: [PATCH 33/36] added api --- yggapi.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 yggapi.py diff --git a/yggapi.py b/yggapi.py new file mode 100644 index 0000000..e8adfd4 --- /dev/null +++ b/yggapi.py @@ -0,0 +1,72 @@ +from flask import Flask, request +from flask_restful import Resource, Api +from sqlalchemy import create_engine +from json import dumps +from flask.ext.jsonpify import jsonify +import time +import sys +import os + +#check if a database exists or not +db_path = "vservdb/yggindex.db" +if not os.path.exists(db_path): + print "could not find a database" + sys.exit(0) + +db_connect = create_engine('sqlite:///' + db_path) +app = Flask(__name__) +api = Api(app) + +#quickly figure out which is old or new +def age_calc(ustamp): + if (time.time() - ustamp) <= 14400 : + return True + else: + return False + +#active nodes in the past 4hrs +class nodes_current(Resource): + def get(self): + conn = db_connect.connect() + query = conn.execute("select * from yggindex") + nodes = {} + for i in query.cursor.fetchall(): + if age_calc(i[2]): + nodes[i[0]] = [i[1],i[2]] + nodelist = {} + nodelist['yggnodes'] = nodes + return nodelist + +#nodes that may not be active anymore or have been offline for a while such as laptops +#could be used as last seen +class nodes_old(Resource): + def get(self): + conn = db_connect.connect() + query = conn.execute("select * from yggindex") + nodes = {} + for i in query.cursor.fetchall(): + if not age_calc(i[2]): + nodes[i[0]] = [i[1],i[2]] + nodelist = {} + nodelist['yggnodes'] = nodes + return nodelist + +#return entire database of nodes regardless of age +class nodes_all(Resource): + def get(self): + conn = db_connect.connect() + query = conn.execute("select * from yggindex") + nodes = {} + for i in query.cursor.fetchall(): + nodes[i[0]] = [i[1],i[2]] + nodelist = {} + nodelist['yggnodes'] = nodes + return nodelist + +#sort out the api request here for the url +api.add_resource(nodes_current, '/current') +api.add_resource(nodes_old, '/old') +api.add_resource(nodes_all, '/all') + +if __name__ == '__main__': + app.run(host='::', port=3000) From 20d9ad7817bfb146b37f14e9adee5bd795cf027a Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 9 Jul 2018 16:05:19 +0200 Subject: [PATCH 34/36] updated Todo list --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 518c82f..a2028fa 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,8 @@ send-view.py 192.168.1.100 9001 ## Todo -__seperate out request for the api:__ -- current/ for active nodes -- old/ anything older than 4hrs -- all/ new and old together - +maybe some kind of testing for current uploads? +maybe add api token to prevent abuse? +create restrictions on how much data can be sent maybe? add rate limiting for sends and requests add postgress function for alternative use From 8b5b750c3279384e1b5506b9ff5c38d248a19723 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 9 Jul 2018 16:05:58 +0200 Subject: [PATCH 35/36] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a2028fa..6090d49 100644 --- a/README.md +++ b/README.md @@ -18,5 +18,5 @@ send-view.py 192.168.1.100 9001 maybe some kind of testing for current uploads? maybe add api token to prevent abuse? create restrictions on how much data can be sent maybe? -add rate limiting for sends and requests +add rate limiting for sends and requests/ rate limit in nginx for requests add postgress function for alternative use From 9a8724756cbd7fbedee566c3e621e7b92367a347 Mon Sep 17 00:00:00 2001 From: yakamok <38737288+yakamok@users.noreply.github.com> Date: Mon, 9 Jul 2018 17:16:09 +0200 Subject: [PATCH 36/36] added flask-limiter prevent abuse --- yggapi.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/yggapi.py b/yggapi.py index e8adfd4..e61ae12 100644 --- a/yggapi.py +++ b/yggapi.py @@ -2,7 +2,10 @@ from flask import Flask, request from flask_restful import Resource, Api from sqlalchemy import create_engine from json import dumps +#rate limiting support from flask.ext.jsonpify import jsonify +from flask_limiter import Limiter +from flask_limiter.util import get_remote_address import time import sys import os @@ -17,6 +20,12 @@ db_connect = create_engine('sqlite:///' + db_path) app = Flask(__name__) api = Api(app) +limiter = Limiter( + app, + key_func=get_remote_address, + default_limits=["500/day", "60/hour"] +) + #quickly figure out which is old or new def age_calc(ustamp): if (time.time() - ustamp) <= 14400 :