11 LISTEN_ADDR
= '127.0.0.1'
14 TOR_ADDR
= '127.0.0.1'
19 class ProxyHandler (BaseHTTPServer
.BaseHTTPRequestHandler
):
20 rbufsize
= 0 # self.rfile Be unbuffered
22 def _connect_to(self
, netloc
, soc
):
25 host_port
= netloc
[:i
], int(netloc
[i
+1:])
27 host_port
= netloc
, 80
28 #print "connect to %s:%d" % host_port
30 soc
.connect(host_port
)
37 soc
= socks
.socksocket()
38 soc
.setproxy(socks
.PROXY_TYPE_SOCKS5
, TOR_ADDR
, TOR_PORT
)
40 if self
._connect
_to
(self
.path
, soc
):
41 #self.log_request(200)
42 self
.wfile
.write(self
.protocol_version
+
43 " 200 Connection established\r\n")
44 self
.wfile
.write("\r\n")
45 self
._read
_write
(soc
, 300)
49 self
.connection
.close()
52 (scm
, netloc
, path
, params
, query
, fragment
) = urlparse
.urlparse(
54 if scm
!= 'http' or fragment
or not netloc
:
55 self
.send_error(400, "bad url %s" % self
.path
)
57 soc
= socks
.socksocket()
58 soc
.setproxy(socks
.PROXY_TYPE_SOCKS5
, TOR_ADDR
, TOR_PORT
)
60 if self
._connect
_to
(netloc
, soc
):
62 soc
.send("%s %s %s\r\n" % (
64 urlparse
.urlunparse(('', '', path
, params
, query
, '')),
65 self
.request_version
))
66 self
.headers
['Connection'] = 'close'
67 del self
.headers
['Proxy-Connection']
68 for header
in self
.headers
.headers
:
69 soc
.send(header
.strip() + '\r\n')
75 self
.connection
.close()
77 def _read_write(self
, soc
, max_idling
=TIMEOUT
):
78 iw
= [self
.connection
, soc
]
82 (ins
, _
, exs
) = select
.select(iw
, [], iw
, 3)
97 if count
== max_idling
:
98 #self.send_error(504, "Connection timed out")
106 class WankProxy(SocketServer
.ThreadingMixIn
, BaseHTTPServer
.HTTPServer
):
108 BaseHTTPServer
.HTTPServer
.__init
__(self
,
109 (LISTEN_ADDR
, LISTEN_PORT
),
112 if __name__
== '__main__':
114 print "Listening on %s:%s..." % (LISTEN_ADDR
, LISTEN_PORT
)
116 httpd
.serve_forever()
117 except KeyboardInterrupt:
118 print "Exiting on ^C..."