Fixed a potential race condition, better naming on PacketTunnelSettingsGenerator methods

Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
This commit is contained in:
Eric Kuck 2018-12-11 16:59:15 -06:00
parent 27265fc222
commit 9bc7e58487
2 changed files with 24 additions and 18 deletions

View File

@ -88,8 +88,28 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
return
}
let wireguardSettings = packetTunnelSettingsGenerator.generateWireGuardSettings()
let handle = connect(interfaceName: tunnelConfiguration.interface.name, settings: wireguardSettings, fd: fd)
let wireguardSettings = packetTunnelSettingsGenerator.uapiConfiguration()
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 {
wg_log(.error, staticMessage: "Starting tunnel failed: Could not start WireGuard")
@ -113,20 +133,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
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.

View File

@ -15,7 +15,7 @@ class PacketTunnelSettingsGenerator {
self.resolvedEndpoints = resolvedEndpoints
}
func endpointFromSettings() -> String {
func endpointUapiConfiguration() -> String {
var wgSettings = "listen_port=\(tunnelConfiguration.interface.listenPort ?? 0)\n"
for (i, peer) in tunnelConfiguration.peers.enumerated() {
@ -29,7 +29,7 @@ class PacketTunnelSettingsGenerator {
return wgSettings
}
func generateWireGuardSettings() -> String {
func uapiConfiguration() -> String {
var wgSettings = ""
let privateKey = tunnelConfiguration.interface.privateKey.hexEncodedString()
wgSettings.append("private_key=\(privateKey)\n")