|
|
import gradio as gr |
|
|
import socket |
|
|
import time |
|
|
|
|
|
def check_dns_resolution(domain): |
|
|
""" |
|
|
尝试将域名解析为IP地址。 |
|
|
这是测试DNS问题的最直接方法。 |
|
|
返回一个包含结果的元组 (domain, status, detail)。 |
|
|
""" |
|
|
try: |
|
|
|
|
|
|
|
|
ip_address = socket.gethostbyname(domain) |
|
|
return (domain, "✅ 可访问", f"成功解析到IP: {ip_address}") |
|
|
except socket.gaierror as e: |
|
|
|
|
|
return (domain, "❌ 无法访问", f"DNS解析失败: {e}") |
|
|
except Exception as e: |
|
|
return (domain, "❌ 出错", f"发生未知错误: {e}") |
|
|
|
|
|
def batch_test_domains(domain_list_str, progress=gr.Progress(track_tqdm=True)): |
|
|
""" |
|
|
对给定的域名列表进行批量测试,并生成Markdown格式的报告。 |
|
|
""" |
|
|
if not domain_list_str or not domain_list_str.strip(): |
|
|
return "⚠️ 请在文本框中输入至少一个域名。" |
|
|
|
|
|
|
|
|
domains_to_test = [d.strip() for d in domain_list_str.split('\n') if d.strip()] |
|
|
if not domains_to_test: |
|
|
return "⚠️ 输入无效,请输入有效域名。" |
|
|
|
|
|
total_domains = len(domains_to_test) |
|
|
results = [] |
|
|
|
|
|
|
|
|
for i, domain in enumerate(domains_to_test): |
|
|
|
|
|
progress(i / total_domains, desc=f"正在测试 ({i+1}/{total_domains}): {domain}") |
|
|
|
|
|
|
|
|
result = check_dns_resolution(domain) |
|
|
results.append(result) |
|
|
|
|
|
|
|
|
time.sleep(0.25) |
|
|
|
|
|
|
|
|
accessible_domains = [] |
|
|
inaccessible_domains = [] |
|
|
|
|
|
for domain, status, detail in results: |
|
|
|
|
|
formatted_line = f"`{domain}` - {detail}" |
|
|
if "可访问" in status: |
|
|
accessible_domains.append(formatted_line) |
|
|
else: |
|
|
inaccessible_domains.append(formatted_line) |
|
|
|
|
|
report = f"## 批量测试报告 (共测试 {total_domains} 个域名)\n\n" |
|
|
|
|
|
report += f"### ✅ 可访问的域名 ({len(accessible_domains)} 个)\n" |
|
|
if accessible_domains: |
|
|
report += "\n".join([f"- {d}" for d in accessible_domains]) |
|
|
else: |
|
|
report += "无" |
|
|
|
|
|
report += f"\n\n### ❌ 无法访问或出错的域名 ({len(inaccessible_domains)} 个)\n" |
|
|
if inaccessible_domains: |
|
|
report += "\n".join([f"- {d}" for d in inaccessible_domains]) |
|
|
else: |
|
|
report += "无" |
|
|
|
|
|
return report |
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="blue")) as demo: |
|
|
gr.Markdown( |
|
|
""" |
|
|
# Hugging Face Space 出站网络可访问性测试 (优化版) |
|
|
在下面的文本框中输入或粘贴您需要测试的域名列表(每行一个)。 |
|
|
工具将以安全、稳定的方式逐个测试,并生成清晰的报告,以避免IP被临时限制。 |
|
|
""" |
|
|
) |
|
|
|
|
|
with gr.Row(): |
|
|
domain_input = gr.Textbox( |
|
|
lines=15, |
|
|
label="域名列表 (每行一个)", |
|
|
placeholder="www.google.com\nwww.facebook.com\nhuggingface.co\napi.openai.com\n..." |
|
|
) |
|
|
|
|
|
submit_btn = gr.Button("🚀 开始批量测试", variant="primary") |
|
|
|
|
|
output_report = gr.Markdown(label="测试报告") |
|
|
|
|
|
|
|
|
submit_btn.click( |
|
|
fn=batch_test_domains, |
|
|
inputs=domain_input, |
|
|
outputs=output_report |
|
|
) |
|
|
|
|
|
gr.Examples( |
|
|
[ |
|
|
"www.google.com\ngithub.com\nhuggingface.co\nwww.wikipedia.org", |
|
|
"www.facebook.com\nwww.instagram.com\nwww.youtube.com\nweb.whatsapp.com" |
|
|
], |
|
|
inputs=domain_input |
|
|
) |
|
|
|
|
|
demo.launch() |
|
|
|