Skip to content

The only downloader you need. 下载器的集大成者。

fix(dns): TUN 模式下使用系统 DNS 解析域名 (#707)

XiaoYouChR53740bff792e
diff --git a/app/client.py b/app/client.py
index a04995e4..042d9bae 100644
--- a/app/client.py
+++ b/app/client.py
@@ -7,6 +7,7 @@ from typing import TYPE_CHECKING
from loguru import logger
from wreq import Client, Emulation, Proxy
+from wreq.dns import DnsOptions
from wreq.emulation import Platform, Profile
from wreq.redirect import Policy
@@ -101,6 +102,8 @@ def buildClient(
) -> Client:
resolved = toEmulation("") if emulation is ... else emulation
config: ClientConfig = {"tls_verify": cfg.shouldVerifySsl.value, "redirect": Policy.limited(10)}
+ if cfg.shouldUseSystemDns.value:
+ config["dns_options"] = DnsOptions(system_dns=True)
url = proxy()
if url:
diff --git a/app/config/cfg.py b/app/config/cfg.py
index 62fb7fa7..44f507a3 100644
--- a/app/config/cfg.py
+++ b/app/config/cfg.py
@@ -308,6 +308,7 @@ class Config(QConfig):
# 网络
clientProfile = ConfigItem("Network", "ClientProfile", "auto", ClientProfileValidator())
+ shouldUseSystemDns = ConfigItem("Network", "ShouldUseSystemDns", True, BoolValidator())
headersPresets = ConfigItem(
"Network", "HeadersPresets", [],
HeadersPresetListValidator(), JsonConfigSerializer(list, list),
diff --git a/app/view/pages/setting_page.py b/app/view/pages/setting_page.py
index d5359e20..7000ecc5 100644
--- a/app/view/pages/setting_page.py
+++ b/app/view/pages/setting_page.py
@@ -115,6 +115,9 @@ class SettingPage(ScrollArea):
cfg.shouldVerifySsl),
self.downloadFolderCard,
ProxySettingCard(cfg.proxyServer, featureService=self._featureService),
+ SwitchSettingCard(FluentIcon.GLOBE, self.tr("使用系统 DNS"),
+ self.tr("使用操作系统的 DNS 解析,兼容 TUN、VPN 和代理等网络环境"),
+ cfg.shouldUseSystemDns),
self.clientProfileCard,
HeadersPresetSettingCard(),
])
diff --git a/tests/test_identity_preset.py b/tests/test_identity_preset.py
index d6311f20..b7dd8595 100644
--- a/tests/test_identity_preset.py
+++ b/tests/test_identity_preset.py
@@ -139,10 +139,12 @@ class TestBuildClientUserAgent:
wreq_mock.emulation.Platform = MagicMock()
wreq_mock.emulation.Profile = type(profile_mock)
wreq_mock.redirect.Policy = MagicMock()
+ wreq_mock.dns.DnsOptions = MagicMock(return_value=MagicMock())
monkeypatch.setitem(sys.modules, "wreq", wreq_mock)
monkeypatch.setitem(sys.modules, "wreq.emulation", wreq_mock.emulation)
monkeypatch.setitem(sys.modules, "wreq.redirect", wreq_mock.redirect)
+ monkeypatch.setitem(sys.modules, "wreq.dns", wreq_mock.dns)
if "app.client" in sys.modules:
del sys.modules["app.client"]
@@ -166,6 +168,23 @@ class TestBuildClientUserAgent:
assert "headers" not in call_kwargs or "user-agent" not in call_kwargs.get("headers", {})
+ def test_c11_build_client_uses_system_dns(self):
+ from app.client import buildClient
+ import wreq
+ buildClient(emulation=None, headers=None, userAgent=None)
+ wreq.dns.DnsOptions.assert_called_once_with(system_dns=True)
+ call_kwargs = wreq.Client.call_args[1]
+ assert call_kwargs["dns_options"] is wreq.dns.DnsOptions.return_value
+
+ def test_c12_build_client_skips_system_dns_when_disabled(self, monkeypatch):
+ from app.client import buildClient
+ from app.config.cfg import cfg
+ import wreq
+ monkeypatch.setattr(cfg.shouldUseSystemDns, "value", False)
+ buildClient(emulation=None, headers=None, userAgent=None)
+ assert "dns_options" not in wreq.Client.call_args[1]
+
+
# ===========================================================================
# D. HttpTaskStep._effectiveHeaders
# ===========================================================================