From 8365dfecafbceefff4d29507260b42fcc96d8cce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kr=C3=BCger?= Date: Mon, 14 Jul 2025 10:25:53 +0200 Subject: [PATCH 1/2] feat: Implement mutex for single instance execution --- main.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/main.go b/main.go index 0e92805..1e0d094 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,11 @@ package main import ( _ "embed" "encoding/json" + "fmt" "os" + "golang.org/x/sys/windows" + "github.com/getlantern/systray" ) @@ -24,8 +27,24 @@ var config Config var tailscalePath string var currentVersion = "v2.1.2" var startupDir = os.Getenv("APPDATA") + `\Microsoft\Windows\Start Menu\Programs\Startup` +var mutexHandle windows.Handle func main() { + mutexName, _ := windows.UTF16PtrFromString("Global\\AutoExitNodeMutex") + handle, err := windows.CreateMutex(nil, false, mutexName) + if err != nil { + fmt.Println("Failed to create mutex:", err) + os.Exit(1) + } + mutexHandle = handle + lastErr := windows.GetLastError() + if lastErr == windows.ERROR_ALREADY_EXISTS { + fmt.Println("Programmet kører allerede.") + os.Exit(0) + } + defer windows.ReleaseMutex(mutexHandle) + defer windows.CloseHandle(mutexHandle) + loadConfig() tailscaleAvailable = isValidTailscalePath(tailscalePath) systray.Run(autoExitNote, nil) From 91d73cb8863595a74093819e83b53b3355b55bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kr=C3=BCger?= Date: Mon, 14 Jul 2025 11:34:02 +0200 Subject: [PATCH 2/2] fix: Update error message for single instance check --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 1e0d094..764b898 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,7 @@ func main() { mutexHandle = handle lastErr := windows.GetLastError() if lastErr == windows.ERROR_ALREADY_EXISTS { - fmt.Println("Programmet kører allerede.") + fmt.Println("Only one instance is allowed.") os.Exit(0) } defer windows.ReleaseMutex(mutexHandle)