summaryrefslogtreecommitdiff
path: root/cmd/client/kill_windows.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/client/kill_windows.go')
-rw-r--r--cmd/client/kill_windows.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/cmd/client/kill_windows.go b/cmd/client/kill_windows.go
new file mode 100644
index 0000000..ba8c75a
--- /dev/null
+++ b/cmd/client/kill_windows.go
@@ -0,0 +1,56 @@
+//go:build windows
+
+package main
+
+import (
+ "log"
+ "os"
+ "os/exec"
+ "strconv"
+ "time"
+)
+
+func killPrevious() {
+ myPID := os.Getpid()
+ log.Printf("killing previous instances (my PID: %d)", myPID)
+
+ // Kill other vpnem.exe processes (not ourselves)
+ // Use WMIC to find PIDs, then taskkill each except ours
+ out, _ := exec.Command("wmic", "process", "where",
+ "name='vpnem.exe'", "get", "processid", "/format:list").Output()
+ for _, line := range splitLines(string(out)) {
+ if len(line) > 10 && line[:10] == "ProcessId=" {
+ pidStr := line[10:]
+ pid, err := strconv.Atoi(pidStr)
+ if err == nil && pid != myPID {
+ log.Printf("killing old vpnem.exe PID %d", pid)
+ exec.Command("taskkill", "/F", "/PID", strconv.Itoa(pid)).Run()
+ }
+ }
+ }
+
+ // Kill any orphaned sing-box.exe
+ exec.Command("taskkill", "/F", "/IM", "sing-box.exe").Run()
+ time.Sleep(500 * time.Millisecond)
+}
+
+func splitLines(s string) []string {
+ var lines []string
+ start := 0
+ for i := 0; i < len(s); i++ {
+ if s[i] == '\n' || s[i] == '\r' {
+ line := s[start:i]
+ if len(line) > 0 && line[len(line)-1] == '\r' {
+ line = line[:len(line)-1]
+ }
+ if line != "" {
+ lines = append(lines, line)
+ }
+ start = i + 1
+ }
+ }
+ if start < len(s) {
+ lines = append(lines, s[start:])
+ }
+ return lines
+}