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)
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) t = PupyConnectionThread(self.pupy_srv, self.service, Channel(self.clients[addr]), config=config) 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)
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)
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)
def connectionMade(self): self.stream = TwistedSocketStream(self.transport) self.conn = rpyc.Connection(self.factory.service, Channel(self.stream), config=self.factory.config, _lazy=True) self.conn._init_service() if self.factory.logging: log.msg("%s: connected %s" % (self, self.conn)) if self.factory.on_connected is not None: reactor.callLater(0, self.factory.on_connected, self.conn)
def _serve_client(self, sock, credentials): addrinfo = sock.getpeername() if credentials: self.logger.info("welcome %s (%r)", addrinfo, credentials) else: self.logger.info("welcome %s", addrinfo) try: config = dict(self.protocol_config, credentials = credentials, endpoints = (sock.getsockname(), addrinfo), logger = self.logger) conn = self.service._connect(Channel(SocketStream(sock)), config) self._handle_connection(conn) finally: self.logger.info("goodbye %s", addrinfo)
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)
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
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: sock, credentials = self.authenticator(sock) else: credentials = None # build a connection h, p = sock.getpeername() config = dict(self.protocol_config, credentials=credentials, connid="%s:%d" % (h, p), endpoints=(sock.getsockname(), (h, p))) return sock, self.service._connect(Channel(SocketStream(sock)), config)
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
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)
def _setup_connection(self, lock, sock, queue): '''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''' tup = sock.getpeername() h, p = tup[0], tup[ 1] # tup can have different sizes depending on ipv4/ipv6 credentials = None if self.authenticator: try: wrapper, credentials = self.authenticator(sock) except AuthenticationError: self.logger.info( '{}:{} failed to authenticate, rejecting connection'. format(h, p)) queue.put_nowait((None, None, None)) return else: wrapper = sock # build a connection config = dict(self.protocol_config, credentials=credentials, connid='{}:{}'.format(h, p)) stream = self.stream_class(wrapper, self.transport_class, self.transport_kwargs) connection = None try: self.logger.debug( '{}:{} Authenticated. Starting connection'.format(h, p)) connection = PupyConnection(lock, self.pupy_srv, self.service, Channel(stream), config=config) self.logger.debug('{}:{} Connection complete'.format(h, p)) finally: self.logger.debug('{}:{} Report connection: {}'.format( h, p, connection)) queue.put_nowait((connection, wrapper, credentials))
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