NE: Update listen port only when first interface changes
When handling network path changes, change the listen port only when the first interface has changed. Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
parent
28ce4d5164
commit
f818cdd963
|
@ -81,17 +81,32 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
|
|
||||||
var handle: Int32 = -1
|
var handle: Int32 = -1
|
||||||
|
|
||||||
|
func interfaceDescription(_ interface: NWInterface?) -> String {
|
||||||
|
if let interface = interface {
|
||||||
|
return "\(interface.name) (\(interface.type))"
|
||||||
|
} else {
|
||||||
|
return "None"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
networkMonitor = NWPathMonitor()
|
networkMonitor = NWPathMonitor()
|
||||||
|
var previousPrimaryNetworkPathInterface = networkMonitor?.currentPath.availableInterfaces.first
|
||||||
|
wg_log(.debug, message: "Network path primary interface: \(interfaceDescription(previousPrimaryNetworkPathInterface))")
|
||||||
networkMonitor?.pathUpdateHandler = { path in
|
networkMonitor?.pathUpdateHandler = { path in
|
||||||
guard handle >= 0 else { return }
|
guard handle >= 0 else { return }
|
||||||
if path.status == .satisfied {
|
if path.status == .satisfied {
|
||||||
wg_log(.debug, message: "Network change detected, re-establishing sockets and IPs: \(path.availableInterfaces)")
|
wg_log(.debug, message: "Network change detected, re-establishing sockets and IPs: \(path.availableInterfaces)")
|
||||||
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: wgGetListenPort(handle))
|
let primaryNetworkPathInterface = path.availableInterfaces.first
|
||||||
|
wg_log(.debug, message: "Network path primary interface: \(interfaceDescription(primaryNetworkPathInterface))")
|
||||||
|
let shouldIncludeListenPort = previousPrimaryNetworkPathInterface != primaryNetworkPathInterface
|
||||||
|
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(shouldIncludeListenPort: shouldIncludeListenPort, currentListenPort: wgGetListenPort(handle))
|
||||||
let err = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
|
let err = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
|
||||||
if err == -EADDRINUSE {
|
if err == -EADDRINUSE {
|
||||||
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: 0)
|
// We expect this to happen only if shouldIncludeListenPort is true
|
||||||
|
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(shouldIncludeListenPort: shouldIncludeListenPort, currentListenPort: 0)
|
||||||
_ = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
|
_ = withStringsAsGoStrings(endpointString, call: { return wgSetConfig(handle, $0.0) })
|
||||||
}
|
}
|
||||||
|
previousPrimaryNetworkPathInterface = primaryNetworkPathInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
networkMonitor?.start(queue: DispatchQueue(label: "NetworkMonitor"))
|
networkMonitor?.start(queue: DispatchQueue(label: "NetworkMonitor"))
|
||||||
|
|
|
@ -15,8 +15,16 @@ class PacketTunnelSettingsGenerator {
|
||||||
self.resolvedEndpoints = resolvedEndpoints
|
self.resolvedEndpoints = resolvedEndpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
func endpointUapiConfiguration(currentListenPort: UInt16) -> String {
|
func endpointUapiConfiguration(shouldIncludeListenPort: Bool, currentListenPort: UInt16?) -> String {
|
||||||
var wgSettings = "listen_port=\(tunnelConfiguration.interface.listenPort ?? currentListenPort)\n"
|
var wgSettings = ""
|
||||||
|
|
||||||
|
if shouldIncludeListenPort {
|
||||||
|
if let tunnelListenPort = tunnelConfiguration.interface.listenPort {
|
||||||
|
wgSettings.append("listen_port=\(tunnelListenPort)\n")
|
||||||
|
} else if let currentListenPort = currentListenPort {
|
||||||
|
wgSettings.append("listen_port=\(currentListenPort)\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (index, peer) in tunnelConfiguration.peers.enumerated() {
|
for (index, peer) in tunnelConfiguration.peers.enumerated() {
|
||||||
wgSettings.append("public_key=\(peer.publicKey.hexEncodedString())\n")
|
wgSettings.append("public_key=\(peer.publicKey.hexEncodedString())\n")
|
||||||
|
|
Loading…
Reference in New Issue