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
Go androidbandwidth-aggregationdownloadergolanglinuxload-balancingmacosnet-mergenetworkingtetheringtunwifiwindowswintun
Tray: Start at sign-in checkbox on Windows, macOS and Linux (#28) (#29)
Rohit Mahto0ca39ee5ca59
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2f12be8..cc8fda1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -5,6 +5,11 @@ entry here before pushing a version tag.## [Unreleased]+### Added+- **Start at sign-in from the tray menu** on Windows, macOS and Linux (#28): tick *Start at sign-in*. It+ does what `net-merge autostart on` does, without a terminal. On a Mac it needs `sudo net-merge install` once,+ as before.+### Fixed- **Windows: fewer antivirus false positives.** `net-merge.exe` and `net-merge-tray.exe` now carry aname, version, icon and manifest. Bare unsigned Go programs are what antivirus machine-learning modelsdiff --git a/README.md b/README.mdindex 3ec76d1..a9a34d3 100644--- a/README.md+++ b/README.md@@ -73,7 +73,7 @@ irm https://raw.githubusercontent.com/rohit267/net-merge/main/install.ps1 | iexThat's it: net-merge is installed and its icon appears in the menu bar or tray. On Linux the scriptuses your package manager (apt, dnf, zypper, apk or pacman) and on macOS Homebrew when you have it, so-net-merge updates with the rest of your system. To start it at sign-in: `net-merge autostart on`.+net-merge updates with the rest of your system. To start it at sign-in, tick *Start at sign-in* in its menu.<details><summary><strong>Rather type the commands yourself?</strong> One block per system, each ready to paste.</summary>@@ -301,7 +301,8 @@ net-merge when you do not want that.4. **Hover** the icon to see the speed of each connection and the total.5. **Right-click** the icon (on Mac, click it) to open the menu. It shows each connection's speed, howmany connections net-merge gave it, and the buttons *Pause merging* / *Start merging* and *Quit*.-6. To start it at sign-in: tick the box in the Windows installer, or run `net-merge autostart on`.+6. To start it at sign-in: tick *Start at sign-in* in the menu (or the box in the Windows installer, or run+ `net-merge autostart on`). On a Mac this needs `sudo net-merge install` once.Tips:- Torrent clients already use every connection. Leave qBittorrent's network interface on *Any interface*.diff --git a/internal/platform/autostart_darwin.go b/internal/platform/autostart_darwin.goindex 47cf83d..173496b 100644--- a/internal/platform/autostart_darwin.go+++ b/internal/platform/autostart_darwin.go@@ -5,18 +5,28 @@ import ("fmt""os""os/exec"+ "os/user""path/filepath""strconv")const agentLabel = "io.github.rohit267.net-merge"-func agentPath() (string, error) {- home, err := os.UserHomeDir()- if err != nil {- return "", err+// signInUser is the account whose sign-in starts net-merge. The tray runs as root through sudo,+// so there it is SUDO_UID's account, not root's.+func signInUser() (*user.User, error) {+ if os.Geteuid() != 0 {+ return user.Current()+ }+ uid := os.Getenv("SUDO_UID")+ if uid == "" || uid == "0" {+ return nil, errors.New("start at sign-in needs the tray started from your account (sudo sets SUDO_UID)")}- return filepath.Join(home, "Library", "LaunchAgents", agentLabel+".plist"), nil+ return user.LookupId(uid)+}++func agentPath(u *user.User) string {+ return filepath.Join(u.HomeDir, "Library", "LaunchAgents", agentLabel+".plist")}// Autostart turns start-at-sign-in "on" or "off", or prints its "status". It is a LaunchAgent@@ -26,15 +36,15 @@ func Autostart(action string) error {if os.Geteuid() == 0 {return errors.New("run this as your normal user, not with sudo")}- path, err := agentPath()+ u, err := signInUser()if err != nil {return err}- domain := "gui/" + strconv.Itoa(os.Getuid())- _, statErr := os.Stat(path)+ path := agentPath(u)+ domain := "gui/" + u.Uidswitch action {case "status":- if statErr == nil {+ if AutostartEnabled() {fmt.Println("autostart is ON (" + path + ").")} else {fmt.Println("autostart is OFF.")@@ -42,20 +52,60 @@ func Autostart(action string) error {return nilcase "off":exec.Command("/bin/launchctl", "bootout", domain+"/"+agentLabel).Run() // not loaded is fine- if statErr != nil {+ if !AutostartEnabled() {fmt.Println("autostart is already OFF.")return nil}- if err := os.Remove(path); err != nil {+ if err := SetAutostart(false); err != nil {return err}fmt.Println("autostart is OFF.")return nil}- if exec.Command("/usr/bin/sudo", "-n", "-l", InstalledExe(), "tray").Run() != nil {+ if err := SetAutostart(true); err != nil {+ return err+ }+ exec.Command("/bin/launchctl", "bootout", domain+"/"+agentLabel).Run()+ if out, err := exec.Command("/bin/launchctl", "bootstrap", domain, path).CombinedOutput(); err != nil {+ return fmt.Errorf("launchctl bootstrap: %w: %s", err, out)+ }+ fmt.Println("autostart is ON: net-merge starts at sign-in (" + path + ").")+ return nil+}++// AutostartEnabled reports whether net-merge starts at sign-in.+func AutostartEnabled() bool {+ u, err := signInUser()+ if err != nil {+ return false+ }+ _, err = os.Stat(agentPath(u))+ return err == nil+}++// SetAutostart writes or removes the LaunchAgent; it takes effect at the next sign-in. It works+// from the root tray too, writing into the user's home with the user as owner. It does not unload+// the agent: if the agent started this tray, unloading it would quit the tray.+func SetAutostart(on bool) error {+ u, err := signInUser()+ if err != nil {+ return err+ }+ path := agentPath(u)+ if !on {+ if err := os.Remove(path); err != nil && !os.IsNotExist(err) {+ return err+ }+ return nil+ }+ check := []string{"-n", "-l", InstalledExe(), "tray"}+ if os.Geteuid() == 0 {+ check = append([]string{"-U", u.Username}, check...) // root may always; ask about the user+ }+ if exec.Command("/usr/bin/sudo", check...).Run() != nil {return errors.New("the tray can't start at sign-in without the passwordless rule; run once: sudo net-merge install")}- home, _ := os.UserHomeDir()+ log := filepath.Join(u.HomeDir, "Library", "Logs", "net-merge.log")plist := `<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict>@@ -64,20 +114,22 @@ func Autostart(action string) error {<key>RunAtLoad</key><true/><key>KeepAlive</key><dict><key>SuccessfulExit</key><false/></dict><key>ThrottleInterval</key><integer>30</integer>-<key>StandardOutPath</key><string>` + filepath.Join(home, "Library", "Logs", "net-merge.log") + `</string>-<key>StandardErrorPath</key><string>` + filepath.Join(home, "Library", "Logs", "net-merge.log") + `</string>+<key>StandardOutPath</key><string>` + log + `</string>+<key>StandardErrorPath</key><string>` + log + `</string></dict></plist>`- if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {+ dir := filepath.Dir(path)+ if err := os.MkdirAll(dir, 0o755); err != nil {return err}if err := os.WriteFile(path, []byte(plist), 0o644); err != nil {return err}- exec.Command("/bin/launchctl", "bootout", domain+"/"+agentLabel).Run()- if out, err := exec.Command("/bin/launchctl", "bootstrap", domain, path).CombinedOutput(); err != nil {- return fmt.Errorf("launchctl bootstrap: %w: %s", err, out)+ if os.Geteuid() == 0 {+ uid, _ := strconv.Atoi(u.Uid)+ gid, _ := strconv.Atoi(u.Gid)+ os.Chown(dir, uid, gid)+ return os.Chown(path, uid, gid)}- fmt.Println("autostart is ON: net-merge starts at sign-in (" + path + ").")return nil}diff --git a/internal/platform/autostart_linux.go b/internal/platform/autostart_linux.goindex 477a9de..45c2b86 100644--- a/internal/platform/autostart_linux.go+++ b/internal/platform/autostart_linux.go@@ -45,26 +45,58 @@ func Autostart(action string) error {if err != nil {return err}- _, statErr := os.Stat(path)switch action {case "status":- if statErr == nil {+ if AutostartEnabled() {fmt.Println("autostart is ON (" + path + ").")} else {fmt.Println("autostart is OFF.")}return nilcase "off":- if statErr != nil {+ if !AutostartEnabled() {fmt.Println("autostart is already OFF.")return nil}- if err := os.Remove(path); err != nil {+ if err := SetAutostart(false); err != nil {return err}fmt.Println("autostart is OFF.")return nil}+ if err := SetAutostart(true); err != nil {+ return err+ }+ fmt.Println("autostart is ON: net-merge starts at sign-in (" + path + ").")+ if !IsAdmin() {+ fmt.Println("note: the tray needs CAP_NET_ADMIN to start merging; see `net-merge tray` or the README.")+ }+ return nil+}++// AutostartEnabled reports whether net-merge starts at sign-in.+func AutostartEnabled() bool {+ path, err := autostartFile()+ if err != nil {+ return false+ }+ _, err = os.Stat(path)+ return err == nil+}++// SetAutostart writes or removes the autostart entry. The tray runs as the signed-in user (with+// CAP_NET_ADMIN), so that user's config directory is the right one.+func SetAutostart(on bool) error {+ path, err := autostartFile()+ if err != nil {+ return err+ }+ if !on {+ if err := os.Remove(path); err != nil && !os.IsNotExist(err) {+ return err+ }+ return nil+ }if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {return err}@@ -75,12 +107,5 @@ func Autostart(action string) error {"Exec=" + desktopQuote(trayExe()) + " tray\n" +"Terminal=false\n" +"X-GNOME-Autostart-enabled=true\n"- if err := os.WriteFile(path, []byte(entry), 0o644); err != nil {- return err- }- fmt.Println("autostart is ON: net-merge starts at sign-in (" + path + ").")- if !IsAdmin() {- fmt.Println("note: the tray needs CAP_NET_ADMIN to start merging; see `net-merge tray` or the README.")- }- return nil+ return os.WriteFile(path, []byte(entry), 0o644)}diff --git a/internal/platform/autostart_linux_test.go b/internal/platform/autostart_linux_test.gonew file mode 100644index 0000000..eecf46c--- /dev/null+++ b/internal/platform/autostart_linux_test.go@@ -0,0 +1,24 @@+package platform++import "testing"++func TestSetAutostart(t *testing.T) {+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())+ if AutostartEnabled() {+ t.Fatal("on before SetAutostart")+ }+ if err := SetAutostart(true); err != nil {+ t.Fatal(err)+ }+ if !AutostartEnabled() {+ t.Fatal("still off after SetAutostart(true)")+ }+ for i := 0; i < 2; i++ { // turning off twice is fine+ if err := SetAutostart(false); err != nil {+ t.Fatal(err)+ }+ }+ if AutostartEnabled() {+ t.Fatal("still on after SetAutostart(false)")+ }+}diff --git a/internal/platform/autostart_windows.go b/internal/platform/autostart_windows.goindex 9940d43..065959f 100644--- a/internal/platform/autostart_windows.go+++ b/internal/platform/autostart_windows.go@@ -63,7 +63,7 @@ func utf16le(s string) []byte {// Autostart turns start-at-sign-in "on" or "off", or prints its "status".func Autostart(action string) error {if action == "status" {- if run("schtasks", "/Query", "/TN", taskName) == nil {+ if AutostartEnabled() {fmt.Println("autostart is ON (starts at sign-in, no UAC prompt).")} else {fmt.Println("autostart is OFF.")@@ -74,16 +74,34 @@ func Autostart(action string) error {return RelaunchElevated([]string{"autostart", action}, false)}if action == "off" {- if run("schtasks", "/Query", "/TN", taskName) != nil {+ if !AutostartEnabled() {fmt.Println("autostart is already OFF.")return nil}- if err := run("schtasks", "/Delete", "/F", "/TN", taskName); err != nil {+ if err := SetAutostart(false); err != nil {return err}fmt.Println("autostart is OFF.")return nil}+ if err := SetAutostart(true); err != nil {+ return err+ }+ fmt.Println("autostart is ON: net-merge starts at sign-in without a UAC prompt.")+ return nil+}++// AutostartEnabled reports whether net-merge starts at sign-in.+func AutostartEnabled() bool { return run("schtasks", "/Query", "/TN", taskName) == nil }++// SetAutostart adds or removes the sign-in task. It needs admin, which the tray has.+func SetAutostart(on bool) error {+ if !on {+ if !AutostartEnabled() {+ return nil+ }+ return run("schtasks", "/Delete", "/F", "/TN", taskName)+ }user := os.Getenv("USERNAME")if d := os.Getenv("USERDOMAIN"); d != "" {user = d + `\` + user@@ -93,11 +111,7 @@ func Autostart(action string) error {return err}defer os.Remove(tmp)- if err := run("schtasks", "/Create", "/F", "/TN", taskName, "/XML", tmp); err != nil {- return err- }- fmt.Println("autostart is ON: net-merge starts at sign-in without a UAC prompt.")- return nil+ return run("schtasks", "/Create", "/F", "/TN", taskName, "/XML", tmp)}// HasConsole is false for the windowless tray build and for a double-clicked GUI launch.diff --git a/internal/tray/tray.go b/internal/tray/tray.goindex 95d7886..08e12d1 100644--- a/internal/tray/tray.go+++ b/internal/tray/tray.go@@ -1,5 +1,5 @@// Package tray is the notification-area icon: green/grey for on/off, live speeds on hover, and-// a menu to pause, change the IPv6 mode or quit.+// a menu to pause, change the IPv6 mode, start at sign-in or quit.package trayimport (@@ -13,6 +13,7 @@ import ("net-merge/internal/engine""net-merge/internal/icon""net-merge/internal/logx"+ "net-merge/internal/platform""net-merge/internal/state""net-merge/internal/tun")@@ -42,14 +43,14 @@ type trayUI struct {ap autoPauser- header, toggleItem, v6Item, autoItem *systray.MenuItem- rows []*systray.MenuItem- lastOn *bool- lastHeader string- lastTip string- lastRows [trayRows]string- lastErr string- busyUntil time.Time+ header, toggleItem, v6Item, autoItem, signInItem *systray.MenuItem+ rows []*systray.MenuItem+ lastOn *bool+ lastHeader string+ lastTip string+ lastRows [trayRows]string+ lastErr string+ busyUntil time.Time}func (u *trayUI) onReady() {@@ -70,6 +71,7 @@ func (u *trayUI) onReady() {systray.AddSeparator()u.v6Item = systray.AddMenuItemCheckbox("Use all adapters (IPv6 only if every adapter has it)", "Ticked: apps are pushed to IPv4 so every adapter is used. Unticked: IPv6 is allowed and funnels onto the adapters that have it.", u.bal.V6Mode() == engine.V6Auto)u.autoItem = systray.AddMenuItemCheckbox("Pause when there is only one connection", "Pauses merging by itself when fewer than two connections have internet, and starts it again when a second one works. Pausing or starting by hand always wins.", u.ap.enabled)+ u.signInItem = systray.AddMenuItemCheckbox("Start at sign-in", "Starts net-merge when you sign in, with no password or admin prompt.", platform.AutostartEnabled())u.toggleItem = systray.AddMenuItem("Start merging", "")quit := systray.AddMenuItem("Quit", "Turns system-wide mode off and exits")@@ -100,6 +102,17 @@ func (u *trayUI) onReady() {u.autoItem.Check()}u.ap.setEnabled(u.autoItem.Checked(), u.tun.IsOn())+ case <-u.signInItem.ClickedCh:+ on := !u.signInItem.Checked()+ u.lastErr = ""+ if err := platform.SetAutostart(on); err != nil {+ u.lastErr = "start at sign-in: " + err.Error()+ logx.Printf("error: %s\n", u.lastErr)+ } else if on {+ u.signInItem.Check()+ } else {+ u.signInItem.Uncheck()+ }case <-u.v6Item.ClickedCh:if u.v6Item.Checked() {u.v6Item.Uncheck()