-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtray.go
More file actions
169 lines (150 loc) · 4.29 KB
/
tray.go
File metadata and controls
169 lines (150 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"crypto/rand"
"fmt"
"math/big"
"time"
"github.com/getlantern/systray"
)
var lastSSID string
var lastCellular bool
var lastCommand string
var tailscaleAvailable = true
var checkInterval = 5 * time.Second
// Use crypto/rand for secure random interval
var updateInterval = getSecureRandomInterval()
// getSecureRandomInterval returns a random duration between 1 and 60 minutes using crypto/rand.
func getSecureRandomInterval() time.Duration {
n, err := rand.Int(rand.Reader, big.NewInt(60))
if err != nil {
// fallback to 15 minutes if crypto/rand fails
return 15 * time.Minute
}
return time.Duration(n.Int64()+1) * time.Minute
}
func autoExitNote() {
mStatus := systray.AddMenuItem("Status: Initializing...", "Current network status")
mStatus.Disable()
mVersion := systray.AddMenuItem(fmt.Sprintf("Version: %s", currentVersion), "Current version")
mVersion.Disable()
mForce := systray.AddMenuItem("Force Sync", "Force immediate sync")
mRunAtStartup := systray.AddMenuItemCheckbox("Run at startup", "Toggle auto-start", isStartupEnabled())
mCheckUpdate := systray.AddMenuItem("Check for update", "Check for new version")
mQuit := systray.AddMenuItem("Quit", "Exit the app")
if !tailscaleAvailable {
mForce.Disable()
systray.SetIcon(iconInactive)
systray.SetTooltip("Tailscale not found! Please install Tailscale.")
} else {
systray.SetIcon(iconInactive)
systray.SetTooltip("AutoExitNode - Tailscale controller")
}
go func() {
for {
select {
case <-mForce.ClickedCh:
checkAndApply(mStatus)
case <-mRunAtStartup.ClickedCh:
if isStartupEnabled() {
removeStartupShortcut()
mRunAtStartup.Uncheck()
} else {
addStartupShortcut()
mRunAtStartup.Check()
}
case <-mCheckUpdate.ClickedCh:
go func() {
checkForUpdate(func(ver, url string) {
updateVersionMenu(mVersion, ver, url)
})
}()
case <-mQuit.ClickedCh:
systray.Quit()
return
}
}
}()
go func() {
for {
checkAndApply(mStatus)
time.Sleep(checkInterval)
}
}()
go func() {
for {
checkForUpdate(func(ver, url string) {
updateVersionMenu(mVersion, ver, url)
})
time.Sleep(updateInterval)
}
}()
}
// checkAndApply handles the main logic for tray status and tailscale actions.
func checkAndApply(mStatus *systray.MenuItem) {
if !tailscaleAvailable {
return
}
ssid, err := getCurrentSSID()
cell := isCellularConnected()
online := hasInternetConnection()
statusText := ""
tooltip := ""
var icon []byte = iconInactive
command := ""
switch {
case cell:
statusText = "Cellular"
tooltip = fmt.Sprintf("Active: %s via cellular", getExitNodeName())
icon = iconActive
command = "activated"
case err != nil || ssid == "":
statusText = "Untrusted SSID"
tooltip = fmt.Sprintf("Active: %s (unknown network)", getExitNodeName())
icon = iconActive
command = "activated"
case !online:
statusText = "No Internet"
tooltip = fmt.Sprintf("Active: %s (unknown network)", getExitNodeName())
icon = iconActive
command = "activated"
activateExitNode()
case isSSIDTrusted(ssid):
statusText = fmt.Sprintf("Trusted SSID: %s", ssid)
tooltip = fmt.Sprintf("Inactive: trusted network (%s)", ssid)
icon = iconInactive
command = "deactivated"
default:
statusText = "Untrusted SSID"
tooltip = fmt.Sprintf("Active: %s (untrusted SSID)", getExitNodeName())
icon = iconActive
command = "activated"
}
mStatus.SetTitle("Status: " + statusText)
systray.SetIcon(icon)
systray.SetTooltip(tooltip)
if ssid == lastSSID && cell == lastCellular && lastCommand == command && online {
return
}
lastSSID = ssid
lastCellular = cell
if lastCommand != command {
if command == "activated" {
fmt.Println("[Activate] via", statusText)
activateExitNode()
} else {
fmt.Println("[Deactivate] via", statusText)
deactivateExitNode()
}
lastCommand = command
}
}
// updateVersionMenu updates the version menu item if a new version is available.
func updateVersionMenu(mVersion *systray.MenuItem, ver, url string) {
if ver != "" && ver != currentVersion {
mVersion.SetTitle(fmt.Sprintf("Version: %s (Update: %s)", currentVersion, ver))
mVersion.SetTooltip(fmt.Sprintf("New version available: %s\n%s", ver, url))
} else {
mVersion.SetTitle(fmt.Sprintf("Version: %s", currentVersion))
mVersion.SetTooltip("Current version")
}
}