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
|
|
|
|
|
|
|
|
2012-08-21 11:12:58 +02:00
|
|
|
import logging
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
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"]
|
2012-01-26 16:18:58 +01:00
|
|
|
app.config.from_pyfile('lg-proxy.cfg')
|
|
|
|
|
2012-08-21 11:12:58 +02:00
|
|
|
file_handler = TimedRotatingFileHandler(filename=app.config["LOG_FILE"], when="midnight")
|
|
|
|
file_handler.setLevel(getattr(logging, app.config["LOG_LEVEL"].upper()))
|
|
|
|
app.logger.addHandler(file_handler)
|
|
|
|
|
|
|
|
|
2012-01-26 16:18:58 +01:00
|
|
|
def check_accesslist():
|
|
|
|
if app.config["ACCESS_LIST"] and request.remote_addr not in app.config["ACCESS_LIST"]:
|
|
|
|
abort(401)
|
|
|
|
|
|
|
|
@app.route("/traceroute")
|
|
|
|
@app.route("/traceroute6")
|
|
|
|
def traceroute():
|
|
|
|
check_accesslist()
|
2012-02-07 15:40:48 +01:00
|
|
|
|
|
|
|
src = []
|
2012-02-07 15:38:23 +01:00
|
|
|
if request.path == '/traceroute6':
|
|
|
|
o = "-6"
|
|
|
|
if app.config.get("IPV6_SOURCE",""):
|
|
|
|
src = [ "-s", app.config.get("IPV6_SOURCE") ]
|
|
|
|
|
|
|
|
else:
|
|
|
|
o = "-4"
|
|
|
|
if app.config.get("IPV4_SOURCE",""):
|
|
|
|
src = [ "-s", app.config.get("IPV4_SOURCE") ]
|
2012-01-26 16:18:58 +01:00
|
|
|
|
|
|
|
query = request.args.get("q","")
|
|
|
|
query = unquote(query)
|
|
|
|
|
2012-02-07 15:38:23 +01:00
|
|
|
command = [ 'traceroute' , o ] + src + [ '-A', '-q1', '-N32', '-w1', '-m15', query ]
|
2012-01-26 16:18:58 +01:00
|
|
|
result = subprocess.Popen( command , stdout=subprocess.PIPE).communicate()[0].decode('utf-8', 'ignore').replace("\n","<br>")
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/bird")
|
|
|
|
@app.route("/bird6")
|
|
|
|
def bird():
|
|
|
|
check_accesslist()
|
|
|
|
|
|
|
|
if request.path == "/bird": b = BirdSocket(file="/var/run/bird.ctl")
|
|
|
|
elif request.path == "/bird6": b = BirdSocket(file="/var/run/bird6.ctl")
|
2012-01-26 17:38:57 +01:00
|
|
|
else: return "No bird socket selected"
|
2012-01-26 16:18:58 +01:00
|
|
|
|
|
|
|
query = request.args.get("q","")
|
|
|
|
query = unquote(query)
|
|
|
|
|
|
|
|
status, result = b.cmd(query)
|
|
|
|
b.close()
|
2012-01-26 17:38:57 +01:00
|
|
|
# FIXME: use status
|
|
|
|
return result
|
2012-01-26 16:18:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run("0.0.0.0")
|
|
|
|
|