From 3f4f5a6960a89cb6329833b4cdb1c09cd868df39 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Thu, 26 Jan 2012 17:12:18 +0100 Subject: [PATCH] Use proxy instead for socket redirect --- README | 14 +++++++++++--- lg.cfg | 8 ++++---- lg.py | 40 +++++++++++++++++++++++++--------------- templates/layout.html | 2 +- 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/README b/README index cd12e3d..6359f8b 100644 --- a/README +++ b/README @@ -3,7 +3,15 @@ bird-lg depend on : - python-flask - python-dnspython -It use TCP socket to talk with bird -To convert unix socket to tcp-socket you can use: -# socat UNIX-CONNECT:/var/run/bird.ctl TCP-LISTEN:9994,reuseaddr,fork,range=XXX.XXX.XXX.XXX/32 +On all bird nodes: + fill the config file lg-proxy.cfg by list all ips that can acces to the proxy + and start "python lg-proxy.py" + + +On frontend: + fill config file lg.cfg by list all bird nodes + and start "python lg.py" + + +Each service can by embeded in any webserver by follow regular python-flask configuration diff --git a/lg.cfg b/lg.cfg index fe37bce..92ca327 100644 --- a/lg.cfg +++ b/lg.cfg @@ -1,9 +1,9 @@ DOMAIN = "tetaneutral.net" -HOST_MAPPING={ - "gw": { "ipv4" : 9994, "ipv6": 9996, "traceroute":5000 }, - "h3": { "ipv4" : 9994, "ipv6": 9996, "traceroute":5000 }, -} +PROXY = { + "gw": 5000, + "h3": 5000, + } SESSION_KEY = '\xd77\xf9\xfa\xc2\xb5\xcd\x85)`+H\x9d\xeeW\\%\xbe/\xbaT\x89\xe8\xa7' diff --git a/lg.py b/lg.py index 2cdedec..7836ee5 100755 --- a/lg.py +++ b/lg.py @@ -4,7 +4,8 @@ import sys import os import subprocess import re -import urllib2 +from urllib2 import urlopen +from urllib import quote from toolbox import mask_is_valid, ipv6_is_valid, ipv4_is_valid, resolve @@ -52,22 +53,35 @@ def set_session(req_type, hosts, proto, request_args): if not history: session["history"] = {} session["history"][req_type] = req_hist[:10] -def bird_command(host, proto, command): - conf = app.config["HOST_MAPPING"].get(host, None) - port = conf.get(proto) - if not conf or not port: +def bird_command(host, proto, query): + return bird_proxy(host, proto, "bird", query) + +def bird_proxy(host, proto, service, query): + path = "" + if proto == "ipv6": path = service + "6" + elif proto == "ipv4": path = service + port = app.config["PROXY"].get(host,"") + if not port or not path: return False, "Host/Proto not allowed" else: - b = BirdSocketSingleton(host, port) - return b.cmd(command) + url = "http://%s.%s:%d/%s?q=%s" % (host, app.config["DOMAIN"], port, path, quote(query)) + try: + f = urlopen(url) + resultat = f.read() + app.logger.debug(resultat) + status = True # retreive remote status + except IOError: + resultat = "Failed retreive url: %s" % url + status = False + return status, resultat @app.context_processor def inject_all_host(): - return dict(all_hosts="+".join(app.config["HOST_MAPPING"].keys())) + return dict(all_hosts="+".join(app.config["PROXY"].keys())) @app.route("/") def hello(): - return redirect("/summary/%s/ipv4" % "+".join(app.config["HOST_MAPPING"].keys()) ) + return redirect("/summary/%s/ipv4" % "+".join(app.config["PROXY"].keys()) ) def error_page(text): return render_template('error.html', data = { "error": text } ), 500 @@ -138,12 +152,8 @@ def traceroute(hosts, proto): infos = {} for host in hosts.split("+"): - url = "http://%s.%s:%s/traceroute%s?q=%s" % ( host, app.config["DOMAIN"], app.config["HOST_MAPPING"].get(host).get("traceroute","5000"), (proto == "ipv6" and "6" or ""), q) - try: - f = urllib2.urlopen(url) - infos[host] = add_links(f.read()) - except IOError: - infos[host] = "Failed retreive url: %s" % url + status, resultat = bird_proxy(host, proto, "traceroute", q) + infos[host] = add_links(resultat) return render_template('traceroute.html', infos=infos) @app.route("/where//") diff --git a/templates/layout.html b/templates/layout.html index dc30621..63b4be5 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -128,7 +128,7 @@ $( function() {
  • all
  • - {% for host in config.HOST_MAPPING %} + {% for host in config.PROXY %}
  • {{host}}
  • {% endfor %}