def ipasn(): d = None if request.method == 'POST': d = request.form elif request.method == 'GET': d = request.args if not d or 'ip' not in d: return render_template('ipasn.html') else: if isinstance(d['ip'], list): ip = d['ip'][0] else: ip = d['ip'] ipasn = get_ipasn() q = Querying() response = ipasn.query(first=(date.today() - timedelta(days=60)).isoformat(), aggregate=True, ip=ip) for r in response['response']: r['asn_descriptions'] = [] asn_descriptions = q.get_asn_descriptions( asn=r['asn'], all_descriptions=True)['response'] for timestamp in sorted(asn_descriptions.keys()): if r['first_seen'] <= timestamp <= r['last_seen']: r['asn_descriptions'].append(asn_descriptions[timestamp]) if not r['asn_descriptions'] and timestamp <= r['last_seen']: r['asn_descriptions'].append(asn_descriptions[timestamp]) return render_template('ipasn.html', ipasn_details=response['response'], **response['meta'])
def json_asn(): # TODO # * Filter on date => if only returning one descr, return the desription at that date query = request.get_json(force=True) to_return = {'meta': query, 'response': {}} if 'asn' not in query: to_return['error'] = f'You need to pass an asn - {query}' return Response(json.dumps(to_return), mimetype='application/json') q = Querying() asn_description_query = {'asn': query['asn']} if 'all_descriptions' in query: asn_description_query['all_descriptions'] = query['all_descriptions'] to_return['response']['asn_description'] = q.get_asn_descriptions( **asn_description_query)['response'] asn_rank_query = {'asn': query['asn']} if 'date' in query: asn_rank_query['date'] = query['date'] if 'source' in query: asn_rank_query['source'] = query['source'] else: asn_rank_query['with_position'] = True if 'ipversion' in query: asn_rank_query['ipversion'] = query['ipversion'] to_return['response']['ranking'] = q.asn_rank(**asn_rank_query)['response'] return Response(json.dumps(to_return), mimetype='application/json')
def country(): load_session() q = Querying() sources = q.get_sources(date=session['date'])['response'] return render_template('country.html', sources=sources, countries=get_country_codes(), **session)
def asn_history(): query = request.get_json(force=True) to_return = {'meta': query, 'response': {}} if 'asn' not in query: to_return['error'] = f'You need to pass an asn - {query}' return Response(json.dumps(to_return), mimetype='application/json') q = Querying() to_return['response']['asn_history'] = q.get_asn_history(**query)['response'] return Response(json.dumps(to_return), mimetype='application/json')
def country_history(): q = Querying() if request.method == 'GET': load_session() return Response(json.dumps(q.country_history(**session)), mimetype='application/json') query = request.get_json(force=True) to_return = {'meta': query, 'response': {}} to_return['response']['country_history'] = q.country_history( **query)['response'] return Response(json.dumps(to_return), mimetype='application/json')
def index(): if request.method == 'HEAD': # Just returns ack if the webserver is running return 'Ack' load_session() q = Querying() sources = q.get_sources(date=session['date'])['response'] session.pop('asn', None) session.pop('country', None) ranks = q.asns_global_ranking(limit=100, **session)['response'] r = [(asn, rank, q.get_asn_descriptions(int(asn))['response']) for asn, rank in ranks] return render_template('index.html', ranks=r, sources=sources, countries=get_country_codes(), **session)
def asn_details(): load_session() q = Querying() if 'asn' not in session: return redirect(url_for('/')) asn_descriptions = q.get_asn_descriptions(asn=session['asn'], all_descriptions=True)['response'] sources = q.get_sources(date=session['date'])['response'] prefix = session.pop('prefix', None) ranks = q.asn_details(**session)['response'] if prefix: prefix_ips = q.get_prefix_ips(prefix=prefix, **session)['response'] 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', sources=sources, ranks=ranks, prefix_ips=prefix_ips, asn_descriptions=asn_descriptions, **session)
def json_asns_global_ranking(): query = request.get_json(force=True) to_return = {'meta': query, 'response': {}} q = Querying() to_return['response'] = q.asns_global_ranking(**query)['response'] return Response(json.dumps(to_return), mimetype='application/json')
def country_history(): query = request.get_json(force=True) to_return = {'meta': query, 'response': {}} q = Querying() to_return['response']['country_history'] = q.country_history(**query)['response'] return Response(json.dumps(to_return), mimetype='application/json')