Documentation
¶
Overview ¶
Package upgrade 提供 agent 自升级:版本查询/比较/下载/校验/替换(CLI 与 WS 推送共用)。
下载源:XPanel-Node 仓库的 GitHub Releases。版本查询走 /releases/latest 的 重定向 Location(不调 REST API,规避匿名速率限制且兼容镜像站);二进制与 checksums.txt 从 /releases/download/<tag>/ 拉取,sha256 必检。
镜像候选链:配置的 update.mirror 置顶,其后自动落回内置候选(与 install-agent.sh 的 DEFAULT_MIRRORS 同源)。逐个尝试,网络/HTTP 级失败切下一个;checksums.txt 拿到了但内容不对(缺条目)视为 release 本身损坏,换镜像无意义,立即报错。 资产与 checksums.txt 始终取自同一镜像,保证校验可信。
Index ¶
- Constants
- Variables
- func Apply(f *Fetcher, exePath string, restart func() error, out io.Writer) error
- func ApplyTarget(f *Fetcher, target string, force bool, exePath string, restart func() error, ...) error
- func AssetName() string
- func Compare(a, b string) int
- func CurrentVersion() string
- func ReplaceBinary(exePath string, data []byte) error
- func Sha256Hex(data []byte) string
- type Fetcher
Constants ¶
const DefaultRepo = "acdc-awa/XPanel-Node"
DefaultRepo 默认发布仓库(XPanel-Node on GitHub)。
Variables ¶
var ErrUpToDate = errors.New("已是最新版本")
ErrUpToDate 已是最新版本。
var TagPattern = regexp.MustCompile(`^v?\d+(\.\d+)+(-[0-9A-Za-z.-]+)?$`)
TagPattern 严格限制版本标签格式,防路径穿越与 URL 注入。
var Version = "dev"
Version 当前 agent 版本(构建期 -ldflags -X 注入;"dev" 为默认开发值)。
Functions ¶
func Apply ¶
Apply 完整升级流程:查版本 → 比较 → 下载 → sha256 强制校验 → 原子替换 → 重启。 exePath 为目标二进制路径(通常 os.Executable());restart 由调用方注入(systemd 重启或手动提示)。
func ApplyTarget ¶ added in v0.1.17
func ApplyTarget(f *Fetcher, target string, force bool, exePath string, restart func() error, out io.Writer) error
ApplyTarget 带目标版本与强制选项的升级/回滚流程: target 为空时查最新 release;force 为 false 时若当前已 >= 目标版本则拒绝(返回 ErrUpToDate); force 为 true 时只要版本不完全相同即允许降级回滚。
func ReplaceBinary ¶
ReplaceBinary 原子替换二进制文件:写入同目录 .tmp → chmod 0755 → rename 覆盖。 失败时清理 .tmp。CLI 升级与 WS 触发升级共用。
Types ¶
type Fetcher ¶
type Fetcher struct {
Repo string // owner/repo,空 = DefaultRepo
Mirror string // 可选:首选镜像/代理前缀(如 https://ghproxy.net/https://github.com),置顶尝试
// Mirrors 显式完整候选列表(测试注入用);非空时忽略 Mirror 与内置列表。
Mirrors []string
// DownloadTimeout 单镜像单次资产下载超时,0 = defaultDownloadTimeout。
DownloadTimeout time.Duration
// Client 测试注入用;非空时两个角色共用(注入方自管重定向策略)。
Client *http.Client
// contains filtered or unexported fields
}
Fetcher 从 GitHub Releases 查询版本并下载 agent 发布资产,按镜像候选链逐个尝试。