Skip to content

Commit

Permalink
Fix watching tunnel state when /var/run/wireguard does not exists at …
Browse files Browse the repository at this point in the history
…start of Helper.
  • Loading branch information
Johan Bloemberg committed Jan 6, 2019
1 parent 52ff4ef commit 118b1bc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion WireGuardStatusbar/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.14</string>
<string>1.15</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
Expand Down
23 changes: 20 additions & 3 deletions WireGuardStatusbarHelper/Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
}
}
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions WireGuardStatusbarHelper/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<key>CFBundleName</key>
<string>WireGuardStatusbarHelper</string>
<key>CFBundleShortVersionString</key>
<string>1.14</string>
<string>1.15</string>
<key>CFBundleVersion</key>
<string>1.0.12</string>
<string>1.0.13</string>
<key>SMAuthorizedClients</key>
<array>
<string>anchor apple generic and identifier &quot;WireGuardStatusbar&quot;</string>
Expand Down

0 comments on commit 118b1bc

Please sign in to comment.