Exemplo n.º 1
0
class RemoteUDP(asyncio.DatagramProtocol):
    def __init__(self, addr, port, data, local_hander):
        super().__init__()
        self.addr = addr
        self.port = port
        self.data = data
        self.local = local_hander
        self.peer = None
        self._transport = None
        self.cipher_man = None
        self._is_closing = False

    def write(self, data):
        self._transport and not self._transport.is_closing() and self._transport.sendto(data)

    def close(self):
        if self._is_closing:
            return

        self._is_closing = True
        self._transport and self._transport.close()
        del self.local

    def connection_made(self, transport):
        self._transport = transport
        self.peer = self._transport.get_extra_info("peername")
        self.write(self.data)

    def datagram_received(self, data, peer, *args):
        if not self.cipher_man:
            self.cipher_man = CipherMan(access_user=self.local.cipher_man.access_user, ts_protocol=flag.TRANSPORT_UDP)

        assert self.peer == peer
        # 源地址和端口
        bind_addr = peer[0]
        bind_port = peer[1]
        if "." in bind_addr:
            addr = socket.inet_pton(socket.AF_INET, bind_addr)
        elif ":" in bind_addr:
            addr = socket.inet_pton(socket.AF_INET6, bind_addr)
        else:
            raise Exception("add not valid")
        port = struct.pack("!H", bind_port)
        # 构造返回的报文结构
        data = b"\x01" + addr + port + data
        data = self.cipher_man.encrypt(data)
        self.local.write(data)

    def error_received(self, exc):
        self.close()

    def connection_lost(self, exc):
        self.close()
Exemplo n.º 2
0
class RemoteTCP(asyncio.Protocol):
    def __init__(self, local_handler):
        super().__init__()

        self.local = local_handler
        self.peername = None
        self._transport = None
        self.ready = False

        self._is_closing = False

    def write(self, data):
        if not self._transport.is_closing():
            self._transport.write(data)

    def close(self):
        if self._is_closing:
            return
        self._is_closing = True
        ACTIVE_CONNECTION_COUNT.inc(-1)

        self._transport and self._transport.close()
        self.local.close()

    def connection_made(self, transport: asyncio.Transport):
        self._transport = transport
        self.peername = self._transport.get_extra_info("peername")
        self.cipher = CipherMan(
            access_user=self.local.cipher.access_user, peername=self.peername
        )
        transport.write(self.local._connect_buffer)
        self.ready = True
        CONNECTION_MADE_COUNT.inc()
        ACTIVE_CONNECTION_COUNT.inc()

    def data_received(self, data):
        self.local.write(self.cipher.encrypt(data))

    def pause_reading(self):
        self.local._transport.pause_reading()

    def resume_reading(self):
        self.local._transport.resume_reading()

    def eof_received(self):
        self.close()

    def connection_lost(self, exc):
        self.close()
Exemplo n.º 3
0
class RemoteTCP(asyncio.Protocol, TimeoutMixin):
    def __init__(self, addr, port, data, local_handler):
        super().__init__()

        self.data = data
        self.local = local_handler
        self.peername = None
        self._transport = None
        self.cipher = CipherMan(access_user=local_handler.cipher.access_user)

        self._is_closing = True

    def write(self, data):
        self._transport and not self._transport.is_closing() and self._transport.write(
            data
        )

    def close(self):
        if self._is_closing:
            return
        self._is_closing = True

        self._transport and self._transport.close()
        del self.local

    def connection_made(self, transport):
        self._transport = transport
        self.peername = self._transport.get_extra_info("peername")
        self.write(self.data)

    def data_received(self, data):
        self.keep_alive()
        self.local.write(self.cipher.encrypt(data))

    def pause_reading(self):
        self._transport and self._transport.pause_reading()

    def resume_reading(self):
        self._transport and self._transport.resume_reading()

    def eof_received(self):
        self.local and self.local.handle_eof_received()
        self.close()

    def connection_lost(self, exc):
        self.close()
Exemplo n.º 4
0
class RemoteUDP(asyncio.DatagramProtocol, TimeoutMixin):
    def __init__(self, addr, port, data, local_hander):
        super().__init__()
        self.data = data
        self.local = local_hander
        self.cipher = CipherMan(access_user=local_handler.cipher.access_user)

        self.peername = None
        self._transport = None

    def write(self, data):
        self._transport and self._transport.sendto(data, self.peername)

    def close(self):
        self._transport and self._transport.close()

    def connection_made(self, transport):
        self._transport = transport
        self.peername = self._transport.get_extra_info("peername")
        self.write(self.data)

    def datagram_received(self, data, peername, *arg):
        self.keep_alive()

        assert self.peername == peername
        # 源地址和端口
        bind_addr = peername[0]
        bind_port = peername[1]
        if "." in bind_addr:
            addr = socket.inet_pton(socket.AF_INET, bind_addr)
        elif ":" in bind_addr:
            addr = socket.inet_pton(socket.AF_INET6, bind_addr)
        else:
            raise Exception("add not valid")
        port = struct.pack("!H", bind_port)
        # 构造返回的报文结构
        data = b"\x01" + addr + port + data
        data = self.cipher.encrypt(data)
        self.local.write(data)

    def error_received(self, exc):
        self.close()

    def connection_lost(self, exc):
        self.close()
Exemplo n.º 5
0
 def _init_cipher(self):
     self.cipher = CipherMan.get_cipher_by_port(self.port,
                                                self._transport_protocol)
Exemplo n.º 6
0
 def _init_cipher(self):
     self.cipher = CipherMan.get_cipher_by_port(self.port)
Exemplo n.º 7
0
 def connection_made(self, transport: asyncio.Transport):
     self._transport = transport
     self.peer = self._transport.get_extra_info("peername")
     self.cipher_man = CipherMan(access_user=self.local.cipher_man.access_user, peer=self.peer)
     transport.write(self.local.connect_buffer)
     self.ready = True