Skip to content

Use all your internet connections at once on Windows, macOS, Linux and Android: per-connection load balancing over Wi-Fi, Ethernet, mobile data and phone tethers

Fix tray icon missing when started at sign-in

Rohit Mahto429c8e105476
diff --git a/autostart_windows.go b/autostart_windows.go
index ba1feda..f0dfedb 100644
--- a/autostart_windows.go
+++ b/autostart_windows.go
@@ -34,7 +34,7 @@ func taskXML(user, exe string) string {
return `<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo><Description>Starts net-merge (multi-adapter connection balancer) at sign-in.</Description></RegistrationInfo>
- <Triggers><LogonTrigger><Enabled>true</Enabled><UserId>` + xmlEscape(user) + `</UserId></LogonTrigger></Triggers>
+ <Triggers><LogonTrigger><Enabled>true</Enabled><UserId>` + xmlEscape(user) + `</UserId><Delay>PT10S</Delay></LogonTrigger></Triggers>
<Principals><Principal id="Author"><UserId>` + xmlEscape(user) + `</UserId><LogonType>InteractiveToken</LogonType><RunLevel>HighestAvailable</RunLevel></Principal></Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
@@ -43,6 +43,7 @@ func taskXML(user, exe string) string {
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
+ <RestartOnFailure><Interval>PT1M</Interval><Count>5</Count></RestartOnFailure>
<Enabled>true</Enabled>
</Settings>
<Actions Context="Author"><Exec><Command>` + xmlEscape(exe) + `</Command><Arguments>tray</Arguments></Exec></Actions>
diff --git a/elevate_windows.go b/elevate_windows.go
index 8921f5f..3c1b140 100644
--- a/elevate_windows.go
+++ b/elevate_windows.go
@@ -1,6 +1,7 @@
package main
import (
+ "log"
"os"
"path/filepath"
"strings"
@@ -34,6 +35,7 @@ func instanceRunning() bool {
// logger) at f, for the hidden elevated process that has no console.
func redirectStd(f *os.File) {
os.Stdout, os.Stderr = f, f
+ log.SetOutput(f) // the std logger captured the old stderr at init; the tray library logs through it
windows.SetStdHandle(windows.STD_OUTPUT_HANDLE, windows.Handle(f.Fd()))
windows.SetStdHandle(windows.STD_ERROR_HANDLE, windows.Handle(f.Fd()))
}
diff --git a/tray_windows.go b/tray_windows.go
index 17e5da3..a83be6c 100644
--- a/tray_windows.go
+++ b/tray_windows.go
@@ -7,8 +7,10 @@ import (
"path/filepath"
"strings"
"time"
+ "unsafe"
"fyne.io/systray"
+ "golang.org/x/sys/windows"
)
const trayRows = 4 // adapters shown in the menu; extra rows stay hidden
@@ -60,11 +62,47 @@ func cmdTray(args []string) {
}
tun.b = bal
+ // Started at sign-in this process can beat explorer.exe by a fraction of a second. With no
+ // taskbar the tray library fails to create the icon and never becomes ready, so wait for it.
+ if !waitForTaskbar(3 * time.Minute) {
+ logf("warning: taskbar not found after 3 minutes, trying anyway\n")
+ }
+ ready := make(chan struct{})
+ go func() { // safety net: if the icon still can't be created, exit so the scheduled task retries
+ select {
+ case <-ready:
+ case <-time.After(30 * time.Second):
+ logf("error: tray icon did not initialise within 30s; exiting so the scheduled task can restart it\n")
+ os.Exit(4)
+ }
+ }()
+
ui := &trayUI{tun: tun, sched: sched, bal: bal, stateDir: c.stateDir, startOn: !*startOff,
- iconOn: trayIcon(true), iconOff: trayIcon(false), toggleReq: make(chan struct{}, 1)}
+ iconOn: trayIcon(true), iconOff: trayIcon(false), toggleReq: make(chan struct{}, 1), ready: ready}
systray.Run(ui.onReady, func() { tun.Off(); clearStatus(c.stateDir) })
}
+// waitForTaskbar blocks until the shell's taskbar and notification area exist.
+func waitForTaskbar(max time.Duration) bool {
+ user32 := windows.NewLazySystemDLL("user32.dll")
+ findWindow, findWindowEx := user32.NewProc("FindWindowW"), user32.NewProc("FindWindowExW")
+ taskbar, _ := windows.UTF16PtrFromString("Shell_TrayWnd")
+ notify, _ := windows.UTF16PtrFromString("TrayNotifyWnd")
+ deadline := time.Now().Add(max)
+ for {
+ if h, _, _ := findWindow.Call(uintptr(unsafe.Pointer(taskbar)), 0); h != 0 {
+ if n, _, _ := findWindowEx.Call(h, 0, uintptr(unsafe.Pointer(notify)), 0); n != 0 {
+ time.Sleep(time.Second) // let the notification area finish initialising
+ return true
+ }
+ }
+ if time.Now().After(deadline) {
+ return false
+ }
+ time.Sleep(500 * time.Millisecond)
+ }
+}
+
func checkWintun() error {
exe, _ := os.Executable()
if _, err := os.Stat(filepath.Join(filepath.Dir(exe), "wintun.dll")); err != nil {
@@ -82,6 +120,7 @@ type trayUI struct {
iconOn []byte
iconOff []byte
toggleReq chan struct{}
+ ready chan struct{} // closed once the tray icon exists
header, toggleItem, v6Item *systray.MenuItem
rows []*systray.MenuItem
@@ -94,6 +133,9 @@ type trayUI struct {
}
func (u *trayUI) onReady() {
+ if u.ready != nil {
+ close(u.ready)
+ }
systray.SetIcon(u.iconOff)
systray.SetTooltip("net-merge")
u.header = systray.AddMenuItem("net-merge", "")