From: Filip Š Date: Fri, 4 Oct 2019 19:38:08 +0000 (+0200) Subject: Support for DoH GET method X-Git-Tag: v2.0.0rc1~345^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04c8e6278630335a0a944f963aba923b87c07a72;p=thirdparty%2Fdnspython.git Support for DoH GET method --- diff --git a/dns/query.py b/dns/query.py index 598fa8bd..6c0f379d 100644 --- a/dns/query.py +++ b/dns/query.py @@ -27,6 +27,7 @@ import socket import struct import sys import time +import base64 import dns.exception import dns.inet @@ -191,13 +192,15 @@ def _destination_and_source(af, where, port, source, source_port): return (af, destination, source) -def doh(query, nameserver): +def doh(query, nameserver, post=True): """Return the response obtained after sending a query via DoH. *query*, a ``dns.message.Message`` containing the query to send *nameserver*, a ``str`` containing the nameserver URL. + *post*, a ``bool`` indicating whether POST method should be used. + Returns a ``dns.message.Message``. """ @@ -207,9 +210,13 @@ def doh(query, nameserver): 'Content-Type': 'application/dns-message', } - request = urllib.request.Request(nameserver, data=wirequery, headers=headers) - response = urllib.request.urlopen(request).read() + if post: + request = urllib.request.Request(nameserver, data=wirequery, headers=headers) + else: + wirequery = base64.urlsafe_b64encode(wirequery).decode('utf-8').strip('=') + request = urllib.request.Request(nameserver + '?dns=' + wirequery, headers=headers) + response = urllib.request.urlopen(request).read() return dns.message.from_wire(response) def send_udp(sock, what, destination, expiration=None): diff --git a/dns/query.pyi b/dns/query.pyi index ccd4b3fe..26012e5d 100644 --- a/dns/query.pyi +++ b/dns/query.pyi @@ -1,6 +1,6 @@ from typing import Optional, Union, Dict, Generator, Any from . import message, tsig, rdatatype, rdataclass, name, message -def doh(query : message.Message, nameserver : str) -> message.Message: +def doh(query : message.Message, nameserver : str, post=True) -> message.Message: pass def tcp(q : message.Message, where : str, timeout : float = None, port=53, af : Optional[int] = None, source : Optional[str] = None, source_port : int = 0,