如何在不中断流的情况下安全地从asyncio读取ReaderStream

时间:2018-03-05 08:45:34

标签: python tcp proxy python-asyncio

我正在尝试构建一个中间人代理服务器,将客户端请求中继到我的应用程序中预定义的各种代理服务。因此,为了做到这一点,我需要区分客户的服务请求 只要我不解析# data = await client_reader.read(2048)我可以解析标题,下面的代码就可以运行。即如果我执行该代码,
假设我是否使用未注释的代码行执行此操作:

r = requests.get('http://api.ipify.org/', headers = {'Proxy-Type':'custom'}, proxies={'http':'http://127.0.0.1:9911'}) 

我会在408 Request Time-out

中从ipify获得r.content
async def proxy_data(reader, writer, connection_string):

  try:
    while True:
      data = await reader.read(2048)
      if not data:
        break
      writer.write(data)
      await writer.drain()
  except Exception as e:
    raise
  finally:
    writer.close()

async def accept_client(client_reader, client_writer):

  try:
    # Get proxy service - [Proxy-Type] from header via client_reader
    # Set remote_address and remote_port based on it

    # data = await client_reader.read(2048)

    (remote_reader, remote_writer) = await asyncio.wait_for(
    asyncio.open_connection(host = remote_address, port = remote_port),
      timeout = 30)
  except asyncio.TimeoutError:
    client_writer.close()
  except Exception as e:
    client_writer.close()
  else:
    # Pipe the streams
    asyncio.ensure_future(proxy_data(client_reader, remote_writer))
    asyncio.ensure_future(proxy_data(remote_reader, client_writer))

def main():

  def handle_client(client_reader, client_writer):
    asyncio.ensure_future(
      accept_client(
        client_reader = client_reader,
        client_writer = client_writer
      )
    )


  loop = asyncio.get_event_loop()
  try:
    server = loop.run_until_complete(
      asyncio.start_server(
        handle_client, host = '127.0.0.1', port = 9911))
  except Exception as e:
    logger.error('Bind error: {}'.format(e))
    sys.exit(1)

  for s in server.sockets:
    logger.debug('Proxy broker listening on {}'.format(s.getsockname()))

  try:
    loop.run_forever()
  except KeyboardInterrupt:
    pass
if __name__ == '__main__':
  main()

有人可以在这里指出问题或如何有条件地打开连接吗?

1 个答案:

答案 0 :(得分:1)

通过重新提供client_reader(ReaderStream

来解决问题

感谢Vincent发表评论,指出请求篡改情况并将请求重新恢复为原生状态

async def accept_client(client_reader, client_writer):

  try:
    # Get proxy service - [Proxy-Type] from header via client_reader
    # Set remote_address and remote_port based on it

    data = await client_reader.read(2048)

    # -------- Edited --------

    # perform operations based on data and obtain remote_address, remote_port

    (remote_reader, remote_writer) = await asyncio.wait_for(
    asyncio.open_connection(host = remote_address, port = remote_port),
      timeout = 30)
  except asyncio.TimeoutError:
    client_writer.close()
  except Exception as e:
    client_writer.close()
  else:

    # Write the data to remote
    remote_writer.write(data)
    await remote_writer.drain()

    # Pipe the streams
    asyncio.ensure_future(proxy_data(client_reader, remote_writer))
    asyncio.ensure_future(proxy_data(remote_reader, client_writer))