From 8ebf69bf0a47da40499fe26a80fb8584000591d1 Mon Sep 17 00:00:00 2001 From: yaosanqi137 Date: Thu, 5 Nov 2026 13:06:57 +0800 Subject: [PATCH] feat: add configurable mirror source generators and version management --- README.md | 16 +- backend/content.go | 11 +- backend/generator-defaults.json | 46 ++++++ backend/generators.go | 123 ++++++++++++++ backend/generators_test.go | 109 +++++++++++++ backend/main.go | 5 + docs/help/README.md | 16 ++ docs/help/anaconda.md | 28 +--- docs/help/archlinux.md | 4 +- docs/help/archlinuxarm.md | 4 +- docs/help/archlinuxcn.md | 5 +- docs/help/debian-security.md | 24 +-- docs/help/elrepo.md | 8 +- docs/help/kubernetes.md | 50 ++---- docs/help/openwrt.md | 18 +- docs/help/proxmox.md | 24 +-- docs/help/tailscale.md | 43 ++--- docs/help/ubuntu.md | 33 +--- package.json | 1 + scripts/check-debian-sources.ts | 2 +- scripts/check-mirror-config.ts | 124 ++++++++++++++ src/App.vue | 2 + src/apt-sources.ts | 30 ++++ src/components/AdminPage.vue | 36 +++- src/components/ConfigCode.vue | 55 +++++++ src/components/DebianSources.vue | 91 ----------- src/components/GeneratorCatalogEditor.vue | 90 ++++++++++ src/components/MirrorConfig.vue | 66 ++++++++ src/components/MirrorHelpContent.vue | 14 +- src/debian-sources.ts | 39 ++--- src/generator-catalog.ts | 42 +++++ src/help-content.ts | 14 +- src/locales/admin-messages.ts | 12 ++ src/locales/en-US/mirrors.ts | 23 +++ src/locales/zh-CN/mirrors.ts | 23 +++ src/mirror-config.ts | 190 ++++++++++++++++++++++ src/ubuntu-sources.ts | 25 +++ src/useGeneratorCatalog.ts | 41 +++++ 38 files changed, 1173 insertions(+), 314 deletions(-) create mode 100644 backend/generator-defaults.json create mode 100644 backend/generators.go create mode 100644 backend/generators_test.go create mode 100644 scripts/check-mirror-config.ts create mode 100644 src/apt-sources.ts create mode 100644 src/components/ConfigCode.vue delete mode 100644 src/components/DebianSources.vue create mode 100644 src/components/GeneratorCatalogEditor.vue create mode 100644 src/components/MirrorConfig.vue create mode 100644 src/generator-catalog.ts create mode 100644 src/mirror-config.ts create mode 100644 src/ubuntu-sources.ts create mode 100644 src/useGeneratorCatalog.ts diff --git a/README.md b/README.md index ef496a2..4eafe37 100644 --- a/README.md +++ b/README.md @@ -51,12 +51,26 @@ go -C backend build -o ../.local/mirrorouc . 常用镜像的帮助原稿见 [docs/help/](docs/help/README.md),可复制到后台发布;线上编辑的文章保存在数据库中,部署不会自动覆盖。 -帮助正文可插入交互式 Debian 配置块,`format` 使用 `list`(传统格式)或 `deb822`: +帮助正文可插入交互式配置块,例如 Ubuntu: + +```mirror-config +{"type":"ubuntu"} +``` + +后台的“插入配置生成器”可直接选择软件源并插入对应标记。支持 Debian、Ubuntu、Debian Security、Arch 系列、Conda、Kubernetes、Tailscale、Proxmox VE、OpenWrt 和 ELRepo;控件按软件源提供版本、格式、频道等选项。 + +可用 `format` 固定配置格式,`options` 设置默认选项。已有 Debian 标记保持兼容: ```mirror-config {"type":"debian","format":"list"} ``` +新增软件源的控件规则在 `src/mirror-config.ts` 中注册,共用界面、复制和 Markdown 解析;APT 格式生成逻辑位于 `src/apt-sources.ts`。 + +已有生成器的版本在后台“配置生成器”中维护,支持添加、排序、移除和设置默认版本。Ubuntu 等发行新版本时,填写官方代号与显示名称即可,无需修改代码或重新部署;Debian 的更新组件、Proxmox 的密钥环路径也在此设置。请先确认镜像已同步对应版本,生成器设置不会创建同步任务。 + +版本目录保存在数据库中,由 `/api/v1/generators` 提供给帮助页。`backend/generator-defaults.json` 只用于首次初始化,升级不会覆盖后台保存的选项。 + 镜像目录与下载由已有的 Web 服务器提供,路径为 `/mirrors/<源名>/`。自定义目录页的 Fancyindex 模板随前端构建生成在 `dist/fancy-index/` 中。 构建还会生成 `dist/errors/` 下的独立错误页。后端自动使用这些页面;使用 Nginx 时,可将其错误页指向对应文件,并保留 API 错误的 JSON 响应。 diff --git a/backend/content.go b/backend/content.go index 79075f6..5c7d4dc 100644 --- a/backend/content.go +++ b/backend/content.go @@ -77,7 +77,7 @@ func strict(raw []byte, value any) bool { } func (a *App) listRecords(w http.ResponseWriter, r *http.Request) { kind := r.PathValue("kind") - if kind != "announcements" && kind != "mirrors" && kind != "settings" { + if kind != "announcements" && kind != "mirrors" && kind != "settings" && kind != "generators" { fail(w, 404, "NOT_FOUND") return } @@ -93,7 +93,7 @@ func (a *App) listRecords(w http.ResponseWriter, r *http.Request) { } func (a *App) putRecord(w http.ResponseWriter, r *http.Request) { kind, id := r.PathValue("kind"), r.PathValue("id") - if !validName(id) || (kind != "announcements" && kind != "mirrors" && kind != "settings") { + if !validName(id) || (kind != "announcements" && kind != "mirrors" && kind != "settings" && kind != "generators") { fail(w, 400, "INVALID_INPUT") return } @@ -110,6 +110,13 @@ func (a *App) putRecord(w http.ResponseWriter, r *http.Request) { } var value any switch kind { + case "generators": + var data GeneratorVersions + if !strict(body.Data, &data) || !validGeneratorVersions(id, data) { + fail(w, 400, "INVALID_INPUT") + return + } + value = data case "announcements": var d Announcement if !strict(body.Data, &d) || strings.TrimSpace(d.Title) == "" || len(d.Title) > 300 || len(d.Body) > 250000 || (d.Published && strings.TrimSpace(d.Body) == "") { diff --git a/backend/generator-defaults.json b/backend/generator-defaults.json new file mode 100644 index 0000000..34ea7b3 --- /dev/null +++ b/backend/generator-defaults.json @@ -0,0 +1,46 @@ +{ + "debian": { + "defaultRelease": "trixie", + "releases": [ + { "id": "trixie", "label": "Debian 13 (trixie)", "stable": true, "firmware": true, "backports": true }, + { "id": "bookworm", "label": "Debian 12 (bookworm)", "stable": true, "firmware": true, "backports": false }, + { "id": "bullseye", "label": "Debian 11 (bullseye)", "stable": true, "firmware": false, "backports": false }, + { "id": "testing", "label": "testing", "stable": false, "firmware": true, "backports": false }, + { "id": "sid", "label": "sid", "stable": false, "firmware": true, "backports": false } + ] + }, + "ubuntu": { + "defaultRelease": "noble", + "releases": [ + { "id": "resolute", "label": "Ubuntu 26.04 LTS (resolute)" }, + { "id": "noble", "label": "Ubuntu 24.04 LTS (noble)" }, + { "id": "jammy", "label": "Ubuntu 22.04 LTS (jammy)" } + ] + }, + "kubernetes": { + "defaultRelease": "v1.35", + "releases": [ + { "id": "v1.37", "label": "v1.37" }, + { "id": "v1.36", "label": "v1.36" }, + { "id": "v1.35", "label": "v1.35" }, + { "id": "v1.34", "label": "v1.34" } + ] + }, + "tailscale": { + "defaultRelease": "debian/bookworm", + "releases": [ + { "id": "debian/trixie", "label": "Debian 13 (trixie)" }, + { "id": "debian/bookworm", "label": "Debian 12 (bookworm)" }, + { "id": "ubuntu/resolute", "label": "Ubuntu 26.04 LTS (resolute)" }, + { "id": "ubuntu/noble", "label": "Ubuntu 24.04 LTS (noble)" }, + { "id": "ubuntu/jammy", "label": "Ubuntu 22.04 LTS (jammy)" } + ] + }, + "proxmox": { + "defaultRelease": "trixie", + "releases": [ + { "id": "trixie", "label": "Proxmox VE 9 / Debian 13 (trixie)", "keyring": "/usr/share/keyrings/proxmox-archive-keyring.gpg" }, + { "id": "bookworm", "label": "Proxmox VE 8 / Debian 12 (bookworm)", "keyring": "/etc/apt/trusted.gpg.d/proxmox-release-bookworm.gpg" } + ] + } +} diff --git a/backend/generators.go b/backend/generators.go new file mode 100644 index 0000000..d20a8a6 --- /dev/null +++ b/backend/generators.go @@ -0,0 +1,123 @@ +package main + +import ( + _ "embed" + "encoding/json" + "errors" + "net/http" + "regexp" + "strings" + "unicode" +) + +//go:embed generator-defaults.json +var generatorDefaultsJSON []byte + +type GeneratorRelease struct { + ID string `json:"id"` + Label string `json:"label"` + Stable *bool `json:"stable,omitempty"` + Firmware *bool `json:"firmware,omitempty"` + Backports *bool `json:"backports,omitempty"` + Keyring string `json:"keyring,omitempty"` +} + +type GeneratorVersions struct { + DefaultRelease string `json:"defaultRelease"` + Releases []GeneratorRelease `json:"releases"` +} + +var generatorTypes = []string{"debian", "ubuntu", "kubernetes", "tailscale", "proxmox"} +var suiteID = regexp.MustCompile(`^[a-z][a-z0-9-]{0,31}$`) +var kubernetesID = regexp.MustCompile(`^v[1-9][0-9]{0,2}\.(0|[1-9][0-9]{0,2})$`) +var tailscaleID = regexp.MustCompile(`^(debian|ubuntu)/[a-z][a-z0-9-]{0,31}$`) +var keyringPath = regexp.MustCompile(`^/(usr/share/keyrings|etc/apt/trusted\.gpg\.d)/[a-zA-Z0-9][a-zA-Z0-9_-]{0,95}\.gpg$`) + +func generatorType(id string) bool { + for _, known := range generatorTypes { + if id == known { + return true + } + } + return false +} + +// Values are inserted into repository files and shell commands. Accept only +// version identifiers and keyring paths, never arbitrary templates or scripts. +func validGeneratorVersions(id string, data GeneratorVersions) bool { + if !generatorType(id) || len(data.Releases) == 0 || len(data.Releases) > 100 { + return false + } + seen := map[string]bool{} + hasDefault, hasStable := false, false + for _, release := range data.Releases { + validID := suiteID.MatchString(release.ID) + if id == "kubernetes" { + validID = kubernetesID.MatchString(release.ID) + } + if id == "tailscale" { + validID = tailscaleID.MatchString(release.ID) + } + if !validID || seen[release.ID] || strings.TrimSpace(release.Label) == "" || len([]rune(release.Label)) > 160 || strings.IndexFunc(release.Label, unicode.IsControl) >= 0 { + return false + } + seen[release.ID] = true + hasDefault = hasDefault || release.ID == data.DefaultRelease + if id == "debian" { + if release.Stable == nil || release.Firmware == nil || release.Backports == nil || (*release.Backports && !*release.Stable) { + return false + } + hasStable = hasStable || *release.Stable + } else if release.Stable != nil || release.Firmware != nil || release.Backports != nil { + return false + } + if id == "proxmox" { + if !keyringPath.MatchString(release.Keyring) { + return false + } + } else if release.Keyring != "" { + return false + } + } + return hasDefault && (id != "debian" || hasStable) +} + +// Seed only missing records. Subsequent releases never overwrite admin edits. +func (a *App) seedGeneratorVersions() error { + var defaults map[string]GeneratorVersions + if err := json.Unmarshal(generatorDefaultsJSON, &defaults); err != nil { + return err + } + for _, id := range generatorTypes { + data := defaults[id] + if !validGeneratorVersions(id, data) { + return errors.New("invalid generator defaults: " + id) + } + if _, err := a.record("generators", id); errors.Is(err, errMissing) { + if _, err = a.saveRecord("generators", id, data, 0); err != nil { + return err + } + } else if err != nil { + return err + } + } + return nil +} + +func (a *App) generatorCatalog(w http.ResponseWriter, r *http.Request) { + records, err := a.records("generators") + if err != nil { + a.internal(w, err) + return + } + catalog := map[string]GeneratorVersions{} + for _, record := range records { + var data GeneratorVersions + if decodeData(record.Data, &data) != nil || !validGeneratorVersions(record.ID, data) { + a.internal(w, errors.New("invalid generator record: "+record.ID)) + return + } + catalog[record.ID] = data + } + respond(w, 200, catalog) +} diff --git a/backend/generators_test.go b/backend/generators_test.go new file mode 100644 index 0000000..57feb3c --- /dev/null +++ b/backend/generators_test.go @@ -0,0 +1,109 @@ +package main + +import ( + "encoding/json" + "reflect" + "testing" +) + +func TestGeneratorCatalogPersistenceAndAuthorization(t *testing.T) { + c := fixture(t) + w := c.call("GET", "/api/v1/generators", nil, false) + status(t, w, 200) + var catalog map[string]GeneratorVersions + json.Unmarshal(w.Body.Bytes(), &catalog) + if len(catalog) != 5 { + t.Fatal("missing seeded version catalogs") + } + settingsBefore, _ := c.app.record("settings", "site") + before, _ := c.app.record("generators", "ubuntu") + versions := catalog["ubuntu"] + // Deliberately fictional future data: no official codename is assumed. + versions.Releases = append(versions.Releases, GeneratorRelease{ID: "future-test", Label: "Ubuntu future (test fixture)"}) + versions.DefaultRelease = "future-test" + body := map[string]any{"data": versions, "revision": before.Revision} + path := "/api/v1/admin/records/generators/ubuntu" + status(t, c.call("PUT", path, body, true), 401) + c.login("editor") + status(t, c.call("GET", "/api/v1/admin/records/generators", nil, false), 403) + status(t, c.call("PUT", path, body, true), 403) + c.login("admin") + status(t, c.call("PUT", path, body, false), 403) + status(t, c.call("PUT", path, body, true), 200) + status(t, c.call("PUT", path, body, true), 409) + status(t, c.call("DELETE", path, map[string]any{"revision": before.Revision + 1}, true), 400) + // Starting a new app on the same DB must not overwrite edited defaults. + c.app.db.Close() + restarted, err := newApp(c.app.cfg) + if err != nil { + t.Fatal(err) + } + c.app = restarted + t.Cleanup(func() { restarted.db.Close() }) + w = c.call("GET", "/api/v1/generators", nil, false) + status(t, w, 200) + json.Unmarshal(w.Body.Bytes(), &catalog) + if !reflect.DeepEqual(catalog["ubuntu"], versions) { + t.Fatal("saved future release lost after restart") + } + settingsAfter, _ := c.app.record("settings", "site") + if !reflect.DeepEqual(settingsBefore, settingsAfter) { + t.Fatal("generator edits changed site settings") + } + var records []Record + w = c.call("GET", "/api/v1/admin/records/generators", nil, false) + status(t, w, 200) + json.Unmarshal(w.Body.Bytes(), &records) + if len(records) != 5 { + t.Fatal("admin cannot read saved version catalogs") + } +} + +func TestGeneratorCatalogRejectsUnsafeAndInconsistentData(t *testing.T) { + c := fixture(t) + c.login("admin") + var defaults map[string]GeneratorVersions + json.Unmarshal(generatorDefaultsJSON, &defaults) + for _, id := range generatorTypes { + if !validGeneratorVersions(id, defaults[id]) { + t.Fatal("invalid shared seed", id) + } + } + for _, id := range []string{"ubuntu", "kubernetes", "tailscale", "proxmox", "debian"} { + for _, bad := range []string{"$(id)", "noble\ntrusted=yes", "../../etc/passwd", "foo;id", "foo bar", "__proto__"} { + data := GeneratorVersions{DefaultRelease: bad, Releases: []GeneratorRelease{{ID: bad, Label: "test"}}} + if validGeneratorVersions(id, data) { + t.Fatal("accepted unsafe ID", id, bad) + } + } + } + invalid := []any{ + map[string]any{"defaultRelease": "noble", "releases": []any{}}, + map[string]any{"defaultRelease": "missing", "releases": []GeneratorRelease{{ID: "noble", Label: "Ubuntu"}}}, + map[string]any{"defaultRelease": "noble", "releases": []GeneratorRelease{{ID: "noble", Label: "Ubuntu"}, {ID: "noble", Label: "duplicate"}}}, + map[string]any{"defaultRelease": "noble", "releases": []GeneratorRelease{{ID: "noble", Label: ""}}}, + map[string]any{"defaultRelease": "noble", "releases": []any{map[string]any{"id": "noble", "label": "Ubuntu", "command": "id"}}}, + map[string]any{"defaultRelease": "noble", "releases": []GeneratorRelease{{ID: "noble", Label: "Ubuntu", Keyring: "/tmp/other.gpg"}}}, + } + record, _ := c.app.record("generators", "ubuntu") + for _, data := range invalid { + status(t, c.call("PUT", "/api/v1/admin/records/generators/ubuntu", map[string]any{"data": data, "revision": record.Revision}, true), 400) + } + status(t, c.call("PUT", "/api/v1/admin/records/generators/unknown", map[string]any{"data": defaults["ubuntu"], "revision": 0}, true), 400) + pve := defaults["proxmox"] + for _, path := range []string{"/tmp/a.gpg", "/usr/share/keyrings/../../tmp/a.gpg", "/usr/share/keyrings/a.gpg\nmalicious", "/usr/share/keyrings/$(id).gpg"} { + pve.Releases[0].Keyring = path + if validGeneratorVersions("proxmox", pve) { + t.Fatal("accepted unsafe keyring", path) + } + } + debian := defaults["debian"] + for index := range debian.Releases { + value := false + debian.Releases[index].Stable = &value + debian.Releases[index].Backports = &value + } + if validGeneratorVersions("debian", debian) { + t.Fatal("Debian Security lost all stable releases") + } +} diff --git a/backend/main.go b/backend/main.go index 64a319f..4a0cd90 100644 --- a/backend/main.go +++ b/backend/main.go @@ -91,6 +91,10 @@ func newApp(cfg Config) (*App, error) { db.Close() return nil, err } + if err = a.seedGeneratorVersions(); err != nil { + db.Close() + return nil, err + } return a, nil } func (a *App) handler() http.Handler { @@ -105,6 +109,7 @@ func (a *App) handler() http.Handler { mux.HandleFunc("GET /api/v1/public", a.public) mux.HandleFunc("GET /api/v1/announcements", a.announcements) mux.HandleFunc("GET /api/v1/help", a.helpIndex) + mux.HandleFunc("GET /api/v1/generators", a.generatorCatalog) mux.HandleFunc("GET /api/v1/help/{name}", a.helpArticle) mux.HandleFunc("GET /api/v1/monitoring", a.monitoring) mux.HandleFunc("GET /api/v1/job-speeds", a.jobSpeeds) diff --git a/docs/help/README.md b/docs/help/README.md index cbb4397..201e339 100644 --- a/docs/help/README.md +++ b/docs/help/README.md @@ -23,3 +23,19 @@ | [ubuntu.md](ubuntu.md) | Ubuntu 软件仓库 | 文章参考 TUNA 帮助和各项目官方文档编写,原始资料链接见各篇末尾。目录、版本和同步范围以本站实际提供的内容为准。 + +## 交互式配置 + +后台可通过“插入配置生成器”添加控件,也可以直接在 Markdown 中插入: + +```mirror-config +{"type":"ubuntu"} +``` + +目前注册的类型为 `debian`、`ubuntu`、`debian-security`、`archlinux`、`archlinuxarm`、`archlinuxcn`、`anaconda`、`kubernetes`、`tailscale`、`proxmox`、`openwrt`、`elrepo`。 + +不指定 `format` 时,支持多格式的控件会显示格式选择。指定格式可固定展示,例如 `{"type":"ubuntu","format":"deb822","options":{"release":"noble"}}`。默认选项只接受对应控件已定义的值,不支持脚本或任意模板。 + +后台保存文章与控件标记;版本列表和默认版本在“配置生成器”中维护,保存后帮助页会自动读取,无需重新编译。`debian-security` 共用 Debian 中的稳定版选项。移除版本前,需检查文章中是否通过 `options.release` 固定引用了它;失效的配置标记会显示为普通代码供作者修正。 + +前端的 `src/mirror-config.ts` 注册生成规则,`MirrorConfig.vue` 和 `ConfigCode.vue` 共用布局和复制逻辑。新增软件源或更改包管理器规则仍需修改代码;`backend/generator-defaults.json` 仅用于首次初始化版本目录。 diff --git a/docs/help/anaconda.md b/docs/help/anaconda.md index 8ad4e0a..5f6bbee 100644 --- a/docs/help/anaconda.md +++ b/docs/help/anaconda.md @@ -10,21 +10,15 @@ conda config --set show_channel_urls yes 切换前确认所需频道的平台目录已有 `repodata.json`,例如 [conda-forge 的 linux-64 索引](/mirrors/anaconda/cloud/conda-forge/linux-64/)。索引尚未同步完成时,先保留原配置。 -## 使用 conda-forge +## 生成频道配置 -将 `.condarc` 中相关配置改为以下内容。已有的 `channels` 应一并检查,避免意外混用不同频道的软件包: +在下面选择 conda-forge、Bioconda 或 defaults,将生成的内容合并到 `.condarc`。已有的 `channels` 应一并检查,避免意外混用不同频道的软件包。 -```yaml -channels: - - conda-forge -show_channel_urls: true -channel_priority: strict -custom_channels: - conda-forge: https://mirrors.app.itstudio.club/mirrors/anaconda/cloud - bioconda: https://mirrors.app.itstudio.club/mirrors/anaconda/cloud +```mirror-config +{"type":"anaconda"} ``` -这里的 `custom_channels` 地址不包含频道名,Conda 会自动补上 `conda-forge` 或 `bioconda`。 +使用社区频道时,`custom_channels` 地址不包含频道名,Conda 会自动补上相应名称。Bioconda 配置会按 conda-forge、bioconda 的顺序启用频道。 刷新索引,并在新环境中验证: @@ -42,17 +36,7 @@ conda create -n bio-test --override-channels -c conda-forge -c bioconda samtools ## 使用 defaults -如果项目需要 Anaconda 的 `defaults`,可改用下面这套配置。它是上一套配置的替代方案: - -```yaml -channels: - - defaults -show_channel_urls: true -default_channels: - - https://mirrors.app.itstudio.club/mirrors/anaconda/pkgs/main - - https://mirrors.app.itstudio.club/mirrors/anaconda/pkgs/r - - https://mirrors.app.itstudio.club/mirrors/anaconda/pkgs/msys2 -``` +如果项目需要 Anaconda 的 `defaults`,在上方控件中切换到 defaults,使用生成的 `default_channels` 配置。它是社区频道配置的替代方案,不要将两套配置直接叠加。 使用前请查阅 Anaconda 对相应仓库的使用条款。自定义环境文件中的完整 URL、`-c` 参数和其他配置文件可能覆盖 `.condarc`;可通过 `conda config --show-sources` 排查。 diff --git a/docs/help/archlinux.md b/docs/help/archlinux.md index 74dcaea..bb65fdd 100644 --- a/docs/help/archlinux.md +++ b/docs/help/archlinux.md @@ -10,8 +10,8 @@ sudo cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak 编辑 `/etc/pacman.d/mirrorlist`,将下面一行放在最前面: -```text -Server = https://mirrors.app.itstudio.club/mirrors/archlinux/$repo/os/$arch +```mirror-config +{"type":"archlinux"} ``` `$repo` 和 `$arch` 是 pacman 使用的变量,请原样保留。`/etc/pacman.conf` 中相应仓库应继续通过 `Include = /etc/pacman.d/mirrorlist` 引用镜像列表。 diff --git a/docs/help/archlinuxarm.md b/docs/help/archlinuxarm.md index 7c2aca9..cf62c00 100644 --- a/docs/help/archlinuxarm.md +++ b/docs/help/archlinuxarm.md @@ -10,8 +10,8 @@ sudo cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak 将下面一行放到 `/etc/pacman.d/mirrorlist` 的最前面: -```text -Server = https://mirrors.app.itstudio.club/mirrors/archlinuxarm/$arch/$repo +```mirror-config +{"type":"archlinuxarm"} ``` 保留 `$arch` 和 `$repo` 原样。这里没有 Arch Linux 官方仓库路径中的 `os/`。 diff --git a/docs/help/archlinuxcn.md b/docs/help/archlinuxcn.md index b16fd4d..e885f45 100644 --- a/docs/help/archlinuxcn.md +++ b/docs/help/archlinuxcn.md @@ -4,9 +4,8 @@ Arch Linux CN 是 Arch Linux 中文社区维护的非官方软件仓库,收录 备份 `/etc/pacman.conf`,在文件末尾添加: -```ini -[archlinuxcn] -Server = https://mirrors.app.itstudio.club/mirrors/archlinuxcn/$arch +```mirror-config +{"type":"archlinuxcn"} ``` `$arch` 保持原样。本站提供的架构可在 [仓库目录](/mirrors/archlinuxcn/)中查看。已有 `[archlinuxcn]` 配置时,修改原来的 `Server`,不要重复添加仓库段。 diff --git a/docs/help/debian-security.md b/docs/help/debian-security.md index 2964069..83d795d 100644 --- a/docs/help/debian-security.md +++ b/docs/help/debian-security.md @@ -14,29 +14,15 @@ 目录存在不代表该版本仍处于官方安全支持期,请同时查阅 Debian 的安全支持和 LTS 公告。 -## DEB822 格式 +## 生成安全更新配置 -以 Debian 13 为例,备份 `/etc/apt/sources.list.d/debian.sources`,将其中安全更新段替换为: +备份 `/etc/apt/sources.list` 或 `/etc/apt/sources.list.d/debian.sources`,在下面选择当前系统版本和格式,只替换原有的安全更新段。保留主仓库、updates 等其他配置。 -```text -Types: deb -URIs: https://mirrors.app.itstudio.club/mirrors/debian-security/ -Suites: trixie-security -Components: main contrib non-free non-free-firmware -Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg +```mirror-config +{"type":"debian-security"} ``` -保留文件中 Debian 主仓库、updates 等其他段。Debian 12 将 `trixie-security` 改为 `bookworm-security`;Debian 11 使用 `bullseye-security`,并移除 `non-free-firmware` 组件。 - -## 传统 sources.list 格式 - -如果系统仍使用传统格式,备份 `/etc/apt/sources.list` 后,将原来的安全更新行改为: - -```text -deb https://mirrors.app.itstudio.club/mirrors/debian-security/ trixie-security main contrib non-free non-free-firmware -``` - -按系统版本调整套件和组件。两种格式选择一种即可,不要重复添加相同的安全更新源。 +组件会按发行版调整:Debian 11 不加入 `non-free-firmware`。传统格式和 DEB822 选择一种即可,不要重复添加安全更新源。这里生成的内容会使用本站安全更新镜像;希望保留官方安全更新时,请使用 [Debian 帮助页](/help/debian/)中的完整配置生成器。 ## 更新索引 diff --git a/docs/help/elrepo.md b/docs/help/elrepo.md index a96605b..c00c097 100644 --- a/docs/help/elrepo.md +++ b/docs/help/elrepo.md @@ -25,14 +25,12 @@ sudo cp /etc/yum.repos.d/elrepo.repo /etc/yum.repos.d/elrepo.repo.bak 2. 启用对应的 `baseurl=`,将其中 `http://elrepo.org/linux` 或 `https://elrepo.org/linux` 替换为 `https://mirrors.app.itstudio.club/mirrors/elrepo`。 3. 保留路径后面的系统版本、仓库名称及 `$basearch` 变量;保留官方配置中的签名检查和密钥地址。 -例如 EL9 的基础仓库地址应为: +也可以使用下方命令完成备份、启用直接下载地址、替换地址及刷新缓存。命令保留仓库配置中的系统版本、架构和签名校验设置。 -```ini -baseurl=https://mirrors.app.itstudio.club/mirrors/elrepo/elrepo/el9/$basearch/ +```mirror-config +{"type":"elrepo"} ``` -这只是一行地址示例,不是完整的 `.repo` 文件。不要用它覆盖 `enabled`、`gpgcheck`、`gpgkey` 等其他设置。 - ## 刷新缓存 ```sh diff --git a/docs/help/kubernetes.md b/docs/help/kubernetes.md index 4f73a70..1e77699 100644 --- a/docs/help/kubernetes.md +++ b/docs/help/kubernetes.md @@ -1,72 +1,54 @@ 本站镜像 Kubernetes 的版本化软件包仓库,可用于安装 `kubelet`、`kubeadm` 和 `kubectl`。容器镜像、容器运行时及集群网络插件需要另行配置。 -本文以 `v1.35` 仓库为例。它是示例版本,不代表最新版本;已有集群应根据官方升级流程及版本偏差策略选择次版本,并在所有仓库地址中保持一致。修改软件源不会自动完成集群升级。 +## 选择版本和软件包格式 -## Debian / Ubuntu +先根据安装或升级计划确定 Kubernetes 次版本。已有集群需要遵守官方版本偏差策略;修改软件源不会自动完成集群升级。控件中的版本是本站已提供的仓库分支,不代表所有版本都仍处于官方支持期。 -安装所需工具: +Debian、Ubuntu 选择 APT,RPM 系统选择 RPM。下面的公钥地址和软件源会一起随版本变化。 + +## 准备工具 + +APT 系统先安装工具: ```sh sudo apt update sudo apt install ca-certificates curl gpg -sudo install -d -m 0755 /etc/apt/keyrings ``` -从 Kubernetes 官方仓库获取该通道的签名公钥。如果同名密钥文件已经存在,先核对、备份,再更新: +备份已有 Kubernetes 源文件和同名签名密钥文件,再按控件中的步骤配置。APT 显示的第一段命令导入官方公钥,第二段是需要保存的源文件内容;RPM 配置通过 `gpgkey` 指定对应的官方公钥。 -```sh -curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.35/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg -sudo chmod 0644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg +```mirror-config +{"type":"kubernetes"} ``` -备份已有 `/etc/apt/sources.list.d/kubernetes.list`,然后写入: +## 刷新索引并安装 -```text -deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://mirrors.app.itstudio.club/mirrors/kubernetes/core:/stable:/v1.35/deb/ / -``` - -更新索引并查看可安装版本: +APT 系统查看可安装版本: ```sh sudo apt update apt-cache madison kubeadm ``` -新节点需要安装组件时,可执行: +新节点需要安装组件时: ```sh sudo apt install kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl ``` -已有集群请按升级指南逐个处理节点,不要直接套用新节点安装步骤。 - -## RPM 系统 - -备份已有 `/etc/yum.repos.d/kubernetes.repo`,配置为: - -```ini -[kubernetes] -name=Kubernetes -baseurl=https://mirrors.app.itstudio.club/mirrors/kubernetes/core:/stable:/v1.35/rpm/ -enabled=1 -gpgcheck=1 -gpgkey=https://pkgs.k8s.io/core:/stable:/v1.35/rpm/repodata/repomd.xml.key -exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni -``` - -刷新缓存并查看版本: +RPM 系统查看可安装版本: ```sh sudo dnf makecache dnf --disableexcludes=kubernetes list --showduplicates kubeadm ``` -新节点安装组件时使用 `sudo dnf install kubelet kubeadm kubectl --disableexcludes=kubernetes`。`exclude` 用于避免普通系统更新意外升级集群组件。 +新节点安装使用 `sudo dnf install kubelet kubeadm kubectl --disableexcludes=kubernetes`。配置中的 `exclude` 避免普通系统更新意外升级集群组件。已有集群应按升级指南逐个处理节点。 ## 检查地址 -本站使用 `core:/stable:/v1.xx/` 目录布局,不使用旧的 `apt.kubernetes.io` 地址。其他版本请先查看 [仓库目录](/mirrors/kubernetes/core:/stable:/)是否已同步,并确认该版本仍符合项目的支持策略。 +本站使用 `core:/stable:/v1.xx/` 目录布局,不使用旧的 `apt.kubernetes.io` 地址。其他版本请查看 [仓库目录](/mirrors/kubernetes/core:/stable:/),并确认对应分支已完成同步。 ## 参考资料 diff --git a/docs/help/openwrt.md b/docs/help/openwrt.md index b5e7e9b..2fd9d49 100644 --- a/docs/help/openwrt.md +++ b/docs/help/openwrt.md @@ -6,22 +6,12 @@ OpenWrt 25.12 起使用 APK;24.10 及更早版本使用 OPKG。第三方固件可能更改包管理器或仓库,应以实际系统为准。下面的命令在路由器的 root 会话中执行。 -## APK:OpenWrt 25.12 及之后 +## 生成换源命令 -备份并替换官方仓库域名: +根据当前系统选择 APK 或 OPKG。下面的命令先备份配置,再替换仓库地址并刷新索引。 -```sh -cp /etc/apk/repositories.d/distfeeds.list /etc/apk/repositories.d/distfeeds.list.bak -sed -i 's|https\?://downloads.openwrt.org|https://mirrors.app.itstudio.club/mirrors/openwrt|g' /etc/apk/repositories.d/distfeeds.list -apk update -``` - -## OPKG:OpenWrt 24.10 及之前 - -```sh -cp /etc/opkg/distfeeds.conf /etc/opkg/distfeeds.conf.bak -sed -i 's|https\?://downloads.openwrt.org|https://mirrors.app.itstudio.club/mirrors/openwrt|g' /etc/opkg/distfeeds.conf -opkg update +```mirror-config +{"type":"openwrt"} ``` 两种方式仅替换官方地址的前缀,保留原来的版本、target、subtarget 和软件包架构路径。自定义仓库保持原样。不要复制其他路由器的整份源配置,否则可能安装不兼容的软件包。 diff --git a/docs/help/proxmox.md b/docs/help/proxmox.md index 854ebc6..7e39cf2 100644 --- a/docs/help/proxmox.md +++ b/docs/help/proxmox.md @@ -11,29 +11,17 @@ Proxmox VE 9 基于 Debian 13(`trixie`),Proxmox VE 8 基于 Debian 12(`b 以下操作在节点的 root 终端中执行。操作前备份 `/etc/apt/sources.list` 及 `/etc/apt/sources.list.d/` 中将要修改的文件。 -## Proxmox VE 9:DEB822 格式 +## 生成仓库配置 -创建或编辑 `/etc/apt/sources.list.d/proxmox.sources`: +选择当前 Proxmox VE 版本和源配置格式,将生成的内容保存到所标注的文件中。PVE 9 通常使用 DEB822,PVE 8 通常使用传统格式。 -```text -Types: deb -URIs: https://mirrors.app.itstudio.club/mirrors/proxmox/debian/pve -Suites: trixie -Components: pve-no-subscription -Signed-By: /usr/share/keyrings/proxmox-archive-keyring.gpg +```mirror-config +{"type":"proxmox"} ``` -密钥环由 Proxmox 官方软件包提供;若文件缺失,请先按官方说明修复密钥环,不要移除 `Signed-By` 或关闭验证。 +Proxmox 官方安装的软件包负责提供签名密钥;PVE 9 使用 `/usr/share/keyrings/proxmox-archive-keyring.gpg`,PVE 8 的官方密钥文件为 `/etc/apt/trusted.gpg.d/proxmox-release-bookworm.gpg`。文件缺失时先按对应版本的官方说明修复,不要移除签名校验。传统格式继续使用系统已有的官方签名密钥配置。 -## Proxmox VE 8:传统格式 - -创建或编辑 `/etc/apt/sources.list.d/pve-no-subscription.list`: - -```text -deb https://mirrors.app.itstudio.club/mirrors/proxmox/debian/pve bookworm pve-no-subscription -``` - -继续使用系统已有的官方签名密钥配置。两种格式按当前版本选择其一,不要重复启用相同仓库。 +两种格式选择其一,不要重复启用相同仓库。 ## 检查仓库并更新 diff --git a/docs/help/tailscale.md b/docs/help/tailscale.md index 7f805f7..0cd6eb6 100644 --- a/docs/help/tailscale.md +++ b/docs/help/tailscale.md @@ -1,50 +1,27 @@ 本站提供 Tailscale 稳定通道的 Debian、Ubuntu APT 软件包,分别位于 `tailscale/debian/` 和 `tailscale/ubuntu/`。镜像地址中不含上游的 `stable/` 路径;本站未提供对应的 RPM 仓库。 -先运行 `cat /etc/os-release` 确认发行版和代号。下面两套配置择一使用,不能在 Ubuntu 上使用 Debian 仓库,反之亦然。 +## 确认系统 -## 准备工具 +运行 `cat /etc/os-release` 确认发行版和代号。不能在 Ubuntu 上选择 Debian 仓库,反之亦然。 + +安装工具: ```sh sudo apt update sudo apt install ca-certificates curl -sudo install -d -m 0755 /usr/share/keyrings ``` -已有 `/etc/apt/sources.list.d/tailscale.list` 或同名密钥环时,先备份并检查,避免重复配置。密钥从 Tailscale 官方站点获取。 +已有 `/etc/apt/sources.list.d/tailscale.list`、`tailscale.sources` 或同名密钥环时,先备份并检查,避免重复配置。 -## Debian 示例 +## 生成配置 -以 Debian 12(`bookworm`)为例,下载官方公钥: +选择当前系统版本。第一段命令从 Tailscale 官方获取对应公钥,第二段是需要保存的源文件内容,两者会一起随版本变化。传统格式和 DEB822 选择一种即可。 -```sh -curl -fsSL https://pkgs.tailscale.com/stable/debian/bookworm.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null -sudo chmod 0644 /usr/share/keyrings/tailscale-archive-keyring.gpg +```mirror-config +{"type":"tailscale"} ``` -将 `/etc/apt/sources.list.d/tailscale.list` 设置为: - -```text -deb [signed-by=/usr/share/keyrings/tailscale-archive-keyring.gpg] https://mirrors.app.itstudio.club/mirrors/tailscale/debian bookworm main -``` - -Debian 13 使用 `trixie`,同时替换官方密钥下载地址和源配置中的代号。其他版本请先确认 [Debian 目录](/mirrors/tailscale/debian/dists/)中存在对应套件。 - -## Ubuntu 示例 - -以 Ubuntu 24.04 LTS(`noble`)为例: - -```sh -curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/noble.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null -sudo chmod 0644 /usr/share/keyrings/tailscale-archive-keyring.gpg -``` - -将 `/etc/apt/sources.list.d/tailscale.list` 设置为: - -```text -deb [signed-by=/usr/share/keyrings/tailscale-archive-keyring.gpg] https://mirrors.app.itstudio.club/mirrors/tailscale/ubuntu noble main -``` - -其他 Ubuntu 版本需同时修改密钥地址和套件代号,并确认 [Ubuntu 目录](/mirrors/tailscale/ubuntu/dists/)中已提供该套件。 +未列出的版本请先检查 [Debian 目录](/mirrors/tailscale/debian/dists/)或 [Ubuntu 目录](/mirrors/tailscale/ubuntu/dists/)是否有对应套件,并参照官方说明配置。 ## 安装和登录 diff --git a/docs/help/ubuntu.md b/docs/help/ubuntu.md index aa519c2..81a2899 100644 --- a/docs/help/ubuntu.md +++ b/docs/help/ubuntu.md @@ -14,38 +14,19 @@ dpkg --print-architecture 切换前检查 [noble 索引目录](/mirrors/ubuntu/dists/noble/)是否有 `InRelease` 或 `Release`。如果索引尚未同步完成,请继续使用官方源,稍后再切换。 -## Ubuntu 24.04 及之后:DEB822 格式 +## 生成软件源配置 -先备份 `/etc/apt/sources.list.d/ubuntu.sources`,再将其中的 Ubuntu 仓库配置替换为: +先备份系统现有的软件源配置。Ubuntu 24.04 及之后通常使用 `/etc/apt/sources.list.d/ubuntu.sources`,Ubuntu 22.04 通常使用 `/etc/apt/sources.list`。 -```text -Types: deb -URIs: https://mirrors.app.itstudio.club/mirrors/ubuntu/ -Suites: noble noble-updates noble-backports -Components: main restricted universe multiverse -Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg +在下面选择当前系统的版本和配置格式,将生成的内容替换到对应文件中。配置中的域名随当前访问的镜像站地址变化,右上角按钮可复制完整配置。 -Types: deb -URIs: http://security.ubuntu.com/ubuntu/ -Suites: noble-security -Components: main restricted universe multiverse -Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg +```mirror-config +{"type":"ubuntu"} ``` -安全更新保留官方源,以减少镜像同步延迟的影响。不要同时保留另一份相同仓库配置。 +安全更新默认保留官方源,以减少镜像同步延迟的影响。`proposed` 默认关闭,普通使用无需开启。源码开关控制是否获取源码包索引。 -## Ubuntu 22.04:传统格式 - -备份 `/etc/apt/sources.list` 后,将 Ubuntu 条目替换为: - -```text -deb https://mirrors.app.itstudio.club/mirrors/ubuntu/ jammy main restricted universe multiverse -deb https://mirrors.app.itstudio.club/mirrors/ubuntu/ jammy-updates main restricted universe multiverse -deb https://mirrors.app.itstudio.club/mirrors/ubuntu/ jammy-backports main restricted universe multiverse -deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse -``` - -保留其他独立软件源的配置。需要源码包时,可以在传统格式中增加对应 `deb-src` 行,或将 DEB822 的 `Types` 改为 `deb deb-src`。 +保留其他独立软件源的配置。传统格式和 DEB822 格式选择一种即可,不要重复启用相同的 Ubuntu 仓库。选择 HTTP 或 HTTPS 时,请确认当前镜像站入口支持对应协议。 ## 更新索引 diff --git a/package.json b/package.json index 2a55255..5b42b06 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "check:monitoring": "node scripts/check-monitoring.ts", "check:help": "node scripts/check-help.ts", "check:debian": "node scripts/check-debian-sources.ts", + "check:generators": "node scripts/check-mirror-config.ts", "check:directory": "node scripts/check-directory.ts", "check:errors": "node scripts/check-errors.ts" }, diff --git a/scripts/check-debian-sources.ts b/scripts/check-debian-sources.ts index b142c18..a053ff6 100644 --- a/scripts/check-debian-sources.ts +++ b/scripts/check-debian-sources.ts @@ -44,7 +44,7 @@ assert.throws(() => debianSources({ ...base, release: 'unknown' as DebianSourceO const fence = (data: string) => '```mirror-config\n' + data + '\n```\n' const source = '## 配置\n\n[Debian][official]\n\n' + fence('{"type":"debian","format":"list"}') + '\n## 新格式\n\n' + fence('{"type":"debian","format":"deb822"}') + '\n[official]: https://www.debian.org/\n' const blocks = helpBlocks(source) -assert.deepEqual(blocks.filter(block => block.kind === 'debian'), [{ kind: 'debian', format: 'list' }, { kind: 'debian', format: 'deb822' }]) +assert.deepEqual(blocks.filter(block => block.kind === 'config'), [{ kind: 'config', config: { type: 'debian', format: 'list' } }, { kind: 'config', config: { type: 'debian', format: 'deb822' } }]) assert.ok(blocks.filter(block => block.kind === 'markdown').map(block => marked.parser(block.tokens)).join('').includes('href="https://www.debian.org/"'), 'Reference-style Markdown links must survive widget insertion') for (const data of ['not JSON', '{"type":"shell","format":"list"}', '{"type":"debian","format":"other"}', '{"type":"debian","format":"list","script":"alert(1)"}', 'null']) { assert.ok(helpBlocks(fence(data)).every(block => block.kind === 'markdown'), 'Invalid or unsupported blocks must remain ordinary Markdown code') diff --git a/scripts/check-mirror-config.ts b/scripts/check-mirror-config.ts new file mode 100644 index 0000000..32a3264 --- /dev/null +++ b/scripts/check-mirror-config.ts @@ -0,0 +1,124 @@ +import assert from 'node:assert/strict' +import { marked } from 'marked' +import { mirrorOrigin } from '../src/apt-sources.ts' +import { ubuntuReleases, ubuntuSources } from '../src/ubuntu-sources.ts' +import { configDefaults, configFields, generateMirrorConfig, mirrorConfigTypes, mirrorGenerators, parseMirrorConfig, type ConfigValues, type MirrorConfig } from '../src/mirror-config.ts' +import { helpBlocks } from '../src/help-content.ts' +import { defaultGeneratorCatalog, parseGeneratorCatalog, validGeneratorVersions, type GeneratorCatalog } from '../src/generator-catalog.ts' + +const origin = 'https://mirrors.example.edu' +let ubuntuCases = 0 +for (const release of ubuntuReleases) for (const format of ['list', 'deb822'] as const) for (const sources of [true, false]) for (const proposed of [true, false]) for (const securityMirror of [true, false]) { + const text = ubuntuSources({ origin, release: release.id, format, sources, proposed, securityMirror }) + assert.equal(text.includes(`${release.id}-proposed`), proposed) + assert.ok(text.includes(`${release.id}-security`)) + assert.ok(text.includes(`${release.id}-updates`)) + assert.ok(text.includes(`${release.id}-backports`)) + assert.equal(text.includes('http://security.ubuntu.com/ubuntu/'), !securityMirror) + assert.ok(text.includes('main restricted universe multiverse')) + assert.ok(!/trusted=yes|allow-insecure|undefined|NaN|ubuntu-ports/.test(text)) + if (format === 'deb822') { + assert.equal(text.includes('Types: deb deb-src'), sources) + assert.ok(text.includes('Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg')) + } else assert.equal(text.split('\n').some(line => line.startsWith('deb-src ')), sources) + ubuntuCases++ +} + +const fixtures: { name: string; file: string; format: string; body: string }[] = [] +let profileCases = 0 +for (const type of mirrorConfigTypes) { + const generator = mirrorGenerators[type] + for (const selected of generator.formats) { + const config: MirrorConfig = { type, format: selected.value } + const initial = configDefaults(config, origin) + const states: ConfigValues[] = [initial] + for (const field of configFields(type)) { + for (const value of field.kind === 'switch' ? [true, false] : field.choices!.map(choice => choice.value)) states.push({ ...initial, [field.key]: value }) + } + for (const values of states) { + const files = generateMirrorConfig(config, origin, values) + assert.ok(files.length > 0) + for (const file of files) { + assert.ok(file.path || file.titleKey) + assert.ok(file.code.trim()) + assert.ok(!/undefined|NaN|trusted=yes|SigLevel\s*=\s*Never|gpgcheck=0|curl\s+-k/.test(file.code)) + assert.ok(file.code.includes('mirrors.example.edu') || file.titleKey === 'mirrors.generator.signingKey') + if (file.path?.endsWith('.sources') || file.path?.endsWith('.list') && file.code.startsWith('deb ')) fixtures.push({ name: type, file: file.path, format: file.path.endsWith('.sources') ? 'sources' : 'list', body: file.code }) + } + profileCases++ + } + } +} + +assert.ok(generateMirrorConfig({ type: 'ubuntu' }, origin).at(-1)!.code.includes('noble-updates')) +assert.ok(!generateMirrorConfig({ type: 'ubuntu' }, origin).at(-1)!.code.includes('noble-proposed')) +assert.ok(generateMirrorConfig({ type: 'archlinux' }, origin)[0]!.code.includes('/$repo/os/$arch')) +assert.ok(generateMirrorConfig({ type: 'archlinuxarm' }, origin)[0]!.code.includes('/$arch/$repo')) +assert.ok(generateMirrorConfig({ type: 'archlinuxcn' }, origin)[0]!.code.includes('[archlinuxcn]')) +assert.ok(generateMirrorConfig({ type: 'openwrt' }, origin)[0]!.code.includes('https\\?://downloads.openwrt.org')) +assert.ok(generateMirrorConfig({ type: 'kubernetes', options: { release: 'v1.36' } }, origin)[0]!.code.includes('v1.36/deb/Release.key')) +const tailscale = generateMirrorConfig({ type: 'tailscale', options: { release: 'ubuntu/noble' } }, origin) +assert.ok(tailscale[0]!.code.includes('/stable/ubuntu/noble.noarmor.gpg')) +assert.ok(tailscale[1]!.code.includes('/mirrors/tailscale/ubuntu noble main')) +assert.ok(!tailscale[1]!.code.includes('/mirrors/tailscale/stable/')) +assert.ok(generateMirrorConfig({ type: 'proxmox', format: 'deb822', options: { release: 'bookworm' } }, origin)[0]!.code.includes('Signed-By: /etc/apt/trusted.gpg.d/proxmox-release-bookworm.gpg')) +assert.ok(generateMirrorConfig({ type: 'proxmox', format: 'deb822', options: { release: 'trixie' } }, origin)[0]!.code.includes('Signed-By: /usr/share/keyrings/proxmox-archive-keyring.gpg')) +assert.throws(() => generateMirrorConfig({ type: 'ubuntu', options: { release: 'sid' } }, origin)) +assert.throws(() => generateMirrorConfig({ type: 'anaconda' }, "https://bad'host.example")) +assert.throws(() => mirrorOrigin('javascript:alert(1)')) +assert.throws(() => mirrorOrigin('https://user:password@example.edu')) +assert.ok(generateMirrorConfig({ type: 'ubuntu', options: { protocol: 'http' } }, origin)[0]!.code.includes('http://mirrors.example.edu/mirrors/ubuntu/')) + +const fence = (body: string) => '```mirror-config\n' + body + '\n```\n' +for (const type of mirrorConfigTypes) { + assert.deepEqual(helpBlocks(fence(JSON.stringify({ type }))), [{ kind: 'config', config: { type } }]) +} +for (const value of [null, [], {}, { type: 'constructor' }, { type: '__proto__' }, { type: 'ubuntu', format: 'shell' }, { type: 'ubuntu', options: { release: 'sid' } }, { type: 'ubuntu', options: { sources: 'true' } }, { type: 'ubuntu', options: { __invalid: true } }, { type: 'ubuntu', options: [] }, { type: 'ubuntu', script: 'alert(1)' }, { type: 'ubuntu', options: { protocol: 'javascript' } }]) { + assert.equal(parseMirrorConfig(value), null) + assert.ok(helpBlocks(fence(JSON.stringify(value))).every(block => block.kind === 'markdown')) +} +assert.ok(helpBlocks(fence('x'.repeat(2049))).every(block => block.kind === 'markdown')) +assert.ok(helpBlocks('````markdown\n' + fence('{"type":"ubuntu"}') + '````').every(block => block.kind === 'markdown')) +const blocks = helpBlocks('[Ubuntu][official]\n\n' + fence('{"type":"ubuntu"}') + '\n[official]: https://ubuntu.com/\n') +assert.ok(blocks.filter(block => block.kind === 'markdown').map(block => marked.parser(block.tokens)).join('').includes('href="https://ubuntu.com/"')) + +// Use deliberately fictional release names to prove data-only additions work +// without asserting the codenames of unreleased distributions. +const future: GeneratorCatalog = structuredClone(defaultGeneratorCatalog) +future.ubuntu.releases.unshift({ id: 'future-test', label: 'Ubuntu future (test fixture)' }) +future.ubuntu.defaultRelease = 'future-test' +future.debian.releases.unshift({ id: 'future-stable', label: 'Debian future (test fixture)', stable: true, firmware: true, backports: true }) +future.debian.releases.push({ id: 'future-rolling', label: 'Rolling (test fixture)', stable: false, firmware: true, backports: false }) +future.debian.defaultRelease = 'future-stable' +future.kubernetes.releases.unshift({ id: 'v2.99', label: 'Kubernetes future (test fixture)' }) +future.kubernetes.defaultRelease = 'v2.99' +future.tailscale.releases.unshift({ id: 'ubuntu/future-test', label: 'Ubuntu future (test fixture)' }) +future.tailscale.defaultRelease = 'ubuntu/future-test' +future.proxmox.releases.unshift({ id: 'future-test', label: 'PVE future (test fixture)', keyring: '/usr/share/keyrings/proxmox-future-test.gpg' }) +future.proxmox.defaultRelease = 'future-test' +assert.ok(parseGeneratorCatalog(future)) +const futureUbuntu = generateMirrorConfig({ type: 'ubuntu' }, origin, {}, future)[0]!.code +assert.ok(futureUbuntu.includes('Suites: future-test future-test-updates future-test-backports')) +assert.ok(futureUbuntu.includes('Suites: future-test-security')) +assert.ok(generateMirrorConfig({ type: 'debian' }, origin, {}, future)[0]!.code.includes('future-stable-backports')) +assert.ok(configFields('debian-security', future)[0]!.choices!.some(choice => choice.value === 'future-stable')) +assert.ok(!configFields('debian-security', future)[0]!.choices!.some(choice => choice.value === 'future-rolling')) +assert.ok(!generateMirrorConfig({ type: 'debian', options: { release: 'future-rolling', securityMirror: true } }, origin, {}, future)[0]!.code.includes('future-rolling-security')) +assert.ok(generateMirrorConfig({ type: 'kubernetes' }, origin, {}, future)[0]!.code.includes('/v2.99/deb/Release.key')) +assert.ok(generateMirrorConfig({ type: 'tailscale' }, origin, {}, future)[0]!.code.includes('/ubuntu/future-test.noarmor.gpg')) +assert.ok(generateMirrorConfig({ type: 'proxmox' }, origin, {}, future)[0]!.code.includes('Signed-By: /usr/share/keyrings/proxmox-future-test.gpg')) +assert.equal(helpBlocks(fence('{"type":"ubuntu","options":{"release":"future-test"}}'), future)[0]!.kind, 'config') +assert.equal(helpBlocks(fence('{"type":"ubuntu","options":{"release":"future-test"}}'))[0]!.kind, 'markdown') +for (const bad of ['foo\ntrusted=yes', 'foo;id', 'foo bar', '$(id)', '../../etc/passwd', '__proto__']) { + const invalid = structuredClone(future) + invalid.ubuntu.releases[0]!.id = bad + invalid.ubuntu.defaultRelease = bad + assert.equal(parseGeneratorCatalog(invalid), null) +} +assert.equal(validGeneratorVersions('proxmox', { defaultRelease: 'foo', releases: [{ id: 'foo', label: 'PVE', keyring: '/tmp/evil.gpg' }] }), false) +assert.equal(validGeneratorVersions('ubuntu', { defaultRelease: 'foo', releases: [{ id: 'foo', label: 'A' }, { id: 'foo', label: 'B' }] }), false) +assert.equal(validGeneratorVersions('debian', { defaultRelease: 'foo', releases: [{ id: 'foo', label: 'Rolling', stable: false, firmware: true, backports: false }] }), false) +assert.equal(parseGeneratorCatalog({ ...future, script: 'id' }), null) + +if (process.argv.includes('--fixtures')) console.log(JSON.stringify(fixtures)) +else console.log(`Generator checks passed: ${mirrorConfigTypes.length} profiles, ${profileCases} profile states, ${ubuntuCases} Ubuntu combinations, data-only future releases for 5 families, safe Markdown parsing, version-coupled keys and repository paths.`) diff --git a/src/App.vue b/src/App.vue index eafe9b2..32e2a9d 100644 --- a/src/App.vue +++ b/src/App.vue @@ -17,12 +17,14 @@ import { content } from './api' import { announcementUrl } from './announcements' import { helpRoute, helpUrl } from './help' import { usePublicData } from './usePublicData' +import { provideGeneratorCatalog } from './useGeneratorCatalog' import { useMirrorFormats } from './useMirrorFormats' import { type Mirror } from './data' import { datetimeFormats, readPreference, savePreference, type Locale } from './locales' const { t, locale } = useI18n() const isAdminPage = window.location.pathname.startsWith('/admin') +provideGeneratorCatalog(isAdminPage || window.location.pathname.startsWith('/help')) const { site, mirrors, announcements: articleRecords, source, loading } = usePublicData() const announcements = computed(() => articleRecords.value.map(row => ({ id: row.id, ...row.data }))) const distributions = computed(() => site.value?.downloads || []) diff --git a/src/apt-sources.ts b/src/apt-sources.ts new file mode 100644 index 0000000..095ea9b --- /dev/null +++ b/src/apt-sources.ts @@ -0,0 +1,30 @@ +export type AptFormat = 'list' | 'deb822' +export interface AptRepository { uri: string; suites: string[]; components: string; keyring: string } + +export function mirrorOrigin(value: string, protocol?: string) { + const origin = new URL(value) + if (!['http:', 'https:'].includes(origin.protocol) || origin.username || origin.password) throw new Error('Invalid mirror origin') + if (!/^[a-z0-9._:\[\]-]+$/i.test(origin.host)) throw new Error('Invalid mirror host') + if (protocol !== undefined) { + if (protocol !== 'http' && protocol !== 'https') throw new Error('Invalid mirror protocol') + origin.protocol = protocol + ':' + } + return origin.origin +} + +export function aptSources(groups: AptRepository[], format: AptFormat, sources: boolean) { + if (format !== 'list' && format !== 'deb822') throw new Error('Invalid APT format') + if (format === 'deb822') { + return groups.map(group => [ + `Types: ${sources ? 'deb deb-src' : 'deb'}`, + `URIs: ${group.uri}`, + `Suites: ${group.suites.join(' ')}`, + `Components: ${group.components}`, + `Signed-By: ${group.keyring}`, + ].join('\n')).join('\n\n') + '\n' + } + return groups.flatMap(group => group.suites.map(suite => [ + `deb ${group.uri} ${suite} ${group.components}`, + `${sources ? '' : '# '}deb-src ${group.uri} ${suite} ${group.components}`, + ].join('\n'))).join('\n\n') + '\n' +} diff --git a/src/components/AdminPage.vue b/src/components/AdminPage.vue index d6ee1c8..e30cf44 100644 --- a/src/components/AdminPage.vue +++ b/src/components/AdminPage.vue @@ -8,6 +8,10 @@ import { helpUrl } from '../help' import MarkdownContent from './MarkdownContent.vue' import DownloadCatalogEditor from './DownloadCatalogEditor.vue' import MirrorHelpContent from './MirrorHelpContent.vue' +import { mirrorConfigTypes, mirrorGenerators, type MirrorConfigType } from '../mirror-config' +import GeneratorCatalogEditor from './GeneratorCatalogEditor.vue' +import type { GeneratorVersions } from '../generator-catalog' +import { useGeneratorCatalog } from '../useGeneratorCatalog' const { t, te, d, n } = useI18n({ useScope: 'global' }) const session = ref(null) @@ -16,13 +20,15 @@ const busy = ref(false) const error = ref('') const notice = ref('') const tab = ref('overview') -const tabs = ['overview', 'mirrors', 'announcements', 'downloads', 'config', 'settings', 'users', 'audit'] +const tabs = ['overview', 'mirrors', 'announcements', 'downloads', 'generators', 'config', 'settings', 'users', 'audit'] const allowedTabs = computed(() => session.value?.role === 'admin' ? tabs : ['announcements', 'users']) const credentials = reactive({ username: '', password: '' }) const overview = ref(null) const articles = ref[]>([]) const metadata = ref[]>([]) const settings = ref | null>(null) +const generatorRecords = ref[]>([]) +const { refresh: refreshGenerators } = useGeneratorCatalog() const downloadEditor = ref>() const users = ref([]) const audit = ref({ items: [], page: 1, pageSize: 10, total: 0, totalPages: 1 }) @@ -35,6 +41,8 @@ const articlePublished = computed(() => articlesEditor.value?.data.published ?? const sortedArticles = computed(() => [...articles.value].sort((a, b) => Number(b.data.pinned) - Number(a.data.pinned) || Date.parse(b.data.date) - Date.parse(a.data.date))) const articleDate = computed(() => articlesEditor.value?.data.date ? new Intl.DateTimeFormat('en-CA', { year: 'numeric', month: '2-digit', day: '2-digit', timeZone: 'Asia/Shanghai' }).format(new Date(articlesEditor.value.data.date)) : '') const mirrorEditor = ref | null>(null) +const mirrorHelpInput = ref() +const helpGenerator = ref('ubuntu') const publishedMirrorHelp = computed(() => metadata.value.some(row => row.id === mirrorEditor.value?.id && row.data.visible && row.data.help?.trim())) const editLocale = ref('zh-CN') const logs = ref<{ name: string; text: string } | null>(null) @@ -85,6 +93,7 @@ async function loadTab(value: string) { if (value === 'overview' || value === 'mirrors') await loadOverview() if (value === 'mirrors') metadata.value = await api('/admin/records/mirrors') if (value === 'announcements') articles.value = await api('/admin/records/announcements') + if (value === 'generators') generatorRecords.value = await api('/admin/records/generators') if (value === 'settings' || value === 'downloads') settings.value = (await api[]>('/admin/records/settings'))[0] || null if (value === 'config') { config.value = await api('/admin/config'); versions.value = await api('/admin/config/versions'); draft.value = null; selectedConfig.value = ''; mirrorConfig.value = null } if (value === 'users' && session.value?.role === 'admin') users.value = await api('/admin/users') @@ -101,6 +110,11 @@ async function saveRecord(kind: string, value: ContentRecord) { const result = await api>(`/admin/records/${kind}/${encodeURIComponent(value.id)}`, { method: 'PUT', body: jsonBody({ data: value.data, revision: value.revision }) }) notice.value = t('admin.saved'); return result } +async function saveGenerator(record: ContentRecord) { + const saved = await saveRecord('generators', record) + generatorRecords.value = generatorRecords.value.map(row => row.id === saved.id ? saved : row) + await refreshGenerators() +} async function ask(message: string, action: () => Promise) { confirmation.value = { message, action } await nextTick() @@ -142,6 +156,19 @@ function deleteArticle(record: ContentRecord) { function editMirror(job: SyncJob) { mirrorEditor.value = clone(metadata.value.find(row => row.id === job.name) || { id: job.name, revision: 0, updatedAt: 0, data: { name: job.name, description: { 'zh-CN': '', 'en-US': '' }, help: '', category: 'os', visible: true, website: '' } }) mirrorEditor.value.data.description ||= {}; mirrorEditor.value.data.help ||= '' + helpGenerator.value = mirrorConfigTypes.includes(job.name as MirrorConfigType) ? job.name as MirrorConfigType : 'ubuntu' +} +async function insertHelpGenerator() { + if (!mirrorEditor.value) return + const input = mirrorHelpInput.value + const body = mirrorEditor.value.data.help + const start = input?.selectionStart ?? body.length + const end = input?.selectionEnd ?? start + const block = '\n\n```mirror-config\n' + JSON.stringify({ type: helpGenerator.value }) + '\n```\n\n' + mirrorEditor.value.data.help = body.slice(0, start) + block + body.slice(end) + await nextTick() + input?.focus() + input?.setSelectionRange(start + block.length, start + block.length) } async function saveMirror() { if (!mirrorEditor.value) return; mirrorEditor.value = await saveRecord('mirrors', mirrorEditor.value); metadata.value = await api('/admin/records/mirrors') } async function saveDownloads() { @@ -213,7 +240,8 @@ onBeforeUnmount(() => clearInterval(poll))

{{ mirrorEditor.id }} · {{ t('admin.metadata') }}