Advertisement
imKobz

OvpnSocks

Nov 10th, 2020 (edited)
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.89 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # encoding: utf-8
  3.  
  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 = 4096 * 4
  15. TIMEOUT = 60
  16. MSG = 'KobZ'
  17. DEFAULT_HOST = '0.0.0.0:1194'
  18. RESPONSE = "HTTP/1.1 200 " + str(MSG) + "\r\n\r\n"
  19.  
  20. class Server(threading.Thread):
  21.     def __init__(self, host, port):
  22.         threading.Thread.__init__(self)
  23.         self.running = False
  24.         self.host = host
  25.         self.port = port
  26.         self.threads = []
  27.     self.threadsLock = threading.Lock()
  28.     self.logLock = threading.Lock()
  29.  
  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.  
  38.         try:                    
  39.             while self.running:
  40.                 try:
  41.                     c, addr = self.soc.accept()
  42.                     c.setblocking(1)
  43.                 except socket.timeout:
  44.                     continue
  45.                
  46.                 conn = ConnectionHandler(c, self, addr)
  47.                 conn.start();
  48.                 self.addConn(conn)
  49.         finally:
  50.             self.running = False
  51.             self.soc.close()
  52.            
  53.     def printLog(self, log):
  54.         self.logLock.acquire()
  55.         print log
  56.         self.logLock.release()
  57.    
  58.     def addConn(self, conn):
  59.         try:
  60.             self.threadsLock.acquire()
  61.             if self.running:
  62.                 self.threads.append(conn)
  63.         finally:
  64.             self.threadsLock.release()
  65.                    
  66.     def removeConn(self, conn):
  67.         try:
  68.             self.threadsLock.acquire()
  69.             self.threads.remove(conn)
  70.         finally:
  71.             self.threadsLock.release()
  72.                
  73.     def close(self):
  74.         try:
  75.             self.running = False
  76.             self.threadsLock.acquire()
  77.            
  78.             threads = list(self.threads)
  79.             for c in threads:
  80.                 c.close()
  81.         finally:
  82.             self.threadsLock.release()
  83.            
  84.  
  85. class ConnectionHandler(threading.Thread):
  86.     def __init__(self, socClient, server, addr):
  87.         threading.Thread.__init__(self)
  88.         self.clientClosed = False
  89.         self.targetClosed = True
  90.         self.client = socClient
  91.         self.client_buffer = ''
  92.         self.server = server
  93.         self.log = 'Conexao: ' + str(addr)
  94.  
  95.     def close(self):
  96.         try:
  97.             if not self.clientClosed:
  98.                 self.client.shutdown(socket.SHUT_RDWR)
  99.                 self.client.close()
  100.         except:
  101.             pass
  102.         finally:
  103.             self.clientClosed = True
  104.            
  105.         try:
  106.             if not self.targetClosed:
  107.                 self.target.shutdown(socket.SHUT_RDWR)
  108.                 self.target.close()
  109.         except:
  110.             pass
  111.         finally:
  112.             self.targetClosed = True
  113.  
  114.     def run(self):
  115.         try:
  116.             self.client_buffer = self.client.recv(BUFLEN)
  117.        
  118.             hostPort = self.findHeader(self.client_buffer, 'X-Real-Host')
  119.            
  120.             if hostPort == '':
  121.                 hostPort = DEFAULT_HOST
  122.  
  123.             split = self.findHeader(self.client_buffer, 'X-Split')
  124.  
  125.             if split != '':
  126.                 self.client.recv(BUFLEN)
  127.            
  128.             if hostPort != '':
  129.                 passwd = self.findHeader(self.client_buffer, 'X-Pass')
  130.                
  131.                 if len(PASS) != 0 and passwd == PASS:
  132.                     self.method_CONNECT(hostPort)
  133.                 elif len(PASS) != 0 and passwd != PASS:
  134.                     self.client.send('HTTP/1.1 400 WrongPass!\r\n\r\n')
  135.                 if hostPort.startswith(IP):
  136.                     self.method_CONNECT(hostPort)
  137.                 else:
  138.                    self.client.send('HTTP/1.1 403 Forbidden!\r\n\r\n')
  139.             else:
  140.                 print '- No X-Real-Host!'
  141.                 self.client.send('HTTP/1.1 400 NoXRealHost!\r\n\r\n')
  142.  
  143.         except Exception as e:
  144.             self.log += ' - error: ' + e.strerror
  145.             self.server.printLog(self.log)
  146.         pass
  147.         finally:
  148.             self.close()
  149.             self.server.removeConn(self)
  150.  
  151.     def findHeader(self, head, header):
  152.         aux = head.find(header + ': ')
  153.    
  154.         if aux == -1:
  155.             return ''
  156.  
  157.         aux = head.find(':', aux)
  158.         head = head[aux+2:]
  159.         aux = head.find('\r\n')
  160.  
  161.         if aux == -1:
  162.             return ''
  163.  
  164.         return head[:aux];
  165.  
  166.     def connect_target(self, host):
  167.         i = host.find(':')
  168.         if i != -1:
  169.             port = int(host[i+1:])
  170.             host = host[:i]
  171.         else:
  172.             if self.method=='CONNECT':
  173.                 port = 1194
  174.             else:
  175.                 port = 22
  176.  
  177.         (soc_family, soc_type, proto, _, address) = socket.getaddrinfo(host, port)[0]
  178.  
  179.         self.target = socket.socket(soc_family, soc_type, proto)
  180.         self.targetClosed = False
  181.         self.target.connect(address)
  182.  
  183.     def method_CONNECT(self, path):
  184.         self.log += ' - CONNECT ' + path
  185.         self.connect_target(path)
  186.         self.client.sendall(RESPONSE)
  187.         self.client_buffer = ''
  188.         self.server.printLog(self.log)
  189.         self.doCONNECT()
  190.                    
  191.     def doCONNECT(self):
  192.         socs = [self.client, self.target]
  193.         count = 0
  194.         error = False
  195.         while True:
  196.             count += 1
  197.             (recv, _, err) = select.select(socs, [], socs, 3)
  198.             if err:
  199.                 error = True
  200.             if recv:
  201.                 for in_ in recv:
  202.             try:
  203.                         data = in_.recv(BUFLEN)
  204.                         if data:
  205.                 if in_ is self.target:
  206.                 self.client.send(data)
  207.                             else:
  208.                                 while data:
  209.                                     byte = self.target.send(data)
  210.                                     data = data[byte:]
  211.  
  212.                             count = 0
  213.             else:
  214.                 break
  215.             except:
  216.                         error = True
  217.                         break
  218.             if count == TIMEOUT:
  219.                 error = True
  220.  
  221.             if error:
  222.                 break
  223.  
  224.  
  225.  
  226. def main(host=IP, port=PORT):
  227.     print "\033[0;34m━"*8,"\033[1;32m PROXY SOCKS","\033[0;34m━"*8,"\n"
  228.     print "\033[1;33mIP:\033[1;32m " + IP
  229.     print "\033[1;33mPORTA:\033[1;32m " + str(PORT) + "\n"
  230.     print "\033[0;34m━"*10,"\033[1;32m KOBEKOBZ","\033[0;34m━\033[1;37m"*11,"\n"
  231.     server = Server(IP, PORT)
  232.     server.start()
  233.     while True:
  234.         try:
  235.             time.sleep(2)
  236.         except KeyboardInterrupt:
  237.             print '\nParando...'
  238.             server.close()
  239.             break
  240. if __name__ == '__main__':
  241.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement