async def init_from_pipe(cls, *, pipe: TextIOBase, level: Union[int, str], formatter: Formatter, filter: Filter = None, protocol_factory: Type[asyncio.Protocol] = None, loop=None) -> 'AsyncStreamHandler': if loop is None: loop = asyncio.get_event_loop() if protocol_factory is None: protocol_factory = AiologgerProtocol transport, protocol = await loop.connect_write_pipe( protocol_factory, pipe) stream_writer = StreamWriter(transport=transport, protocol=protocol, reader=None, loop=loop) return AsyncStreamHandler(level=level, stream=stream_writer, formatter=formatter, filter=filter)
async def test_authenticate(transport, protocol): conn = AsyncTelnetTransport( "localhost", auth_password="******", auth_username="******", timeout_ops=10, timeout_socket=5, ) conn.stdout = StreamReader() conn.stdin = StreamWriter(transport, protocol, reader=None, loop=asyncio.get_event_loop()) # feed data in w/ the username prompt already in it -- auth should then send the username and # clear the buffer... then it should read and see the password that we are inserting later data = b"\xff\xfd\x18\xff\xfd \xff\xfd#username:\xff\xfd'\xff\xfb\x03\xff\xfd\x01\xff\xfd\x1f\xff\xfb\x05\xff\xfd!\xff\xfb\x03\xff\xfb\x01" conn.stdout.feed_data(data) async def _put_login_data_in_reader(): # this sleep is enough that we should *always* see a an additional return char that we send # when we dont get any output on telnet connections await asyncio.sleep(1.5) conn.stdout.feed_data(b"Password:"******"lookforthisusername" in conn.stdin.transport.out_buf assert b"lookforthispassword" in conn.stdin.transport.out_buf # a return for the username, one for password and one for the extra return we send when we dont # see the password prompt soon enough assert conn.stdin.transport.out_buf.count(b"\n") >= 3
async def _open(self): rx = StreamReader() transport, proto = await get_running_loop().connect_write_pipe( lambda: StreamReaderProtocol(rx), os.fdopen(self._fd, "wb")) tx = StreamWriter(transport, proto, rx, get_running_loop()) return transport, tx
def connection_made(self, transport): self._peername = transport.get_extra_info('peername') self._transport = transport self._stream_reader.set_transport(transport) self._reader = StreamReaderAdapter(self._stream_reader) self._stream_writer = StreamWriter(transport, self, self._stream_reader, self._loop) self._loop.create_task(self.start())
def connection_made(self, transport): self._peername = transport.get_extra_info('peername') self._transport = transport logging.info("Mqtt client connected from: {}.".format(self._peername)) self._stream_reader.set_transport(transport) self._reader = StreamReaderAdapter(self._stream_reader) self._stream_writer = StreamWriter(transport, self, self._stream_reader, self._loop) self._loop.create_task(self.start())
async def connect(self): await self.connecting.wait() kcpConn = UKcp(self.conv, self) reader = StreamReader(limit=_DEFAULT_LIMIT, loop=self._loop) writer = StreamWriter(TransportWrapper(self.conv, self), self, reader, self._loop) conn = Conn(kcpConn=kcpConn, reader=reader, writer=writer) self.conns[self.conv] = conn self.activeConns.add(self.conv) self.conv += 1 return reader, writer
async def main(): arg_parser = ArgumentParser(prog="bloXroute CLI", epilog=get_description(), formatter_class=RawDescriptionHelpFormatter) cli_parser = ArgumentParser(prog="bloXroute CLI", epilog=get_description(), formatter_class=RawDescriptionHelpFormatter) add_base_arguments(arg_parser) add_base_arguments(cli_parser) add_run_arguments(cli_parser) args = sys.argv[1:] if "-h" in args or "--help" in args: cli_parser.print_help() arg_parser.exit() opts, params = arg_parser.parse_known_args() stdin_reader = StreamReader() cli_loop = asyncio.get_event_loop() await cli_loop.connect_read_pipe( lambda: StreamReaderProtocol(stdin_reader), sys.stdin) transport, protocol = await cli_loop.connect_write_pipe( lambda: FlowControlMixin(), os.fdopen(sys.stdout.fileno(), "wb")) stdout_writer = StreamWriter(transport, protocol, None, cli_loop) if not validate_args(opts, stdout_writer): exit(1) try: if opts.interactive_shell or len(params) == 0: await run_cli(opts.rpc_host, opts.rpc_port, opts.rpc_user, opts.rpc_password, opts.account_id, opts.secret_hash, opts.cloud_api_url, opts.cloud_api, cli_parser, stdin_reader, stdout_writer) else: if "help" in sys.argv: cli_parser.print_help(file=ArgParserFile(stdout_writer)) async with RpcClient(opts.rpc_host, opts.rpc_port, opts.rpc_user, opts.rpc_password, opts.account_id, opts.secret_hash, opts.cloud_api_url, opts.cloud_api) as client: await parse_and_handle_command(cli_parser, stdout_writer, client) except (ClientConnectorError, ClientConnectionError, TimeoutError, CancelledError) as e: stdout_writer.write( f"Connection to RPC server is broken: {e}, exiting!\n".encode( "utf-8")) await stdout_writer.drain() if opts.debug: raise finally: stdout_writer.close()
async def test_telnet_isauthenticated_failed_password(transport, protocol): conn = AsyncTelnetTransport("localhost") conn.stdout = StreamReader() conn.stdin = StreamWriter(transport, protocol, reader=None, loop=asyncio.get_event_loop()) data = b"password:" conn.stdout.feed_data(data) _isauthenticated = await conn._telnet_isauthenticated() assert _isauthenticated is False
async def _init_writer(self) -> StreamWriter: async with self._initialization_lock: if self.writer is not None: return self.writer transport, protocol = await self.loop.connect_write_pipe( self.protocol_class, self.stream) self.writer = StreamWriter( # type: ignore # https://github.com/python/typeshed/pull/2719 transport=transport, protocol=protocol, reader=None, loop=self.loop, ) return self.writer
def datagram_received(self, data, addr): self.default_receiver = addr conv = UKcp.ikcp_decode32u(data, 0) if conv in self.conns: self.activeConns.add(conv) self.conns[conv].kcpConn.input(data) else: kcpConn = UKcp(conv, self) reader = StreamReader(limit=_DEFAULT_LIMIT, loop=self._loop) writer = StreamWriter(TransportWrapper(conv, self), self, reader, self._loop) conn = Conn(kcpConn=kcpConn, reader=reader, writer=writer) self.conns[conv] = conn self.activeConns.add(conv) self._loop.create_task(self.cb(reader, writer))
async def test_telnet_isauthenticated_success(transport, protocol): conn = AsyncTelnetTransport("localhost") assert conn._isauthenticated is False conn.stdout = StreamReader() conn.stdin = StreamWriter(transport, protocol, reader=None, loop=asyncio.get_event_loop()) # feed data in w/ the username prompt already in it -- auth should then send the username and # clear the buffer... then it should read and see the password that we are inserting later data = b"averyniceswitch#" conn.stdout.feed_data(data) await conn._telnet_isauthenticated() assert conn._isauthenticated is True
async def test_handle_control_chars(transport, protocol): conn = AsyncTelnetTransport("localhost") conn._isauthenticated = True conn.stdout = StreamReader() conn.stdin = StreamWriter(transport, protocol, reader=None, loop=asyncio.get_event_loop()) data = b"\xff\xfd\x18\xff\xfd \xff\xfd#someoutput\xff\xfd'\xff\xfb\x03\xff\xfd\x01\xff\xfd\x1f\xff\xfb\x05\xff\xfd!\xff\xfb\x03\xff\xfb\x01moreoutput" conn.stdout.feed_data(data) output = await conn._handle_control_chars() assert output == b"someoutputmoreoutput" assert ( conn.stdin.transport.out_buf == b"\xff\xfc\x18\xff\xfc \xff\xfc#\xff\xfc'\xff\xfe\x03\xff\xfc\x01\xff\xfc\x1f\xff\xfe\x05\xff\xfc!\xff\xfe\x03\xff\xfe\x01" )
async def test_telnet_isauthenticated_success_set_binary_transmission( transport, protocol): conn = AsyncTelnetTransport("localhost") assert conn._isauthenticated is False assert conn._stdout_binary_transmission is False conn.stdout = StreamReader() conn.stdin = StreamWriter(transport, protocol, reader=None, loop=asyncio.get_event_loop()) data = b"\x00averyniceswitch#" conn.stdout.feed_data(data) await conn._telnet_isauthenticated() assert conn._isauthenticated is True assert conn._stdout_binary_transmission is True
async def open(self, loop=None): """ Open the transmit end on the given event loop, returning an instance of [`asyncio.StreamWriter`](https://docs.python.org/3/library/asyncio-stream.html#streamwriter). If no event loop is given, the default one will be used. """ if loop is None: loop = asyncio.get_event_loop() txTransport, txProto = await loop.connect_write_pipe( lambda: StreamReaderProtocol(StreamReader(loop=loop), loop=loop), os.fdopen(self._fd, "w")) tx = StreamWriter(txTransport, txProto, None, loop) return tx
async def _init_writer(self) -> StreamWriter: async with self._initialization_lock: if not is_pipe_transport_compatible(self.stream): self.writer = DSNonFileStreamWriter(self.stream, loop=self.loop) else: self.loop = asyncio.get_event_loop() transport, protocol = await self.loop.connect_write_pipe( protocol_factory=self.protocol_class, pipe=self.stream) self.writer = StreamWriter( # type: ignore # https://github.com/python/typeshed/pull/2719 transport=transport, protocol=protocol, reader=None, loop=self.loop, ) return self.writer
async def test_authenticate_timeout(transport, protocol): conn = AsyncTelnetTransport("localhost", auth_password="******", auth_username="******") # make the timeout way faster so we dont have to wait conn._timeout_ops_auth = 0.25 conn.stdout = StreamReader() conn.stdin = StreamWriter(transport, protocol, reader=None, loop=asyncio.get_event_loop()) # feed data in w/ the username prompt already in it -- auth should then send the username and # clear the buffer... then it should read and see the password that we are inserting later data = b"\xff\xfd\x18\xff\xfd \xff\xfd#username:\xff\xfd'\xff\xfb\x03\xff\xfd\x01\xff\xfd\x1f\xff\xfb\x05\xff\xfd!\xff\xfb\x03\xff\xfb\x01" conn.stdout.feed_data(data) with pytest.raises(ScrapliTimeout) as exc: await conn._authenticate() assert str(exc.value) == "Timed out looking for telnet login prompts" # asserting that we at least got the username sent assert b"lookforthisusername" in conn.stdin.transport.out_buf