fix: 隐藏后台命令窗口 (#33)
This commit is contained in:
@@ -14,8 +14,11 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
const downloadTimeout = 180 * time.Second
|
||||
@@ -215,6 +218,7 @@ func runFFProbe(ctx context.Context, path string) (ProbeResult, error) {
|
||||
command := exec.CommandContext(ctx, ffprobe,
|
||||
"-v", "error", "-select_streams", "v:0",
|
||||
"-show_entries", "format=duration,size,format_name:stream=width,height", "-of", "json", path)
|
||||
command.SysProcAttr = &syscall.SysProcAttr{HideWindow: true, CreationFlags: windows.CREATE_NO_WINDOW}
|
||||
output, err := command.Output()
|
||||
if err != nil {
|
||||
return ProbeResult{}, fmt.Errorf("ffprobe 执行失败:%w", err)
|
||||
|
||||
@@ -13,9 +13,11 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"cmsp/internal/config"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
const browserStateName = "浏览器运行状态.json"
|
||||
@@ -280,7 +282,7 @@ func processExists(pid int) bool {
|
||||
if pid <= 0 {
|
||||
return false
|
||||
}
|
||||
output, err := exec.Command("tasklist", "/FI", fmt.Sprintf("PID eq %d", pid), "/FO", "CSV", "/NH").Output()
|
||||
output, err := hiddenCommand("tasklist", "/FI", fmt.Sprintf("PID eq %d", pid), "/FO", "CSV", "/NH").Output()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@@ -303,7 +305,7 @@ func processCommandLine(pid int) (string, error) {
|
||||
`$c=(Get-CimInstance Win32_Process -Filter "ProcessId=%d").CommandLine; `+
|
||||
`if ($null -eq $c) { '' } else { `+
|
||||
`[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($c)) }`, pid)
|
||||
output, err := exec.Command("powershell", "-NoProfile", "-Command", command).Output()
|
||||
output, err := hiddenCommand("powershell", "-NoProfile", "-Command", command).Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("读取 PID %d 的命令行失败:%w", pid, err)
|
||||
}
|
||||
@@ -323,7 +325,7 @@ func processCommandLine(pid int) (string, error) {
|
||||
}
|
||||
|
||||
func killProcessTree(pid int) error {
|
||||
output, err := exec.Command("taskkill", "/PID", strconv.Itoa(pid), "/T", "/F").CombinedOutput()
|
||||
output, err := hiddenCommand("taskkill", "/PID", strconv.Itoa(pid), "/T", "/F").CombinedOutput()
|
||||
if err != nil {
|
||||
message := strings.TrimSpace(string(output))
|
||||
if message == "" {
|
||||
@@ -333,3 +335,9 @@ func killProcessTree(pid int) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func hiddenCommand(name string, args ...string) *exec.Cmd {
|
||||
cmd := exec.Command(name, args...)
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true, CreationFlags: windows.CREATE_NO_WINDOW}
|
||||
return cmd
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user