Skip to content

Commit

Permalink
Merge pull request #141 from openziti/dh_dev
Browse files Browse the repository at this point in the history
List identies and status icon on MenuBar, sent NotificationAction when selected
  • Loading branch information
smilindave26 authored Apr 15, 2023
2 parents 32ad3f2 + 70a1244 commit 3cf7aa3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 9 deletions.
2 changes: 1 addition & 1 deletion PacketTunnelProvider/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<string>$(PRODUCT_MODULE_NAME).PacketTunnelProvider</string>
</dict>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2020 NetFoundry. All rights reserved.</string>
<string>Copyright NetFoundry. All rights reserved.</string>
<key>TeamIdentifierPrefix</key>
<string>$(TeamIdentifierPrefix)</string>
<key>MAC_APP_IDENTIFIER</key>
Expand Down
14 changes: 7 additions & 7 deletions ZitiPacketTunnel/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2020 NetFoundry. All rights reserved.</string>
<string>Copyright NetFoundry. All rights reserved.</string>
<key>NSMainStoryboardFile</key>
<string>Main</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>TeamIdentifierPrefix</key>
<string>$(TeamIdentifierPrefix)</string>
<key>MAC_APP_IDENTIFIER</key>
<string>$(MAC_APP_IDENTIFIER)</string>
<key>MAC_EXT_IDENTIFIER</key>
<string>$(MAC_EXT_IDENTIFIER)</string>
<key>TeamIdentifierPrefix</key>
<string>$(TeamIdentifierPrefix)</string>
<key>MAC_APP_IDENTIFIER</key>
<string>$(MAC_APP_IDENTIFIER)</string>
<key>MAC_EXT_IDENTIFIER</key>
<string>$(MAC_EXT_IDENTIFIER)</string>
</dict>
</plist>
83 changes: 82 additions & 1 deletion ZitiPacketTunnel/MainMenuBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ class MainMenuBar : NSObject, NSWindowDelegate {

let appName = Bundle.main.infoDictionary?[kCFBundleNameKey as String] as? String ?? "Ziti"
let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
private var menu:NSMenu!
private var tunStatusItem:NSMenuItem!
private var tunConnectItem:NSMenuItem!
private var snapshotItem:NSMenuItem!
private var showDocItem:NSMenuItem!
private var logLevelMenu:NSMenu!

private var identityItems:[NSMenuItem] = []

private override init() {
statusItem.button?.image = NSImage(named:NSImage.Name("StatusBarConnected"))
super.init()

let menu = NSMenu()
menu = NSMenu()
tunStatusItem = newMenuItem(title:"Status:", action:#selector(MainMenuBar.noop(_:)))
menu.addItem(tunStatusItem)
tunConnectItem = newMenuItem(title:"Connect", action:#selector(MainMenuBar.connect(_:)))
Expand Down Expand Up @@ -99,6 +102,9 @@ class MainMenuBar : NSObject, NSWindowDelegate {
NotificationCenter.default.addObserver(
forName: NSMenu.didBeginTrackingNotification,
object: nil, queue: .main, using: menuDidBeginTracking)
NotificationCenter.default.addObserver(
forName: NSMenu.didEndTrackingNotification,
object: nil, queue: .main, using: menuDidEndTracking)

getMainWindow()?.delegate = self
TunnelMgr.shared.tsChangedCallbacks.append(self.tunnelStatusDidChange)
Expand All @@ -119,6 +125,57 @@ class MainMenuBar : NSObject, NSWindowDelegate {

func menuDidBeginTracking(n: Notification) {
updateLogLevelMenu()

let count = TunnelMgr.shared.zids.count
if count > 0 {
var idIndx = 2
let sepItem = NSMenuItem.separator()
menu.insertItem(sepItem, at: idIndx)
identityItems.append(sepItem)
idIndx += 1

for i in 0...(count-1) {
let zid = TunnelMgr.shared.zids[i]
let tunnelStatus = TunnelMgr.shared.status
var imageName:String = "NSStatusNone"
var tooltip:String = ""

if zid.isEnabled && zid.isMfaEnabled && zid.isMfaPending {
tooltip = " (MFA Pending)"
} else if !zid.allServicePostureChecksPassing() {
tooltip = " (Posture check failing)"
}

if zid.isEnrolled == true, zid.isEnabled == true, let edgeStatus = zid.edgeStatus {
switch edgeStatus.status {
case .Available:
imageName = (tunnelStatus == .connected) ? "NSStatusAvailable" : "NSStatusPartiallyAvailable"
case .PartiallyAvailable:
imageName = "NSStatusPartiallyAvailable"
case .Unavailable:
imageName = "NSStatusUnavailable"
default:
imageName = "NSStatusNone"
}

if tunnelStatus != .connected {
tooltip = ""
}
}
let idItem = newMenuItem(title: "\(zid.name)\(tooltip)", action: #selector(MainMenuBar.onIdentity(_:)), tag:i)
idItem.image = NSImage(named:imageName)
menu.insertItem(idItem, at: idIndx)
identityItems.append(idItem)
idIndx += 1
}
}
}

func menuDidEndTracking(n: Notification) {
identityItems.forEach { item in
menu.removeItem(item)
}
identityItems = []
}

func tunnelStatusDidChange(_ status:NEVPNStatus) {
Expand Down Expand Up @@ -185,6 +242,30 @@ class MainMenuBar : NSObject, NSWindowDelegate {
return false
}

@objc func onIdentity(_ sender: NSMenuItem?) {
guard let indx = sender?.tag else {
zLog.wtf("Unable to retrieve index for selected identity")
return
}

let zidCount = TunnelMgr.shared.zids.count
guard indx >= 0 && indx < zidCount else {
zLog.wtf("invalid zid indx \(indx), zidCount=\(zidCount)")
return
}

let zid = TunnelMgr.shared.zids[indx]

var category = ""
var action = ""
if TunnelMgr.shared.status == .connected && zid.isEnabled && zid.isMfaPending {
category = UserNotifications.Category.Mfa.rawValue
action = UserNotifications.Action.MfaAuth.rawValue
}
let msg = IpcAppexNotificationActionMessage(zid.id, category, action)
NotificationCenter.default.post(name: .onAppexNotification, object: self, userInfo: ["ipcMessage":msg])
}

@objc func showInDock(_ sender: Any?) {
if NSApp.activationPolicy() == .regular {
NSApp.setActivationPolicy(.accessory)
Expand Down

0 comments on commit 3cf7aa3

Please sign in to comment.