aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorAlexander Neumann <alexander.neumann@picos-software.com>2019-04-10 12:48:16 +0200
committerAlexander Neumann <alexander.neumann@picos-software.com>2019-04-23 11:04:59 +0200
commit04b3079ecaaed70fe8359c883040d12223cc6e3a (patch)
tree19fd741454012728c511c73610aafef4151c7a65
parentui: only allow a single tunnel to be active at any time (diff)
downloadwireguard-windows-04b3079ecaaed70fe8359c883040d12223cc6e3a.tar.xz
wireguard-windows-04b3079ecaaed70fe8359c883040d12223cc6e3a.zip
ui: implement [de]activation of tunnels via tray
Signed-off-by: Alexander Neumann <alexander.neumann@picos-software.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to '')
-rw-r--r--ui/tray.go59
1 files changed, 46 insertions, 13 deletions
diff --git a/ui/tray.go b/ui/tray.go
index e7e1758a..e5260ae1 100644
--- a/ui/tray.go
+++ b/ui/tray.go
@@ -14,6 +14,9 @@ import (
"golang.zx2c4.com/wireguard/windows/service"
)
+// Status + active CIDRs + deactivate + separator
+const trayTunnelActionsOffset = 4
+
type Tray struct {
*walk.NotifyIcon
@@ -56,10 +59,12 @@ func (tray *Tray) setup() error {
label string
handler walk.EventHandler
enabled bool
+ hidden bool
separator bool
}{
{label: "Status: Unknown"},
- {label: "Networks: None"},
+ {label: "Networks: None", hidden: true},
+ {label: "Deactivate", handler: tray.onDeactivateTunnel, enabled: true, hidden: true},
{separator: true},
{separator: true},
{label: "&Manage tunnels...", handler: tray.mtw.Show, enabled: true},
@@ -75,6 +80,7 @@ func (tray *Tray) setup() error {
action = walk.NewAction()
action.SetText(item.label)
action.SetEnabled(item.enabled)
+ action.SetVisible(!item.hidden)
if item.handler != nil {
action.Triggered().Attach(item.handler)
}
@@ -104,7 +110,11 @@ func (tray *Tray) addTunnelAction(tunnelName string) {
tunnelAction.SetEnabled(true)
tunnelAction.SetCheckable(true)
tunnelAction.Triggered().Attach(func() {
-
+ if activeTunnel := tray.mtw.tunnelTracker.activeTunnel; activeTunnel != nil && activeTunnel.Name == tunnelName {
+ tray.onDeactivateTunnel()
+ } else {
+ tray.onActivateTunnel(tunnelName)
+ }
})
tray.tunnels[tunnelName] = tunnelAction
@@ -125,10 +135,7 @@ func (tray *Tray) addTunnelAction(tunnelName string) {
}
}
- // Status + active CIDRs + separator
- const offset = 3
-
- tray.ContextMenu().Actions().Insert(offset+idx, tunnelAction)
+ tray.ContextMenu().Actions().Insert(trayTunnelActionsOffset+idx, tunnelAction)
}
func (tray *Tray) removeTunnelAction(tunnelName string) {
@@ -146,18 +153,29 @@ func (tray *Tray) SetTunnelStateWithNotification(tunnel *service.Tunnel, state s
actions := tray.ContextMenu().Actions()
statusAction := actions.At(0)
activeCIDRsAction := actions.At(1)
+ deactivateAction := actions.At(2)
+
+ setTunnelActionsEnabled := func(enabled bool) {
+ for i := 0; i < len(tray.tunnels); i++ {
+ action := actions.At(trayTunnelActionsOffset + i)
+ action.SetEnabled(enabled)
+ }
+ }
switch state {
case service.TunnelStarting:
- statusAction.SetText("Activating")
- tunnelAction.SetEnabled(false)
+ statusAction.SetText("Status: Activating")
+ setTunnelActionsEnabled(false)
tray.SetToolTip("WireGuard: Activating...")
+
case service.TunnelStarted:
- statusAction.SetText("Active")
+ statusAction.SetText("Status: Active")
+ setTunnelActionsEnabled(true)
config, err := tunnel.RuntimeConfig()
activeCIDRsAction.SetVisible(err == nil)
+ deactivateAction.SetVisible(err == nil)
if err == nil {
var sb strings.Builder
for i, addr := range config.Interface.Addresses {
@@ -178,15 +196,18 @@ func (tray *Tray) SetTunnelStateWithNotification(tunnel *service.Tunnel, state s
if showNotifications {
tray.ShowInfo("WireGuard Activated", fmt.Sprintf("The %s tunnel has been activated.", tunnel.Name))
}
+
case service.TunnelStopping:
- statusAction.SetText("Deactivating")
- tunnelAction.SetEnabled(false)
+ statusAction.SetText("Status: Deactivating")
+ setTunnelActionsEnabled(false)
tray.SetToolTip("WireGuard: Deactivating...")
+
case service.TunnelStopped:
- statusAction.SetText("Inactive")
+ statusAction.SetText("Status: Inactive")
activeCIDRsAction.SetVisible(false)
- tunnelAction.SetEnabled(true)
+ deactivateAction.SetVisible(false)
+ setTunnelActionsEnabled(true)
tunnelAction.SetChecked(false)
tray.SetToolTip("WireGuard: Deactivated")
@@ -195,3 +216,15 @@ func (tray *Tray) SetTunnelStateWithNotification(tunnel *service.Tunnel, state s
}
}
}
+
+func (tray *Tray) onActivateTunnel(tunnelName string) {
+ if err := tray.mtw.TunnelTracker().ActivateTunnel(&service.Tunnel{tunnelName}); err != nil {
+ walk.MsgBox(tray.mtw, "Failed to activate tunnel", err.Error(), walk.MsgBoxIconError)
+ }
+}
+
+func (tray *Tray) onDeactivateTunnel() {
+ if err := tray.mtw.TunnelTracker().DeactivateTunnel(); err != nil {
+ walk.MsgBox(tray.mtw, "Failed to deactivate tunnel", err.Error(), walk.MsgBoxIconError)
+ }
+}