]> khamidou — GitHub mirrors - tunnel.git/commitdiff
first commit
authorkhamidou <khamidou@gmail.com>
Tue, 9 Mar 2010 20:26:38 +0000 (21:26 +0100)
committerkhamidou <khamidou@gmail.com>
Tue, 9 Mar 2010 20:26:38 +0000 (21:26 +0100)
COPYING [deleted file]
README [changed mode: 0755->0644]
tunnel [new file with mode: 0755]
tunnel.py [deleted file]

diff --git a/COPYING b/COPYING
deleted file mode 100755 (executable)
index 09f3b46..0000000
--- a/COPYING
+++ /dev/null
@@ -1,23 +0,0 @@
-Copyright (c) 2009 Karim Hamidou
-Copyright (c) 2001 Hisao SUZUKI
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without 
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or
-sell copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  
-NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README b/README
old mode 100755 (executable)
new mode 100644 (file)
index 2aa284a..0a876fd
--- a/README
+++ b/README
@@ -1 +1,3 @@
-tunnel is a personal proxy - that is, a tool that will filter costly distractions of our modern internet world.
\ No newline at end of file
+This is tunnel.
+
+To run it, just type "./tunnel"
\ No newline at end of file
diff --git a/tunnel b/tunnel
new file mode 100755 (executable)
index 0000000..e0a16b3
--- /dev/null
+++ b/tunnel
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+
+import BaseHTTPServer
+import urllib2
+import re
+
+base_url = r"http://0.0.0.0:8000/"
+
+
+def rewrite_link(link):
+    pass
+
+class TunnelHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+    def do_GET(self):
+        if self.path[0] == '/':
+            self.path = self.path[1:]
+
+        try:
+            fd = urllib2.urlopen(self.path)
+        except urllib2.URLError:
+            self.wfile.write("400 : The URL " + self.path + " isn't reachable")
+            return
+
+        if fd.info().type == "text/html":
+            content = fd.read()
+            def reg_repl(m):
+                return base_url + "\0"
+
+            # A replacement function for a relative path (like /~karim)
+            def relative_repl(m):
+                if m.group(1).find('http') != -1:
+                    print "guh"
+                    print "rewritten to : " + base_url + m.group(1)
+                    return base_url + m.group(1)
+
+                print "rewritten to : " "http://" + self.path + "/" +  m.group(1)
+                return base_url + self.path + "/" + m.group(1)
+
+            # Relative urls
+            re.sub("href=\"([a-zA-Z0-9\/\\\:\?\%\.\&\;=#\-\_\!\+\~]*)\"", relative_repl, content)
+
+            # Regular urls
+            # re.sub("href=\"http:\/\/([a-zA-Z0-9\/\\\:\?\%\.\&\;=#\-\_\!\+\~]*)\"", reg_repl, content)
+
+            self.wfile.write(content)
+        else:
+            self.wfile.write(fd.read())
+
+if __name__ == "__main__":
+    server_address = ('', 8000)
+    httpd = BaseHTTPServer.HTTPServer(server_address, TunnelHandler)
+    httpd.serve_forever()
+
diff --git a/tunnel.py b/tunnel.py
deleted file mode 100755 (executable)
index fbf81ab..0000000
--- a/tunnel.py
+++ /dev/null
@@ -1,65 +0,0 @@
-#!/bin/sh -
-"exec" "python" "-O" "$0" "$@"
-
-__doc__ = """tunnel.
-
-This module implements GET, HEAD, POST, PUT and DELETE methods
-on BaseHTTPServer, and behaves as an HTTP proxy.  The CONNECT
-method is also implemented experimentally, but has not been
-tested yet.
-
-Any help will be greatly appreciated.          SUZUKI Hisao
-"""
-
-__version__ = "0.0.1"
-
-import BaseHTTPServer, select, socket, SocketServer, urlparse, urllib2
-
-class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler):
-    __base = BaseHTTPServer.BaseHTTPRequestHandler
-    __base_handle = __base.handle
-
-    server_version = "tunnel/" + __version__
-    rbufsize = 0                        # self.rfile Be unbuffered
-
-    def do_GET(self):
-            headers = {"Proxy-Connection" : "",
-                       "Proxy-Connection": "keep-alive",
-           }
-
-            req = urllib2.Request(self.path, headers=repr(self.headers)) 
-
-            try:
-               data = urllib2.urlopen(req)
-                self.send_response(200)
-                self.end_headers()
-                self.wfile.write(data.read())
-
-            except IOError:
-                self.send_response(500)
-                self.end_headers()
-            
-    do_HEAD = do_GET
-    do_POST = do_GET
-    do_PUT  = do_GET
-    do_DELETE=do_GET
-
-class ThreadingHTTPServer (SocketServer.ThreadingMixIn,
-                           BaseHTTPServer.HTTPServer): pass
-
-if __name__ == '__main__':
-    from sys import argv
-    if argv[1:] and argv[1] in ('-h', '--help'):
-        print argv[0], "[port [allowed_client_name ...]]"
-    else:
-        if argv[2:]:
-            allowed = []
-            for name in argv[2:]:
-                client = socket.gethostbyname(name)
-                allowed.append(client)
-                print "Accept: %s (%s)" % (client, name)
-            ProxyHandler.allowed_clients = allowed
-            del argv[2:]
-        else:
-            print "Any clients will be served..."
-        BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)