| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "syscall" |
| 9 | |
| 10 | "golang.org/x/sys/windows" |
| 11 | ) |
| 12 | |
| 13 | func lockController(f *os.File) error { |
| 14 | return windows.LockFileEx(windows.Handle(f.Fd()), windows.LOCKFILE_EXCLUSIVE_LOCK|windows.LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, new(windows.Overlapped)) |
| 15 | } |
| 16 | func unlockController(f *os.File) error { |
| 17 | return windows.UnlockFileEx(windows.Handle(f.Fd()), 0, 1, 0, new(windows.Overlapped)) |
| 18 | } |
| 19 | func controllerLockContended(err error) bool { return errors.Is(err, windows.ERROR_LOCK_VIOLATION) } |
| 20 | func processExists(pid int) bool { |
| 21 | if pid <= 0 { |
| 22 | return false |
| 23 | } |
| 24 | h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid)) |
| 25 | if err != nil { |
| 26 | return !errors.Is(err, windows.ERROR_INVALID_PARAMETER) |
| 27 | } |
| 28 | defer windows.CloseHandle(h) |
| 29 | var code uint32 |
| 30 | return windows.GetExitCodeProcess(h, &code) != nil || code == 259 |
| 31 | } |
| 32 | func terminateLegacy(pid int) error { |
| 33 | return errors.New("Windows 不支持接管未验证的旧版后台") |
| 34 | } |
| 35 | func detachController(cmd *exec.Cmd) { |
| 36 | cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true, CreationFlags: windows.CREATE_NEW_PROCESS_GROUP | windows.CREATE_NO_WINDOW} |
| 37 | } |
| 38 | func sshExecutable() string { |
| 39 | exe, _ := os.Executable() |
| 40 | bundled := filepath.Join(filepath.Dir(exe), "runtime", "openssh", "ssh.exe") |
| 41 | if st, err := os.Stat(bundled); err == nil && st.Mode().IsRegular() { |
| 42 | return bundled |
| 43 | } |
| 44 | |
| 45 | |
| 46 | return filepath.Join(os.Getenv("SystemRoot"), "System32", "OpenSSH", "ssh.exe") |
| 47 | } |
| 48 | func sshEnvironment() []string { |
| 49 | |
| 50 | |
| 51 | |
| 52 | return []string{"SystemRoot=" + os.Getenv("SystemRoot"), "WINDIR=" + os.Getenv("WINDIR"), "ProgramData=" + os.Getenv("ProgramData"), "TEMP=" + os.TempDir(), "TMP=" + os.TempDir(), "PATH=" + filepath.Dir(sshExecutable()), "LANG=C", "LC_ALL=C", "TERM=xterm-256color"} |
| 53 | } |
| 54 | func defaultKeyboard() string { return "native" } |