feat: 支持 Windows x86_64 平台编译
- 新增 WinTun TUN 设备实现(tun_windows.go),无需安装驱动, wintun.dll 嵌入 exe 运行时自动释放 - 新增 Windows 路由管理(route_windows.go),使用 route/netsh 命令 - 新增 Windows 路径(paths_windows.go),使用 %APPDATA%/%LOCALAPPDATA% - 拆分 daemon 启动逻辑:launch_unix.go(Setsid) / launch_windows.go (CREATE_NEW_PROCESS_GROUP),公共代码提取到 launch_common.go - 拆分提权逻辑:elevation_darwin.go(osascript) / elevation_windows.go (ShellExecute UAC) / elevation_other.go(pkexec) - IPCSocketPath 按平台分散,Windows 用 %TEMP%\lmvpn.sock - daemon 二进制名用编译时常量(daemonBinaryName),按平台选定 .exe 后缀 - Makefile 新增 build-windows 目标 - 重新标记 !darwin 文件为 !darwin && !windows
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
//go:build windows
|
||||
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"lmvpn/internal/paths"
|
||||
)
|
||||
|
||||
// Launch starts the daemon binary (lmvpnd.exe) as a detached process
|
||||
// and returns immediately. On Windows, detachment is achieved via
|
||||
// CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS creation flags instead
|
||||
// of the Unix Setsid call.
|
||||
//
|
||||
// The launcher itself is invoked by the GUI via ShellExecute "runas"
|
||||
// (UAC elevation). It:
|
||||
// 1. Computes the daemon log path using the user's home directory
|
||||
// 2. Opens the log file (append) and NUL
|
||||
// 3. Starts the lmvpnd.exe binary, redirecting stdio
|
||||
// 4. Returns immediately — the child continues running independently
|
||||
func Launch(userHome string, uid, gid int, daemonBin string) error {
|
||||
paths.SetUserHome(userHome)
|
||||
if err := paths.EnsureDirs(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "ensure dirs: %v\n", err)
|
||||
}
|
||||
|
||||
if daemonBin == "" {
|
||||
var err error
|
||||
daemonBin, err = resolveDaemonBinary()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
absBin, err := filepath.Abs(daemonBin)
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve absolute path for %s: %w", daemonBin, err)
|
||||
}
|
||||
daemonBin = absBin
|
||||
|
||||
if _, err := os.Stat(daemonBin); err != nil {
|
||||
return fmt.Errorf("daemon binary not found at %s: %w", daemonBin, err)
|
||||
}
|
||||
|
||||
logPath := paths.DaemonLogFile()
|
||||
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open daemon log %s: %w", logPath, err)
|
||||
}
|
||||
|
||||
devNull, err := os.Open(os.DevNull)
|
||||
if err != nil {
|
||||
logFile.Close()
|
||||
return fmt.Errorf("open NUL: %w", err)
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"--user-home", userHome,
|
||||
"--uid", fmt.Sprintf("%d", uid),
|
||||
"--gid", fmt.Sprintf("%d", gid),
|
||||
}
|
||||
|
||||
fmt.Fprintf(logFile, "=== launcher: starting daemon %s %v ===\n", daemonBin, args)
|
||||
|
||||
// CREATE_NEW_PROCESS_GROUP (0x200) | DETACHED_PROCESS (0x8)
|
||||
procAttr := &os.ProcAttr{
|
||||
Dir: os.TempDir(),
|
||||
Env: append(os.Environ(),
|
||||
"LMVPN_DAEMON=1",
|
||||
),
|
||||
Files: []*os.File{devNull, logFile, logFile},
|
||||
Sys: &syscall.SysProcAttr{
|
||||
CreationFlags: 0x00000200 | 0x00000008,
|
||||
},
|
||||
}
|
||||
|
||||
pid, err := os.StartProcess(daemonBin, append([]string{daemonBin}, args...), procAttr)
|
||||
logFile.Close()
|
||||
devNull.Close()
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("start daemon process: %w", err)
|
||||
}
|
||||
|
||||
_ = pid.Release()
|
||||
|
||||
fmt.Fprintf(os.Stderr, "daemon launched (pid %d, bin %s)\n", pid.Pid, daemonBin)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user