From c71b2bdf7c6cf40836f7ea3e485b09715e9a876e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Thu, 3 Jan 2019 18:59:19 +0100 Subject: [PATCH] new: Proxy for direct calls to IPASN History --- bgpranking/libs/helpers.py | 7 ++++++- website/web/__init__.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/bgpranking/libs/helpers.py b/bgpranking/libs/helpers.py index dda2c74..067aecd 100644 --- a/bgpranking/libs/helpers.py +++ b/bgpranking/libs/helpers.py @@ -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']) diff --git a/website/web/__init__.py b/website/web/__init__.py index 4ef6e8b..2da8ea8 100644 --- a/website/web/__init__.py +++ b/website/web/__init__.py @@ -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/', 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':