1
0
Fork 0
mirror of https://github.com/sileht/bird-lg.git synced 2024-11-25 16:24:43 +01:00

Add proxy looking

This commit is contained in:
Mehdi Abaakouk 2012-01-26 16:18:58 +01:00 committed by root
parent eea79c93ed
commit 535e06a682
3 changed files with 78 additions and 6 deletions

15
bird.py
View file

@ -77,7 +77,8 @@ def BirdSocketSingleton(host, port):
class BirdSocket: class BirdSocket:
def __init__(self, host, port): def __init__(self, host="", port="", file=""):
self.__file = file
self.__host = host self.__host = host
self.__port = port self.__port = port
self.__sock = None self.__sock = None
@ -85,9 +86,17 @@ class BirdSocket:
def __connect(self): def __connect(self):
if self.__sock: return if self.__sock: return
if not file:
self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.__sock.settimeout(3.0) self.__sock.settimeout(3.0)
self.__sock.connect((self.__host, self.__port)) 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): def close(self):
if self.__sock: if self.__sock:
@ -99,14 +108,14 @@ class BirdSocket:
try: try:
self.__connect() self.__connect()
self.__sock.send(cmd + "\n") self.__sock.send(cmd + "\n")
return self.__read() data = self.__read()
return data
except socket.error: except socket.error:
why = sys.exc_info()[1] why = sys.exc_info()[1]
self.close() self.close()
return False, "Bird connection problem: %s" % why return False, "Bird connection problem: %s" % why
def __read(self): def __read(self):
code = "7000" # Not used in bird code = "7000" # Not used in bird
parsed_string = "" parsed_string = ""
lastline = "" lastline = ""

2
lg-proxy.cfg Normal file
View file

@ -0,0 +1,2 @@
ACCESS_LIST = ["91.224.149.206", "178.33.111.110"]

61
lg-proxy.py Normal file
View file

@ -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","<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")
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")