Supply missing pieces of path change
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
@@ -95,16 +95,18 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||
networkMonitor = NWPathMonitor()
|
||||
networkMonitor?.pathUpdateHandler = { path in
|
||||
guard handle >= 0 else { return }
|
||||
|
||||
if path.status == .satisfied {
|
||||
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration()
|
||||
|
||||
let endpointGoString = endpointString.withCString {
|
||||
gostring_t(p: $0, n: endpointString.utf8.count)
|
||||
wg_log(.debug, message: "Network change detected, re-establishing sockets and IPs: \(path.availableInterfaces)")
|
||||
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: wgGetListenPort(handle))
|
||||
let err = endpointString.withCString {
|
||||
wgSetConfig(handle, gostring_t(p: $0, n: endpointString.utf8.count))
|
||||
}
|
||||
if err == -EADDRINUSE {
|
||||
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: 0)
|
||||
_ = endpointString.withCString {
|
||||
wgSetConfig(handle, gostring_t(p: $0, n: endpointString.utf8.count))
|
||||
}
|
||||
}
|
||||
|
||||
wg_log(.debug, staticMessage: "Network change detected, calling wgSetConfig")
|
||||
wgSetConfig(handle, endpointGoString)
|
||||
}
|
||||
}
|
||||
networkMonitor?.start(queue: DispatchQueue(label: "NetworkMonitor"))
|
||||
|
||||
@@ -15,8 +15,8 @@ class PacketTunnelSettingsGenerator {
|
||||
self.resolvedEndpoints = resolvedEndpoints
|
||||
}
|
||||
|
||||
func endpointUapiConfiguration() -> String {
|
||||
var wgSettings = "listen_port=\(tunnelConfiguration.interface.listenPort ?? 0)\n"
|
||||
func endpointUapiConfiguration(currentListenPort: UInt16) -> String {
|
||||
var wgSettings = "listen_port=\(tunnelConfiguration.interface.listenPort ?? currentListenPort)\n"
|
||||
|
||||
for (i, peer) in tunnelConfiguration.peers.enumerated() {
|
||||
wgSettings.append("public_key=\(peer.publicKey.hexEncodedString())\n")
|
||||
|
||||
Reference in New Issue
Block a user