Skip to content

Commit

Permalink
feat: 添加开关选择DNS服务器区域
Browse files Browse the repository at this point in the history
  • Loading branch information
sinspired committed Dec 1, 2024
1 parent ba6d136 commit ad970de
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions setDNS.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"api.github.com",
"assets-cdn.github.com",
"raw.githubusercontent.com",
"4kvm.net",
# "4kvm.net",
]

# DNS服务器列表
Expand All @@ -56,6 +56,10 @@
"ipv4": ["208.67.222.222"],
"ipv6": ["2620:0:ccc::2"],
},
"CloudFlareDNS": {
"ipv4": ["1.1.1.1"],
"ipv6": ["2606:4700:4700::1111"],
},
},
"中国大陆": {
"阿里云DNS": {
Expand Down Expand Up @@ -122,7 +126,7 @@ def evaluate_dns_server(server: str, ip_version: str) -> tuple[float, float, dic
return success_rate, avg_response_time, resolutions


def find_available_dns() -> tuple[dict, dict]:
def find_available_dns(args) -> tuple[dict, dict]:
"""
查找最佳的DNS服务器并获取域名解析结果
Expand All @@ -134,6 +138,9 @@ def find_available_dns() -> tuple[dict, dict]:
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
future_to_server = {}
for region, providers in DNS_SERVERS.items():
if args.only_global and region != "全球":
continue

for provider, servers in providers.items():
for ip_version in ["ipv4", "ipv6"]:
for server in servers[ip_version]:
Expand Down Expand Up @@ -163,7 +170,8 @@ def find_available_dns() -> tuple[dict, dict]:
domain_resolutions[domain][server] = ips

logger.debug(
f"{ip_version.upper()} DNS {server} ({region} - {provider}) 成功率 {success_rate:.2%}, 平均延迟 {avg_response_time:.2f}ms"
f"{ip_version.upper()} DNS {server} ({
region} - {provider}) 成功率 {success_rate:.2%}, 平均延迟 {avg_response_time:.2f}ms"
)
except Exception as exc:
logger.error(f"{server} 测试出错: {str(exc)}")
Expand Down Expand Up @@ -260,7 +268,8 @@ def set_dns_servers(ipv4_dns_list: list[str], ipv6_dns_list: list[str]):
continue
if ipv4_dns_list:
logger.debug(
f"设置IPv4 DNS for {interface}: {', '.join(ipv4_dns_list)}"
f"设置IPv4 DNS for {interface}: {
', '.join(ipv4_dns_list)}"
)
try:
subprocess.run(
Expand Down Expand Up @@ -294,7 +303,8 @@ def set_dns_servers(ipv4_dns_list: list[str], ipv6_dns_list: list[str]):
logger.error(f"设置IPv4 DNS for {interface}失败: {e}")
if ipv6_dns_list:
logger.debug(
f"设置IPv6 DNS for {interface}: {', '.join(ipv6_dns_list)}"
f"设置IPv6 DNS for {interface}: {
', '.join(ipv6_dns_list)}"
)
try:
subprocess.run(
Expand Down Expand Up @@ -378,20 +388,21 @@ def get_recommended_dns(available_dns: dict, algorithm: str) -> dict[str, list]:
"""
recommended = {"ipv4": [], "ipv6": []}
for ip_version in ["ipv4", "ipv6"]:
if algorithm == "region":
cn = get_best_dns_by_region(available_dns[ip_version], "中国大陆")
global_ = get_best_dns_by_region(available_dns[ip_version], "全球")
recommended[ip_version] = [
cn[0] if cn else None,
global_[0] if global_ else None,
]
elif algorithm == "overall":

if args.only_global or algorithm == "overall":
best = get_best_dns_overall(available_dns[ip_version])
# second_best = get_best_dns_overall(
# [dns for dns in available_dns[ip_version] if dns != best]
# )
# recommended[ip_version] = [best[0], second_best[0]]
recommended[ip_version] = [best[0]]
else:
cn = get_best_dns_by_region(available_dns[ip_version], "中国大陆")
global_ = get_best_dns_by_region(available_dns[ip_version], "全球")
recommended[ip_version] = [
cn[0] if cn else None,
global_[0] if global_ else None,
]
return recommended


Expand All @@ -410,7 +421,8 @@ def print_recommended_dns_table(dns_list: list, ip_version: str, available_dns:
if dns:
# 在best_dns列表中查找正确的服务器信息
server_info = next(
(info for server, info in available_dns[ip_version] if server == dns),
(info for server,
info in available_dns[ip_version] if server == dns),
None,
)
if server_info:
Expand Down Expand Up @@ -507,7 +519,7 @@ def main():
logger.addHandler(console_handler)

logger.info("开始测试DNS服务器...")
available_dns, domain_resolutions = find_available_dns()
available_dns, domain_resolutions = find_available_dns(args)

if available_dns["ipv4"] or available_dns["ipv6"]:
if args.show_resolutions:
Expand All @@ -520,7 +532,8 @@ def main():
print_domain_resolutions(domain_resolutions, dns_performance)

# 防止 best_dns_num 数值超过数组长度
num_servers = min(len(available_dns["ipv4"]), len(available_dns["ipv6"]))
num_servers = min(
len(available_dns["ipv4"]), len(available_dns["ipv6"]))
if args.best_dns_num > num_servers:
args.best_dns_num = num_servers

Expand Down Expand Up @@ -615,10 +628,17 @@ def run_as_admin():
parser.add_argument(
"--algorithm",
"--mode",
choices=["region", "overall"],
default="region",
choices=["allregions", "overall"],
default="allregions",
help="推荐最佳DNS的算法 (按区域或整体)",
)
parser.add_argument(
"--only-global", # argparse 会自动将第一个参数名转换为属性名。
"--global",
dest="only_global", # 这里指定属性名
action="store_true",
help="仅使用国际DNS服务器",
)
parser.add_argument(
"--show-resolutions",
"--show",
Expand Down

0 comments on commit ad970de

Please sign in to comment.