解决dns的环路问题

This commit is contained in:
anlicheng 2026-04-09 17:53:34 +08:00
parent a697770187
commit 4538466f6b
3 changed files with 38 additions and 5 deletions

View File

@ -794,10 +794,8 @@ actor SDLContextActor {
let ipv4Settings = NEIPv4Settings(addresses: [networkAddress.ipAddress], subnetMasks: [networkAddress.maskAddress])
//
ipv4Settings.includedRoutes = routes
// TODO
ipv4Settings.excludedRoutes = [
]
//
ipv4Settings.excludedRoutes = self.getIpv4ExcludeRoutes()
networkSettings.ipv4Settings = ipv4Settings
//
@ -830,6 +828,28 @@ actor SDLContextActor {
}
}
private func getIpv4ExcludeRoutes() -> [NEIPv4Route] {
//
let dnsServers = SDLUtil.getMacOSSystemDnsServers()
var ipv4DnsServers = dnsServers.filter {!$0.contains(":")}
// dns
let commonDnsServers = [
"8.8.8.8",
"8.8.4.4",
"223.5.5.5",
"223.6.6.6",
"114.114.114.114"
]
for ip in commonDnsServers {
if !ipv4DnsServers.contains(ip) {
ipv4DnsServers.append(ip)
}
}
return ipv4DnsServers.map { NEIPv4Route(destinationAddress: $0, subnetMask: "255.255.255.255") }
}
deinit {
self.udpHole = nil
self.dnsClient = nil

View File

@ -6,6 +6,7 @@
//
import Foundation
import SystemConfiguration
struct SDLUtil {
@ -64,4 +65,16 @@ struct SDLUtil {
return bytes.map { String(format: "%02X", $0) }.joined(separator: ":").lowercased()
}
public static func getMacOSSystemDnsServers() -> [String] {
var results = [String]()
// DNS
if let dict = SCDynamicStoreCopyValue(nil, "State:/Network/Global/DNS" as CFString) as? [String: Any] {
if let servers = dict["ServerAddresses"] as? [String] {
results = servers
}
}
return results
}
}