Esempio n. 1
0
 def dispatch_data(self, data_received, addr):
     host, port = addr[0], addr[1]
     if addr not in self.clients:
         logging.info("new client connected : %s:%s" % (host, port))
         config = dict(self.protocol_config,
                       credentials=None,
                       connid="%s:%d" % (host, port))
         if self.authenticator:
             try:
                 sock, credentials = self.authenticator(data_received)
                 config["credentials"] = credentials
             except AuthenticationError:
                 logging.info("failed to authenticate, rejecting data")
                 raise
         self.clients[addr] = self.stream_class((self.sock, addr),
                                                self.transport_class,
                                                self.transport_kwargs,
                                                client_side=False)
         conn = Connection(self.service,
                           Channel(self.clients[addr]),
                           config=config,
                           _lazy=True)
         t = multiprocessing.Process(target=self.handle_new_conn,
                                     args=(conn, ))
         t.daemon = True
         t.start()
     with self.clients[addr].downstream_lock:
         self.clients[addr].buf_in.write(data_received)
         self.clients[addr].transport.downstream_recv(
             self.clients[addr].buf_in)
Esempio n. 2
0
 def _authenticate_and_build_connection(self, sock):
     '''Authenticate a client and if it succeeds, wraps the socket in a connection object.
     Note that this code is cut and paste from the rpyc internals and may have to be
     changed if rpyc evolves'''
     # authenticate
     if self.authenticator:
         addrinfo = sock.getpeername()
         h = addrinfo[0]
         p = addrinfo[1]
         try:
             sock, credentials = self.authenticator(sock)
         except AuthenticationError:
             self.logger.info(
                 "%s:%s failed to authenticate, rejecting connection", h, p)
             return None
     else:
         credentials = None
     # build a connection
     addrinfo = sock.getpeername()
     h = addrinfo[0]
     p = addrinfo[1]
     config = dict(self.protocol_config,
                   credentials=credentials,
                   connid="%s:%d" % (h, p))
     return Connection(self.service,
                       Channel(
                           self.stream_class(sock, self.transport_class,
                                             self.transport_kwargs)),
                       config=config)
Esempio n. 3
0
    def _authenticate_and_build_connection(self, sock):
        """
        Authenticate a client and if it succees, wraps the socket
        in a connection object.  Note that this code is cut and paste
        from the rpyc internals and may have to be changed if rpyc
        evolves
        """

        # authenticate
        if self.authenticator:
            h, p = sock.getpeername()
            try:
                sock, credentials = self.authenticator(sock)
            except AuthenticationError:
                self.log_message(
                    "%s:%s failed to authenticate, rejecting connection",
                    h, p)
                return None
        else:
            credentials = None

        # build a connection
        h, p = sock.getpeername()
        config = dict(self.protocol_config,
                      credentials = credentials,
                      connid      = "%s:%d" % (h, p))
        return Connection(self.service,
                          Channel(SocketStream(sock)),
                          config = config)
Esempio n. 4
0
 def _wait(self):
     if not self._listener_poll.poll(0.01):
         return
     sock, addrinfo = self.listener.accept()
     self.clients.add(sock)
     config = dict(self.protocol_config, allow_all_attrs = True, allow_setattr = True)
     self._connection = Connection(self.service, Channel(SocketStream(sock)), config)
     self._connection_callback()
     self._log_message('client connected', addrinfo)
Esempio n. 5
0
 def _serve_client(self, sock, credentials):
     h, p = sock.getpeername()
     self.logger.info("welcome %s:%s", h, p)
     try:
         config = dict(self.protocol_config, credentials=credentials)
         conn = Connection(self.service,
                           Channel(SocketStream(sock)),
                           config=config,
                           _lazy=True)
         conn._init_service()
         conn.serve_all()
     finally:
         self.logger.info("goodbye %s:%s", h, p)
Esempio n. 6
0
 def _serve_client(self, sock, credentials):
     addrinfo = sock.getpeername()
     try:
         config = dict(self.protocol_config,
                       credentials=credentials,
                       endpoints=(sock.getsockname(), addrinfo),
                       logger=self.logger)
         conn = Connection(self.service,
                           Channel(SocketStream(sock)),
                           config=config)
         conn.serve_all()
     finally:
         pass
