Esempio n. 1
0
async def doh1handler(request):
    path, params = utils.extract_path_params(request.rel_url.path_qs)

    if request.method in ["GET", "HEAD"]:
        try:
            ct, body = utils.extract_ct_body(params)
        except DOHParamsException as e:
            return aiohttp.web.Response(status=400, body=e.body())
    elif request.method == "POST":
        body = await request.content.read()
        ct = request.headers.get("content-type")
    else:
        return aiohttp.web.Response(status=501, body=b"Not Implemented")
    if ct != constants.DOH_MEDIA_TYPE:
        return aiohttp.web.Response(status=415,
                                    body=b"Unsupported content type")

    # Do actual DNS Query
    try:
        dnsq = utils.dns_query_from_body(body, debug=request.app.debug)
    except DOHDNSException as e:
        return aiohttp.web.Response(status=400, body=e.body())

    clientip = utils.get_client_ip(request.transport)
    request.app.logger.info("[HTTPS] {} (Original IP: {}) {}".format(
        clientip, request.remote, utils.dnsquery2log(dnsq)))
    return await request.app.resolve(request, dnsq)
Esempio n. 2
0
async def doh1handler(request):
    path, params = utils.extract_path_params(request.rel_url.path_qs)

    if request.method == 'GET':
        try:
            ct, body = utils.extract_ct_body(params)
        except DOHParamsException as e:
            return aiohttp.web.Response(status=400, body=e.body())
    else:
        body = request.content.read()
        ct = request.headers.get('content-type')

    if ct != constants.DOH_MEDIA_TYPE:
        return aiohttp.web.Response(
            status=415, body=b'Unsupported content type'
        )

    # Do actual DNS Query
    try:
        dnsq = utils.dns_query_from_body(body, debug=request.app.debug)
    except DOHDNSException as e:
        return aiohttp.web.Response(status=400, body=e.body())

    dnsq = dns.message.from_wire(body)
    request.app.logger.info(
        '[HTTPS] Received: ID {} Question {} Peer {}'.format(
            dnsq.id,
            dnsq.question[0],
            request.transport.get_extra_info('peername'),
        )
    )
    return await request.app.resolve(request, dnsq)
Esempio n. 3
0
async def doh1handler(request):
    path, params = utils.extract_path_params(request.rel_url.path_qs)

    if request.method in ['GET', 'HEAD']:
        try:
            ct, body = utils.extract_ct_body(params)
        except DOHParamsException as e:
            return aiohttp.web.Response(status=400, body=e.body())
    elif request.method == 'POST':
        body = await request.content.read()
        ct = request.headers.get('content-type')
    else:
        return aiohttp.web.Response(status=501, body=b'Not Implemented')
    if ct != constants.DOH_MEDIA_TYPE:
        return aiohttp.web.Response(
            status=415, body=b'Unsupported content type')

    # Do actual DNS Query
    try:
        dnsq = utils.dns_query_from_body(body, debug=request.app.debug)
    except DOHDNSException as e:
        return aiohttp.web.Response(status=400, body=e.body())

    dnsq = dns.message.from_wire(body)

    clientip = request.transport.get_extra_info('peername')[0]
    request.app.logger.info('[HTTPS] {} (Original IP: {}) {}'.format(
        clientip, request.remote, utils.dnsquery2log(dnsq)))
    return await request.app.resolve(request, dnsq)
Esempio n. 4
0
    def stream_complete(self, stream_id: int):
        """
        When a stream is complete, we can send our response.
        """
        try:
            request_data = self.stream_data[stream_id]
        except KeyError:
            # Just return, we probably 405'd this already
            return

        headers = request_data.headers
        method = request_data.headers[':method']

        # Handle the actual query
        path, params = utils.extract_path_params(headers[':path'])

        if path != self.uri:
            self.return_404(stream_id)
            return

        if method in ['GET', 'HEAD']:
            try:
                ct, body = utils.extract_ct_body(params)
            except DOHParamsException as e:
                self.return_400(stream_id, body=e.body())
                return
        elif method == 'POST':
            body = request_data.data.getvalue()
            ct = headers.get('content-type')
        else:
            self.return_501(stream_id)
            return

        if ct != constants.DOH_MEDIA_TYPE:
            self.return_415(stream_id)
            return

        # Do actual DNS Query
        try:
            dnsq = utils.dns_query_from_body(body, self.debug)
        except DOHDNSException as e:
            self.return_400(stream_id, body=e.body())
            return

        clientip = self.transport.get_extra_info('peername')[0]
        self.logger.info(
            '[HTTPS] {} {}'.format(
                clientip,
                utils.dnsquery2log(dnsq)
            )
        )
        self.time_stamp = time.time()
        asyncio.ensure_future(self.resolve(dnsq, stream_id))
Esempio n. 5
0
 def test_valid_message(self):
     dnsq = dns.message.Message()
     body = dnsq.to_wire()
     self.assertEqual(utils.dns_query_from_body(body), dnsq)
Esempio n. 6
0
 def test_invalid_message_with_debug(self):
     body = 'a'
     with self.assertRaisesRegex(server_protocol.DOHDNSException,
                                 'is too short'):
         utils.dns_query_from_body(body, debug=True)
Esempio n. 7
0
 def test_invalid_message_no_debug(self):
     body = 'a'
     with self.assertRaisesRegex(server_protocol.DOHDNSException,
                                 'Malformed DNS query'):
         utils.dns_query_from_body(body)