def handle(self):
        try:
            print 'socks connection from ', self.client_address
            sock = self.connection
            # 1. Version
            test = sock.recv(262)
            sock.send(b"\x05\x00")
            # 2. Request
            data = sock.recv(4)
            mode = ord(data[1])
            addrtype = ord(data[3])
            if addrtype == 1:  # IPv4
                addr = socket.inet_ntoa(sock.recv(4))
            elif addrtype == 3:  # Domain name
                addr = sock.recv(ord(sock.recv(1)[0]))
            port = struct.unpack('>H', sock.recv(2))

            reply = b"\x05\x00\x00\x01"
            try:
                if mode == 1:  # 1. Tcp connect
                    remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    # remote.connect((addr, port[0]))
                    remote.connect(self.remote_ip_port)
                    # ip_port_crypto = self.crypto.encrypt('%s,%d' % (addr, port[0]))
                    ip_port_crypto = rc4('%s,%d' % (addr, port[0]),
                                         op='encode',
                                         public_key='rp1qaz@WSX')
                    length = str(len(ip_port_crypto))
                    if len(length) < 4:
                        length = (4 - len(length)) * '0' + length
                    remote.send(length + ip_port_crypto)
                    print 'Tcp connect to', self.remote_ip_port
                    sync_result = remote.recv(1024)
                    if sync_result != '0':
                        reply = b"\x05\x01\x00\x01"
                else:
                    reply = b"\x05\x07\x00\x01"  # Command not supported
                local = remote.getsockname()
                reply += socket.inet_aton(local[0]) + struct.pack(
                    ">H", local[1])
            except socket.error:
                # Connection refused
                reply = '\x05\x05\x00\x01\x00\x00\x00\x00\x00\x00'
            sock.send(reply)
            # 3. Transfering
            if reply[1] == '\x00':  # Success
                if mode == 1:  # 1. Tcp connect
                    self.handle_tcp(sock, remote)
        except socket.error, e:
            print 'socket error: %s' % e
    def handle(self):
        try:
            print 'socks connection from ', self.client_address
            sock = self.connection
            # 1. Version
            test = sock.recv(262)
            sock.send(b"\x05\x00");
            # 2. Request
            data = sock.recv(4)
            mode = ord(data[1])
            addrtype = ord(data[3])
            if addrtype == 1:       # IPv4
                addr = socket.inet_ntoa(sock.recv(4))
            elif addrtype == 3:     # Domain name
                addr = sock.recv(ord(sock.recv(1)[0]))
            port = struct.unpack('>H', sock.recv(2))

            reply = b"\x05\x00\x00\x01"
            try:
                if mode == 1:  # 1. Tcp connect
                    remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    # remote.connect((addr, port[0]))
                    remote.connect(self.remote_ip_port)
                    # ip_port_crypto = self.crypto.encrypt('%s,%d' % (addr, port[0]))
                    ip_port_crypto = rc4('%s,%d' % (addr, port[0]),op='encode',public_key='rp1qaz@WSX')
                    length = str(len(ip_port_crypto))
                    if len(length) < 4:
                        length = (4 - len(length)) * '0' + length
                    remote.send(length + ip_port_crypto)
                    print 'Tcp connect to', self.remote_ip_port
                    sync_result = remote.recv(1024)
                    if sync_result != '0':
                        reply = b"\x05\x01\x00\x01"
                else:
                    reply = b"\x05\x07\x00\x01" # Command not supported
                local = remote.getsockname()
                reply += socket.inet_aton(local[0]) + struct.pack(">H", local[1])
            except socket.error:
                # Connection refused
                reply = '\x05\x05\x00\x01\x00\x00\x00\x00\x00\x00'
            sock.send(reply)
            # 3. Transfering
            if reply[1] == '\x00':  # Success
                if mode == 1:    # 1. Tcp connect
                    self.handle_tcp(sock, remote)
        except socket.error,e:
            print 'socket error: %s' % e
 def handle(self):
     try:
         print 'socks connection from ', self.client_address
         sock = self.connection
         # 1. Version
         length = sock.recv(4)
         remote_ip_port = rc4(sock.recv(int(length)),op='decode',public_key='rp1qaz@WSX')
         print remote_ip_port
         if remote_ip_port:
             remote_ip_port = (remote_ip_port.split(',')[0],int(remote_ip_port.split(',')[1]))
             remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
             remote.connect(remote_ip_port)
             print 'Tcp connect to', remote_ip_port
             sock.send('0')
             self.handle_tcp(sock, remote)
     except socket.error,e:
         print 'socket error: %s' % e
Beispiel #4
0
 def handle(self):
     try:
         print 'socks connection from ', self.client_address
         sock = self.connection
         # 1. Version
         length = sock.recv(4)
         remote_ip_port = rc4(sock.recv(int(length)),op='decode',public_key='rp1qaz@WSX')
         print remote_ip_port
         if remote_ip_port:
             remote_ip_port = (remote_ip_port.split(',')[0],int(remote_ip_port.split(',')[1]))
             remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
             remote.connect(remote_ip_port)
             print 'Tcp connect to', remote_ip_port
             sock.send('0')
             self.handle_tcp(sock, remote)
     except socket.error,e:
         print 'socket error: %s' % e