From eadddea02fa5533b504069b8e29c7b80d7bb6b35 Mon Sep 17 00:00:00 2001 From: Han Li Date: Tue, 18 Feb 2025 18:25:31 +0800 Subject: [PATCH] Revert "hotfix: wmic deprecated (#402)" This reverts commit f822f0fc330e3feba06495780a57911d5592d5ef. --- go.mod | 2 - go.sum | 5 --- internal/shell/windows_process.go | 64 +++++++++---------------------- 3 files changed, 19 insertions(+), 52 deletions(-) diff --git a/go.mod b/go.mod index 8f0cac31..cea248be 100644 --- a/go.mod +++ b/go.mod @@ -21,14 +21,12 @@ require ( require ( atomicgo.dev/schedule v0.1.0 // indirect - github.com/StackExchange/wmi v1.2.1 // indirect github.com/andybalholm/brotli v1.1.0 // indirect github.com/andybalholm/cascadia v1.3.2 // indirect github.com/bodgit/plumbing v1.3.0 // indirect github.com/bodgit/windows v1.0.1 // indirect github.com/containerd/console v1.0.4 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect - github.com/go-ole/go-ole v1.2.5 // indirect github.com/gookit/color v1.5.4 // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect diff --git a/go.sum b/go.sum index 3377d90d..23e5a8de 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,6 @@ github.com/MarvinJWendt/testza v0.5.2 h1:53KDo64C1z/h/d/stCYCPY69bt/OSwjq5KpFNwi github.com/MarvinJWendt/testza v0.5.2/go.mod h1:xu53QFE5sCdjtMCKk8YMQ2MnymimEctc4n3EjyIYvEY= github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI= github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY= -github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA= -github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= @@ -66,8 +64,6 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY= -github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -267,7 +263,6 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/internal/shell/windows_process.go b/internal/shell/windows_process.go index 99ac940d..24666924 100644 --- a/internal/shell/windows_process.go +++ b/internal/shell/windows_process.go @@ -22,17 +22,9 @@ import ( "fmt" "os" "os/exec" - - "github.com/StackExchange/wmi" - "github.com/version-fox/vfox/internal/env" + "strings" ) -type Win32_Process struct { - ExecutablePath string - CommandLine string - ProcessId uint32 -} - type windowsProcess struct{} var process = windowsProcess{} @@ -42,44 +34,26 @@ func GetProcess() Process { } func (w windowsProcess) Open(pid int) error { - // Check if shell has hooks configured - if !env.IsHookEnv() { - return handleNoHookFallback(pid) - } - - // Query WMI for process info - var processes []Win32_Process - query := fmt.Sprintf("SELECT ExecutablePath FROM Win32_Process WHERE ProcessId = %d", pid) - if err := wmi.Query(query, &processes); err != nil { - return fmt.Errorf("WMI query failed: %w", err) - } - - if len(processes) == 0 { - return fmt.Errorf("process with PID %d not found", pid) + // On Windows, os.FindProcess does not actually find the process. + // So, we use this workaround to get the parent process name. + cmd := exec.Command("tasklist", "/FI", fmt.Sprintf("PID eq %d", pid), "/NH", "/FO", "CSV") + output, err := cmd.Output() + if err != nil { + return err } - - // Get executable path - path := processes[0].ExecutablePath - if path == "" { - return fmt.Errorf("executable path not found for PID %d", pid) + cmd = exec.Command("wmic", "process", "where", fmt.Sprintf("ProcessId=%d", pid), "get", "ExecutablePath", "/format:list") + output, err = cmd.Output() + if err != nil { + return err } - - // Launch new shell process with proper environment - cmd := exec.Command(path) - cmd.Env = os.Environ() - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if err := cmd.Run(); err != nil { - return fmt.Errorf("failed to launch shell: %w", err) + path := strings.TrimPrefix(strings.TrimSpace(string(output)), "ExecutablePath=") + command := exec.Command(path) + command.Env = os.Environ() + command.Stdin = os.Stdin + command.Stdout = os.Stdout + command.Stderr = os.Stderr + if err := command.Run(); err != nil { + return fmt.Errorf("open a new shell failed, err:%w", err) } - - return nil -} - -func handleNoHookFallback(pid int) error { - // Fall back to global scope if no hooks - fmt.Println("Warning: The current shell lacks hook support. Switching to global scope.") return nil }