1 | #!/usr/bin/env python3 |
3. 配置 Windows 使用本地 DNS
- 打开“控制面板” -> “网络和共享中心” -> “更改适配器设置”
- 右键点击当前使用的网络连接,选择“属性”
- 双击“Internet 协议版本 4 (TCP/IPv4)”
- 选择“使用下面的 DNS 服务器地址”,并输入
127.0.0.1
4. 启动 DNS 服务器
在命令提示符中运行:
1 | python C:\SmartDNS\standalone_smart_dns.py |
结论
通过本文介绍的方案,用户可以在本地搭建一个轻量级的智能 DNS 分流系统,有效解决 DNS 污染问题,并根据网络环境自动选择最佳的解析结果。该方案不仅适用于 macOS,也可以在 Windows 系统上实现,提供了灵活的跨平台解决方案。
3. Create a Windows Service
To run a Python script as a Windows service, we need to use the NSSM (Non-Sucking Service Manager) tool:
Download NSSM
Extract it to a suitable directory, such as
C:\SmartDNS\nssm
Run the command prompt as an administrator, then execute:
1
2cd C:\SmartDNS\nssm\win64
nssm.exe install SmartDNSIn the configuration window that appears, set:
- Path:
C:\Windows\System32\python.exe
- Startup directory:
C:\SmartDNS
- Arguments:
C:\SmartDNS\standalone_smart_dns.py
- Set the service display name and description in the “Details” tab
- Choose “Local System account” in the “Log on” tab
- Click “Install service”
- Path:
Note: In Windows, port 53 is a privileged port, and the Python script needs to run with administrator privileges to bind to this port. The above service setup using “Local System account” can solve this issue. If you still encounter permission issues, consider changing the listening port in the script to a non-privileged port (such as 5353), and then use firewall rules to forward requests from UDP port 53 to 5353.
- Start the service:
1
sc start SmartDNS
4. Configure the System to Use Local DNS
- Open Control Panel > Network and Internet > Network and Sharing Center
- Click on the current active network connection
- Click “Properties”
- Select “Internet Protocol Version 4 (TCP/IPv4)” and click “Properties”
- Choose “Use the following DNS server addresses”
- Set the preferred DNS server to “127.0.0.1”
- Click “OK” to save the settings
5. Verification Test
Execute in the command prompt:
1 | nslookup baidu.com 127.0.0.1 |
If the settings are correct, the domain names should resolve correctly.
6. Restore Default Settings
If you need to restore the default settings:
Stop the DNS Service:
1
2sc stop SmartDNS
sc delete SmartDNSRestore Default DNS Settings:
- Open Control Panel > Network and Internet > Network and Sharing Center
- Click on the current active network connection
- Click “Properties”
- Select “Internet Protocol Version 4 (TCP/IPv4)” and click “Properties”
- Choose “Obtain DNS server address automatically”
- Click “OK” to save the settings
Conclusion
The combination of local anti-pollution DNS and Layer 3 Tunnel provides us with an elegant solution that avoids DNS pollution issues while ensuring the best speed for accessing both domestic and international websites. This solution is particularly suitable for users who need to access both domestic and international resources simultaneously.
When you use both local anti-pollution DNS and a Layer 3 tunnel (configured with regional routing), you will gain the following advantages:
- Pollution-free Resolution: All domain names can obtain the correct IP address
- Efficient Access:
- DNS queries for domestic websites go directly through the local network, obtaining the IP most suitable for your network environment
- DNS queries for international websites go through the tunnel, avoiding pollution and obtaining CDN IPs close to the tunnel exit
- Direct connection for domestic websites, fast speed
- Tunnel connection for international websites, stable and reliable
- Fully Automatic Traffic Splitting: The system automatically determines which route to take, without the need to manually switch DNS or proxies