fix: ensureDaemon 自动检测并重启过期守护进程

问题: 重新构建后 IPv6 等新功能不生效,客户端仍运行旧代码。
根因: ensureDaemon() 通过 IPC socket 拨号时,若旧 daemon 进程仍在
运行(重建后未退出),GUI 会直接复用旧进程,永不启动新二进制。
日志证实旧 daemon 处理连接(init 日志无 ip6 字段),新代码从未执行。

解决方案: 新增版本握手协议,GUI 启动时查询 daemon 构建版本,
版本不匹配则自动关闭旧进程并启动新的。

- 新建 internal/version 包,GUI 与 daemon 共享同一 Version 变量,
  均通过 Makefile ldflags 注入
- IPC 新增 CmdVersion 命令,daemon 返回 version.Version;
  Response 增加 Version 字段,Client 新增 QueryVersion() 方法
- ensureDaemon() 拨号后查询版本,不匹配则发 CmdShutdown、等待
  socket 释放、重新启动 daemon;旧 daemon 不识别 CmdVersion 时
  返回错误,同样触发重启
- Makefile LDFLAGS 改为注入 lmvpn/internal/version.Version
This commit is contained in:
2026-07-07 12:42:23 +08:00
parent a332857973
commit 4420532a66
6 changed files with 75 additions and 10 deletions
+2 -2
View File
@@ -10,8 +10,8 @@ APP_BUNDLE = $(APP_NAME).app
GO = go GO = go
CGO_ENABLED = 1 CGO_ENABLED = 1
GIT_HASH = $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown) GIT_HASH = $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
VERSION = 0.2.5-$(GIT_HASH) VERSION = 0.2.6-$(GIT_HASH)
LDFLAGS = -s -w -X lmvpn/internal/ui.Version=$(VERSION) LDFLAGS = -s -w -X lmvpn/internal/version.Version=$(VERSION)
.PHONY: all build app run daemon clean vet tidy fmt icon .PHONY: all build app run daemon clean vet tidy fmt icon
+3
View File
@@ -26,6 +26,7 @@ import (
"lmvpn/internal/model" "lmvpn/internal/model"
"lmvpn/internal/paths" "lmvpn/internal/paths"
"lmvpn/internal/stats" "lmvpn/internal/stats"
"lmvpn/internal/version"
"lmvpn/internal/vpn" "lmvpn/internal/vpn"
) )
@@ -113,6 +114,8 @@ func (d *daemon) handle(conn net.Conn, req ipc.Request) {
d.server.Broadcast(ipc.Event{Event: ipc.EvStats, Stats: &snap}) d.server.Broadcast(ipc.Event{Event: ipc.EvStats, Stats: &snap})
} }
_ = ipc.WriteOK(conn) _ = ipc.WriteOK(conn)
case ipc.CmdVersion:
_ = ipc.WriteVersion(conn, version.Version)
default: default:
_ = ipc.WriteErr(conn, "unknown command: "+req.Cmd) _ = ipc.WriteErr(conn, "unknown command: "+req.Cmd)
} }
+28 -4
View File
@@ -29,6 +29,7 @@ const (
CmdStop = "stop" CmdStop = "stop"
CmdShutdown = "shutdown" CmdShutdown = "shutdown"
CmdStats = "stats" CmdStats = "stats"
CmdVersion = "version" // query daemon build version
) )
// Event types sent from daemon to GUI. // Event types sent from daemon to GUI.
@@ -49,8 +50,8 @@ type Request struct {
// package (which needs root-only TUN) into the GUI. // package (which needs root-only TUN) into the GUI.
type ClientConfig struct { type ClientConfig struct {
ServerURL string `json:"server_url"` ServerURL string `json:"server_url"`
SNIHost string `json:"sni_host"` // TLS SNI hostname for CDN SNIHost string `json:"sni_host"` // TLS SNI hostname for CDN
ServerIPs []string `json:"server_ips"` // CDN edge IP list for failover ServerIPs []string `json:"server_ips"` // CDN edge IP list for failover
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`
Token string `json:"token"` Token string `json:"token"`
@@ -203,8 +204,9 @@ func (c *Client) Close() error { return c.conn.Close() }
// Response is a simple ack/error reply to a command. // Response is a simple ack/error reply to a command.
type Response struct { type Response struct {
OK bool `json:"ok"` OK bool `json:"ok"`
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
Version string `json:"version,omitempty"` // set for CmdVersion replies
} }
// WriteOK sends a success response to a connection. // WriteOK sends a success response to a connection.
@@ -232,6 +234,28 @@ func SendShutdown(c *Client) error {
return c.Send(Request{Cmd: CmdShutdown}) return c.Send(Request{Cmd: CmdShutdown})
} }
// WriteVersion sends a version response to a connection.
func WriteVersion(conn net.Conn, ver string) error {
return writeMsg(conn, Response{OK: true, Version: ver})
}
// QueryVersion sends a CmdVersion request and reads the daemon's
// build version. It must be called before any VPN session starts (so
// no Event messages are in flight on the connection).
func (c *Client) QueryVersion() (string, error) {
if err := c.Send(Request{Cmd: CmdVersion}); err != nil {
return "", err
}
var resp Response
if err := readMsg(c.r, &resp); err != nil {
return "", err
}
if !resp.OK {
return "", fmt.Errorf("version query failed: %s", resp.Error)
}
return resp.Version, nil
}
// RoutingModeFromIPC converts an IPC routing mode string to route.Mode. // RoutingModeFromIPC converts an IPC routing mode string to route.Mode.
func RoutingModeFromIPC(s string) route.Mode { func RoutingModeFromIPC(s string) route.Mode {
switch s { switch s {
+22 -3
View File
@@ -10,6 +10,7 @@ import (
"lmvpn/internal/ipc" "lmvpn/internal/ipc"
"lmvpn/internal/log" "lmvpn/internal/log"
"lmvpn/internal/paths" "lmvpn/internal/paths"
"lmvpn/internal/version"
) )
// ensureDaemon checks if the daemon is running and launches it (as // ensureDaemon checks if the daemon is running and launches it (as
@@ -25,9 +26,27 @@ import (
// The --daemon-bin flag tells daemon-launch where to find lmvpnd. If // The --daemon-bin flag tells daemon-launch where to find lmvpnd. If
// not given, daemon-launch auto-detects it relative to its own path. // not given, daemon-launch auto-detects it relative to its own path.
func ensureDaemon() (*ipc.Client, error) { func ensureDaemon() (*ipc.Client, error) {
// Fast path: daemon already running. // Fast path: a daemon is already running. Verify its build version
// matches ours; if not, it's a stale process from a previous build
// and must be restarted so the new code takes effect.
if c, err := ipc.Dial(); err == nil { if c, err := ipc.Dial(); err == nil {
return c, nil if ver, qerr := c.QueryVersion(); qerr == nil && ver == version.Version {
return c, nil
} else {
log.L().Info("stale daemon detected, restarting",
"got", ver, "expected", version.Version, "query_err", qerr)
_ = ipc.SendShutdown(c)
c.Close()
// Wait for the old daemon to exit and release the socket.
for i := 0; i < 30; i++ {
time.Sleep(100 * time.Millisecond)
if cc, err := ipc.Dial(); err == nil {
cc.Close()
} else {
break // socket gone
}
}
}
} }
exe, err := os.Executable() exe, err := os.Executable()
@@ -78,7 +97,7 @@ func ensureDaemon() (*ipc.Client, error) {
} }
// shellQuote wraps a string in single quotes for shell safety. // shellQuote wraps a string in single quotes for shell safety.
// Embedded single quotes are escaped using the '\'' pattern. // Embedded single quotes are escaped using the '\ pattern.
func shellQuote(s string) string { func shellQuote(s string) string {
result := "'" result := "'"
for _, r := range s { for _, r := range s {
+5 -1
View File
@@ -1,3 +1,7 @@
package ui package ui
var Version = "dev" import "lmvpn/internal/version"
// Version is the application version, sourced from the shared version
// package so the GUI and daemon report the same string.
var Version = version.Version
+15
View File
@@ -0,0 +1,15 @@
// Package version holds the build version string shared by the GUI
// (lmvpn) and daemon (lmvpnd) binaries. The value is injected at link
// time via ldflags:
//
// -X lmvpn/internal/version.Version=$(VERSION)
//
// Both binaries are built with the same LDFLAGS, so a version mismatch
// between the running daemon and the GUI indicates a stale daemon
// process that predates the current build.
package version
// Version is the application version. Overridden at link time; "dev"
// when running an untagged build (e.g. go run / go build without
// ldflags).
var Version = "dev"