2012-01-27 19:12:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: ts=4
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Copyright (c) 2006 Mehdi Abaakouk
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 3 as
|
|
|
|
# published by the Free Software Foundation
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
#
|
|
|
|
###
|
2012-01-26 16:18:58 +01:00
|
|
|
|
|
|
|
|
2014-01-28 16:33:19 +01:00
|
|
|
import sys
|
2012-08-21 11:12:58 +02:00
|
|
|
import logging
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
2012-11-27 20:05:54 +01:00
|
|
|
from logging import FileHandler
|
2012-01-26 16:18:58 +01:00
|
|
|
import subprocess
|
|
|
|
from urllib import unquote
|
|
|
|
|
|
|
|
from bird import BirdSocket
|
|
|
|
|
|
|
|
from flask import Flask, request, abort
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
2012-08-21 11:12:58 +02:00
|
|
|
app.debug = app.config["DEBUG"]
|
2014-02-18 11:57:44 +01:00
|
|
|
app.config.from_pyfile('lgproxy.cfg')
|
2012-01-26 16:18:58 +01:00
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
file_handler = TimedRotatingFileHandler(filename=app.config["LOG_FILE"], when="midnight")
|
2012-11-27 20:05:54 +01:00
|
|
|
app.logger.setLevel(getattr(logging, app.config["LOG_LEVEL"].upper()))
|
2012-08-21 11:12:58 +02:00
|
|
|
app.logger.addHandler(file_handler)
|
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
|
2012-11-27 20:05:54 +01:00
|
|
|
@app.before_request
|
|
|
|
def access_log_before(*args, **kwargs):
|
2015-01-27 17:22:16 +01:00
|
|
|
app.logger.info("[%s] request %s, %s", request.remote_addr, request.url, "|".join(["%s:%s" % (k, v) for k, v in request.headers.items()]))
|
|
|
|
|
2012-11-27 20:05:54 +01:00
|
|
|
|
|
|
|
@app.after_request
|
|
|
|
def access_log_after(response, *args, **kwargs):
|
2015-01-27 17:22:16 +01:00
|
|
|
app.logger.info("[%s] reponse %s, %s", request.remote_addr, request.url, response.status_code)
|
2012-11-27 20:05:54 +01:00
|
|
|
return response
|
2012-08-21 11:12:58 +02:00
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
|
2012-01-26 16:18:58 +01:00
|
|
|
def check_accesslist():
|
2015-01-27 17:22:16 +01:00
|
|
|
if app.config["ACCESS_LIST"] and request.remote_addr not in app.config["ACCESS_LIST"]:
|
|
|
|
abort(401)
|
|
|
|
|
|
|
|
|
|
|
|
def check_features():
|
|
|
|
features = app.config.get('FEATURES', [])
|
|
|
|
if request.endpoint not in features:
|
2012-01-26 16:18:58 +01:00
|
|
|
abort(401)
|
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
|
2012-01-26 16:18:58 +01:00
|
|
|
@app.route("/traceroute")
|
|
|
|
@app.route("/traceroute6")
|
|
|
|
def traceroute():
|
|
|
|
check_accesslist()
|
2015-01-27 17:55:31 +01:00
|
|
|
check_features()
|
2015-01-27 17:22:16 +01:00
|
|
|
|
2014-01-28 16:55:36 +01:00
|
|
|
if sys.platform.startswith('freebsd') or sys.platform.startswith('netbsd') or sys.platform.startswith('openbsd'):
|
2015-01-27 17:22:16 +01:00
|
|
|
traceroute4 = ['traceroute']
|
|
|
|
traceroute6 = ['traceroute6']
|
|
|
|
else: # For Linux
|
|
|
|
traceroute4 = ['traceroute', '-4']
|
|
|
|
traceroute6 = ['traceroute', '-6']
|
2014-01-28 16:55:36 +01:00
|
|
|
|
2012-02-07 15:40:48 +01:00
|
|
|
src = []
|
2015-01-27 17:22:16 +01:00
|
|
|
if request.path == '/traceroute6':
|
|
|
|
traceroute = traceroute6
|
2015-01-27 17:55:31 +01:00
|
|
|
if app.config.get("IPV6_SOURCE", ""):
|
|
|
|
src = ["-s", app.config.get("IPV6_SOURCE")]
|
2015-01-27 17:22:16 +01:00
|
|
|
else:
|
|
|
|
traceroute = traceroute4
|
2015-01-27 17:55:31 +01:00
|
|
|
if app.config.get("IPV4_SOURCE", ""):
|
|
|
|
src = ["-s", app.config.get("IPV4_SOURCE")]
|
2012-01-26 16:18:58 +01:00
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
query = request.args.get("q", "")
|
2012-01-26 16:18:58 +01:00
|
|
|
query = unquote(query)
|
|
|
|
|
2014-01-28 16:33:19 +01:00
|
|
|
if sys.platform.startswith('freebsd') or sys.platform.startswith('netbsd'):
|
2015-01-27 17:22:16 +01:00
|
|
|
options = ['-a', '-q1', '-w1', '-m15']
|
2014-01-28 16:44:32 +01:00
|
|
|
elif sys.platform.startswith('openbsd'):
|
2015-01-27 17:22:16 +01:00
|
|
|
options = ['-A', '-q1', '-w1', '-m15']
|
|
|
|
else: # For Linux
|
|
|
|
options = ['-A', '-q1', '-N32', '-w1', '-m15']
|
|
|
|
command = traceroute + src + options + [query]
|
|
|
|
result = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0].decode('utf-8', 'ignore').replace("\n", "<br>")
|
2012-01-26 16:18:58 +01:00
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
return result
|
2012-01-26 16:18:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/bird")
|
|
|
|
@app.route("/bird6")
|
|
|
|
def bird():
|
|
|
|
check_accesslist()
|
2015-01-27 17:55:31 +01:00
|
|
|
check_features()
|
2012-01-26 16:18:58 +01:00
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
if request.path == "/bird":
|
|
|
|
b = BirdSocket(file=app.config.get('SOCKET_PATH').get(4))
|
|
|
|
elif request.path == "/bird6":
|
|
|
|
b = BirdSocket(file=app.config.get('SOCKET_PATH').get(6))
|
|
|
|
else:
|
|
|
|
return "No bird socket selected"
|
2012-01-26 16:18:58 +01:00
|
|
|
|
2015-01-27 17:22:16 +01:00
|
|
|
query = request.args.get("q", "")
|
2012-01-26 16:18:58 +01:00
|
|
|
query = unquote(query)
|
|
|
|
|
|
|
|
status, result = b.cmd(query)
|
|
|
|
b.close()
|
2012-01-26 17:38:57 +01:00
|
|
|
# FIXME: use status
|
|
|
|
return result
|
2015-01-27 17:22:16 +01:00
|
|
|
|
2012-01-26 16:18:58 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-02-18 11:57:44 +01:00
|
|
|
app.logger.info("lgproxy start")
|
2012-01-26 16:18:58 +01:00
|
|
|
app.run("0.0.0.0")
|
|
|
|
|