From 535e06a682350fb8857a3c525f013e6ee97f6771 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Thu, 26 Jan 2012 16:18:58 +0100 Subject: [PATCH] Add proxy looking --- bird.py | 21 ++++++++++++------ lg-proxy.cfg | 2 ++ lg-proxy.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 lg-proxy.cfg create mode 100644 lg-proxy.py diff --git a/bird.py b/bird.py index 9cee93c..c4e949f 100644 --- a/bird.py +++ b/bird.py @@ -77,7 +77,8 @@ def BirdSocketSingleton(host, port): class BirdSocket: - def __init__(self, host, port): + def __init__(self, host="", port="", file=""): + self.__file = file self.__host = host self.__port = port self.__sock = None @@ -85,9 +86,17 @@ class BirdSocket: def __connect(self): if self.__sock: return - self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.__sock.settimeout(3.0) - self.__sock.connect((self.__host, self.__port)) + if not file: + self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.__sock.settimeout(3.0) + self.__sock.connect((self.__host, self.__port)) + else: + self.__sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.__sock.settimeout(3.0) + self.__sock.connect(self.__file) + + # read welcome message + self.__sock.recv(1024) def close(self): if self.__sock: @@ -99,14 +108,14 @@ class BirdSocket: try: self.__connect() self.__sock.send(cmd + "\n") - return self.__read() + data = self.__read() + return data except socket.error: why = sys.exc_info()[1] self.close() return False, "Bird connection problem: %s" % why def __read(self): - code = "7000" # Not used in bird parsed_string = "" lastline = "" diff --git a/lg-proxy.cfg b/lg-proxy.cfg new file mode 100644 index 0000000..223af9f --- /dev/null +++ b/lg-proxy.cfg @@ -0,0 +1,2 @@ + +ACCESS_LIST = ["91.224.149.206", "178.33.111.110"] diff --git a/lg-proxy.py b/lg-proxy.py new file mode 100644 index 0000000..5be5735 --- /dev/null +++ b/lg-proxy.py @@ -0,0 +1,61 @@ + + +import subprocess +from urllib import unquote + +from bird import BirdSocket + +from flask import Flask, request, abort + +app = Flask(__name__) +app.config.from_pyfile('lg-proxy.cfg') + +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() + + if request.path == '/traceroute6': o= "-6" + else: o = "-4" + + query = request.args.get("q","") + query = unquote(query) + + command = [ 'traceroute', o, '-A', '-q1', '-w2', query] + result = subprocess.Popen( command , stdout=subprocess.PIPE).communicate()[0].decode('utf-8', 'ignore').replace("\n","
") + + 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") + else: return "No bird socket selected", 700 + + query = request.args.get("q","") + query = unquote(query) + + status, result = b.cmd(query) + b.close() + + app.logger.debug(result) + + if status: status = 200 + else: status = 700 + + return result, status + + +if __name__ == "__main__": + app.debug = True + app.run("0.0.0.0") +