Example #1
0
def proxy_loop(socket_src, socket_dst):
    """ Wait for network activity """
    buff_status = 1
    buff = None
    needlen = 0

    while not EXIT.get_status():
        try:
            print("[ ] select ...")
            reader, _, _ = select.select([socket_src, socket_dst], [], [], 1)
        except select.error as err:
            error("Select failed", err)
            return
        if not reader:
            #print("[ ] socket events is not read evnets")
            continue
        try:
            for sock in reader:
                if sock is socket_src:
                    print("[ ] Debug: sock is src")
                    buffer = Buffer(sock.recv(BUFSIZE))
                    while not buffer.is_empty(): 
                        #### 从 client 来的数据,要解密发给 Internet
                        print("[ ] info: process buffer")
                        data = None
                        if buff_status == 1:
                            print("[ ] 1")
                            data = buffer.pop(4)
                            print("[ ] 1 after")
                            if data:
                                print("[ ] 1 : datalen=" + str(len(data)))
                                buff = data[0:]
                                if len(buff) == 4:
                                    needlen = (struct.unpack("i", buff))[0]
                                    print("[ ] 1 : get data len")
                                    buff = None
                                    buff_status = 3                                 
                                elif len(buff) < 4:
                                    buff_status = 2
                            else:
                                break
                        elif buff_status == 2:
                            print("[ ] 2")
                            data = buffer.pop(4-len(buff))
                            if data:
                                print("[ ] 2 : datalen=" + str(len(data)))
                                buff += data[0:]
                                if len(buff) == 4:
                                    needlen = (struct.unpack("i", buff))[0]
                                    print("[ ] 2 : get data len")
                                    buff = None
                                    buff_status = 3                                 
                            else:
                                break   
                        elif buff_status == 3:
                            print("[ ] 3")
                            data = buffer.pop(needlen)
                            if data:
                                print("[ ] 3 : datalen=" + str(len(data)))
                                buff = data[0:]
                                if len(buff) == needlen:
                                    buff_status = 4
                                    socket_dst.send(encrypter.decrypt(buff))
                                    print("[ ] 3 : get data")
                                    buff = None
                                    needlen = 0
                                    buff_status = 1
                                elif len(buff) < needlen:
                                    buff_status = 4
                            else:
                                break
                        elif buff_status == 4:
                            print("[ ] 4")
                            data = buffer.pop(needlen-len(buff))
                            if data:
                                print("[ ] 4 : datalen=" + str(len(data)))
                                buff += data[0:]
                                if len(buff) == needlen:
                                    socket_dst.send(encrypter.decrypt(buff))
                                    print("[ ] 4 : get data")
                                    buff = None
                                    needlen = 0
                                    buff_status = 1                              
                            else:
                                break
                        else:
                            raise Exception("buff_status error")
                else:
                    #### 从 Internet 来的数据,要加密发给 client
                    print("[ ] Debug: sock is dst")
                    buff = sock.recv(BUFSIZE)
                    if buff:
                        socket_src.send(encrypter.encrypt(buff))
                    buff = None
        except socket.error as err:
            error("Loop failed", err)
            return
Example #2
0
def proxy_loop_of_client(socket_src, socket_dst):
    """ Wait for network activity """

    # 1: 未读数据
    # 2: 等待data_len读取读取完成
    # 3: 读取数据
    # 4: 等待数据读取完成
    buff_status = 1
    buff = None
    needlen = 0

    while not EXIT.get_status():
        try:
            reader, _, _ = select.select([socket_src, socket_dst], [], [], 1)
        except select.error as err:
            error("Select failed", err)
            return
        if not reader:
            continue
        try:
            for sock in reader:
                if sock is socket_dst:
                    buffer = Buffer(sock.recv(BUFSIZE))
                    while not buffer.is_empty():
                        #### 来源于 Sserver 的密文,接收完整后,解密发给 client
                        data = None
                        if buff_status == 1:
                            data = buffer.pop(4)
                            if data:
                                buff = data[0:]
                                if len(buff) == 4:
                                    needlen = (struct.unpack("i", buff))[0]
                                    buff = None
                                    buff_status = 3
                                elif len(buff) < 4:
                                    buff_status = 2
                            else:
                                break
                        elif buff_status == 2:
                            data = buffer.pop(4 - len(buff))
                            if data:
                                buff += data[0:]
                                if len(buff) == 4:
                                    needlen = (struct.unpack("i", buff))[0]
                                    buff = None
                                    buff_status = 3
                            else:
                                break
                        elif buff_status == 3:
                            data = buffer.pop(needlen)
                            if data:
                                buff = data[0:]
                                if len(buff) == needlen:
                                    buff_status = 4
                                    socket_src.send(encrypter.decrypt(buff))
                                    buff = None
                                    needlen = 0
                                    buff_status = 1
                                elif len(buff) < needlen:
                                    buff_status = 4
                            else:
                                break
                        elif buff_status == 4:
                            data = buffer.pop(needlen - len(buff))
                            if data:
                                buff += data[0:]
                                if len(buff) == needlen:
                                    socket_src.send(encrypter.decrypt(buff))
                                    buff = None
                                    needlen = 0
                                    buff_status = 1
                            else:
                                break
                        else:
                            raise Exception("buff_status error")
                else:
                    #### 来源于 client 的明文,直接加密发给 Sserver
                    buff = sock.recv(BUFSIZE)
                    if buff:
                        socket_dst.send(encrypter.encrypt(buff))
                    buff = None

        except socket.error as err:
            error("Loop failed", err)
            return
        except Exception as e:
            error("Loop failed", err)
            return