new: Proxy for direct calls to IPASN History

pull/12/head
Raphaël Vinot 2019-01-03 18:59:19 +01:00
parent b55a2ecb2c
commit c71b2bdf7c
2 changed files with 25 additions and 1 deletions

View File

@ -82,12 +82,17 @@ def get_socket_path(name: str):
return str(get_homedir() / mapping[name])
def get_ipasn():
def load_general_config():
general_config_file = get_config_path() / 'bgpranking.json'
if not general_config_file.exists():
raise MissingConfigFile(f'The general configuration file ({general_config_file}) does not exists.')
with open(general_config_file) as f:
config = json.load(f)
return config, general_config_file
def get_ipasn():
config, general_config_file = load_general_config()
if 'ipasnhistory_url' not in config:
raise MissingConfigEntry(f'"ipasnhistory_url" is missing in {general_config_file}.')
ipasn = IPASNHistory(config['ipasnhistory_url'])

View File

@ -6,10 +6,14 @@ try:
except ImportError:
import json
import requests
from flask import Flask, render_template, request, session, Response, redirect, url_for
from flask_bootstrap import Bootstrap
from bgpranking.querying import Querying
from bgpranking.libs.exceptions import MissingConfigEntry
from bgpranking.libs.helpers import load_general_config
from datetime import date, timedelta
import pycountry
from collections import defaultdict
@ -68,6 +72,21 @@ def get_country_codes():
yield c.alpha_2, c.name
@app.route('/ipasn_history/', defaults={'path': ''}, methods=['GET', 'POST'])
@app.route('/ipasn_history/<path:path>', methods=['GET', 'POST'])
def ipasn_history_proxy(path):
config, general_config_file = load_general_config()
if 'ipasnhistory_url' not in config:
raise MissingConfigEntry(f'"ipasnhistory_url" is missing in {general_config_file}.')
full_path_to_proxy = request.full_path.replace('/ipasn_history/', '')
proxied_url = f'{config["ipasnhistory_url"]}{full_path_to_proxy}'
if request.method in ['GET', 'HEAD']:
to_return = requests.get(proxied_url).json()
elif request.method == 'POST':
to_return = requests.post(proxied_url, data=request.data).json()
return Response(json.dumps(to_return), mimetype='application/json')
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'HEAD':