Beispiel #1
0
    def __init__(self, client, settings, sock=None):
        self.settings = settings

        self.transport_type = settings.get("prudp.transport")
        self.resend_timeout = settings.get("prudp.resend_timeout")
        self.resend_limit = settings.get("prudp.resend_limit")
        self.substreams = settings.get("prudp.substreams")

        self.failure = signal.Signal()

        self.sock = sock
        if not self.sock:
            if self.transport_type == settings.TRANSPORT_UDP:
                self.sock = socket.Socket(socket.TYPE_UDP)
            elif self.transport_type == settings.TRANSPORT_TCP:
                self.sock = socket.Socket(socket.TYPE_TCP)
            else:
                self.sock = websocket.WebSocketClient(True)

        if self.transport_type == settings.TRANSPORT_UDP:
            if settings.get("prudp.version") == 0:
                self.packet_encoder = PRUDPMessageV0(client, settings)
            else:
                self.packet_encoder = PRUDPMessageV1(client, settings)
        else:
            self.packet_encoder = PRUDPLiteMessage(client, settings)

        self.sequence_mgr = SequenceMgr(settings)
        self.message_encoder = PacketEncoder(settings)

        self.ack_events = {}
        self.ack_packets = {}
        self.socket_event = None
        self.packets = []
Beispiel #2
0
    def __init__(self, settings, sock=None):
        self.settings = settings
        self.transport_type = settings.get("prudp.transport")
        self.stream_type = settings.get("prudp.stream_type")
        self.fragment_size = settings.get("prudp.fragment_size")
        self.resend_timeout = settings.get("prudp.resend_timeout")
        self.resend_limit = settings.get("prudp.resend_limit")
        self.ping_timeout = settings.get("prudp.ping_timeout")
        self.silence_timeout = settings.get("prudp.silence_timeout")
        self.use_compression = settings.get("prudp.compression")

        self.sock = sock
        if not self.sock:
            if self.transport_type == settings.TRANSPORT_UDP:
                self.sock = socket.Socket(socket.TYPE_UDP)
            elif self.transport_type == settings.TRANSPORT_TCP:
                self.sock = socket.Socket(socket.TYPE_TCP)
            else:
                self.sock = websocket.WebSocketClient(True)

        access_key = settings.get("server.access_key")
        self.signature_key = hashlib.md5(access_key).digest()
        self.signature_base = sum(access_key)

        if self.transport_type == settings.TRANSPORT_UDP:
            if settings.get("prudp.version") == 0:
                self.packet_encoder = PRUDPMessageV0(self, settings)
            else:
                self.packet_encoder = PRUDPMessageV1(self, settings)
            self.encryption = RC4Encryption(self.DEFAULT_KEY)
        else:
            self.packet_encoder = PRUDPLiteMessage(self, settings)
            self.encryption = DummyEncryption()

        if settings.get("prudp.compression") == 0:
            self.compression = DummyCompression()
        else:
            self.compression = ZlibCompression()

        self.encryption.set_key(self.DEFAULT_KEY)
        self.session_key = b""

        self.source_signature = b""
        self.target_signature = b""

        self.packets = []
        self.packet_queue = {}
        self.fragment_buffer = b""
        self.packet_id_out = itertools.count()
        self.packet_id_in = 0
        self.local_session_id = 0
        self.remote_session_id = 0

        self.ack_events = {}
        self.ping_event = None
        self.timeout_event = None
        self.socket_event = None

        self.state = self.READY
Beispiel #3
0
    def connect(self, host, port, payload=b""):
        if self.state != self.DISCONNECTED:
            raise RuntimeError("Socket was not disconnected")

        logger.info("Connecting to %s:%i", host, port)
        self.state = self.CONNECTING

        self.encryption.set_key(self.DEFAULT_KEY)
        self.secure_key = b""

        self.server_signature = b""
        self.client_signature = b""
        self.connect_response = b""

        self.packets = []
        self.packet_queue = {}
        self.fragment_buffer = b""
        self.packet_id_out = itertools.count()
        self.packet_id_in = 1
        self.session_id = 0

        self.packet_encoder.reset()
        if self.transport_type == self.settings.TRANSPORT_UDP:
            self.s = socket.Socket(socket.TYPE_UDP)
        elif self.transport_type == self.settings.TRANSPORT_TCP:
            self.s = socket.Socket(socket.TYPE_TCP)
        else:
            self.s = websocket.WebSocket()

        if not self.s.connect(host, port):
            logger.error("Socket connection failed")
            self.state = self.DISCONNECTED
            return False

        self.ack_events = {}
        self.ping_event = None
        self.timeout_event = scheduler.add_timeout(self.handle_silence_timeout,
                                                   self.silence_timeout)
        self.socket_event = scheduler.add_socket(self.handle_recv, self.s)

        self.send_packet(self.syn_packet)
        if not self.wait_ack(self.syn_packet):
            logger.error("PRUDP connection failed")
            return False

        self.session_id = random.randint(0, 0xFF)
        if self.transport_type == self.settings.TRANSPORT_UDP:
            self.client_signature = bytes([
                random.randint(0, 0xFF)
                for i in range(self.packet_encoder.signature_size())
            ])
        else:
            self.client_signature = hmac.HMAC(
                self.signature_key,
                self.signature_key + self.server_signature).digest()
        self.connect_packet.signature = self.client_signature
        self.connect_packet.payload = payload

        self.send_packet(self.connect_packet)
        if not self.wait_ack(self.connect_packet):
            logger.error("PRUDP connection failed")
            return False

        self.ping_event = scheduler.add_timeout(self.handle_ping,
                                                self.ping_timeout, True)

        logger.info("PRUDP connection OK")
        self.state = self.CONNECTED
        return True