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:
+28
-4
@@ -29,6 +29,7 @@ const (
|
||||
CmdStop = "stop"
|
||||
CmdShutdown = "shutdown"
|
||||
CmdStats = "stats"
|
||||
CmdVersion = "version" // query daemon build version
|
||||
)
|
||||
|
||||
// Event types sent from daemon to GUI.
|
||||
@@ -49,8 +50,8 @@ type Request struct {
|
||||
// package (which needs root-only TUN) into the GUI.
|
||||
type ClientConfig struct {
|
||||
ServerURL string `json:"server_url"`
|
||||
SNIHost string `json:"sni_host"` // TLS SNI hostname for CDN
|
||||
ServerIPs []string `json:"server_ips"` // CDN edge IP list for failover
|
||||
SNIHost string `json:"sni_host"` // TLS SNI hostname for CDN
|
||||
ServerIPs []string `json:"server_ips"` // CDN edge IP list for failover
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
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.
|
||||
type Response struct {
|
||||
OK bool `json:"ok"`
|
||||
Error string `json:"error,omitempty"`
|
||||
OK bool `json:"ok"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Version string `json:"version,omitempty"` // set for CmdVersion replies
|
||||
}
|
||||
|
||||
// WriteOK sends a success response to a connection.
|
||||
@@ -232,6 +234,28 @@ func SendShutdown(c *Client) error {
|
||||
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.
|
||||
func RoutingModeFromIPC(s string) route.Mode {
|
||||
switch s {
|
||||
|
||||
Reference in New Issue
Block a user