-
Notifications
You must be signed in to change notification settings - Fork 5
/
doh.py
executable file
·135 lines (111 loc) · 3.69 KB
/
doh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
#
# Dig Over Http/s (DoH)
# https://github.com/opendns/doh-client
#
#
import time, sys
import ssl
import dns.message
from urllib import request
DEFAULT_SERVER = "https://doh.opendns.com"
def usage():
print("Usage: doh [@[http[s]://]server[:port]] [TYPE] [+nosslverify] domain")
def print_packet(dnsr):
print(";; ->>HEADER<<- opcode: {}, status: {}, id: {}".format(dns.opcode.to_text(dnsr.opcode()), dns.rcode.to_text(dnsr.rcode()), dnsr.id))
print(";; flags: {}".format(dns.flags.to_text(dnsr.flags)))
print()
is_update = dns.opcode.is_update(dnsr.flags)
print(";; ZONE:") if is_update else print(";; QUESTION:")
for rrset in dnsr.question:
print(rrset.to_text())
print()
print(";; PREREQ:") if is_update else print(";; ANSWER:")
for rrset in dnsr.answer:
print(rrset.to_text())
print()
if dnsr.authority and len(dnsr.authority) > 0:
print(";; UPDATE:") if is_update else print(";; AUTHORITY:")
for rrset in dnsr.authority:
print(rrset.to_text())
print()
if dnsr.additional and len(dnsr.additional) > 0:
print(";; ADDITIONAL:")
for rrset in dnsr.additional:
print(rrset.to_text())
print()
def build_dns_query(domain, record_type):
dnsq = dns.message.make_query(
qname=domain,
rdtype=record_type,
want_dnssec=False,
)
return dnsq
class NoRedirect(request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
print("Redirect:", headers["location"])
return None
def main(argv):
record_name = ""
record_type = "A"
server = DEFAULT_SERVER
ssl_ctx = None
for arg in argv[1:]:
if arg == "-h" or arg == "--help":
usage()
return 0
if arg.startswith("@"):
server=arg[1:]
continue
if arg.startswith("+"):
if arg == "+nosslverify" and ssl_ctx == None:
ssl_ctx = ssl.create_default_context()
ssl_ctx.check_hostname = False
ssl_ctx.verify_mode = ssl.CERT_NONE
continue
try:
if dns.rdatatype.from_text(arg.upper()):
record_type = arg.upper()
continue
except:
pass
if record_name == "":
record_name = arg
continue
print("Invalid argument:", arg)
return 1
if record_name == "":
usage()
return 0
print("; <<>> DoH Client 0.1 <<>> " + " ".join(argv[1:]))
# verify server name
if "://" in server:
if server.startswith("http://") or server.startswith("https://"):
pass
else:
print("Invalid protocol in server name")
return 1
else:
server = "https://" + server
time_start = time.time()
try:
dnsq = build_dns_query(record_name, record_type)
opener = request.build_opener(NoRedirect)
request.install_opener(opener)
req = request.Request(server + "/dns-query", data=dnsq.to_wire())
req.add_header('accept', 'application/dns-message')
req.add_header('content-type', 'application/dns-message')
dnsr = dns.message.from_wire(request.urlopen(req, context=ssl_ctx).read())
except Exception as e:
sys.stderr.write("Error")
sys.stderr.write(str(e))
sys.stderr.write("\n")
return 2
time_end = time.time()
print(";; Got answer:")
print_packet(dnsr)
print(";; Server: {}".format(server))
print(";; Query time: {} msec".format(int((time_end - time_start) * 1000)))
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))