Beispiel #1
0
 def setup_connection(self, conn):
     netlog("setup_connection(%s) timeout=%s, socktype=%s", conn,
            conn.timeout, conn.socktype)
     if conn.socktype == "udp":
         self.add_packet_handler("udp-control", self._process_udp_control,
                                 False)
     protocol_class = get_client_protocol_class(conn.socktype)
     protocol = protocol_class(self.get_scheduler(), conn,
                               self.process_packet, self.next_packet)
     self._protocol = protocol
     for x in (b"keymap-changed", b"server-settings", b"logging",
               b"input-devices"):
         protocol.large_packets.append(x)
     protocol.set_compression_level(self.compression_level)
     protocol.receive_aliases.update(self._aliases)
     protocol.enable_default_encoder()
     protocol.enable_default_compressor()
     encryption = self.get_encryption()
     if encryption and ENCRYPT_FIRST_PACKET:
         key = self.get_encryption_key()
         protocol.set_cipher_out(encryption, DEFAULT_IV, key, DEFAULT_SALT,
                                 DEFAULT_ITERATIONS, INITIAL_PADDING)
     self.have_more = protocol.source_has_more
     if conn.timeout > 0:
         self.timeout_add((conn.timeout + EXTRA_TIMEOUT) * 1000,
                          self.verify_connected)
     process = getattr(conn, "process",
                       None)  #ie: ssh is handled by another process
     if process:
         proc, name, command = process
         if proc:
             getChildReaper().add_process(proc,
                                          name,
                                          command,
                                          ignore=True,
                                          forget=False)
     netlog("setup_connection(%s) protocol=%s", conn, protocol)
     return protocol
    def run(self):
        register_SIGUSR_signals(self.idle_add)
        log.info("started %s", self)
        log.info(" for client %s", self.client_conn)
        log.info(" and server %s", self.server_conn)
        client_protocol_class = get_client_protocol_class(
            self.client_conn.socktype)
        server_protocol_class = get_server_protocol_class(
            self.server_conn.socktype)
        self.client_protocol = client_protocol_class(
            self, self.client_conn, self.process_client_packet,
            self.get_client_packet)
        self.client_protocol.restore_state(self.client_state)
        self.server_protocol = server_protocol_class(
            self, self.server_conn, self.process_server_packet,
            self.get_server_packet)

        log("ProxyProcessProcess.run() pid=%s, uid=%s, gid=%s", os.getpid(),
            getuid(), getgid())
        set_proc_title("Xpra Proxy Instance for %s" % self.server_conn)
        if POSIX and (getuid() != self.uid or getgid() != self.gid):
            #do we need a valid XDG_RUNTIME_DIR for the socket-dir?
            username = get_username_for_uid(self.uid)
            socket_dir = osexpand(self.socket_dir, username, self.uid,
                                  self.gid)
            if not os.path.exists(socket_dir):
                log(
                    "the socket directory '%s' does not exist, checking for $XDG_RUNTIME_DIR path",
                    socket_dir)
                for prefix in ("/run/user/", "/var/run/user/"):
                    if socket_dir.startswith(prefix):
                        from xpra.scripts.server import create_runtime_dir
                        xrd = os.path.join(prefix,
                                           str(self.uid))  #ie: /run/user/99
                        log("creating XDG_RUNTIME_DIR=%s for uid=%i, gid=%i",
                            xrd, self.uid, self.gid)
                        create_runtime_dir(xrd, self.uid, self.gid)
                        break
            #change uid or gid:
            setuidgid(self.uid, self.gid)
        if self.env_options:
            #TODO: whitelist env update?
            os.environ.update(self.env_options)

        signal.signal(signal.SIGTERM, self.signal_quit)
        signal.signal(signal.SIGINT, self.signal_quit)
        log("registered signal handler %s", self.signal_quit)

        start_thread(self.server_message_queue, "server message queue")

        if not self.create_control_socket():
            self.stop(None, "cannot create the proxy control socket")
            return
        self.control_socket_thread = start_thread(self.control_socket_loop,
                                                  "control",
                                                  daemon=True)

        self.main_queue = Queue()

        ProxyInstance.run(self)

        try:
            QueueScheduler.run(self)
        except KeyboardInterrupt as e:
            self.stop(None, str(e))
        finally:
            log("ProxyProcess.run() ending %s", os.getpid())
