distro

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package distro provides distribution-specific definitions and caching rules for apt-proxy. This package contains constants, types, and configurations for supported Linux distributions (Ubuntu, Debian, CentOS, Alpine).

Package distro provides distribution configuration loading from external files

Package distro provides distribution registry for dynamic distribution management

Index

Constants

View Source
const (
	DistroAll         string = "all"
	DistroUbuntu      string = "ubuntu"
	DistroUbuntuPorts string = "ubuntu-ports"
	DistroDebian      string = "debian"
	DistroCentOS      string = "centos"
	DistroAlpine      string = "alpine"
)

Distribution name constants

View Source
const (
	TypeAllDistros  int = 0
	TypeUbuntu      int = 1
	TypeUbuntuPorts int = 2
	TypeDebian      int = 3
	TypeCentOS      int = 4
	TypeAlpine      int = 5
)

Distribution type constants

View Source
const (
	UbuntuPortsGeoMirrorAPI = "http://mirrors.ubuntu.com/mirrors.txt"
	// Ubuntu Ports targets non-amd64 architectures. The previous
	// `dists/noble/InRelease/Release` value pointed to a path that does not
	// exist (InRelease is a file, not a directory) and made every benchmark
	// 404. Use the arm64 Release file which mirrors universally publish.
	UbuntuPortsBenchmarkURL = "dists/noble/main/binary-arm64/Release"
)
View Source
const (
	UbuntuGeoMirrorAPI = "http://mirrors.ubuntu.com/mirrors.txt"
	UbuntuBenchmarkURL = "dists/noble/main/binary-amd64/Release"
)
View Source
const AlpineBenchmarkURL = "MIRRORS.txt"
View Source
const CentosBenchmarkURL = "TIME"
View Source
const (
	DebianBenchmarkURL = "dists/bullseye/main/binary-amd64/Release"
)

Variables

