示例#1
0
class RemoteUDP(asyncio.DatagramProtocol, TimeoutMixin):
    def __init__(self, addr, port, data, local_hander):
        super().__init__()
        self.data = data
        self.local = local_hander
        self.peername = None
        self._transport = None
        self.cipher = CipherMan(
            access_user=self.local.cipher.access_user, ts_protocol=flag.TRANSPORT_UDP
        )
        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.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()
示例#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()
示例#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()