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"))
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")
}
//export wgSetConfig
-func wgSetConfig(tunnelHandle int32, settings string) {
+func wgSetConfig(tunnelHandle int32, settings string) int64 {
device, ok := tunnelHandles[tunnelHandle]
if !ok {
- return
+ return 0
}
bufferedSettings := bufio.NewReadWriter(bufio.NewReader(strings.NewReader(settings)), bufio.NewWriter(ioutil.Discard))
err := ipcSetOperation(device, bufferedSettings)
if err != nil {
device.log.Error.Println(err)
+ return err.Code
+ }
+ return 0
+}
+
+//export wgGetListenPort
+func wgGetListenPort(tunnelHandle int32) uint16 {
+ device, ok := tunnelHandles[tunnelHandle]
+ if !ok {
+ return 0
}
+ return device.net.port
}
//export wgVersion
extern void wgSetLogger(logger_fn_t logger_fn);
extern int wgTurnOn(gostring_t ifname, gostring_t settings, int32_t tun_fd);
extern void wgTurnOff(int handle);
-extern void wgSetConfig(int handle, gostring_t settings);
+extern int64_t wgSetConfig(int handle, gostring_t settings);
+extern uint16_t wgGetListenPort(int handle);
extern char *wgVersion();
#endif