Example #1
0
def get_app(args):
    logger = utils.configure_logger('doh-httpproxy', args.level)
    app = DOHApplication(logger=logger, debug=args.debug)
    app.set_upstream_resolver(args.upstream_resolver, args.upstream_port)
    app.router.add_get(args.uri, doh1handler)
    app.router.add_post(args.uri, doh1handler)
    return app
Example #2
0
 def __init__(self, upstream_resolver, upstream_port, logger=None):
     self.loop = asyncio.get_event_loop()
     self.upstream_resolver = upstream_resolver
     self.upstream_port = upstream_port
     if logger is None:
         logger = utils.configure_logger('DNSClient', 'DEBUG')
     self.logger = logger
Example #3
0
 def __init__(self, dnsq, queue, logger=None):
     self.dnsq = dnsq
     self.queue = queue
     self.transport = None
     self.logger = logger
     if logger is None:
         self.logger = utils.configure_logger('DNSClientProtocol', 'DEBUG')
Example #4
0
 def __init__(self, args, logger=None):
     self.logger = logger
     self.args = args
     self._lock = asyncio.Lock()
     if logger is None:
         self.logger = utils.configure_logger('StubServerProtocol')
     self.client = None
Example #5
0
def main():
    args = parse_args()
    logger = utils.configure_logger('doh-proxy', args.level)
    ssl_ctx = ssl_context(args)
    loop = asyncio.get_event_loop()
    for addr in args.listen_address:
        coro = loop.create_server(
            lambda: H2Protocol(upstream_resolver=args.upstream_resolver,
                               upstream_port=args.upstream_port,
                               uri=args.uri,
                               logger=logger,
                               debug=args.debug),
            host=addr,
            port=args.port,
            ssl=ssl_ctx)
        server = loop.run_until_complete(coro)

        # Serve requests until Ctrl+C is pressed
        logger.info('Serving on {}'.format(server))
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass

    # Close the server
    server.close()
    loop.run_until_complete(server.wait_closed())
    loop.close()
Example #6
0
 async def _test_servers(self, post=False):
     for name, domain, uri, methods in known_servers():
         if post and not methods & METHOD_POST:
             continue
         if not post and not methods & METHOD_GET:
             continue
         with self.subTest(name):
             arglist = [
                 "--domain",
                 domain,
                 "--uri",
                 uri,
             ]
             if post:
                 arglist.append("--post")
             parser = utils.client_parser_base()
             args = parser.parse_args(arglist)
             logger = utils.configure_logger("doh-integrationtest")
             c = Client(args=args, logger=logger)
             fut = c.make_request(None, build_query(qname=domain, qtype="A"))
             try:
                 await asyncio.wait_for(fut, self.test_timeout)
             except asyncio.TimeoutError:
                 raise unittest.SkipTest("%s Timeouted" % name)
             self.assertEqual(1, len(c.result.question))
             self.assertGreater(len(c.result.answer), 0)
Example #7
0
 def __init__(self, dnsq, fut, clientip, logger=None):
     self.transport = None
     self.dnsq = dnsq
     self.fut = fut
     self.clientip = clientip
     if logger is None:
         logger = utils.configure_logger('DNSClientProtocol', 'DEBUG')
     self.logger = logger
Example #8
0
def main():
    args = parse_args()

    logger = utils.configure_logger('doh-httpproxy', args.level)
    app = DOHApplication(logger=logger, debug=args.debug)
    app.set_upstream_resolver(args.upstream_resolver)
    app.router.add_get(args.uri, doh1handler)
    aiohttp.web.run_app(app, host=args.listen_address, port=args.port)
Example #9
0
 def __init__(self, upstream_resolver=None, uri=None, logger=None):
     config = H2Configuration(client_side=False, header_encoding='utf-8')
     self.conn = H2Connection(config=config)
     self.logger = logger
     if logger is None:
         self.logger = utils.configure_logger('doh-proxy', 'DEBUG')
     self.transport = None
     self.stream_data = {}
     self.upstream_resolver = upstream_resolver
     self.uri = constants.DOH_URI if uri is None else uri
     assert upstream_resolver is not None, \
         'An upstream resolver must be provided'
    def __init__(self, args, logger=None, client_store=None):
        self.logger = logger
        self.args = args
        self._lock = asyncio.Lock()
        if logger is None:
            self.logger = utils.configure_logger('StubServerProtocol')

        # The client is wrapped in a mutable dictionary, so it may be shared
        # across multiple contexts if passed from higher in the chain.
        if client_store is None:
            self.client_store = {'client': None}
        else:
            self.client_store = client_store
Example #11
0
def get_app(args):
    logger = utils.configure_logger("doh-httpproxy", args.level)
    app = DOHApplication(logger=logger, debug=args.debug)
    app.set_upstream_resolver(args.upstream_resolver, args.upstream_port)
    app.set_ecs(args.ecs)
    app.router.add_get(args.uri, doh1handler)
    app.router.add_post(args.uri, doh1handler)

    # Get trusted reverse proxies and format it for aiohttp_remotes setup
    if len(args.trusted) == 0:
        x_forwarded_handling = aiohttp_remotes.XForwardedRelaxed()
    else:
        x_forwarded_handling = aiohttp_remotes.XForwardedStrict([args.trusted])

    asyncio.ensure_future(aiohttp_remotes.setup(app, x_forwarded_handling))
    return app
