-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwhois_mng58tlr.py
More file actions
345 lines (280 loc) · 9.76 KB
/
Copy pathwhois_mng58tlr.py
File metadata and controls
345 lines (280 loc) · 9.76 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
import socket
import re
from datetime import datetime, timedelta, timezone
def get_tld(domain):
parts = domain.strip().lower().split(".")
if len(parts) < 2:
return ""
return parts[-1]
def normalize_domain(domain):
domain = domain.strip()
if not domain:
return ""
if "://" in domain:
domain = domain.split("://", 1)[1]
domain = domain.split("/", 1)[0]
domain = domain.split("@", 1)[-1]
if domain.startswith("[") and "]" in domain:
return ""
if ":" in domain:
host, port = domain.rsplit(":", 1)
if re.fullmatch(r"\d+", port):
domain = host
else:
return ""
domain = domain.strip().strip(".").lower()
if not domain:
return ""
try:
domain = domain.encode("idna").decode("ascii")
except UnicodeError:
return ""
if "." not in domain or ".." in domain or len(domain) > 253:
return ""
labels = domain.split(".")
for label in labels:
if not label or len(label) > 63:
return ""
if label.startswith("-") or label.endswith("-"):
return ""
if not re.fullmatch(r"[a-z0-9-]+", label):
return ""
if re.fullmatch(r"\d+", label):
return ""
if re.fullmatch(r"\d{1,3}(?:\.\d{1,3}){3}", domain):
return ""
return domain
def query_whois_server(server, domain):
with socket.create_connection((server, 43), timeout=10) as sock:
sock.sendall((domain + "\r\n").encode("utf-8"))
chunks = []
while True:
data = sock.recv(4096)
if not data:
break
chunks.append(data)
return b"".join(chunks).decode("utf-8", errors="replace")
def find_whois_server(domain):
iana_response = query_whois_server("whois.iana.org", domain)
refer_server = ""
for line in iana_response.splitlines():
line = line.strip()
if not line or ":" not in line:
continue
key, value = line.split(":", 1)
key = key.strip().lower()
value = value.strip()
if key == "whois" and value:
return value
if key == "refer" and value:
refer_server = value
return refer_server
def parse_whois_text(text):
data = {}
dns_list = []
status_list = []
field_map = {
"domain name": "domain",
"domain": "domain",
"registrar": "registrar",
"sponsoring registrar": "registrar",
"registrar organization": "registrar",
"creation date": "creation_date",
"created": "creation_date",
"created on": "creation_date",
"registered on": "creation_date",
"registration time": "creation_date",
"registration date": "creation_date",
"registry expiry date": "expiry_date",
"expiration date": "expiry_date",
"expiry date": "expiry_date",
"expire": "expiry_date",
"expires on": "expiry_date",
"expiration time": "expiry_date",
"paid-till": "expiry_date",
"renewal date": "expiry_date",
"updated date": "updated_date",
"last updated on": "updated_date",
"last modified": "updated_date",
"changed": "updated_date",
"updated date time": "updated_date",
"modification time": "updated_date",
"registrar iana id": "iana_id",
"iana id": "iana_id",
"iana_id": "iana_id",
}
status_keys = {"status", "domain status"}
dns_keys = {"name server", "nserver", "nserver1", "nserver2", "nserver3", "nserver4"}
for raw_line in text.splitlines():
line = raw_line.strip()
if not line or ":" not in line:
continue
key, value = line.split(":", 1)
key = key.strip().lower()
value = value.strip()
if not value:
continue
mapped_key = field_map.get(key)
if mapped_key:
if mapped_key == "domain":
data[mapped_key] = value.lower()
elif mapped_key not in data:
data[mapped_key] = value
continue
if key in status_keys:
status_value = value.split()[0].strip().lower()
if status_value and status_value not in status_list:
status_list.append(status_value)
continue
if key in dns_keys:
dns_value = value.split()[0].strip(".").lower()
if dns_value and dns_value not in dns_list:
dns_list.append(dns_value)
continue
if key == "dnssec" and "dnssec" not in data:
data["dnssec"] = value
data["status_list"] = status_list
data["dns_list"] = dns_list
return data
def parse_datetime(value):
value = value.strip()
if not value:
return None
iso_value = value.replace("Z", "+00:00")
try:
return datetime.fromisoformat(iso_value)
except ValueError:
pass
iso_value = re.sub(r"([+-]\d{2})(\d{2})$", r"\1:\2", iso_value)
try:
return datetime.fromisoformat(iso_value)
except ValueError:
pass
normalized_value = re.sub(r"\s+UTC$", " +0000", value, flags=re.IGNORECASE)
normalized_value = re.sub(r"\s+GMT$", " +0000", normalized_value, flags=re.IGNORECASE)
patterns = [
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%d",
"%d-%b-%Y",
"%d-%b-%Y %H:%M:%S",
"%Y.%m.%d %H:%M:%S",
"%Y/%m/%d %H:%M:%S",
"%Y/%m/%d",
"%Y.%m.%d",
"%Y-%m-%d %H:%M:%S %z",
"%Y.%m.%d %H:%M:%S %z",
"%Y-%m-%dT%H:%M:%S.%f%z",
"%Y-%m-%dT%H:%M:%S%z",
"%a %b %d %H:%M:%S %Y",
]
for pattern in patterns:
try:
return datetime.strptime(normalized_value, pattern)
except ValueError:
pass
return None
def format_datetime_to_utc8(value):
dt = parse_datetime(value)
if not dt:
return value
utc8 = timezone(timedelta(hours=8))
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
dt = dt.astimezone(utc8)
return dt.strftime("%Y-%m-%d %H:%M:%S UTC+8")
def translate_status(status):
status_map = {
"ok": "正常",
"inactive": "未激活",
"active": "已激活",
"clientdeleteprohibited": "客户端禁止删除",
"clienthold": "客户端暂停解析",
"clientrenewprohibited": "客户端禁止续费",
"clienttransferprohibited": "客户端禁止转移",
"clientupdateprohibited": "客户端禁止更新",
"pendingcreate": "创建处理中",
"pendingdelete": "删除处理中",
"pendingrenew": "续费处理中",
"pendingrestore": "恢复处理中",
"pendingtransfer": "转移处理中",
"pendingupdate": "更新处理中",
"serverdeleteprohibited": "注册局禁止删除",
"serverhold": "注册局暂停解析",
"serverrenewprohibited": "注册局禁止续费",
"servertransferprohibited": "注册局禁止转移",
"serverupdateprohibited": "注册局禁止更新",
}
return status_map.get(status.strip().lower(), status)
def get_domain_age_text(creation_date_str):
dt = parse_datetime(creation_date_str)
if not dt:
return ""
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
now = datetime.now(timezone.utc)
years = int((now - dt).days / 365.2425)
if years > 0:
return f"{years}年老米"
return ""
def normalize_dnssec(value):
text = value.strip().lower()
if not text or text in ("unsigned", "unsigned delegation", "no", "false", "inactive"):
return "未配置"
if text in ("signed", "yes", "true", "active"):
return "已配置"
return value
def print_row(label, value):
print(f"{label}: {value}")
def get_whois_server(domain):
whois_server = find_whois_server(domain)
if whois_server:
return whois_server
tld = get_tld(domain)
if not tld:
return ""
return f"whois.nic.{tld}"
def main():
domain = input("请输入你的域名: ").strip()
if not domain:
print("域名格式无效")
return
domain = normalize_domain(domain)
if not domain:
print("域名格式无效")
return
try:
whois_server = get_whois_server(domain)
if not whois_server:
print("无法识别域名后缀")
return
result = query_whois_server(whois_server, domain)
info = parse_whois_text(result)
display_domain = info.get("domain", domain)
age_text = get_domain_age_text(info.get("creation_date", ""))
if age_text:
print_row("域名", f"{display_domain} {age_text}")
else:
print_row("域名", display_domain)
print_row("注册商", info.get("registrar", "未知"))
creation_date = info.get("creation_date")
expiry_date = info.get("expiry_date")
updated_date = info.get("updated_date")
print_row("注册日", format_datetime_to_utc8(creation_date) if creation_date else "未知")
print_row("过期日", format_datetime_to_utc8(expiry_date) if expiry_date else "未知")
if updated_date:
print_row("更新日", format_datetime_to_utc8(updated_date))
if info.get("iana_id"):
print_row("IANA_ID", info["iana_id"])
if info.get("status_list"):
print_row("状态", " ".join(translate_status(s) for s in info["status_list"]))
else:
print_row("状态", "未知")
if info.get("dns_list"):
print_row("DNS", " ".join(info["dns_list"]))
else:
print_row("DNS", "未知")
print_row("DNSSEC", normalize_dnssec(info.get("dnssec", "未配置")))
except Exception as e:
print(f"查询失败: {e}")
if __name__ == "__main__":
main()