From 118b1bcbfcf9da182818a2f5fee1df828a0dc08b Mon Sep 17 00:00:00 2001 From: Johan Bloemberg Date: Sun, 6 Jan 2019 13:34:51 +0100 Subject: [PATCH] Fix watching tunnel state when /var/run/wireguard does not exists at start of Helper. --- WireGuardStatusbar/Info.plist | 2 +- WireGuardStatusbarHelper/Helper.swift | 23 ++++++++++++++++++++--- WireGuardStatusbarHelper/Info.plist | 4 ++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/WireGuardStatusbar/Info.plist b/WireGuardStatusbar/Info.plist index 575cfa7..d40565d 100644 --- a/WireGuardStatusbar/Info.plist +++ b/WireGuardStatusbar/Info.plist @@ -17,7 +17,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.14 + 1.15 CFBundleVersion 1 LSApplicationCategoryType diff --git a/WireGuardStatusbarHelper/Helper.swift b/WireGuardStatusbarHelper/Helper.swift index e81e163..3546cd8 100644 --- a/WireGuardStatusbarHelper/Helper.swift +++ b/WireGuardStatusbarHelper/Helper.swift @@ -5,6 +5,8 @@ import Foundation class Helper: NSObject, HelperProtocol, SKQueueDelegate { private var app: AppXPC? + private var queue: SKQueue? + // Starts the helper daemon func run() { // create XPC to App @@ -21,13 +23,18 @@ class Helper: NSObject, HelperProtocol, SKQueueDelegate { func registerWireGuardStateWatch() { // register watchers to respond to changes in wireguard config/runtime state // will trigger: receivedNotification - let queue = SKQueue(delegate: self)! + if queue == nil { + queue = SKQueue(delegate: self)! + } for directory in configPaths + [runPath] { + // skip already watched paths + if queue!.isPathWatched(directory) { continue } + if FileManager.default.fileExists(atPath: directory) { NSLog("Watching \(directory) for changes") - queue.addPath(directory) + queue!.addPath(directory) } else { - NSLog("Not watching '\(directory)' as it doesn't exist") + NSLog("Not watching '\(directory)' as it does not exist") } } } @@ -44,6 +51,11 @@ class Helper: NSObject, HelperProtocol, SKQueueDelegate { // not for every change in either run or config directories // At first maybe simple debounce to reduce amount of reloads of configuration? + appUpdateState() + } + + // Send a signal to the App that tunnel state/configuration might have changed + func appUpdateState() { for connection in app!.connections { if let remoteObject = connection.remoteObjectProxy as? AppProtocol { remoteObject.updateState() @@ -107,6 +119,11 @@ class Helper: NSObject, HelperProtocol, SKQueueDelegate { NSLog("Set tunnel \(tunnelName) \(state)") reply(wgQuick([state, tunnelName])) + + // because /var/run/wireguard might not exist and can be created after upping the first tunnel + // run the registration of watchdirectories again and force trigger a state update to the app + registerWireGuardStateWatch() + appUpdateState() } // XPC: allow App to query version of helper to allow updating when a new version is available diff --git a/WireGuardStatusbarHelper/Info.plist b/WireGuardStatusbarHelper/Info.plist index 35ba237..6aebdf1 100644 --- a/WireGuardStatusbarHelper/Info.plist +++ b/WireGuardStatusbarHelper/Info.plist @@ -9,9 +9,9 @@ CFBundleName WireGuardStatusbarHelper CFBundleShortVersionString - 1.14 + 1.15 CFBundleVersion - 1.0.12 + 1.0.13 SMAuthorizedClients anchor apple generic and identifier "WireGuardStatusbar"