View Source
var AlpineCustomMirrors = []string{}
View Source
var AlpineDefaultCacheRules = []Rule{
	{Pattern: regexp.MustCompile(`APKINDEX.tar.gz$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TypeAlpine},
	{Pattern: regexp.MustCompile(`tar.gz$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TypeAlpine},
	{Pattern: regexp.MustCompile(`apk$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TypeAlpine},
	{Pattern: regexp.MustCompile(`.*`), CacheControl: `max-age=100000`, Rewrite: true, OS: TypeAlpine},
}
View Source
var AlpineHostPattern = regexp.MustCompile(`/alpine/(.+)$`)
View Source
var AlpineOfficialMirrors = []string{
	"mirrors.tuna.tsinghua.edu.cn/alpine/",
	"mirrors.ustc.edu.cn/alpine/",
	"mirrors.nju.edu.cn/alpine/",
	"mirrors.sjtug.sjtu.edu.cn/alpine/",
	"mirrors.aliyun.com/alpine/",
}

https://mirrors.alpinelinux.org/ 2022.11.19 Sites that contain protocol headers, restrict access to resources using that protocol

View Source
var CentosCustomMirrors = []string{
	"mirrors.aliyun.com/centos/",
}
View Source
var CentosDefaultCacheRules = []Rule{
	{Pattern: regexp.MustCompile(`repomd.xml$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TypeCentOS},
	{Pattern: regexp.MustCompile(`filelist.gz$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TypeCentOS},
	{Pattern: regexp.MustCompile(`dir_sizes$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TypeCentOS},
	{Pattern: regexp.MustCompile(`TIME$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TypeCentOS},
	{Pattern: regexp.MustCompile(`timestamp.txt$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TypeCentOS},
	{Pattern: regexp.MustCompile(`.*`), CacheControl: `max-age=100000`, Rewrite: true, OS: TypeCentOS},
}
View Source
var CentosHostPattern = regexp.MustCompile(`/centos/(.+)$`)
View Source
var CentosOfficialMirrors = []string{
	"mirrors.bfsu.edu.cn/centos/",
	"mirrors.cqu.edu.cn/CentOS/",
	"http://mirrors.neusoft.edu.cn/centos/",
	"mirrors.nju.edu.cn/centos/",
	"mirrors.huaweicloud.com/centos/",
	"mirror.lzu.edu.cn/centos/",
	"http://mirrors.njupt.edu.cn/centos/",
	"mirrors.163.com/centos/",
	"mirrors.bupt.edu.cn/centos/",
	"ftp.sjtu.edu.cn/centos/",
	"mirrors.tuna.tsinghua.edu.cn/centos/",
	"mirrors.ustc.edu.cn/centos/",
}

https://www.centos.org/download/mirrors/ 2022.11.19 Sites that contain protocol headers, restrict access to resources using that protocol

View Source
var DebianCustomMirrors = []string{
	"repo.huaweicloud.com/debian/",
	"mirrors.cloud.tencent.com/debian/",
	"mirrors.hit.edu.cn/debian/",
	"mirrors.aliyun.com/debian/",
	"mirror.lzu.edu.cn/debian/",
	"mirror.nju.edu.cn/debian/",
}
View Source
var DebianDefaultCacheRules = newDebStyleRules(TypeDebian)
View Source
var DebianHostPattern = regexp.MustCompile(`/debian(-security)?/(.+)$`)
View Source
var DebianOfficialMirrors = []string{
	"http://ftp.cn.debian.org/debian/",
	"mirror.bjtu.edu.cn/debian/",
	"mirrors.163.com/debian/",
	"mirrors.bfsu.edu.cn/debian/",
	"mirrors.huaweicloud.com/debian/",
	"http://mirrors.neusoft.edu.cn/debian/",
	"mirrors.tuna.tsinghua.edu.cn/debian/",
	"mirrors.ustc.edu.cn/debian/",
}

https://www.debian.org/mirror/list 2022.11.19 Sites that contain protocol headers, restrict access to resources using that protocol

View Source
var UbuntuCustomMirrors = []string{
	"mirrors.163.com/ubuntu/",
}
View Source
var UbuntuDefaultCacheRules = newDebStyleRules(TypeUbuntu)
View Source
var UbuntuHostPattern = regexp.MustCompile(`/ubuntu/(.+)$`)
View Source
var UbuntuOfficialMirrors = []string{
	"mirrors.cn99.com/ubuntu/",
	"mirrors.tuna.tsinghua.edu.cn/ubuntu/",
	"mirrors.cnnic.cn/ubuntu/",
	"mirror.bjtu.edu.cn/ubuntu/",
	"mirrors.cqu.edu.cn/ubuntu/",
	"http://mirrors.skyshe.cn/ubuntu/",
	"mirrors.yun-idc.com/ubuntu/",
	"http://mirror.dlut.edu.cn/ubuntu/",
	"mirrors.xjtu.edu.cn/ubuntu/",
	"mirrors.huaweicloud.com/repository/ubuntu/",
	"mirrors.bupt.edu.cn/ubuntu/",
	"mirrors.hit.edu.cn/ubuntu/",
	"http://mirrors.sohu.com/ubuntu/",
	"mirror.nju.edu.cn/ubuntu/",
	"mirrors.bfsu.edu.cn/ubuntu/",
	"mirror.lzu.edu.cn/ubuntu/",
	"mirrors.aliyun.com/ubuntu/",
	"ftp.sjtu.edu.cn/ubuntu/",
	"mirrors.njupt.edu.cn/ubuntu/",
	"mirrors.cloud.tencent.com/ubuntu/",
	"http://mirrors.dgut.edu.cn/ubuntu/",
	"mirrors.ustc.edu.cn/ubuntu/",
	"mirrors.sdu.edu.cn/ubuntu/",
	"http://cn.archive.ubuntu.com/ubuntu/",
}

http://mirrors.ubuntu.com/mirrors.txt 2022.11.19 Sites that contain protocol headers, restrict access to resources using that protocol

View Source
var UbuntuPortsCustomMirrors = []string{
	"mirrors.163.com/ubuntu-ports/",
}
View Source
var UbuntuPortsDefaultCacheRules = newDebStyleRules(TypeUbuntuPorts)
View Source
var UbuntuPortsHostPattern = regexp.MustCompile(`/ubuntu-ports/(.+)$`)
View Source
var UbuntuPortsOfficialMirrors = []string{
	"mirrors.cn99.com/ubuntu-ports/",
	"mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/",
	"mirrors.cnnic.cn/ubuntu-ports/",
	"mirror.bjtu.edu.cn/ubuntu-ports/",
	"mirrors.cqu.edu.cn/ubuntu-ports/",
	"http://mirrors.skyshe.cn/ubuntu-ports/",
	"mirrors.yun-idc.com/ubuntu-ports/",
	"http://mirror.dlut.edu.cn/ubuntu-ports/",
	"mirrors.xjtu.edu.cn/ubuntu-ports/",
	"mirrors.huaweicloud.com/repository/ubuntu-ports/",
	"mirrors.bupt.edu.cn/ubuntu-ports/",
	"mirrors.hit.edu.cn/ubuntu-ports/",
	"http://mirrors.sohu.com/ubuntu-ports/",
	"mirror.nju.edu.cn/ubuntu-ports/",
	"mirrors.bfsu.edu.cn/ubuntu-ports/",
	"mirror.lzu.edu.cn/ubuntu-ports/",
	"mirrors.aliyun.com/ubuntu-ports/",
	"ftp.sjtu.edu.cn/ubuntu-ports/",
	"mirrors.njupt.edu.cn/ubuntu-ports/",
	"mirrors.cloud.tencent.com/ubuntu-ports/",
	"http://mirrors.dgut.edu.cn/ubuntu-ports/",
	"mirrors.ustc.edu.cn/ubuntu-ports/",
	"mirrors.sdu.edu.cn/ubuntu-ports/",
	"http://cn.archive.ubuntu.com/ubuntu-ports/",
}

http://mirrors.ubuntu.com/mirrors.txt 2022.11.19 Sites that contain protocol headers, restrict access to resources using that protocol

Functions

func DistributionName

func DistributionName(distType int) string

DistributionName returns the distribution ID string for the given type. Returns empty string for unknown types.

func GenerateAliasFromURL

func GenerateAliasFromURL(url string) string

GenerateAliasFromURL generates an alias from a URL

func GetHostPatternMap

func GetHostPatternMap() map[*regexp.Regexp][]Rule

GetHostPatternMap returns a map from URL pattern regex to cache rules for all registered distributions. Used by the proxy to match requests and apply rules.

func ReloadDistributionsConfig

func ReloadDistributionsConfig(configPath string) error

ReloadDistributionsConfig resets the global registry to built-in distributions, then loads and applies distributions from the given config file path. When configPath is empty, Load() still tries default paths (./config/distributions.yaml, ./distributions.yaml, /etc/apt-proxy/distributions.yaml, etc.).

Errors loading or registering individual distributions are returned (joined) so callers can surface them to operators. The registry is only mutated after a successful YAML load: a parse/regex error preserves the previous registry state. Per-entry Register failures are accumulated but do not roll back previously-applied entries (which would require a deep clone of the registry); built-in entries are always reapplied first.

Safe to call at startup and on SIGHUP/API reload.

Types

type CacheRuleConfig

type CacheRuleConfig struct {
	Pattern      string `yaml:"pattern"`
	CacheControl string `yaml:"cache_control"`
	Rewrite      bool   `yaml:"rewrite"`
}

CacheRuleConfig represents a cache rule configuration

type DistributionConfig

type DistributionConfig struct {
	ID           string            `yaml:"id"`
	Name         string            `yaml:"name"`
	Type         int               `yaml:"type"`
	URLPattern   string            `yaml:"url_pattern"`
	BenchmarkURL string            `yaml:"benchmark_url"`
	GeoMirrorAPI string            `yaml:"geo_mirror_api,omitempty"`
	CacheRules   []CacheRuleConfig `yaml:"cache_rules"`
	Mirrors      MirrorListConfig  `yaml:"mirrors"`
	Aliases      map[string]string `yaml:"aliases,omitempty"`
}

DistributionConfig represents a distribution configuration loaded from YAML

type DistributionsConfig

type DistributionsConfig struct {
	Distributions []DistributionConfig `yaml:"distributions"`
}

DistributionsConfig represents the root configuration structure

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

Loader handles loading distribution configurations

func NewLoader

func NewLoader(configPath string) *Loader

NewLoader creates a new distribution configuration loader

func (*Loader) GetConfig

func (l *Loader) GetConfig() *DistributionsConfig

GetConfig returns the loaded configuration

func (*Loader) Load

func (l *Loader) Load() (*DistributionsConfig, error)

Load loads distribution configurations from the configured file

func (*Loader) Reload

func (l *Loader) Reload() (*DistributionsConfig, error)

Reload reloads the configuration from file

type MirrorListConfig

type MirrorListConfig struct {
	Official []string `yaml:"official"`
	Custom   []string `yaml:"custom"`
}

MirrorListConfig represents mirror list configuration

type RegisteredDistribution

type RegisteredDistribution struct {
	ID           string
	Name         string
	Type         int
	URLPattern   *regexp.Regexp
	BenchmarkURL string
	GeoMirrorAPI string
	CacheRules   []Rule
	Mirrors      []URLWithAlias
	Aliases      map[string]string
}

RegisteredDistribution represents a registered distribution with its configuration

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages distribution registrations

func GetRegistry

func GetRegistry() *Registry

GetRegistry returns the global distribution registry

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new distribution registry

func (*Registry) Clear

func (r *Registry) Clear()

Clear removes all distributions from the registry

func (*Registry) GetAll

func (r *Registry) GetAll() map[string]*RegisteredDistribution

GetAll returns all registered distributions.

The returned map and each value are independent of the registry's internal state: the map is fresh, the *RegisteredDistribution structs are shallow copies, and the Mirrors / CacheRules / Aliases collections are duplicated at the top level. Callers may safely append to / mutate the headers of those collections without affecting concurrent registry reads.

Element-level data (URLWithAlias, Rule, *regexp.Regexp) is shared by reference; we treat those as immutable once registered, which is true for every code path today (registration always builds fresh values).

func (*Registry) GetByID

func (r *Registry) GetByID(id string) (*RegisteredDistribution, bool)

GetByID returns a distribution by its ID

func (*Registry) GetByType

func (r *Registry) GetByType(distType int) (*RegisteredDistribution, bool)

GetByType returns a distribution by its type

func (*Registry) LoadFromConfig

func (r *Registry) LoadFromConfig(config *DistributionConfig) error

LoadFromConfig loads distributions from a DistributionConfig and registers them

func (*Registry) Register

func (r *Registry) Register(dist *RegisteredDistribution) error

Register registers a distribution in the registry

func (*Registry) Unregister

func (r *Registry) Unregister(id string) error

Unregister removes a distribution from the registry

type Rule

type Rule struct {
	OS           int
	Pattern      *regexp.Regexp
	CacheControl string
	Rewrite      bool
}

Rule defines a caching rule for package files

func (*Rule) String

func (r *Rule) String() string

type URLWithAlias added in v0.10.0

type URLWithAlias struct {
	URL       string
	Alias     string
	Scheme    string
	Official  bool
	Bandwidth int64
}

URLWithAlias represents a mirror URL with its alias and metadata. Scheme is "http", "https", or "" (unknown / let downstream pick the default).

func GenerateBuildInList deprecated

func GenerateBuildInList(officialList []string, customList []string) []URLWithAlias

GenerateBuildInList is the original misspelt name kept for callers that haven't migrated.

Deprecated: use GenerateBuiltinList.

func GenerateBuildInMirorItem deprecated

func GenerateBuildInMirorItem(url string, official bool) URLWithAlias

GenerateBuildInMirorItem is the original misspelt name kept for callers that haven't migrated. New code should use GenerateBuiltinMirrorItem.

Deprecated: use GenerateBuiltinMirrorItem.

func GenerateBuiltinList added in v0.10.0

func GenerateBuiltinList(officialList []string, customList []string) (mirrors []URLWithAlias)

GenerateBuiltinList generates a list of mirror URLs with aliases.

func GenerateBuiltinMirrorItem added in v0.10.0

func GenerateBuiltinMirrorItem(url string, official bool) URLWithAlias

GenerateBuiltinMirrorItem creates a URLWithAlias from a URL.

func (URLWithAlias) HTTP added in v0.10.0

func (u URLWithAlias) HTTP() bool

HTTP reports whether the URL was registered as an HTTP-only mirror. It is a thin convenience over Scheme retained so existing callers don't need to be updated. Prefer reading Scheme directly in new code.

func (URLWithAlias) HTTPS added in v0.10.0

func (u URLWithAlias) HTTPS() bool

HTTPS reports whether the URL was registered as an HTTPS mirror. See HTTP for the migration note.

Jump to

Keyboard shortcuts

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