From 078f775d4c2b70ee0e96c8550e3fed6ee9086655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Fri, 1 Jun 2018 17:13:56 +0200 Subject: [PATCH] new: Display IPs --- bgpranking/querying.py | 14 +++++++++++++ website/web/__init__.py | 21 ++++++++++++++++--- website/web/templates/asn.html | 18 +++++++++++++++-- website/web/templates/index.html | 30 +--------------------------- website/web/templates/top_forms.html | 27 +++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 34 deletions(-) create mode 100644 website/web/templates/top_forms.html diff --git a/bgpranking/querying.py b/bgpranking/querying.py index aaa3059..f9fc6d6 100644 --- a/bgpranking/querying.py +++ b/bgpranking/querying.py @@ -5,6 +5,7 @@ from typing import TypeVar import datetime from datetime import timedelta from dateutil.parser import parse +from collections import defaultdict import logging from redis import StrictRedis @@ -77,6 +78,19 @@ class Querying(): return descriptions return descriptions[sorted(descriptions.keys(), reverse=True)[0]] + def get_prefix_ips(self, asn: int, prefix: str, date: Dates=datetime.date.today(), source: str=''): + if source: + sources = [source] + else: + sources = self.get_sources(date) + prefix_ips = defaultdict(list) + d = self.__normalize_date(date) + for source in sources: + ips = set([ip_ts.split('|')[0] + for ip_ts in self.storage.smembers(f'{d}|{source}|{asn}|{prefix}')]) + [prefix_ips[ip].append(source) for ip in ips] + return prefix_ips + def get_asn_history(self, asn: int, period: int=100, source: str='', ipversion: str='v4'): to_return = [] today = datetime.date.today() diff --git a/website/web/__init__.py b/website/web/__init__.py index 4b24658..fa1acee 100644 --- a/website/web/__init__.py +++ b/website/web/__init__.py @@ -9,7 +9,6 @@ from flask_bootstrap import Bootstrap from bgpranking.querying import Querying from datetime import date, timedelta - app = Flask(__name__) app.secret_key = '\xeb\xfd\x1b\xee\xed<\xa5~\xd5H\x85\x00\xa5r\xae\x80t5@\xa2&>\x03S' @@ -18,6 +17,15 @@ Bootstrap(app) app.config['BOOTSTRAP_SERVE_LOCAL'] = True +def get_request_parameter(parameter): + if request.method == 'POST': + d = request.form + elif request.method == 'GET': + d = request.args + + return d.get(parameter, None) + + def load_session(): if request.method == 'POST': d = request.form @@ -57,12 +65,19 @@ def asn_details(): load_session() q = Querying() ranks = q.asn_details(**session) - return render_template('asn.html', ranks=ranks, **session) + prefix = get_request_parameter('prefix') + if prefix: + prefix_ips = q.get_prefix_ips(prefix=prefix, **session) + prefix_ips = [(ip, sorted(sources)) for ip, sources in prefix_ips.items()] + prefix_ips.sort(key=lambda entry: len(entry[1]), reverse=True) + else: + prefix_ips = [] + return render_template('asn.html', ranks=ranks, prefix_ips=prefix_ips, **session) @app.route('/asn_history', methods=['GET', 'POST']) def asn_history(): load_session() - session.pop('date') + session.pop('date', None) q = Querying() return json.dumps(q.get_asn_history(**session)) diff --git a/website/web/templates/asn.html b/website/web/templates/asn.html index 760787c..18d0b19 100644 --- a/website/web/templates/asn.html +++ b/website/web/templates/asn.html @@ -2,7 +2,6 @@ {% block head %} {{ super() }} - {% endblock %} @@ -17,6 +16,7 @@

Ranking - {{asn}}



+ {% include ['top_forms.html'] %} @@ -24,10 +24,24 @@ {% for prefix, rank in ranks %} - + {% endfor %}
Prefix
{{ prefix }}{{ prefix }} {{ rank }}
+ {% if prefix_ips %} + + + + + + {% for ip, sources in prefix_ips %} + + + + + {% endfor %} +
IPSource(s)
{{ ip }}{{ ', '.join(sources) }}
+ {% endif %} {% endblock %} diff --git a/website/web/templates/index.html b/website/web/templates/index.html index 270d9dd..e851ae0 100644 --- a/website/web/templates/index.html +++ b/website/web/templates/index.html @@ -6,35 +6,7 @@

BGP Ranking



-

-

- - -
-
- - -
-
- - -
-

- -
-

- ASN to search: - -
-
+ {% include ['top_forms.html'] %} diff --git a/website/web/templates/top_forms.html b/website/web/templates/top_forms.html new file mode 100644 index 0000000..dfe3265 --- /dev/null +++ b/website/web/templates/top_forms.html @@ -0,0 +1,27 @@ +

+
+ + + +
+ + + +
+ + + +
+ ASN to search: + + +

+

ASN