//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 }