Skip to content

Commit

Permalink
WireGuard tools detection and installation instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Bloemberg committed Dec 7, 2018
1 parent 043c4e1 commit cc30580
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 18 deletions.
12 changes: 12 additions & 0 deletions Shared/Const.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ let config_paths = [
"\(brew_prefix)/etc/wireguard",
"/etc/wireguard",
]
let wireguard_bin = "\(brew_prefix)/bin/wg"
let wgquick_bin = "\(brew_prefix)/bin/wg-quick"

let install_instructions = """
Currently this Application does not come with WireGuard binaries. It is required to manually install these using Homebrew.
Please follow the instructions on:
https://www.wireguard.com/install/
and restart this Application afterwards.
"""
4 changes: 4 additions & 0 deletions WireGuardStatusbar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
04B4BE46211E1E030001213A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 04B4BE45211E1E030001213A /* Assets.xcassets */; };
04B4BE49211E1E030001213A /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 04B4BE47211E1E030001213A /* MainMenu.xib */; };
04B92495211F6A68008D964A /* Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04B92494211F6A68008D964A /* Helper.swift */; };
04C5AE7621BB13E6007F56D1 /* InstallInstructions.xib in Resources */ = {isa = PBXBuildFile; fileRef = 04C5AE7521BB13E6007F56D1 /* InstallInstructions.xib */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -73,6 +74,7 @@
04B92494211F6A68008D964A /* Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helper.swift; sourceTree = "<group>"; };
04B9249B211F7200008D964A /* Launchd.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Launchd.plist; sourceTree = "<group>"; };
04B9249C211F7215008D964A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
04C5AE7521BB13E6007F56D1 /* InstallInstructions.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = InstallInstructions.xib; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -126,6 +128,7 @@
04B4BE43211E1E010001213A /* AppDelegate.swift */,
04B4BE45211E1E030001213A /* Assets.xcassets */,
04B4BE47211E1E030001213A /* MainMenu.xib */,
04C5AE7521BB13E6007F56D1 /* InstallInstructions.xib */,
04B4BE4A211E1E030001213A /* Info.plist */,
0428EA0A21BAFF0200178BA0 /* PrivilegedHelper.swift */,
);
Expand Down Expand Up @@ -222,6 +225,7 @@
files = (
04B4BE46211E1E030001213A /* Assets.xcassets in Resources */,
04B4BE49211E1E030001213A /* MainMenu.xib in Resources */,
04C5AE7621BB13E6007F56D1 /* InstallInstructions.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
45 changes: 29 additions & 16 deletions WireGuardStatusbar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,42 +117,55 @@ class AppDelegate: NSObject, NSApplicationDelegate, SKQueueDelegate {
// TODO: currently just rebuilding the entire menu, maybe opt for replacing the tunnel entries instead?
statusMenu.removeAllItems()

statusMenu.addItem(NSMenuItem(title: "About", action: #selector(AppDelegate.about(_:)), keyEquivalent: ""))
statusMenu.addItem(NSMenuItem.separator())

var connected = false
let fileManager = FileManager.default

if tunnels.isEmpty {
statusMenu.addItem(NSMenuItem(title: "No tunnel configurations found", action: nil, keyEquivalent: ""))
} else if fileManager.fileExists(atPath:wireguard_bin) != true {
NSLog("Wireguard binary not found at \(wireguard_bin)")
statusMenu.addItem(NSMenuItem(title: "Wireguard not installed! Click here for instructions", action: #selector(AppDelegate.showInstallInstructions(_:)), keyEquivalent: ""))
} else {
for (id, tunnel) in tunnels.sorted(by: { $0.0 < $1.0 }) {
let item = NSMenuItem(title: "\(tunnel.interface): \(tunnel.address)", action: #selector(AppDelegate.toggleTunnel(_:)), keyEquivalent: "")
item.representedObject = id
if tunnel.connected {
item.state = NSControl.StateValue.on
connected = true
}
statusMenu.addItem(item)
for peer in tunnel.peers {
statusMenu.addItem(NSMenuItem(title: " \(peer.endpoint): \(peer.allowedIps.joined(separator: ", "))", action: nil, keyEquivalent: ""))
}
addTunnelMenuItem(statusMenu: statusMenu, id: id, tunnel: tunnel)
}
}
statusMenu.addItem(NSMenuItem.separator())
statusMenu.addItem(NSMenuItem(title: "About", action: #selector(AppDelegate.about(_:)), keyEquivalent: ""))
// statusMenu.addItem(NSMenuItem(title: "Preferences...", action: #selector(AppDelegate.preferences(_:)), keyEquivalent: ","))
statusMenu.addItem(NSMenuItem(title: "Quit", action: #selector(AppDelegate.quit(_:)), keyEquivalent: "q"))

// TODO: find better way to do this, computed property or something?
if connected {
let icon = NSImage(named: .connected)
let connected_tunnels = tunnels.filter {$1.connected}
if connected_tunnels.isEmpty {
let icon = NSImage(named: .disconnected)
icon!.isTemplate = true
statusItem.image = icon
} else {
let icon = NSImage(named: .disconnected)
let icon = NSImage(named: .connected)
icon!.isTemplate = true
statusItem.image = icon
}
}

func addTunnelMenuItem(statusMenu: NSMenu, id: String, tunnel: Tunnel){
let item = NSMenuItem(title: "\(tunnel.interface): \(tunnel.address)", action: #selector(AppDelegate.toggleTunnel(_:)), keyEquivalent: "")
item.representedObject = id
if tunnel.connected {
item.state = NSControl.StateValue.on
}
statusMenu.addItem(item)
for peer in tunnel.peers {
statusMenu.addItem(NSMenuItem(title: " \(peer.endpoint): \(peer.allowedIps.joined(separator: ", "))", action: nil, keyEquivalent: ""))
}
}

@objc func showInstallInstructions(_ sender: NSMenuItem) {
let alert = NSAlert()
alert.messageText = install_instructions
alert.runModal()
}

// load tunnel from configuration files
func loadConfiguration() {
for config_path in config_paths {
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.8</string>
<string>1.9</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
Expand Down
48 changes: 48 additions & 0 deletions WireGuardStatusbar/InstallInstructions.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14460.31"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner"/>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="QvC-M9-y7g">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="480" height="270"/>
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
<view key="contentView" wantsLayer="YES" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<scrollView fixedFrame="YES" borderType="none" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" translatesAutoresizingMaskIntoConstraints="NO" id="2Te-qg-7Xj">
<rect key="frame" x="76" y="80" width="240" height="135"/>
<autoresizingMask key="autoresizingMask"/>
<clipView key="contentView" ambiguous="YES" drawsBackground="NO" copiesOnScroll="NO" id="xjm-mR-1V9">
<rect key="frame" x="0.0" y="0.0" width="240" height="135"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textView ambiguous="YES" importsGraphics="NO" richText="NO" verticallyResizable="YES" spellingCorrection="YES" smartInsertDelete="YES" id="Sks-RX-RlS">
<rect key="frame" x="0.0" y="0.0" width="240" height="135"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<size key="minSize" width="240" height="135"/>
<size key="maxSize" width="240" height="10000000"/>
<color key="insertionPointColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
</textView>
</subviews>
</clipView>
<scroller key="verticalScroller" verticalHuggingPriority="750" horizontal="NO" id="Lko-h5-cmJ">
<rect key="frame" x="224" y="0.0" width="16" height="135"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
</subviews>
</view>
<point key="canvasLocation" x="-14" y="140"/>
</window>
</objects>
</document>
3 changes: 2 additions & 1 deletion WireGuardStatusbarHelper/WireGuard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import Foundation
public class WireGuard {
func wg(_ arguments:[String]) -> NSNumber {
let task = Process()
task.launchPath = "\(brew_prefix)/bin/wg-quick"
task.launchPath = wgquick_bin
task.arguments = arguments
// Add brew bin to path as wg-quick requires Bash 4 instead of macOS provided Bash 3
task.environment = ["PATH": "\(brew_prefix)/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"]
let outpipe = Pipe()
task.standardOutput = outpipe
Expand Down

0 comments on commit cc30580

Please sign in to comment.