解决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

@ -23,7 +23,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
SDLLogger.shared.log("NE read message: \(msg ?? "failed")") SDLLogger.shared.log("NE read message: \(msg ?? "failed")")
DarwinNotificationCenter.shared.post(.vpnStatusChanged) DarwinNotificationCenter.shared.post(.vpnStatusChanged)
// host: "192.168.0.101", port: 1265 // host: "192.168.0.101", port: 1265
guard let options, let config = SDLConfiguration.parse(options: options) else { guard let options, let config = SDLConfiguration.parse(options: options) else {
completionHandler(TunnelError.invalidConfiguration) completionHandler(TunnelError.invalidConfiguration)

View File

@ -794,10 +794,8 @@ actor SDLContextActor {
let ipv4Settings = NEIPv4Settings(addresses: [networkAddress.ipAddress], subnetMasks: [networkAddress.maskAddress]) let ipv4Settings = NEIPv4Settings(addresses: [networkAddress.ipAddress], subnetMasks: [networkAddress.maskAddress])
// //
ipv4Settings.includedRoutes = routes ipv4Settings.includedRoutes = routes
// TODO //
ipv4Settings.excludedRoutes = [ ipv4Settings.excludedRoutes = self.getIpv4ExcludeRoutes()
]
networkSettings.ipv4Settings = ipv4Settings 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 { deinit {
self.udpHole = nil self.udpHole = nil
self.dnsClient = nil self.dnsClient = nil

View File

@ -6,6 +6,7 @@
// //
import Foundation import Foundation
import SystemConfiguration
struct SDLUtil { struct SDLUtil {
@ -63,5 +64,17 @@ struct SDLUtil {
return bytes.map { String(format: "%02X", $0) }.joined(separator: ":").lowercased() 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
}
} }