Delete functions that no product code calls, and fold "simple plus WithOptions" pairs into the one signature callers actually use. Rename the live macOS lifecycle functions that were labelled Legacy. The architecture tests now share one source loader and reject functions without a product caller and catalog messages nobody reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
211 lines
5.9 KiB
Go
211 lines
5.9 KiB
Go
//go:build windows
|
|
|
|
package packetbridge
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"unsafe"
|
|
|
|
"github.com/Nwflower/reims-go/internal/networkmsg"
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
type trackedInstaller struct {
|
|
PID uint32 `json:"pid"`
|
|
CreationLow uint32 `json:"creation_low"`
|
|
CreationHigh uint32 `json:"creation_high"`
|
|
ExePath string `json:"exe_path,omitempty"`
|
|
}
|
|
|
|
func guardDir() (string, error) {
|
|
base, err := os.UserConfigDir()
|
|
if err != nil || strings.TrimSpace(base) == "" {
|
|
return "", networkmsg.Error("npcap_install_check", err)
|
|
}
|
|
dir := filepath.Join(base, "reims-go")
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
return "", networkmsg.Error("npcap_install_check", err)
|
|
}
|
|
return dir, nil
|
|
}
|
|
|
|
func defaultGuardPaths() (lockPath, statePath string, err error) {
|
|
dir, err := guardDir()
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
return filepath.Join(dir, "npcap-driver.lock"), filepath.Join(dir, "npcap-installer.json"), nil
|
|
}
|
|
|
|
// AcquireBridgeStart 获取跨进程共享读锁:允许多个桥接 VM 同时启动(GUI/CLI 并行),
|
|
// 仅在 Npcap 安装器持有独占写锁或 Reims-Go 拉起的安装进程尚未退出时阻止。
|
|
// NAT / 关闭 / 非桥接虚拟机不调用此门禁。
|
|
func AcquireBridgeStart() (func(), error) {
|
|
lockPath, statePath, err := defaultGuardPaths()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return acquireGuard(lockPath, statePath, false)
|
|
}
|
|
|
|
// AcquireNpcapInstall 获取跨进程独占写锁:阻止新的桥接 VM 启动与并发驱动安装,直至安装器退出。
|
|
func AcquireNpcapInstall() (func(), error) {
|
|
lockPath, statePath, err := defaultGuardPaths()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return acquireGuard(lockPath, statePath, true)
|
|
}
|
|
|
|
func acquireGuard(lockPath, statePath string, exclusive bool) (func(), error) {
|
|
if err := checkTrackedInstaller(statePath); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(lockPath), 0o700); err != nil {
|
|
return nil, networkmsg.Error("npcap_install_check", err)
|
|
}
|
|
p, err := windows.UTF16PtrFromString(lockPath)
|
|
if err != nil {
|
|
return nil, networkmsg.Error("npcap_install_check", err)
|
|
}
|
|
h, err := windows.CreateFile(
|
|
p,
|
|
windows.GENERIC_READ|windows.GENERIC_WRITE,
|
|
windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE,
|
|
nil,
|
|
windows.OPEN_ALWAYS,
|
|
windows.FILE_ATTRIBUTE_NORMAL,
|
|
0,
|
|
)
|
|
if err != nil {
|
|
return nil, networkmsg.Error("npcap_install_check", err)
|
|
}
|
|
flags := uint32(windows.LOCKFILE_FAIL_IMMEDIATELY)
|
|
if exclusive {
|
|
flags |= windows.LOCKFILE_EXCLUSIVE_LOCK
|
|
}
|
|
var ol windows.Overlapped
|
|
if err := windows.LockFileEx(h, flags, 0, 1, 0, &ol); err != nil {
|
|
_ = windows.CloseHandle(h)
|
|
if !exclusive {
|
|
return nil, networkmsg.Error("npcap_install_pending", err)
|
|
}
|
|
return nil, networkmsg.Error("network_busy", err)
|
|
}
|
|
release := func() {
|
|
var uol windows.Overlapped
|
|
_ = windows.UnlockFileEx(h, 0, 1, 0, &uol)
|
|
_ = windows.CloseHandle(h)
|
|
}
|
|
if err := checkTrackedInstaller(statePath); err != nil {
|
|
release()
|
|
return nil, err
|
|
}
|
|
return release, nil
|
|
}
|
|
|
|
func recordTrackedInstaller(statePath string, h windows.Handle, exePath string) {
|
|
if statePath == "" || h == 0 {
|
|
return
|
|
}
|
|
pid, err := windows.GetProcessId(h)
|
|
if err != nil || pid == 0 {
|
|
return
|
|
}
|
|
var creation, exit, kernel, user windows.Filetime
|
|
_ = windows.GetProcessTimes(h, &creation, &exit, &kernel, &user)
|
|
rec := trackedInstaller{
|
|
PID: pid,
|
|
CreationLow: creation.LowDateTime,
|
|
CreationHigh: creation.HighDateTime,
|
|
ExePath: filepath.Clean(exePath),
|
|
}
|
|
raw, err := json.Marshal(rec)
|
|
if err != nil {
|
|
return
|
|
}
|
|
_ = os.WriteFile(statePath, raw, 0o600)
|
|
}
|
|
|
|
func clearTrackedInstaller(statePath string) {
|
|
if statePath != "" {
|
|
_ = os.Remove(statePath)
|
|
}
|
|
}
|
|
|
|
// checkTrackedInstaller 仅核对 Reims-Go 自己拉起的 Npcap 安装进程 PID 与创建时间,
|
|
// 不按进程名前缀全系统扫描,避免误拦用户为 Wireshark 等软件安装 Npcap。
|
|
func checkTrackedInstaller(statePath string) error {
|
|
if statePath == "" {
|
|
return nil
|
|
}
|
|
raw, err := os.ReadFile(statePath)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return networkmsg.Error("npcap_install_check", err)
|
|
}
|
|
var rec trackedInstaller
|
|
if json.Unmarshal(raw, &rec) != nil || rec.PID == 0 {
|
|
_ = os.Remove(statePath)
|
|
return nil
|
|
}
|
|
h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION|windows.SYNCHRONIZE, false, rec.PID)
|
|
if err != nil {
|
|
// 进程已退出(或 PID 不存在),清理残留记录。
|
|
if errors.Is(err, windows.ERROR_INVALID_PARAMETER) {
|
|
_ = os.Remove(statePath)
|
|
return nil
|
|
}
|
|
// 若遇到提权进程权限受限,用进程快照确认该 PID 是否仍在运行。
|
|
if !pidExists(rec.PID) {
|
|
_ = os.Remove(statePath)
|
|
return nil
|
|
}
|
|
return networkmsg.Error("npcap_install_pending", nil)
|
|
}
|
|
defer windows.CloseHandle(h)
|
|
state, waitErr := windows.WaitForSingleObject(h, 0)
|
|
if waitErr == nil && state != uint32(windows.WAIT_TIMEOUT) {
|
|
_ = os.Remove(statePath)
|
|
return nil
|
|
}
|
|
if rec.CreationLow != 0 || rec.CreationHigh != 0 {
|
|
var creation, exit, kernel, user windows.Filetime
|
|
if err := windows.GetProcessTimes(h, &creation, &exit, &kernel, &user); err == nil {
|
|
if creation.LowDateTime != rec.CreationLow || creation.HighDateTime != rec.CreationHigh {
|
|
// PID 已被系统复用于其他进程。
|
|
_ = os.Remove(statePath)
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
return networkmsg.Error("npcap_install_pending", nil)
|
|
}
|
|
|
|
func pidExists(pid uint32) bool {
|
|
h, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
|
|
if err != nil {
|
|
return true
|
|
}
|
|
defer windows.CloseHandle(h)
|
|
var entry windows.ProcessEntry32
|
|
entry.Size = uint32(unsafe.Sizeof(entry))
|
|
if err := windows.Process32First(h, &entry); err != nil {
|
|
return false
|
|
}
|
|
for {
|
|
if entry.ProcessID == pid {
|
|
return true
|
|
}
|
|
if err := windows.Process32Next(h, &entry); err != nil {
|
|
return false
|
|
}
|
|
}
|
|
}
|