From b88bd19578dd177503ae7e92ef22d83535dcaadb Mon Sep 17 00:00:00 2001 From: lza_menace Date: Fri, 18 Jun 2021 14:42:43 -0700 Subject: [PATCH] add pruning to crawled peers and change colors on map --- xmrnodes/app.py | 32 ++++++++++---- xmrnodes/models.py | 5 +++ xmrnodes/templates/map.html | 85 ++++++++++++++++++++----------------- 3 files changed, 74 insertions(+), 48 deletions(-) diff --git a/xmrnodes/app.py b/xmrnodes/app.py index 9635a52..f5cba84 100644 --- a/xmrnodes/app.py +++ b/xmrnodes/app.py @@ -1,4 +1,3 @@ - import json import re import logging @@ -173,7 +172,8 @@ def check(): @app.cli.command("get_peers") def get_peers(): all_peers = [] - print(f'[+] Retrieving initial peers from {config.NODE_HOST}:{config.NODE_PORT}') + print('[+] Preparing to crawl Monero p2p network') + print(f'[.] Retrieving initial peers from {config.NODE_HOST}:{config.NODE_PORT}') initial_peers = retrieve_peers(config.NODE_HOST, config.NODE_PORT) with geoip2.database.Reader('./data/GeoLite2-City.mmdb') as reader: for peer in initial_peers: @@ -194,13 +194,16 @@ def get_peers(): p.save() print(f'{peer} - saving new peer') else: - print(f'{peer} - already seen') + p = Peer.select().where(Peer.url == peer).first() + p.datetime = datetime.now() + p.save() try: - print(f'[+] Retrieving crawled peers from {_url.netloc}') + print(f'[.] Retrieving crawled peers from {_url.netloc}') new_peers = retrieve_peers(_url.hostname, _url.port) for peer in new_peers: - all_peers.append(peer) + if peer not in all_peers: + all_peers.append(peer) _url = urlparse(peer) url = f"{_url.scheme}://{_url.netloc}".lower() if not Peer.select().where(Peer.url == peer).exists(): @@ -216,12 +219,19 @@ def get_peers(): p.save() print(f'{peer} - saving new peer') else: - print(f'{peer} - already seen') + p = Peer.select().where(Peer.url == peer).first() + p.datetime = datetime.now() + p.save() except: pass - print(f'{len(all_peers)} peers found from {config.NODE_HOST}:{config.NODE_PORT}') - rw_cache('map_peers', all_peers) + print(f'[+] Found {len(all_peers)} peers from {config.NODE_HOST}:{config.NODE_PORT}') + print('[+] Deleting old Monero p2p peers') + for p in Peer.select(): + if p.hours_elapsed() > 24: + print(f'[.] Deleting {p.url}') + p.delete_instance() + rw_cache('map_peers', list(Peer.select().execute())) @app.cli.command("validate") @@ -297,5 +307,11 @@ def humanize(d): t = arrow.get(d, "UTC") return t.humanize() +@app.template_filter("hours_elapsed") +def hours_elapsed(d): + now = datetime.utcnow() + diff = now - d + return diff.total_seconds() / 60 / 60 + if __name__ == "__main__": app.run() diff --git a/xmrnodes/models.py b/xmrnodes/models.py index a6b21ca..cabcd74 100644 --- a/xmrnodes/models.py +++ b/xmrnodes/models.py @@ -35,6 +35,11 @@ class Peer(Model): lon = FloatField(null=True) datetime = DateTimeField(default=datetime.utcnow) + def hours_elapsed(self): + now = datetime.utcnow() + diff = now - self.datetime + return diff.total_seconds() / 60 / 60 + def get_ip(self): return urlparse(self.url).hostname diff --git a/xmrnodes/templates/map.html b/xmrnodes/templates/map.html index ae1fc01..89b407d 100644 --- a/xmrnodes/templates/map.html +++ b/xmrnodes/templates/map.html @@ -33,6 +33,11 @@ .popover-body { min-width: 276px; } + .center.info { + width: 50%; + margin: auto; + padding: 2em; + } @@ -53,36 +58,26 @@ {% endif %} {% endwith %} -
- See more -
- - -

View Map

-
- -

Found Peers (via source node, levin p2p): {{ peers | length }}

-

Source Node: {{ source_node }}

-

- This is not a full representation of the entire Monero network, - just a look into the peers being crawled from the source node ({{ source_node }}) - and the nodes already added to the monero.fail database. - New peers are searched for on a recurring interval throughout the day. -

- -
+
+

Peers seen ~24 hours: {{ peers | length }}

+

Source Node: {{ source_node }}

+

+ This is not a full representation of the entire Monero network, + just a look into the peers being crawled from the source node ({{ source_node }}). + New peers are searched for on a recurring interval throughout the day. + Older peers are shown as more transparent and will be removed if not seen again after 24 hours. +


+ Go home + - Contact me - Source Code - Resources
- - - +
+