geo

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package geo 负责节点上的 Xray 规则库(geoip.dat / geosite.dat)自动更新。

策略(与面板侧约定一致):

  • 配置了 URL 的资产优先按 URL 下载;下载失败或未配置 URL 时,回退到「与当前内核 同版本」的 XTLS 官方发布包(同一次已校验的 zip 里解出),不额外信任第三方镜像;
  • 新文件先落到临时目录,用 `XRAY_LOCATION_ASSET=<临时目录> xray -test` 做语义校验 (比 sha256 更能发现「文件是好的但缺了配置引用的 code」),通过后才原子替换;
  • 落盘前保留 `.prev`,内容未变不重写;
  • 运行期是否生效取决于 xray 配置里的顶层 geodata.cron:有则由 xray 自己按周期重读, 没有则由调用方注入的回调重启 xray(保证旧配置节点也能用上新数据)。

Index

Constants

View Source
const (
	// DefaultDir xray 在 Linux 上的标准资产目录(搜索顺序中的 /usr/local/share/xray)。
	DefaultDir = upgrade.DefaultGeoDir
	// DefaultInterval 默认一周更新一次(用户可在 geo.interval 覆盖)。
	DefaultInterval = 168 * time.Hour
)

Variables

This section is empty.

Functions

func ApplyMirror

func ApplyMirror(url, mirror string) string

ApplyMirror 把 github.com 直链套上配置的镜像前缀;其他域名原样返回。

Types

type Asset

type Asset struct {
	File   string `yaml:"file"`   // 落盘文件名,如 geoip.dat
	URL    string `yaml:"url"`    // 下载地址;空 = 不单独下载
	SHA256 string `yaml:"sha256"` // 期望摘要;空则尝试 <url>.sha256sum,仍取不到时靠 -test 兜底
}

Asset 一个规则库文件的来源。URL 为空表示只用官方内核包兜底。

type Config

type Config struct {
	Dir       string        `yaml:"dir"`        // 落盘目录,空 = DefaultDir
	Interval  time.Duration `yaml:"interval"`   // 更新周期,0 = DefaultInterval;负数 = 关闭自动更新
	StatePath string        `yaml:"state_path"` // 状态文件,空 = /etc/xray-agent/geo_state.json
	Assets    []Asset       `yaml:"assets"`     // 为空 = 只用官方内核包
}

Config agent.yaml 的 geo 段。

func DefaultConfig

func DefaultConfig() Config

DefaultConfig 返回内置默认值(仅目录与周期;资产列表由安装脚本写入)。

type State

type State struct {
	LastAttempt time.Time `json:"last_attempt"`
	LastSuccess time.Time `json:"last_success"`
	LastError   string    `json:"last_error"`
}

State 持久化到磁盘,避免每次 agent 重启都重新下载。

type Updater

type Updater struct {
	Reload func() error // 配置里没有 geodata.cron 时,落盘后调用它让新数据生效
	Logf   func(format string, args ...any)
	// contains filtered or unexported fields
}

Updater 定时更新规则库。零值不可用,请用 New。

func New

func New(cfg Config, binPath, configPath, mirror, nodeID string, fetcher *upgrade.XrayFetcher) *Updater

New 构造更新器。fetcher 复用内核升级的镜像候选链与下载超时。

func (*Updater) Enabled

func (u *Updater) Enabled() bool

Enabled 是否启用自动更新。

func (*Updater) Run

func (u *Updater) Run(ctx context.Context)

Run 常驻循环;ctx 取消即退出。

func (*Updater) Update

func (u *Updater) Update(ctx context.Context) error

Update 执行一轮更新。所有资产都未变化时也返回 nil。

Jump to

Keyboard shortcuts

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