-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns.py
More file actions
345 lines (331 loc) · 10.7 KB
/
dns.py
File metadata and controls
345 lines (331 loc) · 10.7 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"""
使用dnspython库工作,返回结果仅供参考。
"""
import re
import socket
import dns.resolver
def is_valid_domain(domain):
# 排除 IPv4 地址
ipv4_pattern = re.compile(
r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
)
if ipv4_pattern.match(domain):
return False
# 排除 IPv6 地址
ipv6_pattern = re.compile(
r"^(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}$|"
r"^(?:[A-Fa-f0-9]{1,4}:){1,7}:$|"
r"^(?:[A-Fa-f0-9]{1,4}:){1,6}:[A-Fa-f0-9]{1,4}$|"
r"^(?:[A-Fa-f0-9]{1,4}:){1,5}(?::[A-Fa-f0-9]{1,4}){1,2}$|"
r"^(?:[A-Fa-f0-9]{1,4}:){1,4}(?::[A-Fa-f0-9]{1,4}){1,3}$|"
r"^(?:[A-Fa-f0-9]{1,4}:){1,3}(?::[A-Fa-f0-9]{1,4}){1,4}$|"
r"^(?:[A-Fa-f0-9]{1,4}:){1,2}(?::[A-Fa-f0-9]{1,4}){1,5}$|"
r"^[A-Fa-f0-9]{1,4}:(?::[A-Fa-f0-9]{1,4}){1,6}$|"
r"^:(?::[A-Fa-f0-9]{1,4}){1,7}$"
)
if ipv6_pattern.match(domain):
return False
# 正则表达式验证域名格式
domain_pattern = re.compile(
r"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z0-9-]{1,63})*(?<!-)$"
)
# 检查域名长度
if len(domain) > 253:
return False
# 检查域名格式
return bool(domain_pattern.match(domain))
def clean_domain(domain):
# 去除域名前的 "https://" 或 "http://"
if domain.startswith("https://"):
domain = domain[8:]
elif domain.startswith("http://"):
domain = domain[7:]
# 去除域名后的 "/" 如果有的话
domain = domain.rstrip('/')
# 检查是否为有效域名
if not is_valid_domain(domain):
raise ValueError(f"Invalid domain: {domain}")
return domain
def query_dns_record(domain, record_type, tcp=False): # 也可以直接调用 query_dns_record 查询
domain = clean_domain(domain) # 清理域名
try:
back_value = []
answers = dns.resolver.resolve(domain, record_type, tcp=tcp)
print("Non-authoritative answers")
for rdata in answers:
if record_type == "A" or record_type == "AAAA":
back_value.append(rdata.address)
elif record_type == "CNAME":
back_value.append(rdata.target)
elif record_type == "NS" or record_type == "MX":
back_value.append(str(rdata))
return back_value
except dns.resolver.NoAnswer:
return "NoAnswer"
except dns.resolver.NXDOMAIN:
return "NotFoundNS"
except Exception as e:
return False
def whois_query(domain):
try:
# 尝试找到Whois服务器,先从最长的子域开始查询
whois_server = get_whois_server(domain)
# 连接到Whois服务器
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((whois_server, 43))
s.sendall((domain + "\r\n").encode('ascii'))
# 接收Whois服务器的响应
response = b""
while True:
data = s.recv(4096)
if not data:
break
response += data
return response.decode('utf-8')
except Exception as e:
return f"查询失败: {e}"
def get_whois_server(domain):
# 将域名分割成各个部分
parts = domain.split('.')
# 从最长的子域开始查找Whois服务器
for i in range(len(parts)):
# 组合成查询的域名部分
tld = '.'.join(parts[i:])
# 查找Whois服务器
whois_server = whois_servers.get(tld)
if whois_server:
return whois_server
# 如果没有找到特定的Whois服务器,则使用通用的Whois服务器
return "whois.iana.org"
whois_servers = {
"ac": "whois.nic.ac",
"ad": "whois.ripe.net",
"ae": "whois.aeda.net.ae",
"aero": "whois.aero",
"af": "whois.nic.af",
"ag": "whois.nic.ag",
"ai": "whois.ai",
"al": "whois.ripe.net",
"am": "whois.amnic.net",
"as": "whois.nic.as",
"asia": "whois.nic.asia",
"at": "whois.nic.at",
"au": "whois.aunic.net",
"aw": "whois.nic.aw",
"ax": "whois.ax",
"az": "whois.ripe.net",
"ba": "whois.ripe.net",
"bar": "whois.nic.bar",
"be": "whois.dns.be",
"berlin": "whois.nic.berlin",
"best": "whois.nic.best",
"bg": "whois.register.bg",
"bi": "whois.nic.bi",
"biz": "whois.neulevel.biz",
"bj": "www.nic.bj",
"bo": "whois.nic.bo",
"br": "whois.nic.br",
"br.com": "whois.centralnic.com",
"bt": "whois.netnames.net",
"bw": "whois.nic.net.bw",
"by": "whois.cctld.by",
"bz": "whois.belizenic.bz",
"bzh": "whois-bzh.nic.fr",
"ca": "whois.cira.ca",
"cat": "whois.cat",
"cc": "whois.nic.cc",
"cd": "whois.nic.cd",
"ceo": "whois.nic.ceo",
"cf": "whois.dot.cf",
"ch": "whois.nic.ch",
"ci": "whois.nic.ci",
"ck": "whois.nic.ck",
"cl": "whois.nic.cl",
"cloud": "whois.nic.cloud",
"club": "whois.nic.club",
"cn": "whois.cnnic.net.cn",
"cn.com": "whois.centralnic.com",
"co": "whois.nic.co",
"co.nl": "whois.co.nl",
"com": "whois.verisign-grs.com",
"coop": "whois.nic.coop",
"cx": "whois.nic.cx",
"cy": "whois.ripe.net",
"cz": "whois.nic.cz",
"de": "whois.denic.de",
"dk": "whois.dk-hostmaster.dk",
"dm": "whois.nic.cx",
"dz": "whois.nic.dz",
"ec": "whois.nic.ec",
"edu": "whois.educause.net",
"ee": "whois.tld.ee",
"eg": "whois.ripe.net",
"es": "whois.nic.es",
"eu": "whois.eu",
"eu.com": "whois.centralnic.com",
"eus": "whois.nic.eus",
"fi": "whois.fi",
"fo": "whois.nic.fo",
"fr": "whois.nic.fr",
"gb": "whois.ripe.net",
"gb.com": "whois.centralnic.com",
"gb.net": "whois.centralnic.com",
"qc.com": "whois.centralnic.com",
"ge": "whois.ripe.net",
"gg": "whois.gg",
"gi": "whois2.afilias-grs.net",
"gl": "whois.nic.gl",
"gm": "whois.ripe.net",
"gov": "whois.nic.gov",
"gr": "whois.ripe.net",
"gs": "whois.nic.gs",
"gy": "whois.registry.gy",
"hamburg": "whois.nic.hamburg",
"hiphop": "whois.uniregistry.net",
"hk": "whois.hknic.net.hk",
"hm": "whois.registry.hm",
"hn": "whois2.afilias-grs.net",
"host": "whois.nic.host",
"hr": "whois.dns.hr",
"ht": "whois.nic.ht",
"hu": "whois.nic.hu",
"hu.com": "whois.centralnic.com",
"id": "whois.pandi.or.id",
"ie": "whois.domainregistry.ie",
"il": "whois.isoc.org.il",
"im": "whois.nic.im",
"in": "whois.inregistry.net",
"info": "whois.afilias.info",
"ing": "domain-registry-whois.l.google.com",
"ink": "whois.centralnic.com",
"int": "whois.isi.edu",
"io": "whois.nic.io",
"iq": "whois.cmc.iq",
"ir": "whois.nic.ir",
"is": "whois.isnic.is",
"it": "whois.nic.it",
"je": "whois.je",
"jobs": "jobswhois.verisign-grs.com",
"jp": "whois.jprs.jp",
"ke": "whois.kenic.or.ke",
"kg": "whois.domain.kg",
"ki": "whois.nic.ki",
"kr": "whois.kr",
"kz": "whois.nic.kz",
"la": "whois2.afilias-grs.net",
"li": "whois.nic.li",
"london": "whois.nic.london",
"lt": "whois.domreg.lt",
"lu": "whois.restena.lu",
"lv": "whois.nic.lv",
"ly": "whois.lydomains.com",
"ma": "whois.iam.net.ma",
"mc": "whois.ripe.net",
"md": "whois.nic.md",
"me": "whois.nic.me",
"mg": "whois.nic.mg",
"mil": "whois.nic.mil",
"mk": "whois.ripe.net",
"ml": "whois.dot.ml",
"mo": "whois.monic.mo",
"mobi": "whois.dotmobiregistry.net",
"ms": "whois.nic.ms",
"mt": "whois.ripe.net",
"mu": "whois.nic.mu",
"museum": "whois.museum",
"mx": "whois.nic.mx",
"my": "whois.mynic.net.my",
"mz": "whois.nic.mz",
"na": "whois.na-nic.com.na",
"name": "whois.nic.name",
"nc": "whois.nc",
"net": "whois.verisign-grs.com",
"nf": "whois.nic.cx",
"ng": "whois.nic.net.ng",
"nl": "whois.domain-registry.nl",
"no": "whois.norid.no",
"no.com": "whois.centralnic.com",
"nu": "whois.nic.nu",
"nz": "whois.srs.net.nz",
"om": "whois.registry.om",
"ong": "whois.publicinterestregistry.net",
"ooo": "whois.nic.ooo",
"org": "whois.pir.org",
"paris": "whois-paris.nic.fr",
"pe": "kero.yachay.pe",
"pf": "whois.registry.pf",
"pics": "whois.uniregistry.net",
"pl": "whois.dns.pl",
"pm": "whois.nic.pm",
"pr": "whois.nic.pr",
"press": "whois.nic.press",
"pro": "whois.registrypro.pro",
"pt": "whois.dns.pt",
"pub": "whois.unitedtld.com",
"pw": "whois.nic.pw",
"qa": "whois.registry.qa",
"re": "whois.nic.re",
"ro": "whois.rotld.ro",
"rs": "whois.rnids.rs",
"ru": "whois.tcinet.ru",
"sa": "saudinic.net.sa",
"sa.com": "whois.centralnic.com",
"sb": "whois.nic.net.sb",
"sc": "whois2.afilias-grs.net",
"se": "whois.nic-se.se",
"se.com": "whois.centralnic.com",
"se.net": "whois.centralnic.com",
"sg": "whois.nic.net.sg",
"sh": "whois.nic.sh",
"si": "whois.arnes.si",
"sk": "whois.sk-nic.sk",
"sm": "whois.nic.sm",
"st": "whois.nic.st",
"so": "whois.nic.so",
"su": "whois.tcinet.ru",
"sx": "whois.sx",
"sy": "whois.tld.sy",
"tc": "whois.adamsnames.tc",
"tel": "whois.nic.tel",
"tf": "whois.nic.tf",
"th": "whois.thnic.net",
"tj": "whois.nic.tj",
"tk": "whois.nic.tk",
"tl": "whois.domains.tl",
"tm": "whois.nic.tm",
"tn": "whois.ati.tn",
"to": "whois.tonic.to",
"top": "whois.nic.top",
"tp": "whois.domains.tl",
"tr": "whois.nic.tr",
"travel": "whois.nic.travel",
"tw": "whois.twnic.net.tw",
"tv": "whois.nic.tv",
"tz": "whois.tznic.or.tz",
"ua": "whois.ua",
"ug": "whois.co.ug",
"uk": "whois.nic.uk",
"uk.com": "whois.centralnic.com",
"uk.net": "whois.centralnic.com",
"ac.uk": "whois.ja.net",
"gov.uk": "whois.ja.net",
"us": "whois.nic.us",
"us.com": "whois.centralnic.com",
"uy": "nic.uy",
"uy.com": "whois.centralnic.com",
#"us.kg":"whois.us.kg", #不知道什么原因,无法连接服务器,查不了
"uz": "whois.cctld.uz",
"va": "whois.ripe.net",
"vc": "whois2.afilias-grs.net",
"ve": "whois.nic.ve",
"vg": "ccwhois.ksregistry.net",
"vu": "vunic.vu",
"wang": "whois.nic.wang",
"wf": "whois.nic.wf",
"wiki": "whois.nic.wiki",
"ws": "whois.website.ws",
"xxx": "whois.nic.xxx",
"xyz": "whois.nic.xyz",
"yu": "whois.ripe.net",
"za.com": "whois.centralnic.com"
}