proxy.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import socket, threading, thread, select, signal, sys, time, getopt
  2. # CONFIG
  3. LISTENING_ADDR = '0.0.0.0'
  4. LISTENING_PORT = 80
  5. PASS = ''
  6. # CONST
  7. BUFLEN = 4096 * 4
  8. TIMEOUT = 60
  9. DEFAULT_HOST = "127.0.0.1:22"
  10. RESPONSE = 'HTTP/1.1 200 Switching Protocols \r\n\r\n'
  11. class Server(threading.Thread):
  12. def __init__(self, host, port):
  13. threading.Thread.__init__(self)
  14. self.running = False
  15. self.host = host
  16. self.port = port
  17. self.threads = []
  18. self.threadsLock = threading.Lock()
  19. self.logLock = threading.Lock()
  20. def run(self):
  21. self.soc = socket.socket(socket.AF_INET)
  22. self.soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  23. self.soc.settimeout(2)
  24. self.soc.bind((self.host, self.port))
  25. self.soc.listen(0)
  26. self.running = True
  27. try:
  28. while self.running:
  29. try:
  30. c, addr = self.soc.accept()
  31. c.setblocking(1)
  32. except socket.timeout:
  33. continue
  34. conn = ConnectionHandler(c, self, addr)
  35. conn.start();
  36. self.addConn(conn)
  37. finally:
  38. self.running = False
  39. self.soc.close()
  40. def printLog(self, log):
  41. self.logLock.acquire()
  42. print log
  43. self.logLock.release()
  44. def addConn(self, conn):
  45. try:
  46. self.threadsLock.acquire()
  47. if self.running:
  48. self.threads.append(conn)
  49. finally:
  50. self.threadsLock.release()
  51. def removeConn(self, conn):
  52. try:
  53. self.threadsLock.acquire()
  54. self.threads.remove(conn)
  55. finally:
  56. self.threadsLock.release()
  57. def close(self):
  58. try:
  59. self.running = False
  60. self.threadsLock.acquire()
  61. threads = list(self.threads)
  62. for c in threads:
  63. c.close()
  64. finally:
  65. self.threadsLock.release()
  66. class ConnectionHandler(threading.Thread):
  67. def __init__(self, socClient, server, addr):
  68. threading.Thread.__init__(self)
  69. self.clientClosed = False
  70. self.targetClosed = True
  71. self.client = socClient
  72. self.client_buffer = ''
  73. self.server = server
  74. self.log = 'Connection: ' + str(addr)
  75. def close(self):
  76. try:
  77. if not self.clientClosed:
  78. self.client.shutdown(socket.SHUT_RDWR)
  79. self.client.close()
  80. except:
  81. pass
  82. finally:
  83. self.clientClosed = True
  84. try:
  85. if not self.targetClosed:
  86. self.target.shutdown(socket.SHUT_RDWR)
  87. self.target.close()
  88. except:
  89. pass
  90. finally:
  91. self.targetClosed = True
  92. def run(self):
  93. try:
  94. self.client_buffer = self.client.recv(BUFLEN)
  95. hostPort = self.findHeader(self.client_buffer, 'X-Real-Host')
  96. if hostPort == '':
  97. hostPort = DEFAULT_HOST
  98. split = self.findHeader(self.client_buffer, 'X-Split')
  99. if split != '':
  100. self.client.recv(BUFLEN)
  101. if hostPort != '':
  102. passwd = self.findHeader(self.client_buffer, 'X-Pass')
  103. if len(PASS) != 0 and passwd == PASS:
  104. self.method_CONNECT(hostPort)
  105. elif len(PASS) != 0 and passwd != PASS:
  106. self.client.send('HTTP/1.1 400 WrongPass!\r\n\r\n')
  107. elif hostPort.startswith('127.0.0.1') or hostPort.startswith('localhost'):
  108. self.method_CONNECT(hostPort)
  109. else:
  110. self.client.send('HTTP/1.1 403 Forbidden!\r\n\r\n')
  111. else:
  112. print '- No X-Real-Host!'
  113. self.client.send('HTTP/1.1 400 NoXRealHost!\r\n\r\n')
  114. except Exception as e:
  115. self.log += ' - error: ' + e.strerror
  116. self.server.printLog(self.log)
  117. pass
  118. finally:
  119. self.close()
  120. self.server.removeConn(self)
  121. def findHeader(self, head, header):
  122. aux = head.find(header + ': ')
  123. if aux == -1:
  124. return ''
  125. aux = head.find(':', aux)
  126. head = head[aux+2:]
  127. aux = head.find('\r\n')
  128. if aux == -1:
  129. return ''
  130. return head[:aux];
  131. def connect_target(self, host):
  132. i = host.find(':')
  133. if i != -1:
  134. port = int(host[i+1:])
  135. host = host[:i]
  136. else:
  137. if self.method=='CONNECT':
  138. port = 443
  139. else:
  140. port = 80
  141. (soc_family, soc_type, proto, _, address) = socket.getaddrinfo(host, port)[0]
  142. self.target = socket.socket(soc_family, soc_type, proto)
  143. self.targetClosed = False
  144. self.target.connect(address)
  145. def method_CONNECT(self, path):
  146. self.log += ' - CONNECT ' + path
  147. self.connect_target(path)
  148. self.client.sendall(RESPONSE)
  149. self.client_buffer = ''
  150. self.server.printLog(self.log)
  151. self.doCONNECT()
  152. def doCONNECT(self):
  153. socs = [self.client, self.target]
  154. count = 0
  155. error = False
  156. while True:
  157. count += 1
  158. (recv, _, err) = select.select(socs, [], socs, 3)
  159. if err:
  160. error = True
  161. if recv:
  162. for in_ in recv:
  163. try:
  164. data = in_.recv(BUFLEN)
  165. if data:
  166. if in_ is self.target:
  167. self.client.send(data)
  168. else:
  169. while data:
  170. byte = self.target.send(data)
  171. data = data[byte:]
  172. count = 0
  173. else:
  174. break
  175. except:
  176. error = True
  177. break
  178. if count == TIMEOUT:
  179. error = True
  180. if error:
  181. break
  182. def print_usage():
  183. print 'Usage: proxy.py -p <port>'
  184. print ' proxy.py -b <bindAddr> -p <port>'
  185. print ' proxy.py -b 0.0.0.0 -p 1080'
  186. def parse_args(argv):
  187. global LISTENING_ADDR
  188. global LISTENING_PORT
  189. try:
  190. opts, args = getopt.getopt(argv,"hb:p:",["bind=","port="])
  191. except getopt.GetoptError:
  192. print_usage()
  193. sys.exit(2)
  194. for opt, arg in opts:
  195. if opt == '-h':
  196. print_usage()
  197. sys.exit()
  198. elif opt in ("-b", "--bind"):
  199. LISTENING_ADDR = arg
  200. elif opt in ("-p", "--port"):
  201. LISTENING_PORT = int(arg)
  202. def main(host=LISTENING_ADDR, port=LISTENING_PORT):
  203. print "\n ==============================\n"
  204. print "\n PYTHON PROXY \n"
  205. print "\n ==============================\n"
  206. print "corriendo ip: " + LISTENING_ADDR
  207. print "corriendo port: " + str(LISTENING_PORT) + "\n"
  208. print "Se ha Iniciado Por Favor Cierre el Terminal\n"
  209. server = Server(LISTENING_ADDR, LISTENING_PORT)
  210. server.start()
  211. while True:
  212. try:
  213. time.sleep(2)
  214. except KeyboardInterrupt:
  215. print 'Stopping...'
  216. server.close()
  217. break
  218. if __name__ == '__main__':
  219. parse_args(sys.argv[1:])
  220. main()