Skip to content

Commit

Permalink
Allow overriding brewPrefix and path to wg-quick.
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Bloemberg committed Jan 6, 2019
1 parent 118b1bc commit 0e0e93e
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Misc/Uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ sudo rm /Library/PrivilegedHelperTools/WireGuardStatusbarHelper
# Also remove application
sudo rm -r /Applications/WireGuardStatusbar.app/

# And all settings
defaults delete WireGuardStatusbar
sudo defaults delete WireGuardStatusbarHelper

exit 0
11 changes: 2 additions & 9 deletions Shared/Const.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@

import Foundation

let brewPrefix = "/usr/local"
let defaultBrewPrefix = "/usr/local"
let runPath = "/var/run/wireguard"

// paths to search for tunnel configurations, ordered by wg-quick's preferences
let configPaths = [
"/etc/wireguard",
"\(brewPrefix)/etc/wireguard",
]

let wireguardBin = "\(brewPrefix)/bin/wg"
let wgquickBin = "\(brewPrefix)/bin/wg-quick"
let wireguardBinPath = "\(defaultBrewPrefix)/bin/wg"

let installInstructions = """
Currently this Application does not come with WireGuard binaries. \
Expand Down
2 changes: 1 addition & 1 deletion WireGuardStatusbar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, AppProtocol {
var tunnels = Tunnels()

// To check wg binary is enough to also guarentee wg-quick and wireguard-go when installed with Homebrew
var wireguardInstalled = FileManager.default.fileExists(atPath: wireguardBin)
var wireguardInstalled = FileManager.default.fileExists(atPath: wireguardBinPath)

let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)

Expand Down
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.15</string>
<string>1.16</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
Expand Down
6 changes: 3 additions & 3 deletions WireGuardStatusbar/Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func buildMenu(tunnels: Tunnels, allTunnelDetails: Bool = false, connectedTunnel

// WireGaurd missing is a big problem, user should fix this first. TODO, include WireGuard with the App
if showInstallInstructions {
NSLog("WireGuard binary not found at '\(wireguardBin)'")
statusMenu.insertItem(NSMenuItem(title: "WireGuard not installed! Click here for instructions",
NSLog("WireGuard binary was not found at '\(wireguardBinPath)'")
statusMenu.insertItem(NSMenuItem(title: "WireGuard not installed! Click here for instructions...",
action: #selector(AppDelegate.showInstallInstructions(_:)),
keyEquivalent: ""), at: 0)
return statusMenu
statusMenu.insertItem(NSMenuItem.separator(), at: 0)
}

if tunnels.isEmpty {
Expand Down
39 changes: 38 additions & 1 deletion WireGuardStatusbarHelper/Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,43 @@ class Helper: NSObject, HelperProtocol, SKQueueDelegate {

private var queue: SKQueue?

// prefix path for etc/wireguard, bin/wg, bin/wireguard-go and bin/bash (bash 4)
// can be overridden by the user via root defaults to allow custom location for Homebrew
private var brewPrefix: String
// path to wg-quick, can be overriden by the user via root defaults
private var wgquickBinPath: String
// NOTICE: the root defaults override feature is a half implemented feature
// the GUI App will not be aware of these settings and might falsely warn that WireGuard
// is not installed. This warning can be ignored.
// Example, to set defaults as root for wgquickBinPath run:
// sudo defaults write WireGuardStatusbarHelper wgquickBinPath /opt/local/bin/wg-quick

// paths to search for tunnel configurations, ordered by wg-quick's preferences
private var configPaths: [String]

// read preferences set via root defaults
override init() {
if let brewPrefix = CFPreferencesCopyAppValue("brewPrefix" as CFString,
HelperConstants.machServiceName as CFString) as? String {
NSLog("Overriding 'brewPrefix' with: \(brewPrefix)")
self.brewPrefix = brewPrefix
} else {
brewPrefix = defaultBrewPrefix
}
configPaths = [
"/etc/wireguard",
"\(brewPrefix)/etc/wireguard",
]

if let wgquickBinPath = CFPreferencesCopyAppValue("wgquickBinPath" as CFString,
HelperConstants.machServiceName as CFString) as? String {
NSLog("Overriding 'wgquickBinPath' with: \(wgquickBinPath)")
self.wgquickBinPath = wgquickBinPath
} else {
wgquickBinPath = "\(brewPrefix)/bin/wg-quick"
}
}

// Starts the helper daemon
func run() {
// create XPC to App
Expand Down Expand Up @@ -118,7 +155,7 @@ class Helper: NSObject, HelperProtocol, SKQueueDelegate {
}

NSLog("Set tunnel \(tunnelName) \(state)")
reply(wgQuick([state, tunnelName]))
reply(wgQuick([state, tunnelName], brewPrefix: brewPrefix, wgquickBinPath: wgquickBinPath))

// 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
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.15</string>
<string>1.16</string>
<key>CFBundleVersion</key>
<string>1.0.13</string>
<string>1.0.14</string>
<key>SMAuthorizedClients</key>
<array>
<string>anchor apple generic and identifier &quot;WireGuardStatusbar&quot;</string>
Expand Down
10 changes: 8 additions & 2 deletions WireGuardStatusbarHelper/WireGuard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ func validateTunnelName(tunnelName: String) -> Bool {
range: NSRange(location: 0, length: tunnelName.count)) != nil
}

func wgQuick(_ arguments: [String]) -> NSNumber {
func wgQuick(_ arguments: [String], brewPrefix: String, wgquickBinPath: String) -> NSNumber {
// prevent passing an invalid path or else task.launch will result in a fatal NSInvalidArgumentException
guard FileManager.default.fileExists(atPath: wgquickBinPath) else {
NSLog("Path '\(wgquickBinPath)' for 'wg-quick' binary is invalid!")
return 1
}

let task = Process()
task.launchPath = wgquickBin
task.launchPath = wgquickBinPath
task.arguments = arguments
// Add brew bin to path as wg-quick requires Bash 4 instead of macOS provided Bash 3
task.environment = ["PATH": "\(brewPrefix)/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"]
Expand Down

0 comments on commit 0e0e93e

Please sign in to comment.