mirror of
https://github.com/sileht/bird-lg.git
synced 2024-11-22 06:54:43 +01:00
Add proxy looking
This commit is contained in:
parent
eea79c93ed
commit
535e06a682
15
bird.py
15
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
|
||||
|
||||
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 = ""
|
||||
|
|
2
lg-proxy.cfg
Normal file
2
lg-proxy.cfg
Normal file
|
@ -0,0 +1,2 @@
|
|||
|
||||
ACCESS_LIST = ["91.224.149.206", "178.33.111.110"]
|
61
lg-proxy.py
Normal file
61
lg-proxy.py
Normal 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")
|
||||
|
Loading…
Reference in a new issue