Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from subprocess import Popen, PIPE
- import SimpleHTTPServer
- import SocketServer
- import socket
- import urllib2
- import httplib
- import socks
- def get_ip():
- return Popen('px -g -b -o -ch', shell=True, stdout=PIPE).stdout.read().strip().replace('/', '').split(':')
- class SocksiPyConnection(httplib.HTTPConnection):
- def __init__(self, proxytype, proxyaddr, proxyport=None, rdns=True, username=None, password=None, *args, **kwargs):
- self.proxyargs = (proxytype, proxyaddr, proxyport, rdns, username, password)
- httplib.HTTPConnection.__init__(self, *args, **kwargs)
- def connect(self):
- self.sock = socks.socksocket()
- self.sock.setproxy(*self.proxyargs)
- if isinstance(self.timeout, float):
- self.sock.settimeout(self.timeout)
- self.sock.connect((self.host, self.port))
- class SocksiPyHandler(urllib2.HTTPHandler):
- def __init__(self, *args, **kwargs):
- self.args = args
- self.kw = kwargs
- urllib2.HTTPHandler.__init__(self)
- def http_open(self, req):
- def build(host, port=None, strict=None, timeout=0):
- conn = SocksiPyConnection(*self.args, host=host, port=port, strict=strict, timeout=timeout, **self.kw)
- return conn
- return self.do_open(build, req)
- class HTTProxyServer(SimpleHTTPServer.SimpleHTTPRequestHandler):
- proto_map = {'http':socks.HTTP, 'https':socks.HTTP, 'socks5':socks.SOCKS5, 'socks4':socks.SOCKS4}
- def do_GET(self):
- while 1:
- proto, ip, port = get_ip()
- print self.proto_map[proto], ip, port
- opener = urllib2.build_opener(SocksiPyHandler(self.proto_map[proto], ip, int(port)))
- try:
- self.copyfile(opener.open(self.path), self.wfile)
- print('[{}] -> [{}] -> [{}]'.format(self.client_address[0], ip, self.path))
- break
- except (socks.ProxyError, socket.error) as e:
- print('Broken connection! {}'.format(ip))
- # break
- if __name__ == '__main__':
- Handler = HTTProxyServer
- httpd = SocketServer.TCPServer(("", 9090), Handler)
- try:
- httpd.serve_forever()
- except KeyboardInterrupt:
- httpd.server_close()
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement