Fixed a potential race condition, better naming on PacketTunnelSettingsGenerator methods
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
This commit is contained in:
@@ -88,8 +88,28 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let wireguardSettings = packetTunnelSettingsGenerator.generateWireGuardSettings()
|
let wireguardSettings = packetTunnelSettingsGenerator.uapiConfiguration()
|
||||||
let handle = connect(interfaceName: tunnelConfiguration.interface.name, settings: wireguardSettings, fd: fd)
|
|
||||||
|
var handle: Int32 = -1
|
||||||
|
|
||||||
|
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, staticMessage: "Network change detected, calling wgSetConfig")
|
||||||
|
wgSetConfig(handle, endpointGoString)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
networkMonitor?.start(queue: DispatchQueue(label: "NetworkMonitor"))
|
||||||
|
|
||||||
|
handle = connect(interfaceName: tunnelConfiguration.interface.name, settings: wireguardSettings, fd: fd)
|
||||||
|
|
||||||
if handle < 0 {
|
if handle < 0 {
|
||||||
wg_log(.error, staticMessage: "Starting tunnel failed: Could not start WireGuard")
|
wg_log(.error, staticMessage: "Starting tunnel failed: Could not start WireGuard")
|
||||||
@@ -113,20 +133,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
startTunnelCompletionHandler(nil /* No errors */)
|
startTunnelCompletionHandler(nil /* No errors */)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
networkMonitor = NWPathMonitor()
|
|
||||||
networkMonitor?.pathUpdateHandler = { path in
|
|
||||||
if path.status == .satisfied {
|
|
||||||
let endpointString = packetTunnelSettingsGenerator.endpointFromSettings()
|
|
||||||
|
|
||||||
let endpointGoString = endpointString.withCString {
|
|
||||||
gostring_t(p: $0, n: endpointString.utf8.count)
|
|
||||||
}
|
|
||||||
|
|
||||||
wgSetConfig(handle, endpointGoString)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
networkMonitor?.start(queue: DispatchQueue(label: "NetworkMonitor"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Begin the process of stopping the tunnel.
|
/// Begin the process of stopping the tunnel.
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class PacketTunnelSettingsGenerator {
|
|||||||
self.resolvedEndpoints = resolvedEndpoints
|
self.resolvedEndpoints = resolvedEndpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
func endpointFromSettings() -> String {
|
func endpointUapiConfiguration() -> String {
|
||||||
var wgSettings = "listen_port=\(tunnelConfiguration.interface.listenPort ?? 0)\n"
|
var wgSettings = "listen_port=\(tunnelConfiguration.interface.listenPort ?? 0)\n"
|
||||||
|
|
||||||
for (i, peer) in tunnelConfiguration.peers.enumerated() {
|
for (i, peer) in tunnelConfiguration.peers.enumerated() {
|
||||||
@@ -29,7 +29,7 @@ class PacketTunnelSettingsGenerator {
|
|||||||
return wgSettings
|
return wgSettings
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateWireGuardSettings() -> String {
|
func uapiConfiguration() -> String {
|
||||||
var wgSettings = ""
|
var wgSettings = ""
|
||||||
let privateKey = tunnelConfiguration.interface.privateKey.hexEncodedString()
|
let privateKey = tunnelConfiguration.interface.privateKey.hexEncodedString()
|
||||||
wgSettings.append("private_key=\(privateKey)\n")
|
wgSettings.append("private_key=\(privateKey)\n")
|
||||||
|
|||||||
Reference in New Issue
Block a user