Last active 1535433284

A Looking Glass implementation in Python3 and Flask. Vet code for security concerns before deploying, this isn't tested for vulnerabilities in any way

Revision e63ca2d8984d995b97ba0dc7f8844285ead9f4f1

lookingglass.py Raw
1#!/usr/bin/env python3
2from flask import Flask,request,make_response
3from shlex import quote
4from subprocess import check_output
5import socket
6from requests import post
7
8app = Flask(__name__)
9
10@app.route('/')
11def index():
12 return """<form method="post" action="process">
13 <input type="text" name="host">
14 <select name="service">
15 <option value="ping">Ping</option>
16 <option value="mtr">MTR</option>
17 <option value="bgp">bgp</option>
18 </select>
19 <input type="submit"></form>
20"""
21
22@app.route("/process", methods=["POST"])
23def process():
24 host,service = request.form.get("host", "8.8.8.8"),request.form.get("service","ping")
25 try:
26 # Is it an ip?
27 _ = socket.inet_aton(host)
28 except OSError:
29 # Well it's not ipv4,
30 try:
31 _ = socket.inet_pton(socket.AF_INET6, host)
32 except OSError:
33 # Not ipv6 either. We'll try resolving dns
34 try:
35 host = socket.gethostbyname(host)
36 except socket.gaierror:
37 return "We only support ipv4/ipv6/valid domains\n"
38 else:
39 pass # host is ipv6
40 else:
41 pass # host is ipv4
42 print("Processing request {},{} from {} {}".format(host, service, request.environ["REMOTE_ADDR"], request.headers.get("User-Agent")))
43 if service == "ping":
44 resp = make_response(check_output(["ping", "-i", "0.2", "-q", "-c", "4", quote(host)]))
45 elif service == "mtr":
46 resp = make_response(check_output(["mtr", "-c", "1", "--report-wide", "-m", "60", "-b", quote(host)]))
47 elif service == "bgp":
48 data = post("http://noc.i3d.net/looking-glass/execute.php", data={"routers": "ausyd1-rt001i", "query": "bgp", "parameter": host, "dontlook": ""}).json()["result"][8:-6].strip().replace('</kdb></p><pre class="pre-scrollable">', "\n\n")
49 resp = make_response(data)
50 else:
51 return "oi"
52 print(" Completed request {},{} from {} {}".format(host,service, request.environ["REMOTE_ADDR"], request.headers.get("User-Agent")))
53 resp.headers["Content-Type"] = "text/plain"
54 return resp
55
56if __name__ == "__main__":
57 app.run()