Example #12
0
def main():
    args = parse_args()
    logger = utils.configure_logger('doh-stub', args.level)
    loop = asyncio.get_event_loop()
    logger.info("Starting UDP server")
    # One protocol instance will be created to serve all client requests
    listen = loop.create_datagram_endpoint(
        lambda: protocol.StubServerProtocol(args, logger=logger),
        local_addr=(args.listen_address, args.listen_port))
    transport, proto = loop.run_until_complete(listen)
    loop.run_until_complete(proto.setup_client())

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass

    transport.close()
    loop.close()
Example #13
0
def main():
    args = parse_args()
    logger = utils.configure_logger("doh-stub", args.level)
    loop = asyncio.get_event_loop()

    if "all" in args.listen_address:
        listen_addresses = utils.get_system_addresses()
    else:
        listen_addresses = args.listen_address

    transports = []
    for address in listen_addresses:
        logger.info("Starting UDP server: {}".format(address))
        # One protocol instance will be created to serve all client requests
        # for this UDP listen address
        cls = client_protocol.StubServerProtocolUDP
        listen = loop.create_datagram_endpoint(
            lambda: cls(args, logger=logger, client_store=CLIENT_STORE),
            local_addr=(address, args.listen_port),
        )
        transport, proto = loop.run_until_complete(listen)
        transports.append(transport)
        loop.run_until_complete(proto.get_client())

        logger.info("Starting TCP server: {}".format(address))
        cls = client_protocol.StubServerProtocolTCP
        listen_tcp = loop.create_server(
            lambda: cls(args, logger=logger, client_store=CLIENT_STORE),
            host=address,
            port=args.listen_port,
        )
        server_tcp = loop.run_until_complete(listen_tcp)
        transports.append(server_tcp)

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass

    for transport in transports:
        transport.close()
    loop.close()
Example #14
0
    async def test_mock_dnsclient_assigned_logger(self, MockedDNSClient,
                                                  Mockedon_answer,
                                                  Mockedquery):
        """ Test that when MockedDNSClient is created with the doh-httpproxy
        logger and DEBUG level
        """
        Mockedquery.return_value = self.dnsq
        Mockedon_answer.return_value = aiohttp.web.Response(status=200,
                                                            body=b'Done')
        params = utils.build_query_params(self.dnsq.to_wire())
        request = await self.client.request('GET',
                                            self.endpoint,
                                            params=params)
        request.remote = "127.0.0.1"
        app = await self.get_application()
        await app.resolve(request, self.dnsq)

        mylogger = utils.configure_logger(name='doh-httpproxy', level='DEBUG')
        MockedDNSClient.assert_called_with(app.upstream_resolver,
                                           app.upstream_port,
                                           logger=mylogger)
Example #15
0
def main():
    args = parse_args()
    logger = utils.configure_logger("doh-proxy", args.level)
    ssl_ctx = utils.create_ssl_context(args, http2=True)
    loop = asyncio.get_event_loop()
    if "all" in args.listen_address:
        listen_addresses = utils.get_system_addresses()
    else:
        listen_addresses = args.listen_address
    for addr in listen_addresses:
        coro = loop.create_server(
            lambda: H2Protocol(
                upstream_resolver=args.upstream_resolver,
                upstream_port=args.upstream_port,
                uri=args.uri,
                logger=logger,
                debug=args.debug,
                ecs=args.ecs,
            ),
            host=addr,
            port=args.port,
            ssl=ssl_ctx,
        )
        server = loop.run_until_complete(coro)

        # Serve requests until Ctrl+C is pressed
        logger.info("Serving on {}".format(server))
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass

    # Close the server
    server.close()
    loop.run_until_complete(server.wait_closed())
    loop.close()
Example #16
0
 def __init__(
     self,
     upstream_resolver=None,
     upstream_port=None,
     uri=None,
     logger=None,
     debug=False,
     ecs=False,
 ):
     config = H2Configuration(client_side=False, header_encoding="utf-8")
     self.conn = H2Connection(config=config)
     self.logger = logger
     if logger is None:
         self.logger = utils.configure_logger("doh-proxy", "DEBUG")
     self.transport = None
     self.debug = debug
     self.ecs = ecs
     self.stream_data = {}
     self.upstream_resolver = upstream_resolver
     self.upstream_port = upstream_port
     self.time_stamp = 0
     self.uri = constants.DOH_URI if uri is None else uri
     assert upstream_resolver is not None, "An upstream resolver must be provided"
     assert upstream_port is not None, "An upstream resolver port must be provided"
Example #17
0
 def test_configure_logger_unknown_level(self):
     """ Basic test to check that there is no stupid typos.
     """
     with self.assertRaises(Exception):
         utils.configure_logger(level='thisisnotalevel')
Example #18
0
 def test_configure_logger(self):
     """ Basic test to check that there is no stupid typos.
     """
     utils.configure_logger()
Example #19
0
def main_sync(args):
    logger = utils.configure_logger('doh-client', level=args.level)
    client = Client(args=args, logger=logger)

    loop = asyncio.get_event_loop()
    loop.run_until_complete(client.make_request(None, build_query(args)))
Example #20
0
 def setUp(self):
     self.logger = utils.configure_logger('doh-integration',
                                          level=self.ARGS.level)
     self.logger.propagate = self.ARGS.propagate_logs
     self.client = Client(args=self.ARGS, logger=self.logger)