df090b6dd23b — Malcolm Scott 7 years ago
Initial commit
1 files changed, 73 insertions(+), 0 deletions(-)

A => nfw-redirector
A => nfw-redirector +73 -0
@@ 0,0 1,73 @@ 
+#!/usr/bin/python
+
+import os
+import urllib
+
+try:
+	import sh  # python-sh
+except ImportError:
+	try:
+		import pbs as sh  # python-pbs (obsolete version of sh)
+	except ImportError:
+		raise ImportError("No module named either sh or pbs")
+
+
+class RedirectorApplication(object):
+
+	def __init__(self, url_template):
+		self.url_template = url_template + "?%s"
+
+	def build_url(params):
+		return self.url_template % urllib.urlencode({k:v for k,v in params.iteritems() if v is not None})
+
+	def application(self, env, start_response):
+		try:
+			orig_url = "http%s://%s%s" % (['s', '']['HTTPS' not in env or env['HTTPS']==''], env['HTTP_HOST'], env['REQUEST_URI'])
+		except KeyError:
+			orig_url = None
+		client_addr = env['REMOTE_ADDR']
+		neigh_line = sh.ip('neigh', 'ls', env['REMOTE_ADDR']).split(" ")
+		found = False
+		lladdr = None
+		for neigh_word in neigh_line:
+			if found:
+				lladdr = neigh_word
+				break
+			if neigh_word == "lladdr":
+				found = True
+		if lladdr is None:
+			start_response("500 Internal Server Error", [('Content-type', 'text/plain')])
+			return "Could not find the MAC address of your computer (%s)!  Please contact the IT department." % client_addr
+		redirect = build_url({'lladdr': lladdr, 'addr': client_addr, 'url': orig_url})
+		start_response("302 Found", [
+				('Content-type', 'text/plain'),
+				('Location', redirect),
+				('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'),
+				('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT')
+			])
+		return "Redirecting you to the network login page..."
+
+
+if __name__ == "__main__":
+	from flup.server import fcgi  # python-flup, currently Python 2 only
+	import argparse
+
+	parser = argparse.ArgumentParser()
+	parser.add_argument("URL",
+			help="URL stem for redirect generation (will have lladdr, addr and url parameters appended)")
+	parser.add_argument("-a", "--bind-address",
+			default="localhost",
+			help="Address to which the FastCGI server will bind (default: localhost)")
+	parser.add_argument("-p", "--bind-port", type=int,
+			default=8080,
+			help="Port to which the FastCGI server will bind (default: 8080)")
+	parser.add_argument
+	args = parser.parse_args()
+
+	app = RedirectorApplication(args.URL)
+	server = fcgi.WSGIServer(app.application, bindAddress=(args.bind_address, args.bind_port))
+	server.run()
+
+
+# vim: tabstop=4 shiftwidth=4 noexpandtab
+