Esempio n. 7
0
    def _authenticate_and_build_connection(self, sock):
        '''Authenticate a client and if it succeeds, wraps the socket in a connection object.
        Note that this code is cut and paste from the rpyc internals and may have to be
        changed if rpyc evolves'''
        # authenticate
        if self.authenticator:
            addrinfo = sock.getpeername()
            h = addrinfo[0]
            p = addrinfo[1]
            try:
                sock, credentials = self.authenticator(sock)
            except AuthenticationError:
                self.logger.info(
                    "%s:%s failed to authenticate, rejecting connection", h, p)
                return None
        else:
            credentials = None
        # build a connection
        addrinfo = sock.getpeername()
        h = addrinfo[0]
        p = addrinfo[1]
        config = dict(self.protocol_config,
                      credentials=credentials,
                      connid="%s:%d" % (h, p))

        def check_timeout(event, cb, timeout=10):
            start_time = time.time()
            while True:
                if time.time() - start_time > timeout:
                    if not event.is_set():
                        logging.error("timeout occured !")
                        cb()
                    break
                elif event.is_set():
                    break
                time.sleep(0.5)

        stream = self.stream_class(sock, self.transport_class,
                                   self.transport_kwargs)

        event = threading.Event()
        t = threading.Thread(target=check_timeout, args=(event, stream.close))
        t.daemon = True
        t.start()
        try:
            c = Connection(self.service, Channel(stream), config=config)
        finally:
            event.set()
        return c
Esempio n. 8
0
 def _serve_client(self, sock, credentials):
     addrinfo = sock.getpeername()
     h = addrinfo[0]
     p = addrinfo[1]
     if credentials:
         self.logger.info("welcome [%s]:%s (%r)", h, p, credentials)
     else:
         self.logger.info("welcome [%s]:%s", h, p)
     try:
         config = dict(self.protocol_config, credentials = credentials, 
             endpoints = (sock.getsockname(), addrinfo), logger = self.logger)
         conn = Connection(self.service, Channel(SocketStream(sock)),
             config = config, _lazy = True)
         conn._init_service()
         conn.serve_all()
     finally:
         self.logger.info("goodbye [%s]:%s", h, p)
Esempio n. 9
0
    def dispatch_data(self, data_received, host=None, port=None):
        """ receive data, forward it to the stream and send back the stream downstream if any """
        decoded, cookie = self.void_stream.decode_data(data_received)
        if cookie is None:
            logging.debug("failed to retreived cookie, rejecting data %s" %
                          repr(data_received))
            return self.void_stream.encode_data("", None)
        if cookie not in self.clients:
            logging.info("new client connected : %s:%s cookie=%s" %
                         (host, port, cookie))
            config = dict(self.protocol_config,
                          credentials=None,
                          connid="%s:%d" % (host, port))
            if self.authenticator:
                try:
                    sock, credentials = self.authenticator(data_received)
                    config["credentials"] = credentials
                except AuthenticationError:
                    logging.info("failed to authenticate, rejecting data")
                    raise
            self.clients[cookie] = self.stream_class(
                (host, port), self.transport_class, self.transport_kwargs)
            self.clients[cookie].buf_in.cookie = cookie
            self.clients[cookie].buf_out.cookie = cookie
            conn = Connection(self.service,
                              Channel(self.clients[cookie]),
                              config=config,
                              _lazy=True)
            p = multiprocessing.Process(target=self.handle_new_conn,
                                        args=(conn, ))
            p.daemon = True
            p.start()
        resp = None
        with self.clients[cookie].upstream_lock:
            self.clients[cookie].upstream.write(decoded)
            #return self.void_stream.encode_data(self.clients[cookie].downstream.read(), cookie)
            resp = self.clients[cookie].downstream.read()
        if not resp:  # No data to send, so we send the default page with no data
            resp = self.void_stream.encode_data("", cookie)

        return resp