proxy.py 6.7 KB

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