Advertisement
Guest User

Untitled

a guest
Jan 10th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. from subprocess import Popen, PIPE
  2. import SimpleHTTPServer
  3. import SocketServer
  4. import socket
  5. import urllib2
  6. import httplib
  7. import socks
  8.  
  9. def get_ip():
  10. return Popen('px -g -b -o -ch', shell=True, stdout=PIPE).stdout.read().strip().replace('/', '').split(':')
  11.  
  12.  
  13. class SocksiPyConnection(httplib.HTTPConnection):
  14. def __init__(self, proxytype, proxyaddr, proxyport=None, rdns=True, username=None, password=None, *args, **kwargs):
  15. self.proxyargs = (proxytype, proxyaddr, proxyport, rdns, username, password)
  16. httplib.HTTPConnection.__init__(self, *args, **kwargs)
  17.  
  18. def connect(self):
  19. self.sock = socks.socksocket()
  20. self.sock.setproxy(*self.proxyargs)
  21. if isinstance(self.timeout, float):
  22. self.sock.settimeout(self.timeout)
  23. self.sock.connect((self.host, self.port))
  24.  
  25.  
  26. class SocksiPyHandler(urllib2.HTTPHandler):
  27. def __init__(self, *args, **kwargs):
  28. self.args = args
  29. self.kw = kwargs
  30. urllib2.HTTPHandler.__init__(self)
  31.  
  32. def http_open(self, req):
  33. def build(host, port=None, strict=None, timeout=0):
  34. conn = SocksiPyConnection(*self.args, host=host, port=port, strict=strict, timeout=timeout, **self.kw)
  35. return conn
  36.  
  37. return self.do_open(build, req)
  38.  
  39.  
  40. class HTTProxyServer(SimpleHTTPServer.SimpleHTTPRequestHandler):
  41. proto_map = {'http':socks.HTTP, 'https':socks.HTTP, 'socks5':socks.SOCKS5, 'socks4':socks.SOCKS4}
  42.  
  43. def do_GET(self):
  44. while 1:
  45. proto, ip, port = get_ip()
  46. print self.proto_map[proto], ip, port
  47. opener = urllib2.build_opener(SocksiPyHandler(self.proto_map[proto], ip, int(port)))
  48. try:
  49. self.copyfile(opener.open(self.path), self.wfile)
  50. print('[{}] -> [{}] -> [{}]'.format(self.client_address[0], ip, self.path))
  51. break
  52. except (socks.ProxyError, socket.error) as e:
  53. print('Broken connection! {}'.format(ip))
  54. # break
  55.  
  56.  
  57. if __name__ == '__main__':
  58. Handler = HTTProxyServer
  59. httpd = SocketServer.TCPServer(("", 9090), Handler)
  60. try:
  61. httpd.serve_forever()
  62. except KeyboardInterrupt:
  63. httpd.server_close()
  64. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement