async def forward_data_task(
     self,
     dir_str: str,
     reader: StreamReader,
     writer: StreamWriter,
     exit_q: asyncio.Queue,
     exit_status: int,
 ):
     """Forward data from one side to the other."""
     try:
         while True:
             data = await reader.read(8192)
             if len(data) == 0:
                 print(
                     f"{PKG_NAME}: {self.name} {dir_str}: read EOF; shutdown with exit_status={exit_status}"
                 )
                 await exit_q.put(exit_status)
                 return
             if self.verbose >= 4:
                 print(
                     f"{PKG_NAME}: {self.name} {dir_str}: {data} ## {data.hex()}"
                 )
             writer.write(data)
             await writer.drain()
     except asyncio.CancelledError:  # pylint: disable=try-except-raise
         raise
     except Exception as err:  # pylint: disable=broad-except
         print(
             f"{PKG_NAME}: {self.name} {dir_str} got exception {err}; {traceback.format_exc(-1)}"
         )
         await exit_q.put(1)
         return
Ejemplo n.º 2
0
async def send_state(writer: StreamWriter):
    """
    This only sends the pulses and period, nothing else
    """
    writer.write(
        f'{step_seconds},{",".join([str(x) for x in pulses_state])},0,0,0,0,0,0\n'
        .encode('utf8'))
Ejemplo n.º 3
0
    async def setup_for_stdio(self):
        self.reader = asyncio.StreamReader()
        reader_protocol = asyncio.StreamReaderProtocol(self.reader)
        await self.event_loop.connect_read_pipe(lambda: reader_protocol, sys.stdin)

        writer_transport, writer_protocol = await self.event_loop.connect_write_pipe(FlowControlMixin, sys.stdout)
        self.writer = StreamWriter(writer_transport, writer_protocol, None, self.event_loop)
Ejemplo n.º 4
0
    async def handle_connection(self, reader: StreamReader, writer: StreamWriter):
        temp_ref = TempRef()
        world_packet_manager = WorldPacketManager(temp_ref=temp_ref, reader=reader, writer=writer)

        peername = writer.get_extra_info('peername')
        Logger.debug('[World Server]: Accept connection from {}'.format(peername))

        Logger.info('[World Server]: trying to process auth session')
        auth = AuthManager(reader, writer, temp_ref=temp_ref, world_packet_manager=world_packet_manager)
        await auth.process(step=AuthStep.SECOND)

        self._register_tasks()

        while True:
            try:
                request = await asyncio.wait_for(reader.read(4096), timeout=1.0)
                if request:
                    response = await asyncio.wait_for(world_packet_manager.process(request), timeout=1.0)

                    if response:
                        for packet in response:
                            writer.write(packet)
                            await writer.drain()

            except TimeoutError:
                continue

            except Exception as e:
                Logger.error('[World Server]: exception, {}'.format(e))
                traceback.print_exc()
                break

        writer.close()
Ejemplo n.º 5
0
def write_response(
    writer: StreamWriter,
    status: HTTPStatus,
    headers: Iterable[Header],
    body: str,
) -> None:
    write_http_head(writer, status, headers)
    writer.write(body.encode("utf-8"))
Ejemplo n.º 6
0
    def connection_made(self,
                        transport: asyncio.transports.BaseTransport) -> None:
        super().connection_made(transport)
        self._stream_writer = StreamWriter(transport, self,
                                           self._stream_reader, self._loop)

        if self.client:
            asyncio.create_task(self.client.connection_made(transport))
Ejemplo n.º 7
0
    async def handle_connection(self, reader: StreamReader, writer: StreamWriter):
        peername = writer.get_extra_info('peername')
        Logger.info('[Login Server]: Accepted connection from {}'.format(peername))

        temp_ref = TempRef()

        auth = AuthManager(reader, writer, temp_ref=temp_ref)
        await auth.process(step=AuthStep.FIRST)
        writer.close()
Ejemplo n.º 8
0
 def __init__(self,
              logFolder,
              name=DEFAULTLOGNAME,
              messageLevel=MessageType.INFORMATION,
              append=false):
     writer = StreamWriter(filePath, True)
     writer.Write(DEFAULTHTMLHEADER)
     writer.Flush()
     writer.Close()
Ejemplo n.º 9
0
 async def callback(reader: StreamReader, writer: StreamWriter):
     content = b'{ fail to parse'
     writer.write(
         f'content-length: {len(content)}\r\n\r\n'.encode() + content)
     raw_content_length = await reader.readline()
     content_length = int(
         raw_content_length.decode().split(':')[-1].strip())
     self.empty_line = await reader.readline()
     raw_response = await reader.readexactly(content_length)
     writer.close()
     self.response = json.loads(raw_response.decode())
Ejemplo n.º 10
0
 async def client(reader: StreamReader, writer: StreamWriter):
     try:
         send_initialize(writer)
         response = await wait_for_response(reader)
         print(response)
         send_initialized(writer)
         response = await wait_for_response(reader)
         print(response)
     except Exception as err:
         self.err = err
     finally:
         writer.close()
Ejemplo n.º 11
0
 async def callback(reader: StreamReader, writer: StreamWriter):
     self.stub['method'] = 'this method does not exist'
     content = json.dumps(self.stub).encode()
     writer.write(
         f'content-length: {len(content)}\r\n\r\n'.encode() + content)
     raw_content_length = await reader.readline()
     content_length = int(
         raw_content_length.decode().split(':')[-1].strip())
     self.empty_line = await reader.readline()
     raw_response = await reader.readexactly(content_length)
     writer.close()
     self.response = json.loads(raw_response.decode())
Ejemplo n.º 12
0
async def client_connected(reader: StreamReader, writer: StreamWriter,
                           jobs_table: JobsTable):
    line = await read_long_line(reader)
    command = deserialize_command(line)
    reply = await handle_command(command, jobs_table)

    if reply is not None:
        reply_json = reply.serialize()
        writer.write(reply_json.encode())

    writer.close()
    await writer.wait_closed()
Ejemplo n.º 13
0
 async def callback(reader: StreamReader, writer: StreamWriter):
     content = json.dumps(
         {'message': 'Hello, World!'}).encode()
     writer.write(
         f'content-length: {len(content)}\r\n\r\n'.encode() + content)
     raw_content_length = await reader.readline()
     content_length = int(
         raw_content_length.decode().split(':')[-1].strip())
     self.empty_line = await reader.readline()
     raw_response = await reader.readexactly(content_length)
     self.response = json.loads(raw_response.decode())
     writer.close()
Ejemplo n.º 14
0
    async def handle_connection(self, reader: StreamReader,
                                writer: StreamWriter):
        self._register_tasks()

        temp_ref = TempRef()
        world_packet_manager = WorldPacketManager(temp_ref=temp_ref,
                                                  reader=reader,
                                                  writer=writer)

        Logger.info('[World Server]: trying to process auth session')
        auth = AuthManager(reader,
                           writer,
                           temp_ref=temp_ref,
                           world_packet_manager=world_packet_manager,
                           session_keys=self.session_keys)

        is_authenticated = await auth.process(step=AuthStep.SECOND)

        if is_authenticated:

            peer_name = writer.get_extra_info('peername')
            Logger.success(
                '[World Server]: Accept connection from {}'.format(peer_name))

            while True:
                try:
                    request = await asyncio.wait_for(reader.read(4096),
                                                     timeout=0.01)
                    if request:
                        response = await asyncio.wait_for(
                            world_packet_manager.process(request),
                            timeout=0.01)

                        if response:
                            for packet in response:
                                writer.write(packet)
                                await writer.drain()

                except TimeoutError:
                    pass

                except BrokenPipeError:
                    pass

                except Exception as e:
                    Logger.error('[World Server]: exception, {}'.format(e))
                    traceback.print_exc()
                    break
                finally:
                    await asyncio.sleep(0.01)

        writer.close()
Ejemplo n.º 15
0
async def process(reader: StreamReader, writer: StreamWriter):
    host, port = writer.transport.get_extra_info('peername')
    print(f'Connected to: {host}:{port}')

    factory = CommandFactory(reader, writer)

    try:
        while True:
            line = (await reader.readline()).decode().strip()
            command = factory.get_command(line)
            await command.execute()
    except ConnectionResetError:
        writer.close()
        print(f'Disconnected from {host}:{port}')
Ejemplo n.º 16
0
 async def callback(reader: StreamReader, writer: StreamWriter):
     self.stub['method'] = 'initialize'
     # If initialize is called without a `rootUri` param, it will raise, causing an InternalError
     del self.stub['params']['rootUri']
     content = json.dumps(self.stub).encode()
     writer.write(
         f'content-length: {len(content)}\r\n\r\n'.encode() + content)
     raw_content_length = await reader.readline()
     content_length = int(
         raw_content_length.decode().split(':')[-1].strip())
     self.empty_line = await reader.readline()
     raw_response = await reader.readexactly(content_length)
     writer.close()
     self.response = json.loads(raw_response.decode())
Ejemplo n.º 17
0
    async def async_http(reader: StreamReader, writer: StreamWriter) -> None:
        log.info('start callback')
        request_line: bytes = await reader.readline()
        if not request_line:
            writer.close()

        data = request_line.decode()
        request = await parse_http(data)

        response = handle_request(args, request)
        response.write_to(writer)
        await writer.drain()

        log.info('end callback')
        writer.close()
Ejemplo n.º 18
0
    async def handle_connection(self, reader: StreamReader,
                                writer: StreamWriter):
        peername = writer.get_extra_info('peername')
        Logger.info(
            '[Login Server]: Accepted connection from {}'.format(peername))

        auth = AuthManager(reader, writer)

        while not reader.at_eof():
            response = await auth.process()
            if not response:
                break

        Logger.warning('[Login Server]: closing...')
        writer.close()
Ejemplo n.º 19
0
 async def get_writer(self):
     if self._writer is None:
         loop = asyncio.get_event_loop()
         transport, protocol = await loop.connect_write_pipe(
             FlowControlMixin, os.fdopen(0, 'wb'))
         self._writer = StreamWriter(transport, protocol, None, loop)
     return self._writer
Ejemplo n.º 20
0
    def __init__(self, server, writer: StreamWriter):
        self.remote_addr = writer.get_extra_info('peername')
        self.remote_host, self.remote_port = self.remote_addr

        self.local_addr = writer.get_extra_info('sockname')
        self.local_host, self.local_port = self.local_addr

        self.server = server
        self.writer = writer

        # state
        self.registered = False
        self.nickname = None
        self.user = None
        self.realname = None
        self.vhost = None
Ejemplo n.º 21
0
    async def process_request(reader: StreamReader, writer: StreamWriter,
                              world_packet_mgr: WorldPacketManager):
        request = await asyncio.wait_for(
            reader.read(4096), timeout=Config.Realm.Settings.min_timeout)
        if request:
            opcode, data = request[:1], request[1:]

            if data:
                response = await asyncio.wait_for(
                    world_packet_mgr.process(opcode=opcode, data=data),
                    timeout=Config.Realm.Settings.min_timeout)

                if response:
                    for packet in response:
                        writer.write(packet)
                        await writer.drain()
Ejemplo n.º 22
0
def write_chunk(writer: StreamWriter, data: bytes) -> None:
    writer.write(bytes(hex(len(data))[2:], "ascii"))
    writer.write(b"\r\n")
    writer.write(data)
    writer.write(b"\r\n")
    encoded = (data.decode("utf-8", errors="ignore").replace("\r",
                                                             "\\r").replace(
                                                                 "\n", "\\n"))
    logging.debug(f"wrote chunk to listener: {encoded}")
Ejemplo n.º 23
0
async def make_client(reader: StreamReader, writer: StreamWriter):

    # get peer name
    peer_name = writer.get_extra_info(PEER_NAME)
    print(f"Recieved request for peer: {peer_name}")

    # get subscriber channel and add to list
    subscriber_chan = await read_msg(reader)

    # adding this writer to subscriber list as well
    SUBSCRIBER_LIST[subscriber_chan].append(writer)

    # spawn a async task for this writer to pull msgs off to queue and send to subscribers
    asyncio.create_task(msg_writer(writer, SEND_QUEUE[writer]))

    try:
        while True:
            chan = await read_msg(reader)
            if chan not in CHAN_QUEUE:
                # create a queue where we will enqueue msgs for given channel
                CHAN_QUEUE[chan] = Queue(maxsize=CHAN_QUEUE_SIZE)
                # spawn up new channel sender
                asyncio.create_task(channel_sender(chan))

            data = await read_msg(reader)
            """
            put the msg onto the queue for this channel
            channel sender will pull msg off this queue and then put it in 
            the respective writers queues
            """
            await CHAN_QUEUE[chan].put(data)

    except asyncio.CancelledError:
        print(f"remote peer closing : {peer_name}")
        writer.close()
        await writer.wait_closed()

    except asyncio.IncompleteReadError:
        print(f"remote peer {peer_name} disconnected")

    finally:
        print(f"remote peer {peer_name} closed")
        # remove our writer form channel list
        SUBSCRIBER_LIST[subscriber_chan].remove(writer)

        # close async task for this writer
        await SEND_QUEUE[writer].put(None)
Ejemplo n.º 24
0
    async def run(self):
        self._run = True
        started = False
        # start with the login procedure

        # TODO: this is broken, it fails to restart properly

        ctr = 0
        while self._run:
            if self.protocol and not self.protocol.is_connected:
                # reset everything
                self.log('we have been disconnected - reset everything')
                self.reader = None
                self.writer = None
                self.protocol = None
                self.transport = None

            if not self.reader:
                self.log('create a StreamReader')
                self.reader = StreamReader(loop=self.loop)
            if not self.protocol:
                self.log('create OWNProtocol')
                self.protocol = OWNProtocol(self.reader, log=self.log)
            if not self.protocol.is_connected:
                if not self._auto_restart:
                    if started:
                        self.log("no autorestart... bailing out")
                        break
                started = True
                self.log('attempt connection')
                self.is_ready.clear()
                self.log('resetting the msg handler')
                self.msg_handler = self.state_start
                try:
                    self.log('create a new transport connection')
                    self.transport, _ = await self.loop.create_connection(
                        lambda: self.protocol, self.host, self.port)
                except gaierror:
                    self.log('sleep about 5 seconds')
                    await asyncio.sleep(5)
                else:
                    self.log('create a new StreamWriter')
                    self.writer = StreamWriter(
                        self.transport, self.protocol, self.reader, self.loop)
                    self.log('connection is up and running')
            try:
                pkt = await asyncio.wait_for(self.reader.readuntil(b'##'), timeout=1)
            except asyncio.TimeoutError:
                pass
            else:
                # from now on, packets are strings
                msg = pkt.decode('ascii')
                # self.log('packet (%d)=> %s' % (ctr, msg))
                self.log('<= %s' % (msg))
                await self.msg_handler(msg)
                ctr += 1
        self.transport.close()
        self.log('AsyncIOOWNConnection.run : %s the end' % (str(self)))
Ejemplo n.º 25
0
    async def process_request(reader: StreamReader, writer: StreamWriter, world_packet_mgr: WorldPacketManager):
        request: bytes = await wait_for(reader.read(4096), timeout=Config.Realm.Settings.min_timeout)
        if request:
            size, opcode, data = request[:2], request[2:6], request[6:]

            response: List[bytes] = await wait_for(
                world_packet_mgr.process(
                    size=size,
                    opcode=opcode,
                    data=data
                ),
                timeout=Config.Realm.Settings.min_timeout
            )

            if response:
                for packet in response:
                    writer.write(packet)
                    await writer.drain()
Ejemplo n.º 26
0
async def stdio(loop):
    reader = asyncio.StreamReader()
    reader_protocol = asyncio.StreamReaderProtocol(reader)
    await loop.connect_read_pipe(lambda: reader_protocol, sys.stdin)

    writer_transport, writer_protocol = await loop.connect_write_pipe(FlowControlMixin, sys.stdout)
    writer = StreamWriter(writer_transport, writer_protocol, None, loop)

    return reader, writer
Ejemplo n.º 27
0
async def callback(reader: StreamReader, writer: StreamWriter) -> None:
    while True:
        data = await reader.read(100)
        message = data.decode().strip()

        if message == 'calendar':
            answer = datetime.now().strftime("%d.%m.%Y %H:%M")
        elif message.startswith(ECHO_PREFIX):
            answer = message[len(ECHO_PREFIX):]
        elif message == 'stop':
            break
        else:
            answer = HELP_TEXT

        answer += NEWLINE
        writer.write(answer.encode())
        await writer.drain()

    writer.close()
Ejemplo n.º 28
0
 async def client_connected(self, reader: StreamReader,
                            writer: StreamWriter):
     peer_name = writer.transport.get_extra_info('peername')
     log(LogLevel.INFO, f'{peer_name} connected')
     while True:
         try:
             data = await self.bit_protocol.read_data(reader)
             response = await self.process_message(peer_name, data)
             if response:
                 await self.bit_protocol.write_message(
                     writer, response.data)
         except ConnectionError as err:
             log(LogLevel.INFO, f'{peer_name} disconnected: {err}')
             break
         except NotImplementedError as err:
             log(LogLevel.ERROR, f'{peer_name}: {data}')
         except Exception as err:
             log(LogLevel.FATAL, f'{peer_name} disconnected: {data}')
             break
     writer.close()
Ejemplo n.º 29
0
async def process(reader: StreamReader, writer: StreamWriter):
    host, port = writer.transport.get_extra_info('peername')
    logging.info(f'Client {host}:{port} is connected!')

    factory = CommandFactory(reader, writer)

    try:
        while True:
            line = (await reader.readline()).decode().strip()
            command = factory.get_command(line)

            # while True:
            #     line = input('=> ')
            #     command: AbstractCommand = factory.get_command(line)
            #     command.execute()

            await command.execute()
    except ConnectionResetError:
        writer.close()
        logging.info(f'Disconnected from {host}:{port}')
Ejemplo n.º 30
0
    async def open_from_stdio() -> "ChanPro":
        loop = asyncio.get_event_loop()
        reader = asyncio.StreamReader()
        reader_protocol = asyncio.StreamReaderProtocol(reader)
        await loop.connect_read_pipe(lambda: reader_protocol, sys.stdin.buffer)

        writer_transport, writer_protocol = await loop.connect_write_pipe(
            FlowControlMixin, sys.stdout.buffer)
        writer = StreamWriter(writer_transport, writer_protocol, None, loop)

        return ChanPro(reader, writer)
Ejemplo n.º 31
0
async def process(reader: StreamReader, writer: StreamWriter):
    host, port = writer.transport.get_extra_info('peername')
    logging.info(f'Connected to: {host}:{port}')

    factory = CommandFactory(reader, writer)

    while not writer.is_closing():
        line = (await reader.readline()).decode().strip()
        command = factory.get_command(line)
        await command.execute()

    logging.info(f'Disconnected from {host}:{port}')
Ejemplo n.º 32
0
class AsyncIOOWNConnection(object):
    SOCKET_MODES = ('*99*0##', '*99*1##', '*99*9##')
    MODE_COLORS = (COLOR_LT_BLUE, COLOR_LT_GREEN, COLOR_LT_CYAN)
    MODE_NAMES = ('CMD', 'MON', 'SCMD')

    ACK = '*#*1##'

    def __init__(self, host, port, passwd, queue, mode,
                 log=None, loop=None):
        self.host = host
        self.port = port
        self.passwd = passwd
        self.queue = queue
        self.mode = mode
        self.loop = loop
        self._auto_restart = True
        self._run = False
        if self.loop is None:
            self.loop = asyncio.get_event_loop()
        self.is_ready = asyncio.Event(loop=self.loop)
        if log is None:
            hdr = '[%s:%d %s]' % \
                (self.host, self.port, self.MODE_NAMES[self.mode])
            self.log = \
                get_logger(header=hdr,
                           color=self.MODE_COLORS[self.mode])
        else:
            self.log = log
        self.reader = None
        self.writer = None
        self.protocol = None
        self.transport = None

    @property
    def auto_restart(self):
        return self._auto_restart

    @auto_restart.setter
    def auto_restart(self, value):
        self._auto_restart = value

    async def run(self):
        self._run = True
        started = False
        # start with the login procedure

        # TODO: this is broken, it fails to restart properly

        ctr = 0
        while self._run:
            if self.protocol and not self.protocol.is_connected:
                # reset everything
                self.log('we have been disconnected - reset everything')
                self.reader = None
                self.writer = None
                self.protocol = None
                self.transport = None

            if not self.reader:
                self.log('create a StreamReader')
                self.reader = StreamReader(loop=self.loop)
            if not self.protocol:
                self.log('create OWNProtocol')
                self.protocol = OWNProtocol(self.reader, log=self.log)
            if not self.protocol.is_connected:
                if not self._auto_restart:
                    if started:
                        self.log("no autorestart... bailing out")
                        break
                started = True
                self.log('attempt connection')
                self.is_ready.clear()
                self.log('resetting the msg handler')
                self.msg_handler = self.state_start
                try:
                    self.log('create a new transport connection')
                    self.transport, _ = await self.loop.create_connection(
                        lambda: self.protocol, self.host, self.port)
                except gaierror:
                    self.log('sleep about 5 seconds')
                    await asyncio.sleep(5)
                else:
                    self.log('create a new StreamWriter')
                    self.writer = StreamWriter(
                        self.transport, self.protocol, self.reader, self.loop)
                    self.log('connection is up and running')
            try:
                pkt = await asyncio.wait_for(self.reader.readuntil(b'##'), timeout=1)
            except asyncio.TimeoutError:
                pass
            else:
                # from now on, packets are strings
                msg = pkt.decode('ascii')
                # self.log('packet (%d)=> %s' % (ctr, msg))
                self.log('<= %s' % (msg))
                await self.msg_handler(msg)
                ctr += 1
        self.transport.close()
        self.log('AsyncIOOWNConnection.run : %s the end' % (str(self)))

    def stop(self):
        self.log('stop requested', LOG_DEBUG)
        self._run = False

    async def send_packet(self, msg):
        self.log('=> %s' % (msg))
        if isinstance(msg, str):
            pkt = msg.encode('ascii')
            self.writer.write(pkt)
        await self.writer.drain()

    async def state_start(self, msg):
        if msg == self.ACK:
            self.msg_handler = self.state_login
            cmd_msg = self.SOCKET_MODES[self.mode]
            await self.send_packet(cmd_msg)
        else:
            self.log('we didn\'t get ACK')

    async def state_login(self, msg):
        if msg == '*98*2##':
            # this is a call for sha2 hmac authentication
            self.msg_handler = self.state_hmac_sha2
            await self.send_packet(self.ACK)
        else:
            # attempt matching the old password system
            ops_m = re.match(r'^\*#(\d+)##$', msg)
            if ops_m is not None:
                nonce = ops_m.groups()[0]
                # calculate the password
                passwd = ownCalcPass(self.passwd, nonce)
                passwd_msg = ('*#%s##' % (passwd))
                self.msg_handler = self.state_auth
                await self.send_packet(passwd_msg)
            else:
                self.log("unable to parse the openpassword nonce request")
                await asyncio.sleep(0)

    async def state_hmac_sha2(self, msg):
        ra_m = re.match(r'^\*#(\d{128})##$', msg)
        if ra_m is not None:
            ra = ra_m.group(1)
            hmac = ownCalcHmacSha2(self.passwd, ra)
            rb, hmac_client, self.hmac_server = hmac
            self.msg_handler = self.state_hmac_sha2_check_response
            hmac_packet = ('*#%s*%s##' % (rb, hmac_client))
            await self.send_packet(hmac_packet)
        else:
            self.log("unable to parse the hmac_sha2 request")
            await asyncio.sleep(0)

    async def state_hmac_sha2_check_response(self, msg):
        c_resp_m = re.match(r'^\*#(\d{128})##$', msg)
        if c_resp_m is not None:
                c_resp = c_resp_m.group(1)
                if c_resp == self.hmac_server:
                    self.is_ready.set()
                    self.msg_handler = self.state_dispatch
                    await self.send_packet(self.ACK)
                    
                else:
                    self.log("wrong response from server, expected %s", self.hmac_server)
                    await asyncio.sleep(0)
        else:
            self.log("unable to parse the hmac_sha2 request")
            await asyncio.sleep(0)

    async def state_auth(self, msg):
        if msg == self.ACK:
            self.is_ready.set()
            self.msg_handler = self.state_dispatch

    async def state_dispatch(self, msg):
        await self.queue.put((msg, self.mode, ))