Supply missing pieces of path change
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
9bc7e58487
commit
034a1a12f7
|
@ -95,16 +95,18 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||||
networkMonitor = NWPathMonitor()
|
networkMonitor = NWPathMonitor()
|
||||||
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 {
|
||||||
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration()
|
wg_log(.debug, message: "Network change detected, re-establishing sockets and IPs: \(path.availableInterfaces)")
|
||||||
|
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: wgGetListenPort(handle))
|
||||||
let endpointGoString = endpointString.withCString {
|
let err = endpointString.withCString {
|
||||||
gostring_t(p: $0, n: endpointString.utf8.count)
|
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"))
|
networkMonitor?.start(queue: DispatchQueue(label: "NetworkMonitor"))
|
||||||
|
|
|
@ -15,8 +15,8 @@ class PacketTunnelSettingsGenerator {
|
||||||
self.resolvedEndpoints = resolvedEndpoints
|
self.resolvedEndpoints = resolvedEndpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
func endpointUapiConfiguration() -> String {
|
func endpointUapiConfiguration(currentListenPort: UInt16) -> String {
|
||||||
var wgSettings = "listen_port=\(tunnelConfiguration.interface.listenPort ?? 0)\n"
|
var wgSettings = "listen_port=\(tunnelConfiguration.interface.listenPort ?? currentListenPort)\n"
|
||||||
|
|
||||||
for (i, peer) in tunnelConfiguration.peers.enumerated() {
|
for (i, peer) in tunnelConfiguration.peers.enumerated() {
|
||||||
wgSettings.append("public_key=\(peer.publicKey.hexEncodedString())\n")
|
wgSettings.append("public_key=\(peer.publicKey.hexEncodedString())\n")
|
||||||
|
|
|
@ -128,16 +128,27 @@ func wgTurnOff(tunnelHandle int32) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//export wgSetConfig
|
//export wgSetConfig
|
||||||
func wgSetConfig(tunnelHandle int32, settings string) {
|
func wgSetConfig(tunnelHandle int32, settings string) int64 {
|
||||||
device, ok := tunnelHandles[tunnelHandle]
|
device, ok := tunnelHandles[tunnelHandle]
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return 0
|
||||||
}
|
}
|
||||||
bufferedSettings := bufio.NewReadWriter(bufio.NewReader(strings.NewReader(settings)), bufio.NewWriter(ioutil.Discard))
|
bufferedSettings := bufio.NewReadWriter(bufio.NewReader(strings.NewReader(settings)), bufio.NewWriter(ioutil.Discard))
|
||||||
err := ipcSetOperation(device, bufferedSettings)
|
err := ipcSetOperation(device, bufferedSettings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
device.log.Error.Println(err)
|
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
|
//export wgVersion
|
||||||
|
|
|
@ -14,7 +14,8 @@ typedef void(*logger_fn_t)(int level, const char *msg);
|
||||||
extern void wgSetLogger(logger_fn_t logger_fn);
|
extern void wgSetLogger(logger_fn_t logger_fn);
|
||||||
extern int wgTurnOn(gostring_t ifname, gostring_t settings, int32_t tun_fd);
|
extern int wgTurnOn(gostring_t ifname, gostring_t settings, int32_t tun_fd);
|
||||||
extern void wgTurnOff(int handle);
|
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();
|
extern char *wgVersion();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue