2018-04-11 14:55:20 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-05-31 15:48:11 +02:00
|
|
|
import json
|
|
|
|
|
2018-04-12 18:09:04 +02:00
|
|
|
from flask import Flask, render_template, request, session
|
2018-04-11 14:55:20 +02:00
|
|
|
from flask_bootstrap import Bootstrap
|
|
|
|
|
|
|
|
from bgpranking.querying import Querying
|
2018-04-12 18:09:04 +02:00
|
|
|
from datetime import date, timedelta
|
|
|
|
|
2018-04-11 14:55:20 +02:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
2018-04-12 18:09:04 +02:00
|
|
|
app.secret_key = '\xeb\xfd\x1b\xee\xed<\xa5~\xd5H\x85\x00\xa5r\xae\x80t5@\xa2&>\x03S'
|
|
|
|
|
2018-04-11 14:55:20 +02:00
|
|
|
Bootstrap(app)
|
|
|
|
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
|
|
|
|
|
2018-04-12 18:09:04 +02:00
|
|
|
|
2018-06-01 17:13:56 +02:00
|
|
|
def get_request_parameter(parameter):
|
|
|
|
if request.method == 'POST':
|
|
|
|
d = request.form
|
|
|
|
elif request.method == 'GET':
|
|
|
|
d = request.args
|
|
|
|
|
|
|
|
return d.get(parameter, None)
|
|
|
|
|
|
|
|
|
2018-04-12 18:09:04 +02:00
|
|
|
def load_session():
|
|
|
|
if request.method == 'POST':
|
|
|
|
d = request.form
|
|
|
|
elif request.method == 'GET':
|
|
|
|
d = request.args
|
2018-04-11 14:55:20 +02:00
|
|
|
|
2018-04-12 18:09:04 +02:00
|
|
|
if 'date' in d:
|
|
|
|
session['date'] = d['date']
|
|
|
|
if 'ipversion' in d:
|
|
|
|
session['ipversion'] = d['ipversion']
|
|
|
|
if 'source' in d:
|
|
|
|
session['source'] = d['source']
|
|
|
|
if 'asn' in d:
|
|
|
|
session['asn'] = d['asn']
|
|
|
|
set_default_date_session()
|
2018-04-11 14:55:20 +02:00
|
|
|
|
2018-04-12 18:09:04 +02:00
|
|
|
|
|
|
|
def set_default_date_session():
|
|
|
|
if 'date' not in session:
|
|
|
|
session['date'] = (date.today() - timedelta(days=1)).isoformat()
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
2018-04-11 14:55:20 +02:00
|
|
|
def index():
|
2018-04-12 18:09:04 +02:00
|
|
|
load_session()
|
2018-04-11 14:55:20 +02:00
|
|
|
q = Querying()
|
2018-04-12 18:09:04 +02:00
|
|
|
sources = q.get_sources(date=session['date'])
|
|
|
|
session.pop('asn', None)
|
2018-05-31 15:48:11 +02:00
|
|
|
ranks = q.asns_global_ranking(limit=100, **session)
|
2018-04-13 18:02:44 +02:00
|
|
|
descriptions = [q.get_asn_descriptions(int(asn)) for asn, rank in ranks]
|
|
|
|
r = zip(ranks, descriptions)
|
|
|
|
return render_template('index.html', ranks=r, sources=sources, **session)
|
2018-04-11 15:31:34 +02:00
|
|
|
|
|
|
|
|
2018-04-11 16:16:19 +02:00
|
|
|
@app.route('/asn', methods=['GET', 'POST'])
|
|
|
|
def asn_details():
|
2018-04-12 18:09:04 +02:00
|
|
|
load_session()
|
2018-04-11 15:31:34 +02:00
|
|
|
q = Querying()
|
2018-06-07 16:52:05 +02:00
|
|
|
asn_descriptions = q.get_asn_descriptions(asn=session['asn'], all_descriptions=True)
|
2018-06-07 16:18:50 +02:00
|
|
|
sources = q.get_sources(date=session['date'])
|
2018-04-12 18:09:04 +02:00
|
|
|
ranks = q.asn_details(**session)
|
2018-06-01 17:13:56 +02:00
|
|
|
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 = []
|
2018-06-07 16:52:05 +02:00
|
|
|
return render_template('asn.html', sources=sources, ranks=ranks, prefix_ips=prefix_ips, asn_descriptions=asn_descriptions, **session)
|
2018-05-31 15:48:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/asn_history', methods=['GET', 'POST'])
|
|
|
|
def asn_history():
|
|
|
|
load_session()
|
|
|
|
q = Querying()
|
|
|
|
return json.dumps(q.get_asn_history(**session))
|