binstall

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 30 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package binstall 为托管的外部程序(frp、cloudflared、tailscale、FileBrowser Quantum)提供在线安装、 更新与回滚:从上游下载指定或最新版本并校验 SHA-256,安装到配置目录的 bin 下; 替换前把旧版本移入 bin/.versions/<程序>/,最多保留 MaxBackups 个,可随时切回。

Index

Constants

View Source
const (
	StageChecking    = "checking"
	StageDownloading = "downloading"
	StageVerifying   = "verifying"
	StageExtracting  = "extracting"
	StageDone        = "done"
	// StageUpToDate 表示未指定版本且已安装的就是最新版,没有下载
	StageUpToDate = "uptodate"
	StageFailed   = "failed"
)

安装阶段。

View Source
const (
	// MaxBackups 是每个程序保留的旧版本数量
	MaxBackups = 3
)

Variables

View Source
var ErrBusy = errors.New("正在安装,请等待完成")

ErrBusy 表示已有安装或回滚在进行。

View Source
var ErrChecking = errors.New("正在检查更新,请稍候")

ErrChecking 表示已有检查更新在进行。

View Source
var ErrNoBackup = errors.New("该旧版本不存在")

ErrNoBackup 表示要回滚的旧版本不存在。

View Source
var GitHubAPI = "https://api.github.com"

GitHubAPI 是 GitHub API 地址,测试中可替换。

Functions

func ExeName

func ExeName(name string) string

ExeName 在 Windows 下为可执行文件名加上 .exe;已带扩展名的文件(如 wintun.dll 这样与程序放在一起的依赖库)原样返回。

func FetchText

func FetchText(ctx context.Context, client *http.Client, rawURL string) (string, error)

FetchText 供 Spec.Resolve 读取校验文件、版本列表等小文件。

func FindChecksum

func FindChecksum(text, name string) string

FindChecksum 在校验文本中查找 name 的 SHA-256,兼容 “<摘要> <文件名>” 与 “<文件名>: <摘要>” 两种写法。

func ProbeOutput

func ProbeOutput(path string, re *regexp.Regexp, args ...string) (string, error)

ProbeOutput 执行 path args 并用 re 的第一个分组提取版本号,无法运行或没有匹配时返回错误。

func Unsupported

func Unsupported(format string, args ...any) error

Unsupported 构造不支持当前平台的错误。

func ValidateProxy

func ValidateProxy(raw string) error

ValidateProxy 校验下载代理地址,支持 http、https 与 socks5。

func VersionNewer added in v0.6.4

func VersionNewer(latest, installed string) bool

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 New

func New(spec Spec, opt Options) *Installer

New 创建安装器。

func (*Installer) Backups

func (in *Installer) Backups() []Backup

Backups 返回保存的旧版本,最新的在前。

func (*Installer) BinDir

func (in *Installer) BinDir() string

BinDir 返回安装目录。

func (*Installer) CheckLatest added in v0.6.4

func (in *Installer) CheckLatest(ctx context.Context) (Latest, error)

CheckLatest 查询上游最新正式版并缓存结果,供界面显示与定时检查使用;不下载、不改动已安装的程序。 使用该程序单独设置的下载代理,未设置时沿用全局“更新代理”。

func (*Installer) DeleteBackup

func (in *Installer) DeleteBackup(id string) error

DeleteBackup 删除一个保存的旧版本。

func (*Installer) Installed

func (in *Installer) Installed() Installed

Installed 返回 bin 下当前安装的程序与版本。

func (*Installer) Key added in v0.6.4

func (in *Installer) Key() string

Key 返回程序标识(旧版本目录名),定时检查以它区分不同程序。

func (*Installer) LatestInfo added in v0.6.4

func (in *Installer) LatestInfo() Latest

LatestInfo 返回缓存的最新版本,并按 bin 下当前安装的版本计算是否有更新;从未检查过时 CheckedAt 为零值。

func (*Installer) MainPath

func (in *Installer) MainPath() string

MainPath 返回主程序安装后的路径。

func (*Installer) Name added in v0.6.4

func (in *Installer) Name() string

Name 返回程序名,供定时检查生成通知。

func (*Installer) Register

func (in *Installer) Register(r module.Router)

Register 在模块路由下注册安装接口:

GET    /install               安装信息与进度
POST   /install               开始安装或更新
POST   /install/check         检查更新:查询上游最新版本,不下载
POST   /install/rollback      切换到保存的旧版本
DELETE /install/backups/{id}  删除保存的旧版本
DELETE /install               卸载:删除安装的程序与全部旧版本

func (*Installer) Rollback

func (in *Installer) Rollback(id string) (string, error)

Rollback 切换到保存的旧版本 id:当前版本另存为旧版本,之后仍可切回。完成后通知使用方重启进程。

func (*Installer) Start

func (in *Installer) Start(version, proxy string) (State, error)

Start 在后台开始安装 version(为空时安装最新正式版),proxy 非空时用它访问上游,否则沿用全局更新代理。

func (*Installer) State

func (in *Installer) State() State

State 返回安装进度。

func (*Installer) Uninstall

func (in *Installer) Uninstall() error

Uninstall 删除 bin 下安装的程序与全部保存的旧版本,完成后通知使用方重启进程 (之后改用 PATH 中的同名程序,没有时进程无法启动)。模块配置不受影响。

func (*Installer) View

func (in *Installer) View() View

View 返回安装信息。

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

type Platform struct {
	OS   string
	Arch string
	// ARM 为 32 位 ARM 的 GOARM 版本(5、6、7)
	ARM string
}

Platform 是当前运行平台。

func CurrentPlatform

func CurrentPlatform() Platform

CurrentPlatform 返回 Gately 自身的运行平台。托管程序与 Gately 使用相同的系统与架构。

func (Platform) String

func (p Platform) String() string

String 返回 linux/arm64、linux/armv7 这样的平台名。

type Release

type Release struct {
	Version string
	Asset   Asset
}

Release 是解析出的一个版本。

func GitHubAssetRelease

func GitHubAssetRelease(ctx context.Context, client *http.Client, rel *GitHubRelease, version, name string, checksumFiles ...string) (*Release, error)

GitHubAssetRelease 把 GitHub 发布中名为 name 的文件转换为 Release:摘要优先取 GitHub 记录的 digest, 没有时依次在名为 checksumFiles 的校验文件与发布说明中查找。

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 返回的安装信息。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL