diff --git a/bc b/bc index 88f9517..674ad1c 100755 --- a/bc +++ b/bc @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python # -*- coding: iso-8859-15 -*- """ BC (Border-Check) is a tool to retrieve info of traceroute tests over website navigation routes. diff --git a/bc_gtk.py b/bc_gtk.py deleted file mode 100644 index 69cace4..0000000 --- a/bc_gtk.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env python -# -*- coding: iso-8859-15 -*- -""" -BC (Border-Check) is a tool to retrieve info of traceroute tests over website navigation routes. -GPLv3 - 2013 by psy (epsylon@riseup.net) -""" -from main_gtk import GuiStarter, GuiUtils - -try: - import gtk, gtk.glade -except: - print ("\nError importing: Gtk/Glade libs. \n\nOn Debian based systems, please try like root:\n\n $ apt-get install python-gtk2\n") - sys.exit(2) - -class BCGTK(): - @staticmethod - def run(): - GuiStarter() - gtk.main() - diff --git a/builder.xml b/builder.xml deleted file mode 100644 index befa3e8..0000000 --- a/builder.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - True - Border Check - GTK Version - center - - - True - WM1.svg - - - - diff --git a/main.py b/main.py index 2b80db9..9f11fb2 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python # -*- coding: iso-8859-15 -*- """ BC (Border-Check) is a tool to retrieve info of traceroute tests over website navigation routes. @@ -19,7 +19,7 @@ except: import subprocess, socket from options import BCOptions -from bc_gtk import BCGTK +from webserver import BorderCheckWebserver # set to emit debug messages about errors (0 = off). DEBUG = 1 @@ -77,7 +77,7 @@ class bc(object): b = subprocess.Popen(['locate', 'places.sqlite']) # check for Firefox if b != "": self.browser = "F" #Firefox - self.browser_path = "/Users/ARRA/Library/Application Support/Firefox/Profiles/m9absgs2.default/places.sqlite" #automatic extracion of path + self.browser_path = "" #automatic extracion of path else: self.browser = "C" #Chrome self.browser_path = "" @@ -145,8 +145,9 @@ class bc(object): # extract url url = self.try_running(self.getURL, "\nInternal error getting urls from browser's database.") print "url:", self.url - # start gtk mode - BCGTK.run() + # start web mode + print("Running webserver\n") + BorderCheckWebserver(self) while True: url = urlparse(self.url[0]).netloc diff --git a/main_gtk.py b/main_gtk.py deleted file mode 100644 index c5f153b..0000000 --- a/main_gtk.py +++ /dev/null @@ -1,108 +0,0 @@ -#!/usr/bin/env python -# -*- coding: iso-8859-15 -*- -""" -BC (Border-Check) is a tool to retrieve info of traceroute tests over website navigation routes. -GPLv3 - 2013 by psy (epsylon@riseup.net) -""" -import sys -try: - import gtk, gtk.glade -except: - print ("\nError importing: Gtk/Glade libs. \n\nOn Debian based systems, please try like root:\n\n $ apt-get install python-gtk2\n") - sys.exit(2) - -class GuiUtils(object): - @staticmethod - def GetBuilder(name): - builder = gtk.Builder() - if not builder.add_from_file('builder.xml'): - print 'XML file not found!' - sys.exit(1) - else: - return builder - - @staticmethod - def Error(title, text): - """Show error popup""" - dialog = gtk.MessageDialog( - parent = None, - flags = gtk.DIALOG_DESTROY_WITH_PARENT, - type = gtk.MESSAGE_ERROR, - buttons = gtk.BUTTONS_OK, - message_format = text) - dialog.set_title(title) - dialog.connect('response', lambda dialog, response: dialog.destroy()) - dialog.show() - print text - - @staticmethod - def Info(title, text): - """Show info popup""" - dialog = gtk.MessageDialog( - parent = None, - flags = gtk.DIALOG_DESTROY_WITH_PARENT, - type = gtk.MESSAGE_INFO, - buttons = gtk.BUTTONS_OK, - message_format = text) - dialog.set_title(title) - dialog.connect('response', lambda dialog, response: dialog.destroy()) - dialog.show() - - @staticmethod - def Loading(title, text): - """Show loading popup""" - dialog = gtk.MessageDialog( - parent = None, - flags = gtk.DIALOG_DESTROY_WITH_PARENT, - type = gtk.MESSAGE_INFO, - buttons = gtk.BUTTONS_NONE, - message_format = text) - dialog.set_title(title) - dialog.connect('response', lambda dialog, response: dialog.destroy()) - dialog.show() - return dialog - - @staticmethod - def Warning(title, text): - """Show warning popup""" - dialog = gtk.MessageDialog( - parent = None, - flags = gtk.DIALOG_DESTROY_WITH_PARENT, - type = gtk.MESSAGE_WARNING, - buttons = gtk.BUTTONS_OK, - message_format = text) - dialog.set_title(title) - dialog.connect('response', lambda dialog, response: dialog.destroy()) - dialog.show() - return dialog - - @staticmethod - def Question(title, text): - """Show question popup""" - dialog = gtk.MessageDialog( - parent = None, - flags = gtk.DIALOG_DESTROY_WITH_PARENT, - type = gtk.MESSAGE_QUESTION, - buttons = gtk.BUTTONS_YES_NO, - message_format = text) - dialog.set_title(title) - dialog.connect('response', lambda dialog, response: dialog.destroy()) - dialog.show() - return dialog - -class GuiStarter(object): - """ - Init the starter GUI box. - """ - def __init__(self): - """ - Start the GUI up and set the connections with the components. - """ - builder = GuiUtils.GetBuilder('builder') - - # get objects - self.window = builder.get_object('builder') - - # defaults - - # signals diff --git a/options.py b/options.py index 0f81209..8600184 100644 --- a/options.py +++ b/options.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python # -*- coding: iso-8859-15 -*- """ BC (Border-Check) is a tool to retrieve info of traceroute tests over website navigation routes. diff --git a/web/__init__.py b/web/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/WM1.svg b/web/images/WM1.svg similarity index 100% rename from WM1.svg rename to web/images/WM1.svg diff --git a/web/index.py b/web/index.py new file mode 100644 index 0000000..97c1121 --- /dev/null +++ b/web/index.py @@ -0,0 +1,12 @@ +output = """ + + + Border Check + + +
+
+
+ + +""" diff --git a/webserver.py b/webserver.py new file mode 100644 index 0000000..301a29d --- /dev/null +++ b/webserver.py @@ -0,0 +1,137 @@ +#!/usr/bin/python +# -*- coding: iso-8859-15 -*- +""" +BC (Border-Check) is a tool to retrieve info of traceroute tests over website navigation routes. +GPLv3 - 2013 by psy (epsylon@riseup.net) +""" +import os +import sys +from SocketServer import ForkingMixIn, ThreadingMixIn +from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +from runpy import run_module +from urlparse import urlparse +from cgi import parse_qs #, parse_header, parse_multipart +import cgi + +port = 8080 +wwwroot = "web/" +http = {} # global storage + +class ForkingTCPServer(ForkingMixIn, HTTPServer): pass +class ThreadingTCPServer(ThreadingMixIn, HTTPServer): pass + +def print_exception(type=None, value=None, tb=None, limit=None): + if type is None: + type, value, tb = sys.exc_info() + import traceback + ret = "

Traceback (most recent call last):

" + ret += "
"
+	list = traceback.format_tb(tb, limit) + \
+		traceback.format_exception_only(type, value)
+	ret += "exception error"
+	ret += "%s: %s
\n" % ( ("\n".join(list[:-1])), (list[-1])) + ret +="" + del tb + return ret + +class HttpHandler(BaseHTTPRequestHandler): + + # TODO: whitelist out there + def client_not_allowed(self, addr): + return False + if addr == "127.0.0.1": + return False + print ("Client not allowed ",addr) + return True + + def serve(self): + output = "" + uri = self.path + tmp = uri.find ('?') + args = parse_qs(urlparse(uri)[4]) + + #from ipdb import set_trace;set_trace() + if tmp != -1: + uri = uri[0:tmp] + for a in uri[tmp:-1].split("&"): + sep = a.find ("=") + if sep != -1: + print "%s)(%s"%(a[0:sep],a[sep:-1]) + args[a[0:sep]]=a[sep:-1] + + file = wwwroot + "/" + uri + if self.client_not_allowed (self.client_address[0]): + self.wfile.write ("HTTP/1.0 503 Not allowed\r\n\r\nYou are not whitelisted") + return + content = "" + try: + ctype,pdict = cgi.parse_header(self.headers.getheader('content-type')) + print "CTYPE IS ",ctype + if ctype == 'multipart/form-data': + query = cgi.parse_multipart(self.rfile, pdict) + content = query.get('upfile') + except: + pass + print "Request from %s:%d"%self.client_address+" "+uri + if uri[-1] == '/' or os.path.isdir(file): + file = file + "/index.py" + if os.path.isfile(file+".py"): + file = file + ".py" + if file.find("py") != -1: + modname = file.replace(".py", "") + cwd = modname[0:modname.rfind('/')]+"/" + modname = modname.replace("/", ".") + while modname.find("..") != -1: + modname = modname.replace("..",".") + globals = { + "output": output, + "http": http, + "uri": uri, + "args": args, + "cwd": cwd, + "headers": self.headers, + "content": content + } + try: + a = run_module(modname, init_globals=globals) + output = a["output"] + except: + output = print_exception() + else: + try: + f = open (file, "r") + output = f.read () + f.close () + except: + output = "404" + if output == "404": + self.wfile.write ("HTTP/1.0 404 Not found\r\n\r\n") + else: + self.wfile.write ("HTTP/1.0 200 OK\r\n\r\n") + self.wfile.write (output) + + def do_POST (self): + self.serve () + + def do_GET (self): + self.serve () + +class BorderCheckWebserver(): + def __init__(self, ref, *args): + HttpHandler.ref = ref + httpd = HTTPServer(('', port), HttpHandler) + print "http://127.0.0.1:%d/ : Serving directory '%s/www'" % (port, os.getcwd()) + try: + httpd.serve_forever() + except KeyboardInterrupt: + print 'Server killed on user request (keyboard interrupt).' + +if __name__=="__main__": + wwwroot = "www" + httpd = HTTPServer(('', port), HttpHandler) + print "http://127.0.0.1:%d/ : Serving directory '%s/www'" % (port, os.getcwd()) + + try: + httpd.serve_forever() + except KeyboardInterrupt: + print 'Server killed on user request (keyboard interrupt).'