Files
Reims-Go/internal/packetbridge/installer_windows.go
T

92 lines
3.0 KiB
Go

//go:build windows
package packetbridge
import (
"context"
"unsafe"
"github.com/Nwflower/reims-go/internal/networkmsg"
"golang.org/x/sys/windows"
)
func verifyInstaller(path string) error {
name, err := windows.UTF16PtrFromString(path)
if err != nil {
return err
}
file := windows.WinTrustFileInfo{Size: uint32(unsafe.Sizeof(windows.WinTrustFileInfo{})), FilePath: name}
data := windows.WinTrustData{Size: uint32(unsafe.Sizeof(windows.WinTrustData{})), UIChoice: windows.WTD_UI_NONE, UnionChoice: windows.WTD_CHOICE_FILE, FileOrCatalogOrBlobOrSgnrOrCert: unsafe.Pointer(&file), StateAction: windows.WTD_STATEACTION_VERIFY}
err = windows.WinVerifyTrustEx(windows.InvalidHWND, &windows.WINTRUST_ACTION_GENERIC_VERIFY_V2, &data)
data.StateAction = windows.WTD_STATEACTION_CLOSE
_ = windows.WinVerifyTrustEx(windows.InvalidHWND, &windows.WINTRUST_ACTION_GENERIC_VERIFY_V2, &data)
return err
}
type installerShellInfo struct {
Size, Mask uint32
Window uintptr
Verb, File, Parameters, Directory *uint16
Show int32
Instance, IDList uintptr
Class *uint16
ClassKey uintptr
HotKey uint32
Icon uintptr
Process windows.Handle
}
// 句柄由 installMu 保护;调用方取消后后台继续等待同一安装程序。
var installerProcess windows.Handle
func runInstaller(ctx context.Context, path string) error {
if err := ctx.Err(); err != nil {
return err
}
_, statePath, _ := defaultGuardPaths()
if installerProcess == 0 {
verb, _ := windows.UTF16PtrFromString("runas")
file, _ := windows.UTF16PtrFromString(path)
params, _ := windows.UTF16PtrFromString("/winpcap_mode=no /dot11_support=no")
info := installerShellInfo{Mask: 0x40, Verb: verb, File: file, Parameters: params, Show: 1}
info.Size = uint32(unsafe.Sizeof(info))
ok, _, err := windows.NewLazySystemDLL("shell32.dll").NewProc("ShellExecuteExW").Call(uintptr(unsafe.Pointer(&info)))
if ok == 0 {
return networkmsg.Error("npcap_installer_open", err)
}
installerProcess = info.Process
recordTrackedInstaller(statePath, installerProcess, path)
}
for {
status, err := windows.WaitForSingleObject(installerProcess, 200)
if err != nil {
clearTrackedInstaller(statePath)
return err
}
if status == windows.WAIT_OBJECT_0 {
break
}
// 调用方可取消等待,但后台必须等到安装进程退出才能释放启动限制。
}
var code uint32
err := windows.GetExitCodeProcess(installerProcess, &code)
_ = windows.CloseHandle(installerProcess)
installerProcess = 0
clearTrackedInstaller(statePath)
if err != nil {
return err
}
switch code {
case 0:
return nil
case 3010:
return networkmsg.Error("npcap_reboot_required", nil)
case 350:
return networkmsg.Error("npcap_reboot_retry", nil)
case 1:
return networkmsg.Error("npcap_cancelled", nil)
default:
return networkmsg.Error("npcap_install_failed", nil, "status", code)
}
}