Documentation
¶
Overview ¶
Package binstall 为托管的外部程序(frp、cloudflared、tailscale、FileBrowser Quantum)提供在线安装、 更新与回滚:从上游下载指定或最新版本并校验 SHA-256,安装到配置目录的 bin 下; 替换前把旧版本移入 bin/.versions/<程序>/,最多保留 MaxBackups 个,可随时切回。
Index ¶
- Constants
- Variables
- func ExeName(name string) string
- func FetchText(ctx context.Context, client *http.Client, rawURL string) (string, error)
- func FindChecksum(text, name string) string
- func ProbeOutput(path string, re *regexp.Regexp, args ...string) (string, error)
- func Unsupported(format string, args ...any) error
- func ValidateProxy(raw string) error
- func VersionNewer(latest, installed string) bool
- type Asset
- type Backup
- type Config
- type GitHubAsset
- type GitHubRelease
- type Installed
- type Installer
- func (in *Installer) Backups() []Backup
- func (in *Installer) BinDir() string
- func (in *Installer) CheckLatest(ctx context.Context) (Latest, error)
- func (in *Installer) DeleteBackup(id string) error
- func (in *Installer) Installed() Installed
- func (in *Installer) Key() string
- func (in *Installer) LatestInfo() Latest
- func (in *Installer) MainPath() string
- func (in *Installer) Name() string
- func (in *Installer) Register(r module.Router)
- func (in *Installer) Rollback(id string) (string, error)
- func (in *Installer) Start(version, proxy string) (State, error)
- func (in *Installer) State() State
- func (in *Installer) Uninstall() error
- func (in *Installer) View() View
- type Latest
- type Options
- type Platform
- type Release
- type Spec
- type State
- type UnsupportedError
- type View
Constants ¶
const ( StageChecking = "checking" StageDownloading = "downloading" StageVerifying = "verifying" StageExtracting = "extracting" StageDone = "done" // StageUpToDate 表示未指定版本且已安装的就是最新版,没有下载 StageUpToDate = "uptodate" StageFailed = "failed" )
安装阶段。
const (
// MaxBackups 是每个程序保留的旧版本数量
MaxBackups = 3
)
Variables ¶
var ErrBusy = errors.New("正在安装,请等待完成")
ErrBusy 表示已有安装或回滚在进行。
var ErrChecking = errors.New("正在检查更新,请稍候")
ErrChecking 表示已有检查更新在进行。
var ErrNoBackup = errors.New("该旧版本不存在")
ErrNoBackup 表示要回滚的旧版本不存在。
var GitHubAPI = "https://api.github.com"
GitHubAPI 是 GitHub API 地址,测试中可替换。
Functions ¶
func FindChecksum ¶
FindChecksum 在校验文本中查找 name 的 SHA-256,兼容 “<摘要> <文件名>” 与 “<文件名>: <摘要>” 两种写法。
func ProbeOutput ¶
ProbeOutput 执行 path args 并用 re 的第一个分组提取版本号,无法运行或没有匹配时返回错误。
func ValidateProxy ¶
ValidateProxy 校验下载代理地址,支持 http、https 与 socks5。
func VersionNewer ¶ added in v0.6.4
VersionNewer 判断 latest 是否比 installed 新:只比较开头的数字段(缺少的段按 0 处理), 数字相同即视为不更新,避免 -stable、-beta 之类的后缀在上游与探测结果之间写法不一致时误报; 任一版本号无法解析时返回 false。
Types ¶
type Asset ¶
type Asset struct {
// Name 为文件名,按后缀判断格式:.tar.gz / .tgz / .zip 为压缩包,其余为可执行文件本身
Name string
URL string
// SHA256 为十六进制摘要,为空时拒绝安装
SHA256 string
Size int64
}
Asset 是一次安装要下载的文件。
type Backup ¶
type Backup struct {
ID string `json:"id"`
Version string `json:"version"`
SavedAt time.Time `json:"savedAt"`
}
Backup 是保存的一个旧版本。
type Config ¶
type Config struct {
// Proxy 为下载该程序使用的代理,空表示沿用全局“更新代理”
Proxy string `json:"proxy"`
}
Config 是保存在模块配置中的安装设置。
type GitHubAsset ¶
type GitHubAsset struct {
Name string `json:"name"`
URL string `json:"browser_download_url"`
Size int64 `json:"size"`
// Digest 为 GitHub 记录的文件摘要,格式 sha256:<十六进制>,2025 年 6 月前上传的文件没有
Digest string `json:"digest"`
}
GitHubAsset 是 GitHub 发布中的一个文件。
type GitHubRelease ¶
type GitHubRelease struct {
TagName string `json:"tag_name"`
Body string `json:"body"`
Assets []GitHubAsset `json:"assets"`
}
GitHubRelease 是 GitHub 的一个发布。
func FetchGitHubRelease ¶
func FetchGitHubRelease(ctx context.Context, client *http.Client, repo, version string) (*GitHubRelease, error)
FetchGitHubRelease 查询 repo(owner/name)的发布:version 为空时取最新正式版, 否则按标签查找,标签带不带前缀 v 都会尝试。
func (*GitHubRelease) Find ¶
func (r *GitHubRelease) Find(name string) *GitHubAsset
Find 返回名为 name 的文件。
type Installed ¶
type Installed struct {
// Present 表示 bin 下有主程序
Present bool `json:"present"`
Version string `json:"version"`
// Missing 列出 bin 下缺少的其他文件,例如只有 frpc 没有 frps
Missing []string `json:"missing,omitempty"`
}
Installed 是 bin 下当前安装的程序。
type Installer ¶
type Installer struct {
// contains filtered or unexported fields
}
Installer 管理一个程序的安装、更新与回滚。
func (*Installer) CheckLatest ¶ added in v0.6.4
CheckLatest 查询上游最新正式版并缓存结果,供界面显示与定时检查使用;不下载、不改动已安装的程序。 使用该程序单独设置的下载代理,未设置时沿用全局“更新代理”。
func (*Installer) DeleteBackup ¶
DeleteBackup 删除一个保存的旧版本。
func (*Installer) LatestInfo ¶ added in v0.6.4
LatestInfo 返回缓存的最新版本,并按 bin 下当前安装的版本计算是否有更新;从未检查过时 CheckedAt 为零值。
func (*Installer) Register ¶
Register 在模块路由下注册安装接口:
GET /install 安装信息与进度
POST /install 开始安装或更新
POST /install/check 检查更新:查询上游最新版本,不下载
POST /install/rollback 切换到保存的旧版本
DELETE /install/backups/{id} 删除保存的旧版本
DELETE /install 卸载:删除安装的程序与全部旧版本
type Latest ¶ added in v0.6.4
type Latest struct {
// Version 为最新正式版的版本号,查询失败时保留上一次成功的结果
Version string `json:"version,omitempty"`
CheckedAt time.Time `json:"checkedAt"`
// Error 为最近一次查询的错误,成功时为空
Error string `json:"error,omitempty"`
// HasUpdate 表示最新版本比 bin 下安装的版本新,由 View 按当前安装的版本计算
HasUpdate bool `json:"hasUpdate"`
}
Latest 是最近一次查询到的上游最新版本。
type Options ¶
type Options struct {
// BinDir 为安装目录(配置目录下的 bin)
BinDir string
Logs *logbuf.Buffer
// Client 返回遵循全局“更新代理”的客户端,可为 nil
Client func() *http.Client
// GetProxy 与 SetProxy 读取、保存该程序的下载代理(保存在模块配置中),可为 nil
GetProxy func() string
SetProxy func(proxy string) error
// OnChange 在安装或回滚替换文件后调用,用于重启使用该程序的进程;调用时不持有安装器的锁
OnChange func()
// Platform 为空时使用 CurrentPlatform,测试中可替换
Platform *Platform
}
Options 是 Installer 的依赖。
type Platform ¶
Platform 是当前运行平台。
func CurrentPlatform ¶
func CurrentPlatform() Platform
CurrentPlatform 返回 Gately 自身的运行平台。托管程序与 Gately 使用相同的系统与架构。
type Spec ¶
type Spec struct {
// Key 为旧版本目录名与日志来源,例如 frp
Key string
// Name 为界面与日志中的程序名,例如 frp、cloudflared
Name string
// Files 为安装到 bin 下的可执行文件名(不含 .exe),第一个为主程序,用于探测版本
Files []string
// Resolve 查询版本,version 为空时取最新正式版
Resolve func(ctx context.Context, client *http.Client, p Platform, version string) (*Release, error)
// Probe 返回可执行文件的版本,无法运行时返回错误
Probe func(path string) (string, error)
// Check 判断上游是否提供平台 p 的程序,不提供时返回 Unsupported 错误;为 nil 表示都支持
Check func(p Platform) error
}
Spec 描述一个可安装的程序。
type State ¶
type State struct {
Running bool `json:"running"`
Stage string `json:"stage"`
// Version 为正在安装或已安装的版本
Version string `json:"version"`
Downloaded int64 `json:"downloaded"`
Total int64 `json:"total"`
Error string `json:"error"`
}
State 是安装进度。
type UnsupportedError ¶
type UnsupportedError struct{ Msg string }
UnsupportedError 表示上游没有提供当前平台的程序,由 Spec.Resolve 返回。
func (*UnsupportedError) Error ¶
func (e *UnsupportedError) Error() string
type View ¶
type View struct {
State State `json:"state"`
// Program 为程序名,Files 为安装的可执行文件
Program string `json:"program"`
Files []string `json:"files"`
// BinDir 为安装目录,MainPath 为安装后主程序的路径
BinDir string `json:"binDir"`
MainPath string `json:"mainPath"`
Installed Installed `json:"installed"`
Backups []Backup `json:"backups"`
// Latest 为最近一次检查更新查询到的上游最新版本,从未检查过时为空
Latest *Latest `json:"latest,omitempty"`
// Proxy 为该程序单独设置的下载代理,空表示沿用全局“更新代理”
Proxy string `json:"proxy"`
Platform string `json:"platform"`
// Unsupported 非空时表示上游没有提供当前平台的程序,无法在线安装
Unsupported string `json:"unsupported"`
}
View 是 GET /install 返回的安装信息。