feat(update): 第20条 agent 一键在线更新(意向→轮询→校验换名→自重启)

- 新增 internal/selfupdate:发行坐标(基址常量+env 覆盖)、Gitea
  latest tag 查询、sha256 旁车校验下载、原子换名(旧二进制留 .bak、
  失败可回滚),含单测
- 主控:UpdatePanels 对 agent 节点改为记录更新意向(最新 tag 的
  V 系→A 系目标;已达标报错不记意向);意向经现有 GET config 轮询
  捎带下发;心跳收 updateStatus 回报(failed/current 清意向留状态、
  过期 requested_at 忽略),遥测版本达标自动确认 success
- agent:识别意向→比自身 A 版本(已达标报 current 不下载)→按架构
  下载资产+sha256 校验→换名留 .bak→刷一次心跳→stopXray→
  syscall.Exec 自重启;exec 失败回滚 .bak 并报 failed;失败 10 分钟
  内不重下同一意向;旧版全程保持运行
- NodeView 增 agentUpdateState/Error/Target 供节点页可观测
- 单测:selfupdate 换名/校验、agent 换名重启/坏校验保旧版/已达标
  跳过/exec 失败回滚、主控意向记录/已达标跳过、config 捎带意向、
  心跳 failed 清意向、心跳新版本确认 success
This commit is contained in:
Muse
2026-10-22 13:14:24 +00:00
parent f18aca9410
commit 2365eb138b
11 changed files with 1103 additions and 9 deletions
+19 -2
View File
@@ -75,6 +75,15 @@ type Agent struct {
xrayVer string
xrayVerFor string // binary path the cached version was read from
xrayVerMod time.Time
// One-click update bookkeeping (item 20, see update.go).
updateMu sync.Mutex
updateRunning bool
updateHandled int64
updateFailedReq int64
updateFailedAt time.Time
updateReport *agentUpdateStatus
exePathOverride string
}
// New creates a new agent instance
@@ -272,6 +281,9 @@ func (a *Agent) sendHeartbeat() error {
"timestamp": time.Now().Unix(),
"telemetry": a.collectTelemetry(),
}
if rep := a.currentUpdateReport(); rep != nil {
payload["updateStatus"] = rep
}
body, _ := json.Marshal(payload)
req, err := http.NewRequest("POST", url, bytes.NewReader(body))
if err != nil {
@@ -312,13 +324,18 @@ func (a *Agent) fetchAndApplyConfig() error {
}
var result struct {
ConfigHash string `json:"config_hash"`
XrayConfig json.RawMessage `json:"xray_config"`
ConfigHash string `json:"config_hash"`
XrayConfig json.RawMessage `json:"xray_config"`
AgentUpdate *updateIntent `json:"agent_update"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return err
}
// One-click update intents ride this poll (item 20); handle them even
// when the xray config itself is unchanged.
a.maybeHandleUpdate(result.AgentUpdate)
// Skip if config hasn't changed
if result.ConfigHash == a.lastConfigHash && result.ConfigHash != "" {
return nil