Beispiel #3
0
    def run(self):
        log("ProxyProcess.run() pid=%s, uid=%s, gid=%s", os.getpid(), getuid(), getgid())
        self.setproctitle("Xpra Proxy Instance for %s" % self.server_conn)
        if POSIX and (os.getuid()!=self.uid or os.getgid()!=self.gid):
            #do we need a valid XDG_RUNTIME_DIR for the socket-dir?
            username = get_username_for_uid(self.uid)
            socket_dir = osexpand(self.socket_dir, username, self.uid, self.gid)
            if not os.path.exists(socket_dir):
                log("the socket directory '%s' does not exist, checking for $XDG_RUNTIME_DIR path", socket_dir)
                for prefix in ("/run/user/", "/var/run/user/"):
                    if socket_dir.startswith(prefix):
                        from xpra.scripts.server import create_runtime_dir
                        xrd = os.path.join(prefix, str(self.uid))   #ie: /run/user/99
                        log("creating XDG_RUNTIME_DIR=%s for uid=%i, gid=%i", xrd, self.uid, self.gid)
                        create_runtime_dir(xrd, self.uid, self.gid)
                        break
            #change uid or gid:
            setuidgid(self.uid, self.gid)
        if self.env_options:
            #TODO: whitelist env update?
            os.environ.update(self.env_options)
        self.video_init()

        log.info("new proxy instance started")
        log.info(" for client %s", self.client_conn)
        log.info(" and server %s", self.server_conn)

        signal.signal(signal.SIGTERM, self.signal_quit)
        signal.signal(signal.SIGINT, self.signal_quit)
        log("registered signal handler %s", self.signal_quit)

        start_thread(self.server_message_queue, "server message queue")

        if not self.create_control_socket():
            #TODO: should send a message to the client
            return
        self.control_socket_thread = start_thread(self.control_socket_loop, "control")

        self.main_queue = Queue()
        #setup protocol wrappers:
        self.server_packets = Queue(PROXY_QUEUE_SIZE)
        self.client_packets = Queue(PROXY_QUEUE_SIZE)
        client_protocol_class = get_client_protocol_class(self.client_conn.socktype)
        server_protocol_class = get_server_protocol_class(self.server_conn.socktype)
        self.client_protocol = client_protocol_class(self, self.client_conn, self.process_client_packet, self.get_client_packet)
        self.client_protocol.restore_state(self.client_state)
        self.server_protocol = server_protocol_class(self, self.server_conn, self.process_server_packet, self.get_server_packet)
        #server connection tweaks:
        for x in (b"input-devices", b"draw", b"window-icon", b"keymap-changed", b"server-settings"):
            self.server_protocol.large_packets.append(x)
        if self.caps.boolget("file-transfer"):
            for x in (b"send-file", b"send-file-chunk"):
                self.server_protocol.large_packets.append(x)
                self.client_protocol.large_packets.append(x)
        self.server_protocol.set_compression_level(self.session_options.get("compression_level", 0))
        self.server_protocol.enable_default_encoder()

        self.lost_windows = set()
        self.encode_queue = Queue()
        self.encode_thread = start_thread(self.encode_loop, "encode")

        log("starting network threads")
        self.server_protocol.start()
        self.client_protocol.start()

        self.send_hello()
        self.timeout_add(VIDEO_TIMEOUT*1000, self.timeout_video_encoders)

        try:
            self.run_queue()
        except KeyboardInterrupt as e:
            self.stop(str(e))
        finally:
            log("ProxyProcess.run() ending %s", os.getpid())