feat: add backend, admin console and live monitoring
Integrate tunasync management, SQLite authentication and content settings, real monitoring and per-repository pull speeds. Add Chinese-only announcements and mirror help, editable related links, admin layout updates, deployment tools and tests.
This commit is contained in:
@@ -8,3 +8,9 @@ dist/
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
|
||||
backend/data/
|
||||
__pycache__/
|
||||
*.db
|
||||
*.db-wal
|
||||
*.db-shm
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
中国海洋大学软件镜像源(MirrorOUC)网页界面
|
||||
|
||||
本项目计划提供镜像检索、同步状态、使用帮助、软件下载及站点公告,并配套管理后台,用于维护站点内容、软件源配置和同步任务
|
||||
本项目提供镜像检索、同步状态、使用帮助、软件下载及站点公告,并配套管理后台,用于维护站点内容、软件源配置和同步任务
|
||||
|
||||
同步引擎采用 [TUNA tunasync](https://github.com/tuna/tunasync)
|
||||
|
||||
@@ -19,7 +19,7 @@ npm run dev
|
||||
|
||||
```sh
|
||||
npm run check:i18n # 检查中英文语言键与插值参数
|
||||
npm run check:monitoring # 检查监控演示数据、时间窗口和资源总量
|
||||
npm run check:monitoring # 检查监控图表的数据夹具与资源总量约束
|
||||
npm run build # TypeScript 检查与生产构建
|
||||
npm run preview # 预览 dist 中的构建结果
|
||||
```
|
||||
@@ -32,20 +32,48 @@ Vue 3 + TypeScript + Vite,使用 vue-i18n 管理界面文案。当前首页支
|
||||
|
||||
桌面端使用三列镜像列表与侧栏;手机端使用折叠导航。表格的后两列每 5 秒在「最近同步 + Size」与「下次同步 + Status」之间统一切换。表头每轮向上滚动一次,条目内容随机滚动 3~6 次后落定;使用系统的减少动态效果设置时直接显示最终值。页面隐藏时暂停轮换并取消未完成的动画。项目说明在悬浮提示及详情中查看。页脚使用宽幅分栏结构,在手机端纵向排列。新增页面也需兼顾手机端,并验证 320、390、768 和 1440 像素宽度下的中英文与明暗主题。
|
||||
|
||||
当前为界面原型,15 个镜像、仓库容量及同步任务均为演示数据,尚未连接镜像服务器。示例配置使用 `example.invalid` 保留域名;下载入口前往项目官网。Markdown 公告发布、管理后台、真实目录与 tunasync 接入属于后续开发范围,详细方案见 [plan.md](plan.md)。
|
||||
前台已接入真实后端,不会在连接失败时切换为演示数据。首页每 15 秒获取镜像状态,连接中断时保留上一次数据并显示提示。镜像名称直接打开 `/mirrors/<name>/`,使用帮助和公告通过 Markdown 安全渲染。公告只展示已发布内容;公告标题/正文和镜像使用说明均维护一份中文内容,切换界面语言时显示同一版。界面文案、镜像介绍及站点设置继续支持多语言。
|
||||
|
||||
`/status` 提供服务器监控与同步明细:IPv4 / IPv6 流量、两组磁盘容量、IOPS、吞吐量、CPU 和内存图表;可切换 1 / 6 / 24 小时范围,默认每 5 秒刷新图表,页面隐藏时暂停刷新。支持暂停图表刷新、悬停或方向键查看时间点、点击图例隐藏曲线,以及按名称或上游地址搜索同步任务。桌面端图表双列排列,手机端单列排列,同步表改为纵向条目。监控数据在 `src/monitoring.ts` 中生成,示例上游元数据在 `src/data.ts` 中,均未连接真实服务器。
|
||||
`/status` 展示服务器实测 IPv4 / IPv6 HTTPS、rsync 流量,NFS 磁盘容量、IOPS、吞吐量以及 CPU / 内存;支持 1 / 6 / 24 小时范围,图表每 10 秒刷新,页面隐藏时暂停。同步明细另提供按仓库汇总的上游 TCP 拉取速度,约每 2 秒采样与刷新,并适配手机端。统计值从完整采样计算,曲线最多取 241 个点。桌面端图表双列,手机端单列,同步明细在窄屏改为纵向布局。未采集到的指标不会冒充为零。
|
||||
|
||||
`/admin` 提供登录、Markdown 公告草稿/发布、镜像展示信息与使用帮助、任务命令和日志、全局/单个镜像调度配置、站点标识/头像/首页相关链接/页脚友链/下载项、管理员和公告编辑账户,以及操作记录。同步间隔遵循 tunasync 的原生语义:从上次同步结束后开始计算,单位为分钟。任务命令只表示已提交,最终状态以 Worker 为准。
|
||||
|
||||
## 后端与部署
|
||||
|
||||
后端使用 Go 标准库 HTTP、SQLite 和 TOML 解析器,不依赖数据库外部服务。详细部署、权限、备份和回退操作见 [deploy/README.md](deploy/README.md)。
|
||||
|
||||
```sh
|
||||
# 需要 Go 1.27 或更高版本
|
||||
cd backend
|
||||
go mod download
|
||||
go test ./...
|
||||
go vet ./...
|
||||
go build -o mirrorouc .
|
||||
# 首次运行生成初始管理员密码,妥善保存终端输出
|
||||
./mirrorouc -data ./data -init-admin admin
|
||||
# 对接自己的 Manager 和 Worker;前端开发服务器已代理 /api 与 /uploads
|
||||
./mirrorouc -data ./data -manager http://127.0.0.1:12345 -worker YOUR_WORKER_ID
|
||||
```
|
||||
|
||||
生产构建通过 `-static ../dist` 或 `MIRROROUC_STATIC` 指定静态目录;`MIRROROUC_WORKER_CONFIG`、`MIRROROUC_LOGS`、`MIRROROUC_METRICS` 分别指定 Worker 配置、日志根目录和采样历史。缺少配置时,对应管理功能会显示不可用,不会启动假的同步引擎。管理页复用站点的明暗主题和语言选择。
|
||||
|
||||
当前服务器预览地址为 `http://100.69.2.2:8080`,管理入口 `/admin`,原有 80 端口下载服务保留。预览仅监听该 Tailnet 地址,正式域名与 HTTPS 尚未配置。
|
||||
|
||||
## 源码约定
|
||||
|
||||
- `src/App.vue`:首页与弹窗交互。
|
||||
- `src/style.css`:爱特官网同款明暗配色、布局、动画和移动端样式;色值来源与映射见 [DESIGN.md](DESIGN.md)。
|
||||
- `src/locales/zh-CN/`、`src/locales/en-US/`:界面文案及演示内容;新增文案通过语言键引用。
|
||||
- `src/locales/zh-CN/`、`src/locales/en-US/`:界面文案及数据夹具文案;新增文案通过语言键引用。
|
||||
- 各语言的 `mirrors.ts` 定义表头、状态、相对时间、容量与公告日期模板,`formats.ts` 定义日期和数字格式;组件通过 `t`、`d`、`n` 使用,不自行生成界面文案。
|
||||
- `src/data.ts`:演示镜像与公告元数据。
|
||||
- `src/data.ts`:镜像类型与旧版界面数据夹具;生产页面通过 `src/usePublicData.ts` 和 `src/api.ts` 获取接口数据。
|
||||
- `src/components/AdminPage.vue`、`src/locales/admin-messages.ts`:管理表单与中英文文案。
|
||||
- `backend/`:鉴权、内容管理、tunasync 适配、配置预览与版本恢复、监控接口及集成测试。
|
||||
- `deploy/`:systemd、Nginx、实测采集器与受限配置写入工具。
|
||||
- `src/components/StatusPage.vue`、`MonitoringChart.vue`:监控布局、图表与同步明细交互;`src/monitoring.ts` 定义监控数据结构和演示曲线,`src/useMirrorFormats.ts` 复用首页与状态页的时间、容量格式。
|
||||
- `public/assets/`:页面实际使用的素材。
|
||||
|
||||
根目录 `素材/` 是本地素材目录,已加入 `.gitignore`,不得提交;`node_modules/`、`dist/`、`.local/` 和环境变量文件同样不纳入 Git。
|
||||
|
||||
中国海洋大学软件镜像源(MirrorOUC)由[中国海洋大学爱特工作室](https://www.itouc.cn/)承建。
|
||||
|
||||
首页侧栏“相关链接”在后台「站点设置 → 首页相关链接」管理,支持多语言名称、站内/站外网址、使用帮助入口、增删与排序。它使用独立的 `relatedLinks` 配置;页脚友情链接继续使用 `links`。保存站点设置后,首页下次刷新或轮询即可生效。
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type testClient struct {
|
||||
t *testing.T
|
||||
app *App
|
||||
cookie *http.Cookie
|
||||
csrf string
|
||||
}
|
||||
|
||||
func fixture(t *testing.T) *testClient {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
cfgDir := filepath.Join(root, "config")
|
||||
os.MkdirAll(filepath.Join(cfgDir, "mirrors.d"), 0755)
|
||||
worker := filepath.Join(cfgDir, "worker.conf")
|
||||
inc := filepath.Join(cfgDir, "mirrors.d", "source.conf")
|
||||
global := "[global]\nname='test'\nconcurrent=2\ninterval=60\nretry=2\ntimeout=600\n[include]\ninclude_mirrors=" + strconvQuote(filepath.ToSlash(filepath.Join(cfgDir, "mirrors.d", "*.conf"))) + "\n"
|
||||
os.WriteFile(worker, []byte(global), 0644)
|
||||
os.WriteFile(inc, []byte("[[mirrors]]\nname='alpine'\nprovider='rsync'\ncommand='/trusted/parallel-rsync'\nupstream='rsync://example.invalid/alpine/'\ninterval=60\nrsync_options=['--no-owner']\n"), 0644)
|
||||
manager := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/workers/test/jobs":
|
||||
respond(w, 200, []Job{{Name: "alpine", Worker: "test", Status: "failed", LastUpdate: time.Now().Add(-time.Hour), LastStarted: time.Now().Add(-time.Minute), Upstream: "https://user:secret@example.invalid/repo?token=secret", Size: "1.5G"}})
|
||||
case "/workers":
|
||||
respond(w, 200, []Worker{{ID: "test"}})
|
||||
case "/cmd":
|
||||
respond(w, 200, map[string]bool{"ok": true})
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(manager.Close)
|
||||
a, err := newApp(Config{Data: filepath.Join(root, "data"), Static: root, Manager: manager.URL, Worker: "test", WorkerConfig: worker, Logs: filepath.Join(root, "logs")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { a.db.Close() })
|
||||
a.logger.SetOutput(io.Discard)
|
||||
hash, err := hashPassword("test-only-password-123")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = a.db.Exec("INSERT INTO users(username,password,role,created) VALUES('admin',?,'admin',0),('editor',?,'editor',0)", hash, hash)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a.refresh(context.Background())
|
||||
return &testClient{t: t, app: a}
|
||||
}
|
||||
func strconvQuote(s string) string { b, _ := json.Marshal(s); return string(b) }
|
||||
func (c *testClient) call(method, path string, body any, csrf bool) *httptest.ResponseRecorder {
|
||||
c.t.Helper()
|
||||
var raw []byte
|
||||
if body != nil {
|
||||
raw, _ = json.Marshal(body)
|
||||
}
|
||||
r := httptest.NewRequest(method, "http://localhost"+path, bytes.NewReader(raw))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
if c.cookie != nil {
|
||||
r.AddCookie(c.cookie)
|
||||
}
|
||||
if csrf {
|
||||
r.Header.Set("X-CSRF-Token", c.csrf)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
c.app.handler().ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
func (c *testClient) login(user string) {
|
||||
c.t.Helper()
|
||||
w := c.call("POST", "/api/v1/auth/login", map[string]string{"username": user, "password": "test-only-password-123"}, false)
|
||||
if w.Code != 200 {
|
||||
c.t.Fatalf("login %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
cookies := w.Result().Cookies()
|
||||
if len(cookies) != 1 || !cookies[0].HttpOnly || cookies[0].SameSite != http.SameSiteStrictMode {
|
||||
c.t.Fatal("unsafe session cookie")
|
||||
}
|
||||
c.cookie = cookies[0]
|
||||
var result map[string]string
|
||||
json.Unmarshal(w.Body.Bytes(), &result)
|
||||
c.csrf = result["csrf"]
|
||||
}
|
||||
func status(t *testing.T, w *httptest.ResponseRecorder, want int) {
|
||||
t.Helper()
|
||||
if w.Code != want {
|
||||
t.Fatalf("want %d got %d: %s", want, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
func TestAuthenticationAndRoles(t *testing.T) {
|
||||
c := fixture(t)
|
||||
status(t, c.call("GET", "/api/v1/admin/overview", nil, false), 401)
|
||||
c.login("editor")
|
||||
status(t, c.call("GET", "/api/v1/admin/config", nil, false), 403)
|
||||
ann := Announcement{Title: "测试", Body: "草稿", Date: time.Now().UTC().Format(time.RFC3339)}
|
||||
status(t, c.call("PUT", "/api/v1/admin/records/announcements/test", map[string]any{"data": ann, "revision": 0}, false), 403)
|
||||
status(t, c.call("PUT", "/api/v1/admin/records/announcements/test", map[string]any{"data": ann, "revision": 0}, true), 200)
|
||||
w := c.call("GET", "/api/v1/public", nil, false)
|
||||
status(t, w, 200)
|
||||
var public struct {
|
||||
Announcements []Record `json:"announcements"`
|
||||
Mirrors []PublicMirror `json:"mirrors"`
|
||||
}
|
||||
json.Unmarshal(w.Body.Bytes(), &public)
|
||||
if len(public.Announcements) != 0 || len(public.Mirrors) != 1 || strings.Contains(public.Mirrors[0].Upstream, "secret") || strings.Contains(public.Mirrors[0].Upstream, "user") {
|
||||
t.Fatal("draft or private upstream data exposed")
|
||||
}
|
||||
ann.Published = true
|
||||
status(t, c.call("PUT", "/api/v1/admin/records/announcements/test", map[string]any{"data": ann, "revision": 1}, true), 200)
|
||||
status(t, c.call("PUT", "/api/v1/admin/records/announcements/test", map[string]any{"data": ann, "revision": 1}, true), 409)
|
||||
status(t, c.call("PUT", "/api/v1/auth/password", map[string]string{"current": "test-only-password-123", "password": "new-test-only-password-456"}, true), 200)
|
||||
status(t, c.call("GET", "/api/v1/auth/session", nil, false), 401)
|
||||
}
|
||||
func TestCrossOriginAndManagerActions(t *testing.T) {
|
||||
c := fixture(t)
|
||||
c.login("admin")
|
||||
r := httptest.NewRequest("POST", "http://localhost/api/v1/admin/jobs/alpine/start", nil)
|
||||
r.AddCookie(c.cookie)
|
||||
r.Header.Set("Origin", "https://attacker.invalid")
|
||||
r.Header.Set("X-CSRF-Token", c.csrf)
|
||||
w := httptest.NewRecorder()
|
||||
c.app.handler().ServeHTTP(w, r)
|
||||
status(t, w, 403)
|
||||
status(t, c.call("POST", "/api/v1/admin/jobs/alpine/not-a-command", nil, true), 400)
|
||||
status(t, c.call("POST", "/api/v1/admin/jobs/missing/start", nil, true), 404)
|
||||
status(t, c.call("POST", "/api/v1/admin/jobs/alpine/start", nil, true), 202)
|
||||
}
|
||||
func TestConfigApplyRollbackAndConflict(t *testing.T) {
|
||||
c := fixture(t)
|
||||
c.login("admin")
|
||||
initial, err := c.app.loadConfig()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mirror := initial.mirrors[0]
|
||||
mirror.Interval = 120
|
||||
w := c.call("POST", "/api/v1/admin/config/preview", map[string]any{"baseHash": initial.hash, "mirror": mirror}, true)
|
||||
status(t, w, 201)
|
||||
var draft configDraft
|
||||
json.Unmarshal(w.Body.Bytes(), &draft)
|
||||
status(t, c.call("POST", "/api/v1/admin/config/apply", map[string]string{"id": draft.ID}, true), 202)
|
||||
updated, err := c.app.loadConfig()
|
||||
if err != nil || updated.mirrors[0].Interval != 120 {
|
||||
t.Fatal("config was not applied")
|
||||
}
|
||||
for _, f := range updated.files {
|
||||
if strings.Contains(f.raw, "[[mirrors]]") && (!strings.Contains(f.raw, "/trusted/parallel-rsync") || !strings.Contains(f.raw, "--no-owner")) {
|
||||
t.Fatal("custom configuration was lost")
|
||||
}
|
||||
}
|
||||
status(t, c.call("POST", "/api/v1/admin/config/rollback", map[string]string{"id": draft.ID}, true), 202)
|
||||
restored, err := c.app.loadConfig()
|
||||
if err != nil || restored.hash != initial.hash {
|
||||
t.Fatal("rollback did not restore exact bytes")
|
||||
}
|
||||
w = c.call("POST", "/api/v1/admin/config/preview", map[string]any{"baseHash": restored.hash, "mirror": mirror}, true)
|
||||
status(t, w, 201)
|
||||
json.Unmarshal(w.Body.Bytes(), &draft)
|
||||
f, err := os.OpenFile(c.app.cfg.WorkerConfig, os.O_APPEND|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.WriteString("\n# concurrent edit\n")
|
||||
f.Close()
|
||||
status(t, c.call("POST", "/api/v1/admin/config/apply", map[string]string{"id": draft.ID}, true), 409)
|
||||
}
|
||||
func TestConfigReloadFailureRestoresFiles(t *testing.T) {
|
||||
c := fixture(t)
|
||||
c.login("admin")
|
||||
initial, _ := c.app.loadConfig()
|
||||
mirror := initial.mirrors[0]
|
||||
mirror.Interval = 90
|
||||
w := c.call("POST", "/api/v1/admin/config/preview", map[string]any{"baseHash": initial.hash, "mirror": mirror}, true)
|
||||
status(t, w, 201)
|
||||
var draft configDraft
|
||||
json.Unmarshal(w.Body.Bytes(), &draft)
|
||||
broken := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.Error(w, "unavailable", 503) }))
|
||||
defer broken.Close()
|
||||
c.app.cfg.Manager = broken.URL
|
||||
status(t, c.call("POST", "/api/v1/admin/config/apply", map[string]string{"id": draft.ID}, true), 502)
|
||||
restored, err := c.app.loadConfig()
|
||||
if err != nil || restored.hash != initial.hash {
|
||||
t.Fatal("failed reload did not restore configuration files")
|
||||
}
|
||||
}
|
||||
func TestNullTimesSizesAndStaleCache(t *testing.T) {
|
||||
if stamp(time.Time{}) != nil || parseSize("unknown") != nil || parseSize("NaN") != nil {
|
||||
t.Fatal("missing values must remain missing")
|
||||
}
|
||||
if value := parseSize("1.5G"); value == nil || *value != 1.5*1024*1024*1024 {
|
||||
t.Fatal("size conversion")
|
||||
}
|
||||
c := fixture(t)
|
||||
c.app.cfg.Manager = "http://127.0.0.1:1"
|
||||
c.app.refresh(context.Background())
|
||||
jobs, updated, code := c.app.cachedJobs()
|
||||
if len(jobs) != 1 || updated == 0 || code == "" {
|
||||
t.Fatal("last valid jobs were lost after upstream failure")
|
||||
}
|
||||
}
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/pbkdf2"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func randomToken() string {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(b)
|
||||
}
|
||||
func tokenHash(s string) string { h := sha256.Sum256([]byte(s)); return hex.EncodeToString(h[:]) }
|
||||
func hashPassword(password string) (string, error) {
|
||||
if len(password) < 12 || len(password) > 256 {
|
||||
return "", errors.New("INVALID_PASSWORD")
|
||||
}
|
||||
salt := make([]byte, 24)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return "", err
|
||||
}
|
||||
key, err := pbkdf2.Key(sha256.New, password, salt, 600000, 32)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "pbkdf2$600000$" + base64.RawStdEncoding.EncodeToString(salt) + "$" + base64.RawStdEncoding.EncodeToString(key), nil
|
||||
}
|
||||
func verifyPassword(password, encoded string) bool {
|
||||
p := strings.Split(encoded, "$")
|
||||
if len(p) != 4 || p[0] != "pbkdf2" || len(password) > 256 {
|
||||
return false
|
||||
}
|
||||
iterations, err := strconv.Atoi(p[1])
|
||||
if err != nil || iterations < 100000 || iterations > 1000000 {
|
||||
return false
|
||||
}
|
||||
salt, err := base64.RawStdEncoding.DecodeString(p[2])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
want, err := base64.RawStdEncoding.DecodeString(p[3])
|
||||
if err != nil || len(want) != 32 {
|
||||
return false
|
||||
}
|
||||
got, err := pbkdf2.Key(sha256.New, password, salt, iterations, 32)
|
||||
return err == nil && subtle.ConstantTimeCompare(got, want) == 1
|
||||
}
|
||||
func (a *App) sameOrigin(r *http.Request) bool {
|
||||
if r.Header.Get("Sec-Fetch-Site") == "cross-site" {
|
||||
return false
|
||||
}
|
||||
if origin := r.Header.Get("Origin"); origin != "" {
|
||||
u, err := url.Parse(origin)
|
||||
return err == nil && (u.Scheme == "http" || u.Scheme == "https") && strings.EqualFold(u.Host, r.Host)
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (a *App) currentSession(r *http.Request) (Session, error) {
|
||||
var s Session
|
||||
c, err := r.Cookie("mirrorouc_session")
|
||||
if err != nil {
|
||||
return s, err
|
||||
}
|
||||
err = a.db.QueryRow("SELECT users.id,username,role,csrf FROM sessions JOIN users ON users.id=sessions.user_id WHERE token=? AND expires>?", tokenHash(c.Value), time.Now().Unix()).Scan(&s.UserID, &s.Username, &s.Role, &s.CSRF)
|
||||
return s, err
|
||||
}
|
||||
func (a *App) require(w http.ResponseWriter, r *http.Request, admin bool) (Session, bool) {
|
||||
s, err := a.currentSession(r)
|
||||
if err != nil {
|
||||
fail(w, 401, "UNAUTHORIZED")
|
||||
return s, false
|
||||
}
|
||||
if admin && s.Role != "admin" {
|
||||
fail(w, 403, "FORBIDDEN")
|
||||
return s, false
|
||||
}
|
||||
if r.Method != "GET" && r.Method != "HEAD" {
|
||||
if !a.sameOrigin(r) || subtle.ConstantTimeCompare([]byte(r.Header.Get("X-CSRF-Token")), []byte(s.CSRF)) != 1 {
|
||||
fail(w, 403, "CSRF_INVALID")
|
||||
return s, false
|
||||
}
|
||||
}
|
||||
return s, true
|
||||
}
|
||||
func (a *App) session(w http.ResponseWriter, r *http.Request) {
|
||||
s, err := a.currentSession(r)
|
||||
if err != nil {
|
||||
fail(w, 401, "UNAUTHORIZED")
|
||||
return
|
||||
}
|
||||
respond(w, 200, map[string]any{"username": s.Username, "role": s.Role, "csrf": s.CSRF})
|
||||
}
|
||||
func (a *App) login(w http.ResponseWriter, r *http.Request) {
|
||||
if !a.sameOrigin(r) {
|
||||
fail(w, 403, "CSRF_INVALID")
|
||||
return
|
||||
}
|
||||
address, _, _ := net.SplitHostPort(r.RemoteAddr)
|
||||
a.loginMu.Lock()
|
||||
attempt := a.attempts[address]
|
||||
if time.Since(attempt.at) > time.Minute {
|
||||
attempt = loginAttempt{at: time.Now()}
|
||||
}
|
||||
attempt.count++
|
||||
a.attempts[address] = attempt
|
||||
for key, value := range a.attempts {
|
||||
if time.Since(value.at) > 10*time.Minute {
|
||||
delete(a.attempts, key)
|
||||
}
|
||||
}
|
||||
a.loginMu.Unlock()
|
||||
if attempt.count > 10 {
|
||||
w.Header().Set("Retry-After", "60")
|
||||
fail(w, 429, "RATE_LIMITED")
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if !readJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
var s Session
|
||||
var encoded string
|
||||
err := a.db.QueryRow("SELECT id,username,role,password FROM users WHERE username=?", body.Username).Scan(&s.UserID, &s.Username, &s.Role, &encoded)
|
||||
if err != nil {
|
||||
encoded = a.dummyPassword
|
||||
}
|
||||
valid := verifyPassword(body.Password, encoded)
|
||||
if err != nil || !valid {
|
||||
a.audit(Session{Username: body.Username}, "login", "", "denied")
|
||||
fail(w, 401, "INVALID_CREDENTIALS")
|
||||
return
|
||||
}
|
||||
token, csrf := randomToken(), randomToken()
|
||||
expires := time.Now().Add(12 * time.Hour)
|
||||
if _, err = a.db.Exec("INSERT INTO sessions(token,user_id,csrf,expires) VALUES(?,?,?,?)", tokenHash(token), s.UserID, csrf, expires.Unix()); err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
a.db.Exec("DELETE FROM sessions WHERE expires<?", time.Now().Unix())
|
||||
http.SetCookie(w, &http.Cookie{Name: "mirrorouc_session", Value: token, Path: "/", HttpOnly: true, Secure: a.cfg.SecureCookie, SameSite: http.SameSiteStrictMode, MaxAge: 43200, Expires: expires})
|
||||
a.audit(s, "login", "", "success")
|
||||
respond(w, 200, map[string]any{"username": s.Username, "role": s.Role, "csrf": csrf})
|
||||
}
|
||||
func (a *App) logout(w http.ResponseWriter, r *http.Request) {
|
||||
s, ok := a.require(w, r, false)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if c, err := r.Cookie("mirrorouc_session"); err == nil {
|
||||
a.db.Exec("DELETE FROM sessions WHERE token=?", tokenHash(c.Value))
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{Name: "mirrorouc_session", Value: "", Path: "/", HttpOnly: true, Secure: a.cfg.SecureCookie, SameSite: http.SameSiteStrictMode, MaxAge: -1})
|
||||
a.audit(s, "logout", "", "success")
|
||||
respond(w, 200, map[string]bool{"ok": true})
|
||||
}
|
||||
func (a *App) password(w http.ResponseWriter, r *http.Request) {
|
||||
s, ok := a.require(w, r, false)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Current string `json:"current"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if !readJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
var old string
|
||||
if err := a.db.QueryRow("SELECT password FROM users WHERE id=?", s.UserID).Scan(&old); err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
if !verifyPassword(body.Current, old) {
|
||||
fail(w, 400, "INVALID_CREDENTIALS")
|
||||
return
|
||||
}
|
||||
hash, err := hashPassword(body.Password)
|
||||
if err != nil {
|
||||
fail(w, 400, "INVALID_PASSWORD")
|
||||
return
|
||||
}
|
||||
tx, err := a.db.Begin()
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err = tx.Exec("UPDATE users SET password=? WHERE id=?", hash, s.UserID); err == nil {
|
||||
_, err = tx.Exec("DELETE FROM sessions WHERE user_id=?", s.UserID)
|
||||
}
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
a.audit(s, "password", s.Username, "success")
|
||||
respond(w, 200, map[string]bool{"ok": true})
|
||||
}
|
||||
@@ -0,0 +1,518 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type ConfigNumbers struct {
|
||||
Concurrent int `json:"concurrent"`
|
||||
Interval int `json:"interval"`
|
||||
Retry int `json:"retry"`
|
||||
Timeout int `json:"timeout"`
|
||||
}
|
||||
type ConfigMirror struct {
|
||||
Name string `json:"name"`
|
||||
Provider string `json:"provider"`
|
||||
Upstream string `json:"upstream"`
|
||||
Interval int `json:"interval"`
|
||||
Retry int `json:"retry"`
|
||||
Timeout int `json:"timeout"`
|
||||
Source string `json:"source,omitempty"`
|
||||
}
|
||||
type configFile struct {
|
||||
raw string
|
||||
doc map[string]any
|
||||
}
|
||||
type loadedConfig struct {
|
||||
files map[string]configFile
|
||||
mirrors []ConfigMirror
|
||||
global ConfigNumbers
|
||||
hash string
|
||||
includeDir string
|
||||
}
|
||||
type fileChange struct {
|
||||
Before *string `json:"before"`
|
||||
After *string `json:"after"`
|
||||
}
|
||||
type configDraft struct {
|
||||
ID string `json:"id"`
|
||||
Files map[string]fileChange `json:"files"`
|
||||
BaseHash string `json:"baseHash"`
|
||||
Status string `json:"status"`
|
||||
Created int64 `json:"created"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
func asMap(value any) map[string]any {
|
||||
if m, ok := value.(map[string]any); ok {
|
||||
return m
|
||||
}
|
||||
return map[string]any{}
|
||||
}
|
||||
func asTables(value any) []map[string]any {
|
||||
switch v := value.(type) {
|
||||
case []map[string]any:
|
||||
return v
|
||||
case []any:
|
||||
out := []map[string]any{}
|
||||
for _, entry := range v {
|
||||
out = append(out, asMap(entry))
|
||||
}
|
||||
return out
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func integer(value any, fallback int) int {
|
||||
switch n := value.(type) {
|
||||
case int64:
|
||||
return int(n)
|
||||
case int:
|
||||
return n
|
||||
case float64:
|
||||
return int(n)
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
func text(value any) string { s, _ := value.(string); return s }
|
||||
func (a *App) loadConfig() (loadedConfig, error) {
|
||||
c := loadedConfig{files: map[string]configFile{}, mirrors: []ConfigMirror{}}
|
||||
if a.cfg.WorkerConfig == "" {
|
||||
return c, errors.New("CONFIG_UNAVAILABLE")
|
||||
}
|
||||
read := func(path string) error {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var doc map[string]any
|
||||
if _, err = toml.Decode(string(raw), &doc); err != nil {
|
||||
return err
|
||||
}
|
||||
c.files[path] = configFile{string(raw), doc}
|
||||
return nil
|
||||
}
|
||||
if err := read(a.cfg.WorkerConfig); err != nil {
|
||||
return c, err
|
||||
}
|
||||
main := c.files[a.cfg.WorkerConfig].doc
|
||||
g := asMap(main["global"])
|
||||
c.global = ConfigNumbers{integer(g["concurrent"], 1), integer(g["interval"], 1440), integer(g["retry"], 0), integer(g["timeout"], 86400)}
|
||||
pattern := text(asMap(main["include"])["include_mirrors"])
|
||||
if pattern != "" {
|
||||
paths, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
for _, path := range paths {
|
||||
if err = read(path); err != nil {
|
||||
return c, err
|
||||
}
|
||||
}
|
||||
if filepath.Base(pattern) == "*.conf" {
|
||||
c.includeDir = filepath.Dir(pattern)
|
||||
}
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
var signature strings.Builder
|
||||
for _, path := range sortedKeys(c.files) {
|
||||
f := c.files[path]
|
||||
fmt.Fprintf(&signature, "%d:%s%d:%s", len(path), path, len(f.raw), f.raw)
|
||||
for _, m := range asTables(f.doc["mirrors"]) {
|
||||
name := text(m["name"])
|
||||
if !validName(name) || seen[name] {
|
||||
return c, errors.New("invalid or duplicate mirror name")
|
||||
}
|
||||
seen[name] = true
|
||||
c.mirrors = append(c.mirrors, ConfigMirror{name, text(m["provider"]), text(m["upstream"]), integer(m["interval"], c.global.Interval), integer(m["retry"], c.global.Retry), integer(m["timeout"], c.global.Timeout), path})
|
||||
}
|
||||
}
|
||||
c.hash = tokenHash(signature.String())
|
||||
return c, nil
|
||||
}
|
||||
func (a *App) configView(w http.ResponseWriter, r *http.Request) {
|
||||
if _, ok := a.require(w, r, true); !ok {
|
||||
return
|
||||
}
|
||||
a.configMu.Lock()
|
||||
defer a.configMu.Unlock()
|
||||
c, err := a.loadConfig()
|
||||
if err != nil {
|
||||
a.logger.Printf("read config: %v", err)
|
||||
fail(w, 503, "CONFIG_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
respond(w, 200, map[string]any{"worker": a.cfg.Worker, "baseHash": c.hash, "global": c.global, "mirrors": c.mirrors, "canAdd": c.includeDir != ""})
|
||||
}
|
||||
func validNumbers(interval, retry, timeout int) bool {
|
||||
return interval >= 1 && interval <= 525600 && retry >= 0 && retry <= 20 && timeout >= 30 && timeout <= 1209600
|
||||
}
|
||||
func validUpstream(value string) bool {
|
||||
u, err := url.Parse(value)
|
||||
return err == nil && (u.Scheme == "rsync" || u.Scheme == "https" || u.Scheme == "http") && u.Host != "" && u.User == nil && !strings.ContainsAny(value, "\r\n\t `;$|<>\\\"'") && len(value) < 2048
|
||||
}
|
||||
func (a *App) configPreview(w http.ResponseWriter, r *http.Request) {
|
||||
s, ok := a.require(w, r, true)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
BaseHash string `json:"baseHash"`
|
||||
Global *ConfigNumbers `json:"global"`
|
||||
Mirror *ConfigMirror `json:"mirror"`
|
||||
}
|
||||
if !readJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
a.configMu.Lock()
|
||||
defer a.configMu.Unlock()
|
||||
c, err := a.loadConfig()
|
||||
if err != nil {
|
||||
fail(w, 503, "CONFIG_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
if body.BaseHash != c.hash {
|
||||
fail(w, 409, "CONFLICT")
|
||||
return
|
||||
}
|
||||
changed := map[string]bool{}
|
||||
if body.Global != nil {
|
||||
g := body.Global
|
||||
if g.Concurrent < 1 || g.Concurrent > 64 || !validNumbers(g.Interval, g.Retry, g.Timeout) {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
if *g != c.global {
|
||||
f := c.files[a.cfg.WorkerConfig]
|
||||
m := asMap(f.doc["global"])
|
||||
m["concurrent"], m["interval"], m["retry"], m["timeout"] = g.Concurrent, g.Interval, g.Retry, g.Timeout
|
||||
f.doc["global"] = m
|
||||
changed[a.cfg.WorkerConfig] = true
|
||||
}
|
||||
}
|
||||
if body.Mirror != nil {
|
||||
m := body.Mirror
|
||||
if !validName(m.Name) || !validUpstream(m.Upstream) || !validNumbers(m.Interval, m.Retry, m.Timeout) {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
var current *ConfigMirror
|
||||
for _, entry := range c.mirrors {
|
||||
if entry.Name == m.Name {
|
||||
copy := entry
|
||||
current = ©
|
||||
break
|
||||
}
|
||||
}
|
||||
if current != nil {
|
||||
if current.Provider != m.Provider {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
if current.Upstream != m.Upstream || current.Interval != m.Interval || current.Retry != m.Retry || current.Timeout != m.Timeout {
|
||||
f := c.files[current.Source]
|
||||
for _, table := range asTables(f.doc["mirrors"]) {
|
||||
if text(table["name"]) == m.Name {
|
||||
table["upstream"], table["interval"], table["retry"], table["timeout"] = m.Upstream, m.Interval, m.Retry, m.Timeout
|
||||
}
|
||||
}
|
||||
changed[current.Source] = true
|
||||
}
|
||||
} else {
|
||||
if c.includeDir == "" || (m.Provider != "rsync" && m.Provider != "two-stage-rsync") {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
path := filepath.Join(c.includeDir, "mirrorouc-"+m.Name+".conf")
|
||||
if _, err = os.Lstat(path); !os.IsNotExist(err) {
|
||||
fail(w, 409, "CONFLICT")
|
||||
return
|
||||
}
|
||||
entry := map[string]any{"name": m.Name, "provider": m.Provider, "upstream": m.Upstream, "interval": m.Interval, "retry": m.Retry, "timeout": m.Timeout}
|
||||
if m.Provider == "two-stage-rsync" {
|
||||
entry["stage1_profile"] = "debian"
|
||||
}
|
||||
c.files[path] = configFile{"", map[string]any{"mirrors": []map[string]any{entry}}}
|
||||
changed[path] = true
|
||||
}
|
||||
}
|
||||
if len(changed) == 0 {
|
||||
fail(w, 400, "NO_CHANGES")
|
||||
return
|
||||
}
|
||||
files := map[string]fileChange{}
|
||||
for path := range changed {
|
||||
f := c.files[path]
|
||||
var b bytes.Buffer
|
||||
if err = toml.NewEncoder(&b).Encode(f.doc); err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
after := b.String()
|
||||
var before *string
|
||||
if _, err = os.Stat(path); err == nil {
|
||||
old := f.raw
|
||||
before = &old
|
||||
}
|
||||
files[path] = fileChange{before, &after}
|
||||
}
|
||||
id := tokenHash(randomToken())[:24]
|
||||
raw, _ := json.Marshal(files)
|
||||
now := time.Now().UnixMilli()
|
||||
_, err = a.db.Exec("INSERT INTO config_versions(id,files,base_hash,status,created,username) VALUES(?,?,?,'draft',?,?)", id, string(raw), c.hash, now, s.Username)
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
a.audit(s, "config_preview", id, "draft")
|
||||
respond(w, 201, configDraft{id, files, c.hash, "draft", now, s.Username})
|
||||
}
|
||||
func atomicFile(path string, content *string) error {
|
||||
if content == nil {
|
||||
return os.Remove(path)
|
||||
}
|
||||
mode := os.FileMode(0660)
|
||||
if info, err := os.Stat(path); err == nil {
|
||||
mode = info.Mode().Perm()
|
||||
}
|
||||
f, err := os.CreateTemp(filepath.Dir(path), ".mirrorouc-*.tmp")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tmp := f.Name()
|
||||
defer os.Remove(tmp)
|
||||
if err = f.Chmod(mode); err == nil {
|
||||
_, err = f.WriteString(*content)
|
||||
}
|
||||
if err == nil {
|
||||
err = f.Sync()
|
||||
}
|
||||
if e := f.Close(); err == nil {
|
||||
err = e
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp, path)
|
||||
}
|
||||
func (a *App) readVersion(id string) (configDraft, error) {
|
||||
var v configDraft
|
||||
var raw string
|
||||
v.ID = id
|
||||
err := a.db.QueryRow("SELECT files,base_hash,status,created,username FROM config_versions WHERE id=?", id).Scan(&raw, &v.BaseHash, &v.Status, &v.Created, &v.Username)
|
||||
if err != nil {
|
||||
return v, err
|
||||
}
|
||||
err = json.Unmarshal([]byte(raw), &v.Files)
|
||||
return v, err
|
||||
}
|
||||
func (a *App) applyVersion(r *http.Request, v configDraft, s Session) (string, error) {
|
||||
c, err := a.loadConfig()
|
||||
if err != nil {
|
||||
return "CONFIG_UNAVAILABLE", err
|
||||
}
|
||||
if c.hash != v.BaseHash {
|
||||
return "CONFLICT", errConflict
|
||||
}
|
||||
for path, change := range v.Files {
|
||||
raw, e := os.ReadFile(path)
|
||||
if change.Before == nil {
|
||||
if !os.IsNotExist(e) {
|
||||
return "CONFLICT", errConflict
|
||||
}
|
||||
} else if e != nil || string(raw) != *change.Before {
|
||||
return "CONFLICT", errConflict
|
||||
}
|
||||
}
|
||||
if a.cfg.ConfigSocket != "" {
|
||||
conn, err := net.DialTimeout("unix", a.cfg.ConfigSocket, 5*time.Second)
|
||||
if err != nil {
|
||||
return "CONFIG_UNAVAILABLE", err
|
||||
}
|
||||
defer conn.Close()
|
||||
conn.SetDeadline(time.Now().Add(28 * time.Second))
|
||||
if err = json.NewEncoder(conn).Encode(v); err != nil {
|
||||
return "CONFIG_STATE_UNCERTAIN", err
|
||||
}
|
||||
var result struct {
|
||||
Code string `json:"code"`
|
||||
}
|
||||
if err = json.NewDecoder(bufio.NewReader(conn)).Decode(&result); err != nil {
|
||||
return "CONFIG_STATE_UNCERTAIN", err
|
||||
}
|
||||
if result.Code != "" {
|
||||
a.db.Exec("UPDATE config_versions SET status='failed' WHERE id=?", v.ID)
|
||||
return result.Code, errors.New(result.Code)
|
||||
}
|
||||
if _, err = a.db.Exec("UPDATE config_versions SET status='applied' WHERE id=?", v.ID); err != nil {
|
||||
return "CONFIG_STATE_UNCERTAIN", err
|
||||
}
|
||||
a.audit(s, "config_apply", v.ID, "submitted")
|
||||
return "", nil
|
||||
}
|
||||
written := []string{}
|
||||
restore := func() error {
|
||||
var result error
|
||||
for i := len(written) - 1; i >= 0; i-- {
|
||||
path := written[i]
|
||||
if e := atomicFile(path, v.Files[path].Before); e != nil {
|
||||
result = e
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
for _, path := range sortedKeys(v.Files) {
|
||||
if err = atomicFile(path, v.Files[path].After); err != nil {
|
||||
if e := restore(); e != nil {
|
||||
return "CONFIG_ROLLBACK_FAILED", e
|
||||
}
|
||||
return "CONFIG_APPLY_FAILED", err
|
||||
}
|
||||
written = append(written, path)
|
||||
}
|
||||
if _, err = a.loadConfig(); err == nil {
|
||||
err = a.sendCommand(r.Context(), "", "reload")
|
||||
}
|
||||
if err != nil {
|
||||
restoreErr := restore()
|
||||
if restoreErr == nil {
|
||||
restoreErr = a.sendCommand(r.Context(), "", "reload")
|
||||
}
|
||||
a.db.Exec("UPDATE config_versions SET status='failed' WHERE id=?", v.ID)
|
||||
if restoreErr != nil {
|
||||
return "CONFIG_ROLLBACK_FAILED", restoreErr
|
||||
}
|
||||
return "CONFIG_APPLY_FAILED", err
|
||||
}
|
||||
if _, err = a.db.Exec("UPDATE config_versions SET status='applied' WHERE id=?", v.ID); err != nil {
|
||||
return "CONFIG_STATE_UNCERTAIN", err
|
||||
}
|
||||
a.audit(s, "config_apply", v.ID, "submitted")
|
||||
return "", nil
|
||||
}
|
||||
func (a *App) configApply(w http.ResponseWriter, r *http.Request) {
|
||||
s, ok := a.require(w, r, true)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if !readJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
a.configMu.Lock()
|
||||
defer a.configMu.Unlock()
|
||||
v, err := a.readVersion(body.ID)
|
||||
if err != nil {
|
||||
fail(w, 404, "NOT_FOUND")
|
||||
return
|
||||
}
|
||||
if v.Status != "draft" {
|
||||
fail(w, 409, "CONFLICT")
|
||||
return
|
||||
}
|
||||
code, err := a.applyVersion(r, v, s)
|
||||
if err != nil {
|
||||
a.logger.Printf("config apply: %v", err)
|
||||
a.audit(s, "config_apply", v.ID, code)
|
||||
status := 502
|
||||
if code == "CONFLICT" {
|
||||
status = 409
|
||||
}
|
||||
fail(w, status, code)
|
||||
return
|
||||
}
|
||||
respond(w, 202, map[string]string{"status": "submitted"})
|
||||
}
|
||||
func (a *App) configVersions(w http.ResponseWriter, r *http.Request) {
|
||||
if _, ok := a.require(w, r, true); !ok {
|
||||
return
|
||||
}
|
||||
rows, err := a.db.Query("SELECT id,base_hash,status,created,username FROM config_versions ORDER BY created DESC LIMIT 50")
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []configDraft{}
|
||||
for rows.Next() {
|
||||
var v configDraft
|
||||
if err = rows.Scan(&v.ID, &v.BaseHash, &v.Status, &v.Created, &v.Username); err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
out = append(out, v)
|
||||
}
|
||||
respond(w, 200, out)
|
||||
}
|
||||
func (a *App) configRollback(w http.ResponseWriter, r *http.Request) {
|
||||
s, ok := a.require(w, r, true)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if !readJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
a.configMu.Lock()
|
||||
defer a.configMu.Unlock()
|
||||
old, err := a.readVersion(body.ID)
|
||||
if err != nil {
|
||||
fail(w, 404, "NOT_FOUND")
|
||||
return
|
||||
}
|
||||
if old.Status != "applied" {
|
||||
fail(w, 409, "CONFLICT")
|
||||
return
|
||||
}
|
||||
c, err := a.loadConfig()
|
||||
if err != nil {
|
||||
fail(w, 503, "CONFIG_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
reverse := map[string]fileChange{}
|
||||
for path, change := range old.Files {
|
||||
raw, e := os.ReadFile(path)
|
||||
if change.After == nil {
|
||||
if !os.IsNotExist(e) {
|
||||
fail(w, 409, "CONFLICT")
|
||||
return
|
||||
}
|
||||
} else if e != nil || string(raw) != *change.After {
|
||||
fail(w, 409, "CONFLICT")
|
||||
return
|
||||
}
|
||||
reverse[path] = fileChange{change.After, change.Before}
|
||||
}
|
||||
id := tokenHash(randomToken())[:24]
|
||||
now := time.Now().UnixMilli()
|
||||
raw, _ := json.Marshal(reverse)
|
||||
v := configDraft{id, reverse, c.hash, "draft", now, s.Username}
|
||||
if _, err = a.db.Exec("INSERT INTO config_versions(id,files,base_hash,status,created,username) VALUES(?,?,?,'draft',?,?)", id, string(raw), c.hash, now, s.Username); err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
code, err := a.applyVersion(r, v, s)
|
||||
if err != nil {
|
||||
a.audit(s, "config_rollback", old.ID, code)
|
||||
fail(w, 502, code)
|
||||
return
|
||||
}
|
||||
a.audit(s, "config_rollback", old.ID, "submitted")
|
||||
respond(w, 202, map[string]string{"status": "submitted"})
|
||||
}
|
||||
@@ -0,0 +1,402 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/mail"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var identifier = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.+-]{0,127}$`)
|
||||
|
||||
func validName(value string) bool {
|
||||
return identifier.MatchString(value) && value != "." && value != ".."
|
||||
}
|
||||
func validText(value Localized, required bool, limit int) bool {
|
||||
has := false
|
||||
if len(value) > 20 {
|
||||
return false
|
||||
}
|
||||
for locale, text := range value {
|
||||
if len(locale) > 48 || !localeIdentifier.MatchString(locale) {
|
||||
return false
|
||||
}
|
||||
if len(text) > limit {
|
||||
return false
|
||||
}
|
||||
if strings.TrimSpace(text) != "" {
|
||||
has = true
|
||||
}
|
||||
}
|
||||
return !required || has
|
||||
}
|
||||
|
||||
var localeIdentifier = regexp.MustCompile(`^[a-zA-Z]{2,3}(?:-[a-zA-Z0-9]{2,8})*$`)
|
||||
|
||||
func webURL(value string, empty bool) bool {
|
||||
if empty && value == "" {
|
||||
return true
|
||||
}
|
||||
u, e := url.Parse(value)
|
||||
return e == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != "" && u.User == nil && len(value) < 2048
|
||||
}
|
||||
func navigationURL(value string) bool {
|
||||
if webURL(value, false) {
|
||||
return true
|
||||
}
|
||||
if !strings.HasPrefix(value, "/") || strings.HasPrefix(value, "//") || len(value) >= 2048 || strings.ContainsAny(value, "\\\r\n\t") {
|
||||
return false
|
||||
}
|
||||
u, err := url.Parse(value)
|
||||
return err == nil && u.Scheme == "" && u.Host == "" && u.User == nil
|
||||
}
|
||||
func imageURL(value string) bool {
|
||||
if strings.HasPrefix(value, "/assets/") || strings.HasPrefix(value, "/uploads/") {
|
||||
return !strings.Contains(value, "..") && !strings.ContainsAny(value, "\\?#")
|
||||
}
|
||||
u, e := url.Parse(value)
|
||||
return e == nil && u.Scheme == "https" && u.Host != "" && u.User == nil
|
||||
}
|
||||
func strict(raw []byte, value any) bool {
|
||||
d := json.NewDecoder(bytes.NewReader(raw))
|
||||
d.DisallowUnknownFields()
|
||||
return d.Decode(value) == nil
|
||||
}
|
||||
func (a *App) listRecords(w http.ResponseWriter, r *http.Request) {
|
||||
kind := r.PathValue("kind")
|
||||
if kind != "announcements" && kind != "mirrors" && kind != "settings" {
|
||||
fail(w, 404, "NOT_FOUND")
|
||||
return
|
||||
}
|
||||
if _, ok := a.require(w, r, kind != "announcements"); !ok {
|
||||
return
|
||||
}
|
||||
records, err := a.records(kind)
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, records)
|
||||
}
|
||||
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") {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
s, ok := a.require(w, r, kind != "announcements")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Data json.RawMessage `json:"data"`
|
||||
Revision int `json:"revision"`
|
||||
}
|
||||
if !readJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
var value any
|
||||
switch kind {
|
||||
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) == "") {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
if d.Date == "" {
|
||||
d.Date = time.Now().UTC().Format(time.RFC3339)
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339, d.Date); err != nil {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
value = d
|
||||
case "mirrors":
|
||||
var d MirrorMetadata
|
||||
if !strict(body.Data, &d) || d.Name != id || !validText(d.Description, false, 1000) || len(d.Help) > 250000 || !webURL(d.Website, true) || (d.Category != "os" && d.Category != "language" && d.Category != "other") {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
value = d
|
||||
case "settings":
|
||||
var d Settings
|
||||
if id != "site" || !strict(body.Data, &d) || !validText(d.Name, true, 200) || !validText(d.Description, false, 2000) || !validText(d.Address, false, 2000) || !imageURL(d.LogoLight) || !imageURL(d.LogoDark) || !imageURL(d.Favicon) || !imageURL(d.Avatar) || len(d.Links) > 50 || len(d.RelatedLinks) > 50 || len(d.Downloads) > 100 {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
json.Unmarshal(body.Data, &fields)
|
||||
if _, provided := fields["relatedLinks"]; !provided {
|
||||
// Preserve saved sidebar changes when an older open form submits without this field.
|
||||
current, err := a.record("settings", "site")
|
||||
var existing Settings
|
||||
if err != nil || decodeData(current.Data, &existing) != nil {
|
||||
fail(w, 409, "CONFLICT")
|
||||
return
|
||||
}
|
||||
d.RelatedLinks = existing.RelatedLinks
|
||||
}
|
||||
if d.RelatedLinks == nil {
|
||||
d.RelatedLinks = []RelatedLink{}
|
||||
}
|
||||
for _, link := range d.RelatedLinks {
|
||||
if !validText(link.Title, true, 200) || (link.Kind != "help" && link.Kind != "url") || (link.Kind == "help" && link.URL != "") || (link.Kind == "url" && !navigationURL(link.URL)) {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
}
|
||||
if d.Email != "" {
|
||||
if parsed, err := mail.ParseAddress(d.Email); err != nil || parsed.Address != d.Email {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
}
|
||||
for _, link := range d.Links {
|
||||
if !validText(link.Title, true, 200) || !webURL(link.URL, false) {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
}
|
||||
for _, download := range d.Downloads {
|
||||
if strings.TrimSpace(download.Name) == "" || len(download.Name) > 100 || len(download.Version) > 100 || !webURL(download.URL, false) {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
}
|
||||
value = d
|
||||
}
|
||||
result, err := a.saveRecord(kind, id, value, body.Revision)
|
||||
if errors.Is(err, errConflict) {
|
||||
fail(w, 409, "CONFLICT")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
a.audit(s, "record_save", kind+"/"+id, "success")
|
||||
respond(w, 200, result)
|
||||
}
|
||||
func (a *App) deleteRecord(w http.ResponseWriter, r *http.Request) {
|
||||
kind, id := r.PathValue("kind"), r.PathValue("id")
|
||||
if kind != "announcements" && kind != "mirrors" {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
s, ok := a.require(w, r, kind != "announcements")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Revision int `json:"revision"`
|
||||
}
|
||||
if !readJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
result, err := a.db.Exec("UPDATE records SET deleted=1,revision=revision+1,updated=? WHERE kind=? AND id=? AND revision=? AND deleted=0", time.Now().UnixMilli(), kind, id, body.Revision)
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
count, _ := result.RowsAffected()
|
||||
if count != 1 {
|
||||
fail(w, 409, "CONFLICT")
|
||||
return
|
||||
}
|
||||
a.audit(s, "record_delete", kind+"/"+id, "success")
|
||||
respond(w, 200, map[string]bool{"ok": true})
|
||||
}
|
||||
func (a *App) upload(w http.ResponseWriter, r *http.Request) {
|
||||
s, ok := a.require(w, r, true)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 5<<20)
|
||||
if err := r.ParseMultipartForm(4 << 20); err != nil {
|
||||
fail(w, 400, "INVALID_IMAGE")
|
||||
return
|
||||
}
|
||||
if r.MultipartForm != nil {
|
||||
defer r.MultipartForm.RemoveAll()
|
||||
}
|
||||
file, _, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
fail(w, 400, "INVALID_IMAGE")
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
content, err := io.ReadAll(io.LimitReader(file, 4<<20+1))
|
||||
if err != nil || len(content) > 4<<20 {
|
||||
fail(w, 400, "INVALID_IMAGE")
|
||||
return
|
||||
}
|
||||
conf, format, err := image.DecodeConfig(bytes.NewReader(content))
|
||||
if err != nil || (format != "png" && format != "jpeg") || conf.Width < 1 || conf.Height < 1 || conf.Width > 8192 || conf.Height > 8192 || int64(conf.Width)*int64(conf.Height) > 32000000 {
|
||||
fail(w, 400, "INVALID_IMAGE")
|
||||
return
|
||||
}
|
||||
ext := ".png"
|
||||
if format == "jpeg" {
|
||||
ext = ".jpg"
|
||||
}
|
||||
name := tokenHash(randomToken())[:24] + ext
|
||||
if err = os.WriteFile(filepath.Join(a.cfg.Data, "uploads", name), content, 0644); err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
a.audit(s, "upload", name, "success")
|
||||
respond(w, 201, map[string]string{"url": "/uploads/" + name})
|
||||
}
|
||||
func (a *App) public(w http.ResponseWriter, r *http.Request) {
|
||||
site, err := a.record("settings", "site")
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
metadata, err := a.records("mirrors")
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
metas := map[string]MirrorMetadata{}
|
||||
for _, record := range metadata {
|
||||
var m MirrorMetadata
|
||||
if decodeData(record.Data, &m) == nil {
|
||||
metas[record.ID] = m
|
||||
}
|
||||
}
|
||||
jobs, updated, upstreamError := a.cachedJobs()
|
||||
mirrors := []PublicMirror{}
|
||||
for _, job := range jobs {
|
||||
meta, exists := metas[job.Name]
|
||||
if exists && !meta.Visible {
|
||||
continue
|
||||
}
|
||||
if !exists {
|
||||
meta = MirrorMetadata{Name: job.Name, Category: "other", Visible: true, Description: Localized{}}
|
||||
}
|
||||
mirrors = append(mirrors, publicMirror(job, meta))
|
||||
}
|
||||
sort.Slice(mirrors, func(i, j int) bool { return mirrors[i].Name < mirrors[j].Name })
|
||||
announcements, err := a.records("announcements")
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
published := []Record{}
|
||||
for _, record := range announcements {
|
||||
var d Announcement
|
||||
if decodeData(record.Data, &d) == nil && d.Published {
|
||||
record.Data = d
|
||||
published = append(published, record)
|
||||
}
|
||||
}
|
||||
sort.SliceStable(published, func(i, j int) bool {
|
||||
a, b := published[i].Data.(Announcement), published[j].Data.(Announcement)
|
||||
if a.Pinned != b.Pinned {
|
||||
return a.Pinned
|
||||
}
|
||||
return a.Date > b.Date
|
||||
})
|
||||
if len(published) > 100 {
|
||||
published = published[:100]
|
||||
}
|
||||
respond(w, 200, map[string]any{"site": site.Data, "mirrors": mirrors, "announcements": published, "source": map[string]any{"updatedAt": updated, "stale": upstreamError != "" || updated == 0 || time.Now().UnixMilli()-updated > 60000, "code": upstreamError}})
|
||||
}
|
||||
func (a *App) overview(w http.ResponseWriter, r *http.Request) {
|
||||
if _, ok := a.require(w, r, false); !ok {
|
||||
return
|
||||
}
|
||||
jobs, updated, code := a.cachedJobs()
|
||||
workers := []Worker{}
|
||||
err := a.managerGet(r.Context(), "/workers", &workers)
|
||||
if err != nil && code == "" {
|
||||
code = "TUNASYNC_UNAVAILABLE"
|
||||
}
|
||||
respond(w, 200, map[string]any{"jobs": jobs, "workers": workers, "worker": a.cfg.Worker, "updatedAt": updated, "code": code, "configAvailable": a.cfg.WorkerConfig != "", "logsAvailable": a.cfg.Logs != ""})
|
||||
}
|
||||
func (a *App) auditList(w http.ResponseWriter, r *http.Request) {
|
||||
if _, ok := a.require(w, r, true); !ok {
|
||||
return
|
||||
}
|
||||
rows, err := a.db.Query("SELECT id,username,action,target,result,created FROM audit ORDER BY id DESC LIMIT 200")
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []map[string]any{}
|
||||
for rows.Next() {
|
||||
var id, created int64
|
||||
var username, action, target, result string
|
||||
if err = rows.Scan(&id, &username, &action, &target, &result, &created); err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
out = append(out, map[string]any{"id": id, "username": username, "action": action, "target": target, "result": result, "created": created})
|
||||
}
|
||||
respond(w, 200, out)
|
||||
}
|
||||
func (a *App) users(w http.ResponseWriter, r *http.Request) {
|
||||
if _, ok := a.require(w, r, true); !ok {
|
||||
return
|
||||
}
|
||||
rows, err := a.db.Query("SELECT id,username,role,created FROM users ORDER BY id")
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []map[string]any{}
|
||||
for rows.Next() {
|
||||
var id, created int64
|
||||
var username, role string
|
||||
if err = rows.Scan(&id, &username, &role, &created); err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
out = append(out, map[string]any{"id": id, "username": username, "role": role, "created": created})
|
||||
}
|
||||
respond(w, 200, out)
|
||||
}
|
||||
func (a *App) createUser(w http.ResponseWriter, r *http.Request) {
|
||||
s, ok := a.require(w, r, true)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
if !readJSON(w, r, &body) {
|
||||
return
|
||||
}
|
||||
if !validName(body.Username) || (body.Role != "admin" && body.Role != "editor") {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
hash, err := hashPassword(body.Password)
|
||||
if err != nil {
|
||||
fail(w, 400, "INVALID_PASSWORD")
|
||||
return
|
||||
}
|
||||
if _, err = a.db.Exec("INSERT INTO users(username,password,role,created) VALUES(?,?,?,?)", body.Username, hash, body.Role, time.Now().UnixMilli()); err != nil {
|
||||
fail(w, 409, "CONFLICT")
|
||||
return
|
||||
}
|
||||
a.audit(s, "user_create", body.Username, "success")
|
||||
respond(w, 201, map[string]bool{"ok": true})
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Content is a single shared Chinese edition; interface labels remain localized.
|
||||
// Keep every original row before converting legacy language maps, including deleted rows.
|
||||
func migrateSharedContent(db *sql.DB) error {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err = tx.Exec(`CREATE TABLE IF NOT EXISTS shared_content_backups(kind TEXT NOT NULL,id TEXT NOT NULL,revision INTEGER NOT NULL,data TEXT NOT NULL,created INTEGER NOT NULL,PRIMARY KEY(kind,id,revision))`); err != nil {
|
||||
return err
|
||||
}
|
||||
rows, err := tx.Query("SELECT kind,id,data,revision FROM records WHERE kind IN ('announcements','mirrors')")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
type legacyRow struct {
|
||||
kind, id, before, after string
|
||||
revision int
|
||||
}
|
||||
changes := []legacyRow{}
|
||||
for rows.Next() {
|
||||
var row legacyRow
|
||||
if err = rows.Scan(&row.kind, &row.id, &row.before, &row.revision); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
var data map[string]json.RawMessage
|
||||
if err = json.Unmarshal([]byte(row.before), &data); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
fields := []string{"help"}
|
||||
if row.kind == "announcements" {
|
||||
fields = []string{"title", "body"}
|
||||
}
|
||||
changed := false
|
||||
for _, field := range fields {
|
||||
value, exists := data[field]
|
||||
if !exists {
|
||||
data[field] = json.RawMessage(`""`)
|
||||
changed = true
|
||||
continue
|
||||
}
|
||||
var single string
|
||||
if string(value) != "null" && json.Unmarshal(value, &single) == nil {
|
||||
continue
|
||||
}
|
||||
var localized map[string]string
|
||||
if err = json.Unmarshal(value, &localized); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
// A present Chinese edition wins, including an intentionally empty one.
|
||||
single, exists = localized["zh-CN"]
|
||||
if !exists {
|
||||
single, exists = localized["zh"]
|
||||
}
|
||||
if !exists {
|
||||
keys := make([]string, 0, len(localized))
|
||||
for key := range localized {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
if strings.TrimSpace(localized[key]) != "" {
|
||||
single = localized[key]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
data[field], _ = json.Marshal(single)
|
||||
changed = true
|
||||
}
|
||||
if changed {
|
||||
raw, e := json.Marshal(data)
|
||||
if e != nil {
|
||||
rows.Close()
|
||||
return e
|
||||
}
|
||||
row.after = string(raw)
|
||||
changes = append(changes, row)
|
||||
}
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
rows.Close()
|
||||
for _, row := range changes {
|
||||
now := time.Now().UnixMilli()
|
||||
if _, err = tx.Exec("INSERT INTO shared_content_backups(kind,id,revision,data,created) VALUES(?,?,?,?,?)", row.kind, row.id, row.revision, row.before, now); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec("UPDATE records SET data=?,revision=revision+1,updated=? WHERE kind=? AND id=? AND revision=?", row.after, now, row.kind, row.id, row.revision); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSharedChineseContentMigration(t *testing.T) {
|
||||
c := fixture(t)
|
||||
legacy := map[string]any{"title": Localized{"zh-CN": "中文公告", "en-US": "English title"}, "body": Localized{"zh-CN": "# 中文正文", "en-US": "English body"}, "published": true, "pinned": false, "date": time.Now().UTC().Format(time.RFC3339)}
|
||||
if _, err := c.app.saveRecord("announcements", "legacy", legacy, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
metadata := map[string]any{"name": "alpine", "description": Localized{"zh-CN": "介绍", "en-US": "Description"}, "help": Localized{"zh-CN": "# 中文帮助", "en-US": "English help"}, "category": "os", "visible": true, "website": ""}
|
||||
if _, err := c.app.saveRecord("mirrors", "alpine", metadata, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := migrateSharedContent(c.app.db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
record, _ := c.app.record("announcements", "legacy")
|
||||
var article Announcement
|
||||
if err := decodeData(record.Data, &article); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if article.Title != "中文公告" || article.Body != "# 中文正文" || record.Revision != 2 {
|
||||
t.Fatal("Chinese edition not preserved")
|
||||
}
|
||||
record, _ = c.app.record("mirrors", "alpine")
|
||||
var mirror MirrorMetadata
|
||||
if err := decodeData(record.Data, &mirror); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if mirror.Help != "# 中文帮助" || mirror.Description["en-US"] != "Description" {
|
||||
t.Fatal("help migration changed unrelated metadata")
|
||||
}
|
||||
var backup string
|
||||
if err := c.app.db.QueryRow("SELECT data FROM shared_content_backups WHERE kind='announcements' AND id='legacy'").Scan(&backup); err != nil || !strings.Contains(backup, "English body") {
|
||||
t.Fatal("original language editions were not backed up")
|
||||
}
|
||||
if err := migrateSharedContent(c.app.db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
record, _ = c.app.record("announcements", "legacy")
|
||||
if record.Revision != 2 {
|
||||
t.Fatal("migration is not idempotent")
|
||||
}
|
||||
response := c.call("GET", "/api/v1/public", nil, false)
|
||||
var payload struct{ Announcements []struct{ Data Announcement } }
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil || len(payload.Announcements) != 1 || payload.Announcements[0].Data.Title != "中文公告" {
|
||||
t.Fatal("public API did not expose shared strings")
|
||||
}
|
||||
c.login("admin")
|
||||
status(t, c.call("PUT", "/api/v1/admin/records/announcements/legacy", map[string]any{"revision": 2, "data": legacy}, true), 400)
|
||||
article.Body = " "
|
||||
status(t, c.call("PUT", "/api/v1/admin/records/announcements/legacy", map[string]any{"revision": 2, "data": article}, true), 400)
|
||||
}
|
||||
|
||||
func TestSharedContentMigrationKeepsEmptyChineseAndLegacyOnlyContent(t *testing.T) {
|
||||
c := fixture(t)
|
||||
for name, help := range map[string]any{"empty-zh": Localized{"zh-CN": "", "en-US": "Alternate text"}, "legacy-only": Localized{"en-US": "Existing content"}, "missing": nil} {
|
||||
if _, err := c.app.saveRecord("mirrors", name, map[string]any{"name": name, "help": help}, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := migrateSharedContent(c.app.db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for name, want := range map[string]string{"empty-zh": "", "legacy-only": "Existing content", "missing": ""} {
|
||||
record, _ := c.app.record("mirrors", name)
|
||||
var mirror MirrorMetadata
|
||||
if err := decodeData(record.Data, &mirror); err != nil || mirror.Help != want {
|
||||
t.Fatalf("unexpected migrated help for %s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
module mirrorouc/backend
|
||||
|
||||
go 1.27.0
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.6.0
|
||||
modernc.org/sqlite v1.61.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.1.0 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.24 // indirect
|
||||
github.com/ncruces/go-strftime v1.1.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/sys v0.49.0 // indirect
|
||||
modernc.org/libc v1.79.0 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.12.1 // indirect
|
||||
)
|
||||
@@ -0,0 +1,52 @@
|
||||
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
|
||||
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg=
|
||||
github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI=
|
||||
github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50 h1:PlNWJRxu5APSV0pWqfIsZ0qt9OOGHSWfZQ4zvx+WgWs=
|
||||
github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50/go.mod h1:yUo1AMMpsPIDFGuwDK8nLcf8+vvAd5l2O6JDZ6hpwrM=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI=
|
||||
github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A=
|
||||
github.com/ncruces/go-strftime v1.1.0 h1:vtPmPWMkb539RGV9WpWT9EwJGvrOCKvMNI4obWL47Hk=
|
||||
github.com/ncruces/go-strftime v1.1.0/go.mod h1:yVaq7iwCN09vHRrKBrrflXT0M9tTMG2KmZ9Fj7xeG/g=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg=
|
||||
golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs=
|
||||
golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA=
|
||||
golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
|
||||
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs=
|
||||
golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs=
|
||||
modernc.org/cc/v4 v4.30.3 h1:aDq8gIX1X7+VFgO/JYYji6pLWL2H4tv3g8WatT3KCys=
|
||||
modernc.org/cc/v4 v4.30.3/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI=
|
||||
modernc.org/ccgo/v4 v4.37.0 h1:BAuJCUtIHIxFIYaFhqXIw5YaoMBlNCUlFovejHfMip4=
|
||||
modernc.org/ccgo/v4 v4.37.0/go.mod h1:/8627VhwWtAbqUXmPORxFQU5SgLrAx2jl9seLw9GBhY=
|
||||
modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM=
|
||||
modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU=
|
||||
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||
modernc.org/gc/v3 v3.2.0 h1:/Y3LGDMQaoYf55PebP1qsZ5FDGgAZyG1RYKEHeQzh1o=
|
||||
modernc.org/gc/v3 v3.2.0/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=
|
||||
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||
modernc.org/libc v1.79.0 h1:9Dvvv++bAMSkSuBzuOFAatbTV5OIoq6Y6FohmD3e7q8=
|
||||
modernc.org/libc v1.79.0/go.mod h1:uT3kR61NsbeKJSLqq83GJwSnZMVLlSjyAz4DfTaBy1M=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.12.1 h1:nFMiWrpStgZczNl6XI9GnIk/rWhYIyHGUaR04pGbp9g=
|
||||
modernc.org/memory v1.12.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg=
|
||||
modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||
modernc.org/sqlite v1.61.0 h1:sQi63oLQ9uquAUA4CzOisH35Q3pEszkPBuHM0tFL8CY=
|
||||
modernc.org/sqlite v1.61.0/go.mod h1:f4gr5tbpx2umklaT/QVtbi/50hNdM+4ng+2zMo8KXJ0=
|
||||
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestConfigurationHelperTransport(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Unix domain socket integration runs on Linux")
|
||||
}
|
||||
c := fixture(t)
|
||||
c.login("admin")
|
||||
root, err := os.MkdirTemp("", "mouc-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(root)
|
||||
socket := filepath.Join(root, "helper.sock")
|
||||
listener, err := net.Listen("unix", socket)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer listener.Close()
|
||||
c.app.cfg.ConfigSocket = socket
|
||||
config, _ := c.app.loadConfig()
|
||||
numbers := config.global
|
||||
numbers.Concurrent++
|
||||
response := c.call("POST", "/api/v1/admin/config/preview", map[string]any{"baseHash": config.hash, "global": numbers}, true)
|
||||
status(t, response, 201)
|
||||
var draft configDraft
|
||||
json.Unmarshal(response.Body.Bytes(), &draft)
|
||||
received := make(chan configDraft, 1)
|
||||
go func() {
|
||||
connection, e := listener.Accept()
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
defer connection.Close()
|
||||
connection.SetDeadline(time.Now().Add(5 * time.Second))
|
||||
var value configDraft
|
||||
if json.NewDecoder(connection).Decode(&value) == nil {
|
||||
received <- value
|
||||
}
|
||||
json.NewEncoder(connection).Encode(map[string]string{"code": ""})
|
||||
}()
|
||||
status(t, c.call("POST", "/api/v1/admin/config/apply", map[string]string{"id": draft.ID}, true), 202)
|
||||
select {
|
||||
case value := <-received:
|
||||
if value.ID != draft.ID || value.BaseHash != config.hash || len(value.Files) != 1 {
|
||||
t.Fatal("helper did not receive the validated draft")
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("helper request missing")
|
||||
}
|
||||
version, _ := c.app.readVersion(draft.ID)
|
||||
if version.Status != "applied" {
|
||||
t.Fatal("helper success not recorded")
|
||||
}
|
||||
// Only the helper writes in this mode; the ordinary web process leaves files untouched.
|
||||
after, _ := c.app.loadConfig()
|
||||
if after.hash != config.hash {
|
||||
t.Fatal("web process bypassed configuration helper")
|
||||
}
|
||||
}
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Listen, Data, Static, Manager, Worker, WorkerConfig, Logs, Metrics, ConfigSocket string
|
||||
SecureCookie bool
|
||||
}
|
||||
type loginAttempt struct {
|
||||
at time.Time
|
||||
count int
|
||||
}
|
||||
type App struct {
|
||||
cfg Config
|
||||
db *sql.DB
|
||||
logger *log.Logger
|
||||
client *http.Client
|
||||
dummyPassword string
|
||||
loginMu sync.Mutex
|
||||
attempts map[string]loginAttempt
|
||||
jobsMu sync.RWMutex
|
||||
jobs []Job
|
||||
jobsUpdated int64
|
||||
jobsError string
|
||||
configMu sync.Mutex
|
||||
}
|
||||
|
||||
func respond(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
func fail(w http.ResponseWriter, status int, code string) {
|
||||
respond(w, status, map[string]string{"code": code})
|
||||
}
|
||||
func readJSON(w http.ResponseWriter, r *http.Request, target any) bool {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 2<<20)
|
||||
d := json.NewDecoder(r.Body)
|
||||
d.DisallowUnknownFields()
|
||||
if err := d.Decode(target); err != nil {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return false
|
||||
}
|
||||
if err := d.Decode(new(any)); err != io.EOF {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (a *App) internal(w http.ResponseWriter, err error) {
|
||||
a.logger.Printf("request failed: %v", err)
|
||||
fail(w, 500, "INTERNAL_ERROR")
|
||||
}
|
||||
func env(name, fallback string) string {
|
||||
if value := os.Getenv(name); value != "" {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
func newApp(cfg Config) (*App, error) {
|
||||
db, err := openDatabase(filepath.Join(cfg.Data, "mirrorouc.db"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dummy, _ := hashPassword(randomToken())
|
||||
a := &App{cfg: cfg, db: db, logger: log.Default(), client: &http.Client{Timeout: 10 * time.Second}, dummyPassword: dummy, attempts: map[string]loginAttempt{}, jobs: []Job{}}
|
||||
if _, err = a.record("settings", "site"); errors.Is(err, errMissing) {
|
||||
if _, err = a.saveRecord("settings", "site", initialSettings(), 0); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err = os.MkdirAll(filepath.Join(cfg.Data, "uploads"), 0755); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
func (a *App) handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := a.db.PingContext(r.Context()); err != nil {
|
||||
fail(w, 503, "DATABASE_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
respond(w, 200, map[string]string{"status": "ok"})
|
||||
})
|
||||
mux.HandleFunc("GET /api/v1/public", a.public)
|
||||
mux.HandleFunc("GET /api/v1/monitoring", a.monitoring)
|
||||
mux.HandleFunc("GET /api/v1/job-speeds", a.jobSpeeds)
|
||||
mux.HandleFunc("POST /api/v1/auth/login", a.login)
|
||||
mux.HandleFunc("GET /api/v1/auth/session", a.session)
|
||||
mux.HandleFunc("POST /api/v1/auth/logout", a.logout)
|
||||
mux.HandleFunc("PUT /api/v1/auth/password", a.password)
|
||||
mux.HandleFunc("GET /api/v1/admin/overview", a.overview)
|
||||
mux.HandleFunc("GET /api/v1/admin/audit", a.auditList)
|
||||
mux.HandleFunc("GET /api/v1/admin/users", a.users)
|
||||
mux.HandleFunc("POST /api/v1/admin/users", a.createUser)
|
||||
mux.HandleFunc("GET /api/v1/admin/records/{kind}", a.listRecords)
|
||||
mux.HandleFunc("PUT /api/v1/admin/records/{kind}/{id}", a.putRecord)
|
||||
mux.HandleFunc("DELETE /api/v1/admin/records/{kind}/{id}", a.deleteRecord)
|
||||
mux.HandleFunc("POST /api/v1/admin/uploads", a.upload)
|
||||
mux.HandleFunc("POST /api/v1/admin/jobs/{name}/{action}", a.command)
|
||||
mux.HandleFunc("GET /api/v1/admin/jobs/{name}/logs", a.logs)
|
||||
mux.HandleFunc("GET /api/v1/admin/config", a.configView)
|
||||
mux.HandleFunc("POST /api/v1/admin/config/preview", a.configPreview)
|
||||
mux.HandleFunc("POST /api/v1/admin/config/apply", a.configApply)
|
||||
mux.HandleFunc("GET /api/v1/admin/config/versions", a.configVersions)
|
||||
mux.HandleFunc("POST /api/v1/admin/config/rollback", a.configRollback)
|
||||
mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) { fail(w, 404, "NOT_FOUND") })
|
||||
mux.HandleFunc("GET /uploads/{name}", func(w http.ResponseWriter, r *http.Request) {
|
||||
name := r.PathValue("name")
|
||||
if !validName(name) || (!strings.HasSuffix(name, ".png") && !strings.HasSuffix(name, ".jpg")) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
http.ServeFile(w, r, filepath.Join(a.cfg.Data, "uploads", name))
|
||||
})
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "GET" && r.Method != "HEAD" {
|
||||
w.Header().Set("Allow", "GET, HEAD")
|
||||
fail(w, 405, "METHOD_NOT_ALLOWED")
|
||||
return
|
||||
}
|
||||
path := r.URL.Path
|
||||
if path == "/" || path == "/status" || path == "/status/" || path == "/admin" || strings.HasPrefix(path, "/admin/") {
|
||||
http.ServeFile(w, r, filepath.Join(a.cfg.Static, "index.html"))
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(path, "/assets/") {
|
||||
http.FileServer(http.Dir(a.cfg.Static)).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
})
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if v := recover(); v != nil {
|
||||
a.logger.Printf("panic: %v", v)
|
||||
fail(w, 500, "INTERNAL_ERROR")
|
||||
}
|
||||
}()
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("X-Frame-Options", "DENY")
|
||||
w.Header().Set("Referrer-Policy", "same-origin")
|
||||
w.Header().Set("Content-Security-Policy", "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; connect-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'")
|
||||
mux.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
func main() {
|
||||
var cfg Config
|
||||
var initUser string
|
||||
flag.StringVar(&cfg.Listen, "listen", env("MIRROROUC_LISTEN", "127.0.0.1:8081"), "HTTP listen address")
|
||||
flag.StringVar(&cfg.Data, "data", env("MIRROROUC_DATA", "./data"), "local data directory")
|
||||
flag.StringVar(&cfg.Static, "static", env("MIRROROUC_STATIC", "../dist"), "frontend dist directory")
|
||||
flag.StringVar(&cfg.Manager, "manager", env("MIRROROUC_MANAGER", "http://127.0.0.1:12345"), "tunasync Manager URL")
|
||||
flag.StringVar(&cfg.Worker, "worker", env("MIRROROUC_WORKER", ""), "worker ID")
|
||||
flag.StringVar(&cfg.WorkerConfig, "worker-config", env("MIRROROUC_WORKER_CONFIG", ""), "worker TOML configuration")
|
||||
flag.StringVar(&cfg.Logs, "logs", env("MIRROROUC_LOGS", ""), "tunasync log root")
|
||||
flag.StringVar(&cfg.Metrics, "metrics", env("MIRROROUC_METRICS", ""), "metrics history JSON")
|
||||
flag.StringVar(&cfg.ConfigSocket, "config-socket", env("MIRROROUC_CONFIG_SOCKET", ""), "restricted configuration helper Unix socket")
|
||||
flag.BoolVar(&cfg.SecureCookie, "secure-cookies", os.Getenv("MIRROROUC_SECURE_COOKIES") == "true", "require HTTPS cookies")
|
||||
flag.StringVar(&initUser, "init-admin", "", "create initial administrator using MIRROROUC_ADMIN_PASSWORD (generated if absent)")
|
||||
flag.Parse()
|
||||
a, err := newApp(cfg)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer a.db.Close()
|
||||
if initUser != "" {
|
||||
password := os.Getenv("MIRROROUC_ADMIN_PASSWORD")
|
||||
generated := password == ""
|
||||
if generated {
|
||||
password = randomToken()
|
||||
}
|
||||
hash, err := hashPassword(password)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if !validName(initUser) {
|
||||
log.Fatal("invalid username")
|
||||
}
|
||||
if _, err = a.db.Exec("INSERT INTO users(username,password,role,created) VALUES(?,?,'admin',?)", initUser, hash, time.Now().UnixMilli()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if generated {
|
||||
os.Stdout.WriteString(password + "\n")
|
||||
}
|
||||
return
|
||||
}
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
a.loadCache()
|
||||
go a.poll(ctx)
|
||||
srv := &http.Server{Addr: cfg.Listen, Handler: a.handler(), ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 20 * time.Second, WriteTimeout: 30 * time.Second, IdleTimeout: 60 * time.Second, MaxHeaderBytes: 1 << 20}
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
stop, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
srv.Shutdown(stop)
|
||||
}()
|
||||
log.Printf("MirrorOUC API listening on %s", cfg.Listen)
|
||||
if err = srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package main
|
||||
|
||||
import "time"
|
||||
|
||||
type Localized map[string]string
|
||||
type Link struct {
|
||||
Title Localized `json:"title"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
type RelatedLink struct {
|
||||
Title Localized `json:"title"`
|
||||
Kind string `json:"kind"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
type Download struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
type Settings struct {
|
||||
Name Localized `json:"name"`
|
||||
Description Localized `json:"description"`
|
||||
LogoLight string `json:"logoLight"`
|
||||
LogoDark string `json:"logoDark"`
|
||||
Favicon string `json:"favicon"`
|
||||
Avatar string `json:"avatar"`
|
||||
Email string `json:"email"`
|
||||
Address Localized `json:"address"`
|
||||
Links []Link `json:"links"`
|
||||
RelatedLinks []RelatedLink `json:"relatedLinks"`
|
||||
Downloads []Download `json:"downloads"`
|
||||
}
|
||||
type Announcement struct {
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Published bool `json:"published"`
|
||||
Pinned bool `json:"pinned"`
|
||||
Date string `json:"date"`
|
||||
}
|
||||
type MirrorMetadata struct {
|
||||
Name string `json:"name"`
|
||||
Description Localized `json:"description"`
|
||||
Help string `json:"help"`
|
||||
Category string `json:"category"`
|
||||
Visible bool `json:"visible"`
|
||||
Website string `json:"website"`
|
||||
}
|
||||
type Job struct {
|
||||
Name string `json:"name"`
|
||||
Worker string `json:"worker"`
|
||||
Status string `json:"status"`
|
||||
LastUpdate time.Time `json:"last_update"`
|
||||
LastStarted time.Time `json:"last_started"`
|
||||
LastEnded time.Time `json:"last_ended"`
|
||||
NextSchedule time.Time `json:"next_schedule"`
|
||||
Upstream string `json:"upstream"`
|
||||
Size string `json:"size"`
|
||||
Error string `json:"error_msg"`
|
||||
}
|
||||
type PublicMirror struct {
|
||||
Name string `json:"name"`
|
||||
Description Localized `json:"description"`
|
||||
Help string `json:"help"`
|
||||
Category string `json:"category"`
|
||||
Status string `json:"status"`
|
||||
LastSuccess *int64 `json:"lastSuccess"`
|
||||
LastAttempt *int64 `json:"lastAttempt"`
|
||||
SyncStarted *int64 `json:"syncStarted"`
|
||||
NextSync *int64 `json:"nextSync"`
|
||||
Size *float64 `json:"size"`
|
||||
Website string `json:"website"`
|
||||
Upstream string `json:"upstream"`
|
||||
BrowseURL string `json:"browseUrl"`
|
||||
}
|
||||
type Worker struct {
|
||||
ID string `json:"id"`
|
||||
LastOnline time.Time `json:"last_online"`
|
||||
}
|
||||
type Record struct {
|
||||
ID string `json:"id"`
|
||||
Data any `json:"data"`
|
||||
Revision int `json:"revision"`
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
}
|
||||
type Session struct {
|
||||
UserID int64
|
||||
Username string
|
||||
Role string
|
||||
CSRF string
|
||||
}
|
||||
|
||||
func initialSettings() Settings {
|
||||
return Settings{
|
||||
Name: Localized{"zh-CN": "中国海洋大学软件镜像源", "en-US": "Ocean University of China Software Mirrors"},
|
||||
Description: Localized{"zh-CN": "中国海洋大学软件镜像源(MirrorOUC)由中国海洋大学爱特工作室承建。", "en-US": "MirrorOUC is built by IT Studio, Ocean University of China."},
|
||||
LogoLight: "/assets/mirrorOUC_dark.png", LogoDark: "/assets/mirrorOUC.png", Favicon: "/assets/ouc-seal.png", Avatar: "/assets/ouc-seal.png", Email: "contact@itstudio.club",
|
||||
Address: Localized{"zh-CN": "山东省青岛市中国海洋大学西海岸校区 · 信息南楼五八创新创业工坊", "en-US": "58 Innovation Workshop, Information South Building, West Coast Campus, Ocean University of China, Qingdao, Shandong"},
|
||||
RelatedLinks: initialRelatedLinks(),
|
||||
Links: []Link{
|
||||
{Localized{"zh-CN": "中国海洋大学", "en-US": "Ocean University of China"}, "https://www.ouc.edu.cn/"},
|
||||
{Localized{"zh-CN": "爱特工作室", "en-US": "IT Studio"}, "https://www.itouc.cn/"},
|
||||
{Localized{"zh-CN": "信息科学与工程学部", "en-US": "Faculty of Information Science and Engineering"}, "https://it.ouc.edu.cn/"},
|
||||
{Localized{"zh-CN": "爱特荣誉墙", "en-US": "IT Studio honors"}, "https://honor.itouc.cn/"},
|
||||
{Localized{"zh-CN": "GitOUC", "en-US": "GitOUC"}, "https://git.itouc.cn/"},
|
||||
{Localized{"zh-CN": "爱特博客网", "en-US": "IT Studio blog"}, "https://blog.itouc.cn/"},
|
||||
}, Downloads: []Download{},
|
||||
}
|
||||
}
|
||||
|
||||
func initialRelatedLinks() []RelatedLink {
|
||||
return []RelatedLink{
|
||||
{Title: Localized{"zh-CN": "软件源使用帮助", "en-US": "Mirror usage guides"}, Kind: "help", URL: ""},
|
||||
{Title: Localized{"zh-CN": "中国海洋大学爱特工作室", "en-US": "IT Studio, Ocean University of China"}, Kind: "url", URL: "https://www.itouc.cn/"},
|
||||
{Title: Localized{"zh-CN": "中国海洋大学", "en-US": "Ocean University of China"}, Kind: "url", URL: "https://www.ouc.edu.cn/"},
|
||||
}
|
||||
}
|
||||
|
||||
// Older settings records get the existing sidebar entries until explicitly edited.
|
||||
// An empty array is intentional and must stay empty.
|
||||
func settingsDefaults(data any) any {
|
||||
if object, ok := data.(map[string]any); ok {
|
||||
if value, exists := object["relatedLinks"]; !exists || value == nil {
|
||||
object["relatedLinks"] = initialRelatedLinks()
|
||||
}
|
||||
}
|
||||
return data
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type metricsHistory struct {
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
MemoryTotal float64 `json:"memoryTotal"`
|
||||
Disks []map[string]any `json:"disks"`
|
||||
Samples []map[string]float64 `json:"samples"`
|
||||
}
|
||||
type metricStats struct {
|
||||
Mean float64 `json:"mean"`
|
||||
Latest float64 `json:"latest"`
|
||||
Max float64 `json:"max"`
|
||||
}
|
||||
type metricSeries struct {
|
||||
ID string `json:"id"`
|
||||
LabelKey string `json:"labelKey"`
|
||||
Color string `json:"color"`
|
||||
Values []float64 `json:"values"`
|
||||
Stats metricStats `json:"stats"`
|
||||
}
|
||||
type metricPanel struct {
|
||||
ID string `json:"id"`
|
||||
TitleKey string `json:"titleKey"`
|
||||
Unit string `json:"unit"`
|
||||
Maximum float64 `json:"maximum"`
|
||||
Stacked bool `json:"stacked"`
|
||||
Available bool `json:"available"`
|
||||
Series []metricSeries `json:"series"`
|
||||
}
|
||||
|
||||
func niceMaximum(value float64) float64 {
|
||||
if value <= 0 {
|
||||
return 1
|
||||
}
|
||||
power := math.Pow(10, math.Floor(math.Log10(value)))
|
||||
for _, step := range []float64{1, 2, 5, 10} {
|
||||
if step*power >= value*1.05 {
|
||||
return step * power
|
||||
}
|
||||
}
|
||||
return 20 * power
|
||||
}
|
||||
func (a *App) monitoring(w http.ResponseWriter, r *http.Request) {
|
||||
hours, err := strconv.Atoi(r.URL.Query().Get("hours"))
|
||||
if err != nil || (hours != 1 && hours != 6 && hours != 24) {
|
||||
fail(w, 400, "INVALID_RANGE")
|
||||
return
|
||||
}
|
||||
var history metricsHistory
|
||||
if a.cfg.Metrics == "" {
|
||||
fail(w, 503, "METRICS_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
raw, err := os.ReadFile(a.cfg.Metrics)
|
||||
if err != nil || len(raw) > 32<<20 {
|
||||
fail(w, 503, "METRICS_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
if err = json.Unmarshal(raw, &history); err != nil {
|
||||
fail(w, 503, "METRICS_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
samples := []map[string]float64{}
|
||||
cutoff := float64(time.Now().Add(-time.Duration(hours) * time.Hour).UnixMilli())
|
||||
for _, sample := range history.Samples {
|
||||
if sample["time"] >= cutoff {
|
||||
samples = append(samples, sample)
|
||||
}
|
||||
}
|
||||
indices := []int{}
|
||||
if len(samples) <= 241 {
|
||||
for i := range samples {
|
||||
indices = append(indices, i)
|
||||
}
|
||||
} else {
|
||||
for i := 0; i < 241; i++ {
|
||||
indices = append(indices, int(math.Round(float64(i)*float64(len(samples)-1)/240)))
|
||||
}
|
||||
}
|
||||
timestamps := []int64{}
|
||||
for _, index := range indices {
|
||||
timestamps = append(timestamps, int64(samples[index]["time"]))
|
||||
}
|
||||
makeSeries := func(id, label, color string) metricSeries {
|
||||
series := metricSeries{ID: id, LabelKey: "monitor.series." + label, Color: "var(--" + color + ")", Values: []float64{}}
|
||||
count := 0
|
||||
for _, sample := range samples {
|
||||
if value, ok := sample[id]; ok {
|
||||
series.Stats.Mean += value
|
||||
series.Stats.Max = math.Max(value, series.Stats.Max)
|
||||
series.Stats.Latest = value
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count > 0 {
|
||||
series.Stats.Mean /= float64(count)
|
||||
}
|
||||
for _, index := range indices {
|
||||
series.Values = append(series.Values, samples[index][id])
|
||||
}
|
||||
return series
|
||||
}
|
||||
panel := func(id, unit string, maximum float64, stacked bool, series ...metricSeries) metricPanel {
|
||||
available := len(samples) > 1
|
||||
peak := float64(0)
|
||||
for _, sample := range samples {
|
||||
sum := float64(0)
|
||||
for _, s := range series {
|
||||
value, ok := sample[s.ID]
|
||||
if !ok {
|
||||
available = false
|
||||
}
|
||||
if stacked {
|
||||
sum += value
|
||||
} else {
|
||||
sum = math.Max(sum, value)
|
||||
}
|
||||
}
|
||||
peak = math.Max(peak, sum)
|
||||
}
|
||||
if maximum == 0 {
|
||||
maximum = niceMaximum(peak)
|
||||
}
|
||||
if maximum <= 0 {
|
||||
maximum = 1
|
||||
}
|
||||
return metricPanel{id, "monitor.charts." + id, unit, maximum, stacked, available, series}
|
||||
}
|
||||
panels := []metricPanel{
|
||||
panel("ipv4", "gigabits", 0, true, makeSeries("https4", "https", "blue"), makeSeries("rsync4", "rsync", "violet")),
|
||||
panel("ipv6", "gigabits", 0, true, makeSeries("https6", "https", "blue"), makeSeries("rsync6", "rsync", "violet")),
|
||||
panel("iops", "operations", 0, false, makeSeries("readOps", "read", "green"), makeSeries("writeOps", "write", "amber")),
|
||||
panel("throughput", "gigabytes", 0, false, makeSeries("readBytes", "read", "blue"), makeSeries("writeBytes", "write", "violet")),
|
||||
panel("cpu", "percent", 100, true, makeSeries("user", "user", "blue"), makeSeries("system", "system", "green"), makeSeries("wait", "wait", "amber"), makeSeries("idle", "idle", "chart-muted")),
|
||||
panel("memory", "memory", history.MemoryTotal, true, makeSeries("used", "used", "blue"), makeSeries("cached", "cached", "green"), makeSeries("buffers", "buffers", "amber"), makeSeries("free", "free", "chart-muted")),
|
||||
}
|
||||
respond(w, 200, map[string]any{"timestamps": timestamps, "panels": panels, "disks": history.Disks, "updatedAt": history.UpdatedAt, "stale": time.Now().UnixMilli()-history.UpdatedAt > 90000})
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRelatedLinksPersistIndependentlyAndSupportOlderForms(t *testing.T) {
|
||||
c := fixture(t)
|
||||
c.login("admin")
|
||||
initial, _ := c.app.record("settings", "site")
|
||||
var legacy map[string]any
|
||||
decodeData(initial.Data, &legacy)
|
||||
delete(legacy, "relatedLinks")
|
||||
if _, err := c.app.saveRecord("settings", "site", legacy, initial.Revision); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
current, _ := c.app.record("settings", "site")
|
||||
var settings Settings
|
||||
decodeData(current.Data, &settings)
|
||||
if len(settings.RelatedLinks) != 3 || settings.RelatedLinks[0].Kind != "help" {
|
||||
t.Fatal("legacy sidebar defaults missing")
|
||||
}
|
||||
response := c.call("GET", "/api/v1/admin/records/settings", nil, false)
|
||||
var adminRecords []struct{ Data Settings }
|
||||
json.Unmarshal(response.Body.Bytes(), &adminRecords)
|
||||
if len(adminRecords) != 1 || !reflect.DeepEqual(adminRecords[0].Data.RelatedLinks, settings.RelatedLinks) {
|
||||
t.Fatal("admin/public defaults differ")
|
||||
}
|
||||
footer := append([]Link{}, settings.Links...)
|
||||
settings.RelatedLinks = []RelatedLink{
|
||||
{Title: Localized{"zh-CN": "站点状态", "en-US": "Status"}, Kind: "url", URL: "/status"},
|
||||
{Title: Localized{"zh-CN": "使用帮助"}, Kind: "help", URL: ""},
|
||||
}
|
||||
response = c.call("PUT", "/api/v1/admin/records/settings/site", map[string]any{"data": settings, "revision": current.Revision}, true)
|
||||
status(t, response, 200)
|
||||
var saved Record
|
||||
json.Unmarshal(response.Body.Bytes(), &saved)
|
||||
legacy = nil
|
||||
decodeData(saved.Data, &legacy)
|
||||
delete(legacy, "relatedLinks")
|
||||
response = c.call("PUT", "/api/v1/admin/records/settings/site", map[string]any{"data": legacy, "revision": saved.Revision}, true)
|
||||
status(t, response, 200)
|
||||
json.Unmarshal(response.Body.Bytes(), &saved)
|
||||
response = c.call("GET", "/api/v1/public", nil, false)
|
||||
var public struct{ Site Settings }
|
||||
json.Unmarshal(response.Body.Bytes(), &public)
|
||||
if !reflect.DeepEqual(public.Site.RelatedLinks, settings.RelatedLinks) || !reflect.DeepEqual(public.Site.Links, footer) {
|
||||
t.Fatal("sidebar edits changed footer links or were lost by older form")
|
||||
}
|
||||
settings.RelatedLinks = []RelatedLink{}
|
||||
status(t, c.call("PUT", "/api/v1/admin/records/settings/site", map[string]any{"data": settings, "revision": saved.Revision}, true), 200)
|
||||
c.app.db.Close()
|
||||
var err error
|
||||
c.app.db, err = openDatabase(filepath.Join(c.app.cfg.Data, "mirrorouc.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response = c.call("GET", "/api/v1/public", nil, false)
|
||||
public.Site = Settings{}
|
||||
json.Unmarshal(response.Body.Bytes(), &public)
|
||||
if public.Site.RelatedLinks == nil || len(public.Site.RelatedLinks) != 0 {
|
||||
t.Fatal("explicitly cleared sidebar reset after restart")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelatedLinkValidation(t *testing.T) {
|
||||
c := fixture(t)
|
||||
c.login("admin")
|
||||
record, _ := c.app.record("settings", "site")
|
||||
settings := initialSettings()
|
||||
bad := []RelatedLink{
|
||||
{Title: Localized{"zh-CN": "测试"}, Kind: "url", URL: "javascript:alert(1)"},
|
||||
{Title: Localized{"zh-CN": "测试"}, Kind: "url", URL: "data:text/html,example"},
|
||||
{Title: Localized{"zh-CN": "测试"}, Kind: "url", URL: "//example.invalid"},
|
||||
{Title: Localized{"zh-CN": "测试"}, Kind: "url", URL: "/\\example.invalid"},
|
||||
{Title: Localized{"zh-CN": "测试"}, Kind: "shell", URL: "/status"},
|
||||
{Title: Localized{"zh-CN": "测试"}, Kind: "help", URL: "https://example.invalid/"},
|
||||
{Title: Localized{"zh-CN": " "}, Kind: "url", URL: "https://example.invalid/"},
|
||||
}
|
||||
for _, link := range bad {
|
||||
settings.RelatedLinks = []RelatedLink{link}
|
||||
status(t, c.call("PUT", "/api/v1/admin/records/settings/site", map[string]any{"data": settings, "revision": record.Revision}, true), 400)
|
||||
}
|
||||
for _, value := range []string{"https://example.invalid/", "http://example.invalid/", "/status", "/status?range=1#monitor-sync"} {
|
||||
if !navigationURL(value) {
|
||||
t.Fatalf("valid navigation URL rejected: %s", value)
|
||||
}
|
||||
}
|
||||
c.login("editor")
|
||||
status(t, c.call("GET", "/api/v1/admin/records/settings", nil, false), 403)
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"image"
|
||||
"image/png"
|
||||
"mime/multipart"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestUploadsAreValidatedAndNotListed(t *testing.T) {
|
||||
c := fixture(t)
|
||||
c.login("admin")
|
||||
upload := func(value []byte) *httptest.ResponseRecorder {
|
||||
var body bytes.Buffer
|
||||
form := multipart.NewWriter(&body)
|
||||
part, _ := form.CreateFormFile("file", "../../unsafe.html")
|
||||
part.Write(value)
|
||||
form.Close()
|
||||
request := httptest.NewRequest("POST", "http://localhost/api/v1/admin/uploads", &body)
|
||||
request.Header.Set("Content-Type", form.FormDataContentType())
|
||||
request.Header.Set("X-CSRF-Token", c.csrf)
|
||||
request.AddCookie(c.cookie)
|
||||
response := httptest.NewRecorder()
|
||||
c.app.handler().ServeHTTP(response, request)
|
||||
return response
|
||||
}
|
||||
status(t, upload([]byte("<script>unsafe()</script>")), 400)
|
||||
var pixel bytes.Buffer
|
||||
png.Encode(&pixel, image.NewRGBA(image.Rect(0, 0, 1, 1)))
|
||||
result := upload(pixel.Bytes())
|
||||
status(t, result, 201)
|
||||
var value map[string]string
|
||||
json.Unmarshal(result.Body.Bytes(), &value)
|
||||
status(t, c.call("GET", value["url"], nil, false), 200)
|
||||
status(t, c.call("GET", "/uploads/", nil, false), 404)
|
||||
status(t, c.call("GET", "/uploads/mirrorouc.db", nil, false), 404)
|
||||
}
|
||||
|
||||
func TestMonitoringUsesRawStatistics(t *testing.T) {
|
||||
c := fixture(t)
|
||||
history := metricsHistory{UpdatedAt: time.Now().UnixMilli(), MemoryTotal: 32, Samples: []map[string]float64{}}
|
||||
for index := 0; index < 500; index++ {
|
||||
value := float64(1)
|
||||
if index == 1 {
|
||||
value = 200
|
||||
}
|
||||
history.Samples = append(history.Samples, map[string]float64{"time": float64(time.Now().Add(time.Duration(index-500) * time.Second).UnixMilli()), "https4": value, "rsync4": 0})
|
||||
}
|
||||
raw, _ := json.Marshal(history)
|
||||
path := filepath.Join(t.TempDir(), "history.json")
|
||||
os.WriteFile(path, raw, 0644)
|
||||
c.app.cfg.Metrics = path
|
||||
response := c.call("GET", "/api/v1/monitoring?hours=1", nil, false)
|
||||
status(t, response, 200)
|
||||
var result struct {
|
||||
Panels []metricPanel
|
||||
Timestamps []int64
|
||||
}
|
||||
json.Unmarshal(response.Body.Bytes(), &result)
|
||||
if len(result.Timestamps) != 241 || result.Panels[0].Series[0].Stats.Max != 200 || !result.Panels[0].Available || result.Panels[1].Available {
|
||||
t.Fatal("incorrect sampled statistics or missing-metric handling")
|
||||
}
|
||||
status(t, c.call("GET", "/api/v1/monitoring?hours=2", nil, false), 400)
|
||||
}
|
||||
func TestJobSpeedsFilterInactiveAndStaleData(t *testing.T) {
|
||||
c := fixture(t)
|
||||
root := t.TempDir()
|
||||
c.app.cfg.Metrics = filepath.Join(root, "history.json")
|
||||
c.app.jobsMu.Lock()
|
||||
c.app.jobs = []Job{{Name: "debian", Status: "syncing"}, {Name: "ubuntu", Status: "success"}}
|
||||
c.app.jobsMu.Unlock()
|
||||
snapshot := speedSnapshot{UpdatedAt: time.Now().UnixMilli(), Interval: 2, Available: true, Jobs: map[string]jobSpeed{"debian": {BytesPerSecond: 12345, Connections: 3}, "ubuntu": {BytesPerSecond: 99999}, "secret-job": {BytesPerSecond: 99999}}}
|
||||
write := func() {
|
||||
raw, _ := json.Marshal(snapshot)
|
||||
os.WriteFile(filepath.Join(root, "job-speeds.json"), raw, 0644)
|
||||
}
|
||||
write()
|
||||
response := c.call("GET", "/api/v1/job-speeds", nil, false)
|
||||
status(t, response, 200)
|
||||
var result speedSnapshot
|
||||
json.Unmarshal(response.Body.Bytes(), &result)
|
||||
if len(result.Jobs) != 1 || result.Jobs["debian"].BytesPerSecond != 12345 {
|
||||
t.Fatal("inactive or unknown jobs leaked into speeds")
|
||||
}
|
||||
snapshot.UpdatedAt = time.Now().Add(-time.Minute).UnixMilli()
|
||||
write()
|
||||
response = c.call("GET", "/api/v1/job-speeds", nil, false)
|
||||
json.Unmarshal(response.Body.Bytes(), &result)
|
||||
// Decode into a new map: encoding/json merges map entries when reusing one.
|
||||
result = speedSnapshot{}
|
||||
json.Unmarshal(response.Body.Bytes(), &result)
|
||||
if !result.Stale || len(result.Jobs) != 0 {
|
||||
t.Fatal("outdated rates presented as current")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
type jobSpeed struct {
|
||||
BytesPerSecond float64 `json:"bytesPerSecond"`
|
||||
Connections int `json:"connections"`
|
||||
}
|
||||
type speedSnapshot struct {
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
Interval float64 `json:"interval"`
|
||||
Available bool `json:"available"`
|
||||
Jobs map[string]jobSpeed `json:"jobs"`
|
||||
Stale bool `json:"stale"`
|
||||
}
|
||||
|
||||
func (a *App) jobSpeeds(w http.ResponseWriter, r *http.Request) {
|
||||
if a.cfg.Metrics == "" {
|
||||
fail(w, 503, "SPEEDS_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
raw, err := os.ReadFile(filepath.Join(filepath.Dir(a.cfg.Metrics), "job-speeds.json"))
|
||||
var snapshot speedSnapshot
|
||||
if err != nil || len(raw) > 1<<20 || json.Unmarshal(raw, &snapshot) != nil {
|
||||
fail(w, 503, "SPEEDS_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
snapshot.Stale = time.Now().UnixMilli()-snapshot.UpdatedAt > 15000 || snapshot.UpdatedAt > time.Now().Add(time.Minute).UnixMilli()
|
||||
filtered := map[string]jobSpeed{}
|
||||
jobs, _, code := a.cachedJobs()
|
||||
if code == "" && snapshot.Available && !snapshot.Stale {
|
||||
for _, job := range jobs {
|
||||
if speed, exists := snapshot.Jobs[job.Name]; exists && job.Status == "syncing" && speed.BytesPerSecond >= 0 && speed.Connections >= 0 {
|
||||
filtered[job.Name] = speed
|
||||
}
|
||||
}
|
||||
}
|
||||
snapshot.Jobs = filtered
|
||||
respond(w, 200, snapshot)
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
_ "modernc.org/sqlite"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
var errConflict = errors.New("CONFLICT")
|
||||
var errMissing = errors.New("NOT_FOUND")
|
||||
|
||||
func openDatabase(path string) (*sql.DB, error) {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db, err := sql.Open("sqlite", path+"?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)&_pragma=foreign_keys(1)")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, username TEXT NOT NULL UNIQUE, password TEXT NOT NULL, role TEXT NOT NULL CHECK(role IN ('admin','editor')), created INTEGER NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS sessions(token TEXT PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, csrf TEXT NOT NULL, expires INTEGER NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS records(kind TEXT NOT NULL,id TEXT NOT NULL,data TEXT NOT NULL,revision INTEGER NOT NULL DEFAULT 1,updated INTEGER NOT NULL,deleted INTEGER NOT NULL DEFAULT 0,PRIMARY KEY(kind,id));
|
||||
CREATE TABLE IF NOT EXISTS audit(id INTEGER PRIMARY KEY,username TEXT NOT NULL,action TEXT NOT NULL,target TEXT NOT NULL,result TEXT NOT NULL,created INTEGER NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS config_versions(id TEXT PRIMARY KEY,files TEXT NOT NULL,base_hash TEXT NOT NULL,status TEXT NOT NULL,created INTEGER NOT NULL,username TEXT NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS cache(key TEXT PRIMARY KEY,data TEXT NOT NULL,updated INTEGER NOT NULL);`)
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err = migrateSharedContent(db); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
func (a *App) record(kind, id string) (Record, error) {
|
||||
var r Record
|
||||
var raw string
|
||||
r.ID = id
|
||||
err := a.db.QueryRow("SELECT data,revision,updated FROM records WHERE kind=? AND id=? AND deleted=0", kind, id).Scan(&raw, &r.Revision, &r.UpdatedAt)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return r, errMissing
|
||||
}
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
err = json.Unmarshal([]byte(raw), &r.Data)
|
||||
if kind == "settings" && id == "site" {
|
||||
r.Data = settingsDefaults(r.Data)
|
||||
}
|
||||
return r, err
|
||||
}
|
||||
func (a *App) records(kind string) ([]Record, error) {
|
||||
rows, err := a.db.Query("SELECT id,data,revision,updated FROM records WHERE kind=? AND deleted=0 ORDER BY updated DESC,id", kind)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []Record{}
|
||||
for rows.Next() {
|
||||
var r Record
|
||||
var raw string
|
||||
if err = rows.Scan(&r.ID, &raw, &r.Revision, &r.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = json.Unmarshal([]byte(raw), &r.Data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if kind == "settings" && r.ID == "site" {
|
||||
r.Data = settingsDefaults(r.Data)
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
func (a *App) saveRecord(kind, id string, data any, revision int) (Record, error) {
|
||||
raw, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
if revision == 0 {
|
||||
_, err = a.db.Exec("INSERT INTO records(kind,id,data,revision,updated) VALUES(?,?,?,1,?)", kind, id, string(raw), now)
|
||||
if err != nil {
|
||||
return Record{}, errConflict
|
||||
}
|
||||
} else {
|
||||
var result sql.Result
|
||||
result, err = a.db.Exec("UPDATE records SET data=?,revision=revision+1,updated=?,deleted=0 WHERE kind=? AND id=? AND revision=? AND deleted=0", string(raw), now, kind, id, revision)
|
||||
if err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
if n != 1 {
|
||||
return Record{}, errConflict
|
||||
}
|
||||
}
|
||||
return a.record(kind, id)
|
||||
}
|
||||
func (a *App) audit(s Session, action, target, result string) {
|
||||
_, err := a.db.Exec("INSERT INTO audit(username,action,target,result,created) VALUES(?,?,?,?,?)", s.Username, action, target, result, time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
a.logger.Printf("audit write failed: %v", err)
|
||||
}
|
||||
}
|
||||
func decodeData(value any, target any) error {
|
||||
raw, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(raw, target)
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (a *App) managerGet(ctx context.Context, path string, target any) error {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", strings.TrimRight(a.cfg.Manager, "/")+path, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := a.client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != 200 {
|
||||
return fmt.Errorf("manager HTTP %d", res.StatusCode)
|
||||
}
|
||||
return json.NewDecoder(io.LimitReader(res.Body, 8<<20)).Decode(target)
|
||||
}
|
||||
func (a *App) sendCommand(ctx context.Context, name, action string) error {
|
||||
if a.cfg.Worker == "" {
|
||||
return errors.New("worker not configured")
|
||||
}
|
||||
data, _ := json.Marshal(map[string]any{"worker_id": a.cfg.Worker, "mirror_id": name, "cmd": action, "args": []string{}, "options": map[string]bool{}})
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", strings.TrimRight(a.cfg.Manager, "/")+"/cmd", bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res, err := a.client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
io.Copy(io.Discard, io.LimitReader(res.Body, 1<<20))
|
||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||
return fmt.Errorf("manager command HTTP %d", res.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (a *App) cachedJobs() ([]Job, int64, string) {
|
||||
a.jobsMu.RLock()
|
||||
defer a.jobsMu.RUnlock()
|
||||
jobs := append([]Job{}, a.jobs...)
|
||||
return jobs, a.jobsUpdated, a.jobsError
|
||||
}
|
||||
func (a *App) loadCache() {
|
||||
var raw string
|
||||
var updated int64
|
||||
if a.db.QueryRow("SELECT data,updated FROM cache WHERE key='jobs'").Scan(&raw, &updated) == nil {
|
||||
var jobs []Job
|
||||
if json.Unmarshal([]byte(raw), &jobs) == nil {
|
||||
a.jobs = jobs
|
||||
a.jobsUpdated = updated
|
||||
a.jobsError = "TUNASYNC_UNAVAILABLE"
|
||||
}
|
||||
}
|
||||
}
|
||||
func (a *App) refresh(ctx context.Context) {
|
||||
jobs := []Job{}
|
||||
err := error(nil)
|
||||
if a.cfg.Worker == "" {
|
||||
err = errors.New("worker not configured")
|
||||
} else {
|
||||
err = a.managerGet(ctx, "/workers/"+url.PathEscape(a.cfg.Worker)+"/jobs", &jobs)
|
||||
}
|
||||
a.jobsMu.Lock()
|
||||
if err != nil {
|
||||
a.jobsError = "TUNASYNC_UNAVAILABLE"
|
||||
a.jobsMu.Unlock()
|
||||
a.logger.Printf("tunasync poll: %v", err)
|
||||
return
|
||||
}
|
||||
a.jobs = jobs
|
||||
a.jobsUpdated = time.Now().UnixMilli()
|
||||
a.jobsError = ""
|
||||
updated := a.jobsUpdated
|
||||
a.jobsMu.Unlock()
|
||||
raw, _ := json.Marshal(jobs)
|
||||
if _, err = a.db.Exec("INSERT INTO cache(key,data,updated) VALUES('jobs',?,?) ON CONFLICT(key) DO UPDATE SET data=excluded.data,updated=excluded.updated", string(raw), updated); err != nil {
|
||||
a.logger.Printf("cache: %v", err)
|
||||
}
|
||||
}
|
||||
func (a *App) poll(ctx context.Context) {
|
||||
a.refresh(ctx)
|
||||
ticker := time.NewTicker(15 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
a.refresh(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
func stamp(value time.Time) *int64 {
|
||||
if value.IsZero() || value.Unix() <= 0 {
|
||||
return nil
|
||||
}
|
||||
ms := value.UnixMilli()
|
||||
return &ms
|
||||
}
|
||||
|
||||
var humanSize = regexp.MustCompile(`(?i)^\s*(\d+(?:\.\d+)?)\s*([kmgtpe]?)(?:i?b)?\s*$`)
|
||||
|
||||
func parseSize(value string) *float64 {
|
||||
parts := humanSize.FindStringSubmatch(value)
|
||||
if parts == nil {
|
||||
return nil
|
||||
}
|
||||
n, err := strconv.ParseFloat(parts[1], 64)
|
||||
if err != nil || math.IsNaN(n) || math.IsInf(n, 0) {
|
||||
return nil
|
||||
}
|
||||
power := strings.Index("kmgtpe", strings.ToLower(parts[2])) + 1
|
||||
if parts[2] == "" {
|
||||
power = 0
|
||||
}
|
||||
n *= math.Pow(1024, float64(power))
|
||||
return &n
|
||||
}
|
||||
func redactURL(value string) string {
|
||||
u, err := url.Parse(value)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
u.User = nil
|
||||
u.RawQuery = ""
|
||||
u.Fragment = ""
|
||||
return u.String()
|
||||
}
|
||||
func publicMirror(job Job, meta MirrorMetadata) PublicMirror {
|
||||
status := map[string]string{"success": "success", "syncing": "syncing", "pre-syncing": "waiting", "failed": "failed", "paused": "paused", "disabled": "paused", "none": "unknown"}[job.Status]
|
||||
if status == "" {
|
||||
status = "unknown"
|
||||
}
|
||||
return PublicMirror{Name: job.Name, Description: meta.Description, Help: meta.Help, Category: meta.Category, Status: status, LastSuccess: stamp(job.LastUpdate), LastAttempt: stamp(job.LastStarted), SyncStarted: stamp(job.LastStarted), NextSync: stamp(job.NextSchedule), Size: parseSize(job.Size), Website: meta.Website, Upstream: redactURL(job.Upstream), BrowseURL: "/mirrors/" + url.PathEscape(job.Name) + "/"}
|
||||
}
|
||||
func (a *App) command(w http.ResponseWriter, r *http.Request) {
|
||||
s, ok := a.require(w, r, true)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
name, action := r.PathValue("name"), r.PathValue("action")
|
||||
if action != "start" && action != "stop" && action != "restart" && action != "disable" {
|
||||
fail(w, 400, "INVALID_ACTION")
|
||||
return
|
||||
}
|
||||
jobs, _, code := a.cachedJobs()
|
||||
found := false
|
||||
for _, job := range jobs {
|
||||
if job.Name == name {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !validName(name) || !found {
|
||||
fail(w, 404, "NOT_FOUND")
|
||||
return
|
||||
}
|
||||
if code != "" {
|
||||
fail(w, 503, "TUNASYNC_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
if err := a.sendCommand(r.Context(), name, action); err != nil {
|
||||
a.audit(s, "job_"+action, name, "uncertain")
|
||||
a.logger.Printf("command: %v", err)
|
||||
fail(w, 502, "COMMAND_UNCERTAIN")
|
||||
return
|
||||
}
|
||||
a.audit(s, "job_"+action, name, "submitted")
|
||||
a.refresh(r.Context())
|
||||
respond(w, 202, map[string]string{"status": "submitted"})
|
||||
}
|
||||
func (a *App) logs(w http.ResponseWriter, r *http.Request) {
|
||||
if _, ok := a.require(w, r, true); !ok {
|
||||
return
|
||||
}
|
||||
name := r.PathValue("name")
|
||||
if a.cfg.Logs == "" {
|
||||
fail(w, 503, "LOGS_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
if !validName(name) {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
base, baseErr := filepath.EvalSymlinks(a.cfg.Logs)
|
||||
if baseErr != nil {
|
||||
fail(w, 503, "LOGS_UNAVAILABLE")
|
||||
return
|
||||
}
|
||||
root, err := filepath.EvalSymlinks(filepath.Join(base, name))
|
||||
if err != nil {
|
||||
fail(w, 404, "NOT_FOUND")
|
||||
return
|
||||
}
|
||||
rootRel, err := filepath.Rel(base, root)
|
||||
if err != nil || rootRel == ".." || strings.HasPrefix(rootRel, ".."+string(os.PathSeparator)) || filepath.IsAbs(rootRel) {
|
||||
fail(w, 403, "FORBIDDEN")
|
||||
return
|
||||
}
|
||||
path, err := filepath.EvalSymlinks(filepath.Join(root, "latest"))
|
||||
if err != nil {
|
||||
fail(w, 404, "NOT_FOUND")
|
||||
return
|
||||
}
|
||||
rel, err := filepath.Rel(root, path)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) || filepath.IsAbs(rel) {
|
||||
fail(w, 403, "FORBIDDEN")
|
||||
return
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
fail(w, 404, "NOT_FOUND")
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
info, err := f.Stat()
|
||||
if err != nil || !info.Mode().IsRegular() {
|
||||
fail(w, 400, "INVALID_INPUT")
|
||||
return
|
||||
}
|
||||
truncated := info.Size() > 65536
|
||||
if truncated {
|
||||
if _, err = f.Seek(-65536, io.SeekEnd); err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
content, err := io.ReadAll(io.LimitReader(f, 65536))
|
||||
if err != nil {
|
||||
a.internal(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, map[string]any{"content": strings.ToValidUTF8(string(content), "�"), "truncated": truncated})
|
||||
}
|
||||
func sortedKeys[V any](m map[string]V) []string {
|
||||
keys := make([]string, 0, len(m))
|
||||
for key := range m {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
# MirrorOUC 部署
|
||||
|
||||
当前预览:`http://100.69.2.2:8080`,后台:`/admin`。这是单 Worker 适配版本,使用服务器现有的 tunasync Manager、Worker 和并行 rsync 配置,不重新安装或替换同步引擎。
|
||||
|
||||
## 运行结构
|
||||
|
||||
| 服务 | 位置与用途 |
|
||||
| --- | --- |
|
||||
| Nginx 预览 | 只监听 `100.69.2.2:8080`;原有 80 端口站点保持原配置 |
|
||||
| mirrorouc.service | `mirrorouc` 普通用户,API 与静态文件监听 `127.0.0.1:8081` |
|
||||
| mirrorouc-config.service | root 受限配置写入器,Unix socket 只允许 `mirrorouc` 组访问 |
|
||||
| mirrorouc-metrics.timer | 每 10 秒采集 Linux CPU/内存、NFS `/data` I/O 和磁盘容量 |
|
||||
| mirrorouc-counters.service | 添加独立 nftables 计数表,只计数,不修改已有规则、不丢弃数据包 |
|
||||
| mirrorouc-speeds.service | 每约 2 秒读取 TCP 接收计数,按仓库汇总并行连接 |
|
||||
| tunasync | 继续使用已有 Manager `127.0.0.1:12345` 与 Worker `srv-software-mirror` |
|
||||
|
||||
代码发布目录 `/opt/mirrorouc/releases/<release>`;`/opt/mirrorouc/current` 指向当前版本。业务数据库和上传图片保存在本地磁盘 `/var/lib/mirrorouc/`,不要放在 NFS 上。监控历史位于其 `metrics/history.json`,保留最近 24 小时;刚启动时没有完整 24 小时历史。
|
||||
|
||||
## 首次安装/更新
|
||||
|
||||
本地 Node.js 22.18+ 与 Go 1.27+:
|
||||
|
||||
```sh
|
||||
npm ci
|
||||
npm run check:i18n
|
||||
npm run check:monitoring
|
||||
npm run build
|
||||
cd backend
|
||||
go test ./...
|
||||
go vet ./...
|
||||
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -o ../mirrorouc .
|
||||
```
|
||||
|
||||
把 `mirrorouc`、`dist/` 和 `deploy/` 放进新的 `/opt/mirrorouc/releases/<release>/`,确认二进制可执行。在 Debian 服务器上执行:
|
||||
|
||||
```sh
|
||||
python3 /opt/mirrorouc/releases/<release>/deploy/test_config_helper.py
|
||||
python3 /opt/mirrorouc/releases/<release>/deploy/test_job_speeds.py
|
||||
sh /opt/mirrorouc/releases/<release>/deploy/install-metrics.sh
|
||||
sh /opt/mirrorouc/releases/<release>/deploy/install-preview.sh /opt/mirrorouc/releases/<release>
|
||||
systemctl status mirrorouc mirrorouc-config mirrorouc-metrics.timer
|
||||
curl http://127.0.0.1:8081/healthz
|
||||
nginx -t
|
||||
```
|
||||
|
||||
安装脚本针对当前服务器路径。迁移到其他服务器时,应先调整 `nginx-preview.conf`、环境文件、配置写入器的固定路径与 Manager 地址。脚本不会清理旧发布目录或删除镜像文件。
|
||||
|
||||
首次安装会创建管理员 `admin`,随机密码写入 root 才能读取的 `/etc/mirrorouc/initial-admin-password`。本次部署的本地凭据文件为被 Git 忽略的 `.local/deployment/admin-credentials.json`。后台「账户」可修改当前密码,修改后所有旧会话失效。此密码文件不会跟随密码修改而更新。
|
||||
|
||||
环境配置文件 `/etc/mirrorouc/mirrorouc.env` 不随发布覆盖,权限 0600。正式启用 HTTPS 后将 `MIRROROUC_SECURE_COOKIES=true` 并重启服务;当前 Tailnet 预览通过 HTTP 访问,故为 false。登录 Cookie 为 HttpOnly、SameSite=Strict,有效期 12 小时;写入接口另需会话 CSRF Token 与同源请求。
|
||||
|
||||
## 配置发布和权限
|
||||
|
||||
Web 服务不能直接写 `/etc/tunasync`。配置写入器固定只接受 `/etc/tunasync/worker.conf` 和 `/etc/tunasync/mirrors.d/*.conf`:
|
||||
|
||||
- 全局只可修改 concurrent、interval、retry、timeout。
|
||||
- 已有镜像只可修改 upstream、interval、retry、timeout。自定义 command、rsync_options、provider 等字段必须保留。
|
||||
- 新增只支持 rsync/two-stage-rsync,文件名固定为 `mirrorouc-<name>.conf`;不接受任意 Shell 命令。
|
||||
- 编辑前后均验证 TOML、数值、上游地址和文件内容哈希。产生冲突时拒绝覆盖。
|
||||
- 先生成可阅读的完整文件前后预览,再由管理员确认应用;应用失败恢复原文件并尝试重新加载。
|
||||
- SQLite 保留变更版本与审计记录;应用过的变更可以恢复至变更前,若文件已被其他修改覆盖则拒绝恢复。
|
||||
- 写入器保留待完成事务日志 `/var/lib/mirrorouc-config/pending.json`;若进程在写入中退出,下次启动恢复原文件。若恢复失败则拒绝继续写入,需要运维检查该文件与 Worker 状态。
|
||||
|
||||
Manager 返回成功表示 reload 命令已提交,并不等于 Worker 已完成重载,需查看 Worker 服务和任务状态。已有仓库禁用后用「开始同步」重新启动;tunasync 不提供单独的 enable 命令。
|
||||
|
||||
## 监控口径
|
||||
|
||||
HTTPS/rsync 使用 TCP 443/873 的 IPv4、IPv6 收发字节计数,双向合计,排除回环接口。8080/80 端口的 HTTP 预览流量不计入 HTTPS,也不展示 HTTP 曲线。没有经过这些端口的流量不会被推算进对应协议。没有有效采样时显示等待采集。
|
||||
|
||||
`/status` 同步明细的「拉取速度」来自 Linux TCP_INFO 的 `bytes_received`,通过 `ss -tinpeH` 读取,关联 `tunasync-worker.service` 的进程与仓库目标路径,并追溯 git/ssh 等辅助进程的父进程。多个 rsync 子进程共享同一个 socket 时按 inode/socket cookie 去重,多个并行连接求和;约每 2 秒采样并刷新,单位 B/s、KB/s、MB/s、GB/s。只计算 TCP 接收,不把上传量、NFS I/O 或磁盘写入速度当作拉取速度。
|
||||
|
||||
这是采样窗口的接收速率,采样间建立并关闭的短连接可能漏记,不作为精确流量计费数据。首次采样不使用连接生存期累计值冒充瞬时速度。仅同步中的仓库显示数值;有效采样中的零表示当时没有收到数据,未同步/采样失败/数据过期时显示「—」。数据超过 15 秒即失效。相关原始字段见 [Linux TCP_INFO](https://github.com/torvalds/linux/blob/master/include/uapi/linux/tcp.h) 和 [iproute2 ss](https://github.com/iproute2/iproute2/blob/main/misc/ss.c)。
|
||||
|
||||
NFS I/O 来自 `/proc/self/mountstats` 的 `/data` 挂载,磁盘容量来自 statvfs;CPU、内存来自 `/proc`。采样历史仅覆盖采集器启用后的时间,不补造旧数据。图表最多展示 241 个采样点,均值、当前值、峰值从当前窗口全部原始有效采样计算。
|
||||
|
||||
镜像文件由 Nginx 直接提供,API 不读取整个仓库;镜像目录响应设置 CSP sandbox 和 nosniff,避免仓库中的 HTML/脚本获得管理页面同源权限。
|
||||
|
||||
## 备份与回退
|
||||
|
||||
日常备份包括 SQLite、`uploads/`、`/etc/mirrorouc/` 和 `/etc/tunasync/`。数据库运行时使用 SQLite backup API 或 `sqlite3 ... '.backup ...'`;直接复制数据库文件前先停止 `mirrorouc.service`,同时处理 WAL 文件。备份文件包含账户哈希、会话和可能带内部地址的配置,应限制读取权限。
|
||||
|
||||
应用版本回退:将 `/opt/mirrorouc/current` 指回旧发布目录,再 `systemctl restart mirrorouc`。本次新增数据库表使用 `CREATE TABLE IF NOT EXISTS`;未来若出现不兼容数据库迁移,需要同时恢复对应数据库备份。
|
||||
|
||||
只撤下预览时,删除 `/etc/nginx/sites-enabled/mirrorouc-preview`,通过 `nginx -t` 后 reload Nginx,并停止 `mirrorouc.service` 与 `mirrorouc-config.service`。这不会修改原有 80 端口镜像站点。停止新增采集器不会影响 tunasync;若需要删除计数表,只删除 `inet mirrorouc_monitor`,不要 flush 全局 nftables 规则。
|
||||
|
||||
## 验证范围
|
||||
|
||||
`backend/*_test.go` 使用临时数据库与假的 Manager,覆盖鉴权、角色、CSRF、草稿隔离、并发冲突、任务提交、配置应用/恢复、上游不可用缓存、图片校验及监控统计。`test_config_helper.py` 独立验证写入白名单、自定义命令保留、注入拒绝与失败恢复,不操作真实同步配置。
|
||||
|
||||
本次部署核验真实 API、镜像目录、日志、监控与后台草稿保存。没有为验证效果而停止、重启仓库,或应用修改后的生产同步参数。
|
||||
|
||||
## 统一中文内容升级
|
||||
|
||||
公告的 title/body 和镜像的 help 已改为单个字符串,所有界面语言共用中文内容。旧版本的语言映射在服务启动时以事务迁移:优先保留 zh-CN,其次 zh;没有中文键的历史记录保留原内容供管理员整理。原始 JSON 保存在 SQLite 的 shared_content_backups 表,迁移会递增记录 revision,避免旧编辑页面覆盖新内容;重复启动不会重复迁移。
|
||||
|
||||
此次内容格式升级需要在回退旧应用时同时恢复升级前的数据库备份。部署前使用 SQLite backup API 生成一致性快照,备份目录仅 root 可读。迁移不修改镜像介绍、站点文案或同步配置。
|
||||
|
||||
## 首页相关链接
|
||||
|
||||
`settings/site` 的 `relatedLinks` 独立控制首页侧栏,与页脚 `links` 分开。旧设置缺少该字段时,接口补充原有的三个入口,不改写数据库。显式保存空数组会隐藏该栏目;仍打开旧版表单的客户端提交时,若未携带该字段,后端保留当前已保存的相关链接。条目支持 `kind=help` 的站内使用帮助与 `kind=url` 的 HTTP(S)/站内绝对路径;不接受脚本、data URL、协议相对地址或反斜杠路径。名称支持多语言,数组顺序即显示顺序。
|
||||
@@ -0,0 +1,124 @@
|
||||
#!/usr/bin/python3
|
||||
"""Sample per-socket TCP receive counters and attribute worker processes to mirrors."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
PROC = Path('/proc')
|
||||
MIRRORS = '/data/mirrors/'
|
||||
OUTPUT = Path('/var/lib/mirrorouc/metrics/job-speeds.json')
|
||||
NAME = re.compile(r'^[a-zA-Z0-9][a-zA-Z0-9_.+-]{0,127}$')
|
||||
|
||||
def mirror_name(value):
|
||||
if not value.startswith(MIRRORS):
|
||||
return None
|
||||
name = value[len(MIRRORS):].split('/')[0]
|
||||
return name if NAME.fullmatch(name) and name not in ('.', '..') else None
|
||||
|
||||
def process_jobs(proc=PROC):
|
||||
jobs = {}
|
||||
parents = {}
|
||||
for entry in proc.iterdir():
|
||||
if not entry.name.isdigit():
|
||||
continue
|
||||
try:
|
||||
if '/tunasync-worker.service' not in (entry / 'cgroup').read_text():
|
||||
continue
|
||||
pid = int(entry.name)
|
||||
status = (entry / 'stat').read_text().rsplit(') ', 1)[1].split()
|
||||
parents[pid] = int(status[1])
|
||||
arguments = (entry / 'cmdline').read_bytes().decode(errors='replace').split('\0')
|
||||
candidates = {name for argument in arguments if (name := mirror_name(argument))}
|
||||
if not candidates:
|
||||
name = mirror_name(os.readlink(entry / 'cwd'))
|
||||
if name:
|
||||
candidates.add(name)
|
||||
if len(candidates) == 1:
|
||||
jobs[pid] = candidates.pop()
|
||||
except (OSError, ValueError, IndexError):
|
||||
continue
|
||||
# git/ssh/curl helpers may inherit a job's parent without its destination argument.
|
||||
for pid in parents:
|
||||
ancestor = pid
|
||||
for _ in range(16):
|
||||
if ancestor in jobs:
|
||||
jobs[pid] = jobs[ancestor]
|
||||
break
|
||||
ancestor = parents.get(ancestor, 0)
|
||||
if not ancestor:
|
||||
break
|
||||
return jobs
|
||||
|
||||
def parse_sockets(output, jobs):
|
||||
sockets = {}
|
||||
header = None
|
||||
for line in output.splitlines():
|
||||
if line and not line[0].isspace():
|
||||
header = line
|
||||
continue
|
||||
if header is None:
|
||||
continue
|
||||
identity = re.search(r'\bino:(\d+)\s+sk:([a-fA-F0-9]+)', header)
|
||||
if not identity:
|
||||
continue
|
||||
pids = [int(value) for value in re.findall(r'\bpid=(\d+)', header)]
|
||||
names = {jobs[pid] for pid in pids if pid in jobs}
|
||||
received = re.search(r'\bbytes_received:(\d+)', line)
|
||||
# The same inherited socket can list several PIDs: count its inode/cookie once.
|
||||
sockets[':'.join(identity.groups())] = {'bytes': int(received.group(1)) if received else 0, 'job': names.pop() if len(names) == 1 else None}
|
||||
header = None
|
||||
return sockets
|
||||
|
||||
def rates(current, previous, active, elapsed):
|
||||
result = {name: {'bytesPerSecond': 0.0, 'connections': 0} for name in active}
|
||||
if previous is None or elapsed <= 0 or elapsed > 15:
|
||||
return {}
|
||||
for identity, socket in current.items():
|
||||
name = socket['job']
|
||||
if name is None:
|
||||
continue
|
||||
old = previous.get(identity)
|
||||
delta = socket['bytes'] - old['bytes'] if old else socket['bytes']
|
||||
if delta < 0: # A reset/reused socket must never produce a negative rate/spike.
|
||||
continue
|
||||
value = result.setdefault(name, {'bytesPerSecond': 0.0, 'connections': 0})
|
||||
value['bytesPerSecond'] += delta / elapsed
|
||||
value['connections'] += 1
|
||||
return result
|
||||
|
||||
def write(value):
|
||||
fd, temporary = tempfile.mkstemp(prefix='.speeds-', dir=OUTPUT.parent)
|
||||
try:
|
||||
with os.fdopen(fd, 'w') as stream:
|
||||
json.dump(value, stream, allow_nan=False, separators=(',', ':'))
|
||||
os.chmod(temporary, 0o640)
|
||||
os.replace(temporary, OUTPUT)
|
||||
finally:
|
||||
if os.path.exists(temporary):
|
||||
os.unlink(temporary)
|
||||
|
||||
def run():
|
||||
previous = None
|
||||
last = time.monotonic()
|
||||
while True:
|
||||
start = time.monotonic()
|
||||
try:
|
||||
jobs = process_jobs()
|
||||
result = subprocess.run(['/usr/bin/ss', '-tinpeH', 'state', 'established'], capture_output=True, text=True, timeout=4, check=True, env={**os.environ, 'LC_ALL': 'C'})
|
||||
current = parse_sockets(result.stdout, jobs)
|
||||
sampled = time.monotonic()
|
||||
elapsed = sampled - last
|
||||
values = rates(current, previous, set(jobs.values()), elapsed)
|
||||
write({'updatedAt': round(time.time() * 1000), 'interval': elapsed, 'available': previous is not None and elapsed <= 15, 'jobs': values})
|
||||
previous, last = current, sampled
|
||||
except (OSError, ValueError, subprocess.SubprocessError):
|
||||
previous = None
|
||||
write({'updatedAt': round(time.time() * 1000), 'interval': 2, 'available': False, 'jobs': {}})
|
||||
time.sleep(max(.1, 2 - (time.monotonic() - start)))
|
||||
|
||||
if __name__ == '__main__':
|
||||
run()
|
||||
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/python3
|
||||
"""Read Linux/NFS counters. A separate passive nftables table supplies protocol bytes."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
ROOT = Path(os.environ.get('MIRROROUC_METRICS_ROOT', '/var/lib/mirrorouc/metrics'))
|
||||
ROOT.mkdir(parents=True, exist_ok=True)
|
||||
path = ROOT / 'history.json'
|
||||
try:
|
||||
history = json.loads(path.read_text())
|
||||
except (OSError, ValueError):
|
||||
history = {'samples': []}
|
||||
|
||||
now = time.time()
|
||||
cpu = list(map(int, Path('/proc/stat').read_text().splitlines()[0].split()[1:9]))
|
||||
mem = {line.split(':')[0]: int(line.split()[1]) * 1024 for line in Path('/proc/meminfo').read_text().splitlines() if len(line.split()) > 1}
|
||||
nfs = {}
|
||||
for block in Path('/proc/self/mountstats').read_text().split('device '):
|
||||
if ' mounted on /data ' not in block:
|
||||
continue
|
||||
for line in block.splitlines():
|
||||
values = line.strip().split()
|
||||
if values and values[0] in ('READ:', 'WRITE:'):
|
||||
nfs[values[0][:-1]] = list(map(int, values[1:]))
|
||||
|
||||
counters = {}
|
||||
try:
|
||||
result = subprocess.run(['/usr/sbin/nft', '-j', 'list', 'counters', 'table', 'inet', 'mirrorouc_monitor'], capture_output=True, timeout=4, check=True)
|
||||
for item in json.loads(result.stdout).get('nftables', []):
|
||||
counter = item.get('counter', {})
|
||||
if counter.get('name') in ('https4', 'https6', 'rsync4', 'rsync6'):
|
||||
counters[counter['name']] = counter['bytes']
|
||||
except (OSError, ValueError, subprocess.SubprocessError):
|
||||
pass
|
||||
|
||||
previous = history.get('previous')
|
||||
sample = {'time': round(now * 1000)}
|
||||
if previous:
|
||||
elapsed = max(.1, now - previous['time'])
|
||||
delta = [max(0, a - b) for a, b in zip(cpu, previous['cpu'])]
|
||||
total = sum(delta)
|
||||
if total:
|
||||
sample.update(user=(delta[0] + delta[1]) / total * 100, system=(delta[2] + delta[5] + delta[6] + delta[7]) / total * 100, wait=delta[4] / total * 100, idle=delta[3] / total * 100)
|
||||
for key, value in counters.items():
|
||||
old = previous.get('counters', {}).get(key)
|
||||
if old is not None and value >= old:
|
||||
sample[key] = (value - old) * 8 / elapsed / 1e9
|
||||
for operation, prefix in [('READ', 'read'), ('WRITE', 'write')]:
|
||||
a, b = nfs.get(operation), previous.get('nfs', {}).get(operation)
|
||||
if a and b and a[0] >= b[0]:
|
||||
sample[prefix + 'Ops'] = (a[0] - b[0]) / elapsed / 1000
|
||||
byte_index = 4 if operation == 'READ' else 3
|
||||
sample[prefix + 'Bytes'] = max(0, a[byte_index] - b[byte_index]) / elapsed / 1e9
|
||||
cached = max(0, mem.get('Cached', 0) + mem.get('SReclaimable', 0) - mem.get('Shmem', 0))
|
||||
free, buffers, total_mem = mem['MemFree'], mem.get('Buffers', 0), mem['MemTotal']
|
||||
used = max(0, total_mem - free - cached - buffers)
|
||||
sample.update(used=used / 2**30, cached=cached / 2**30, buffers=buffers / 2**30, free=free / 2**30)
|
||||
if 'user' in sample:
|
||||
history['samples'].append(sample)
|
||||
|
||||
disks = []
|
||||
for mount in ['/data', '/']:
|
||||
try:
|
||||
disk = os.statvfs(mount)
|
||||
disks.append({'name': mount, 'total': disk.f_blocks * disk.f_frsize / 1e12, 'used': (disk.f_blocks - disk.f_bfree) * disk.f_frsize / 1e12})
|
||||
except OSError:
|
||||
pass
|
||||
history.update(previous={'time': now, 'cpu': cpu, 'nfs': nfs, 'counters': counters}, updatedAt=round(now * 1000), memoryTotal=mem['MemTotal'] / 2**30, disks=disks)
|
||||
history['samples'] = [point for point in history['samples'] if point['time'] >= (now - 86400) * 1000][-8640:]
|
||||
fd, temporary = tempfile.mkstemp(dir=ROOT, prefix='.metrics-')
|
||||
try:
|
||||
with os.fdopen(fd, 'w') as output:
|
||||
json.dump(history, output, allow_nan=False, separators=(',', ':'))
|
||||
output.flush()
|
||||
os.fsync(output.fileno())
|
||||
os.chmod(temporary, 0o640)
|
||||
os.replace(temporary, path)
|
||||
finally:
|
||||
if os.path.exists(temporary):
|
||||
os.unlink(temporary)
|
||||
@@ -0,0 +1,178 @@
|
||||
#!/usr/bin/python3
|
||||
"""Root-only configuration writer, reachable only through a group-restricted Unix socket.
|
||||
|
||||
Independently verifies allowed TOML changes; never executes submitted commands.
|
||||
"""
|
||||
import copy
|
||||
import glob
|
||||
import grp
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import socketserver
|
||||
import tempfile
|
||||
import tomllib
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
ROOT = '/etc/tunasync'
|
||||
MAIN = ROOT + '/worker.conf'
|
||||
INCLUDES = ROOT + '/mirrors.d'
|
||||
SOCKET = '/run/mirrorouc-config/control.sock'
|
||||
JOURNAL = '/var/lib/mirrorouc-config/pending.json'
|
||||
IDENTIFIER = re.compile(r'^[a-zA-Z0-9][a-zA-Z0-9_.+-]{0,127}$')
|
||||
|
||||
def number(value, lower, upper):
|
||||
return type(value) is int and lower <= value <= upper
|
||||
|
||||
def upstream(value):
|
||||
if not isinstance(value, str) or len(value) >= 2048 or re.search(r'[\s`;$|<>\\\"\']', value):
|
||||
return False
|
||||
parsed = urllib.parse.urlsplit(value)
|
||||
return parsed.scheme in ('rsync', 'https', 'http') and bool(parsed.hostname) and parsed.username is None
|
||||
|
||||
def sync_numbers(table):
|
||||
return all(key not in table or number(table[key], low, high) for key, low, high in [('interval', 1, 525600), ('retry', 0, 20), ('timeout', 30, 1209600)])
|
||||
|
||||
def stripped_mirrors(doc):
|
||||
result = copy.deepcopy(doc)
|
||||
mirrors = result.get('mirrors', [])
|
||||
assert isinstance(mirrors, list)
|
||||
for mirror in mirrors:
|
||||
for field in ('upstream', 'interval', 'retry', 'timeout'):
|
||||
mirror.pop(field, None)
|
||||
return result
|
||||
|
||||
def validate_new(doc, path):
|
||||
assert set(doc) == {'mirrors'} and len(doc['mirrors']) == 1
|
||||
mirror = doc['mirrors'][0]
|
||||
assert set(mirror) <= {'name', 'provider', 'upstream', 'interval', 'retry', 'timeout', 'stage1_profile'}
|
||||
assert IDENTIFIER.fullmatch(mirror['name']) and mirror['name'] not in ('.', '..')
|
||||
assert path == INCLUDES + '/mirrorouc-' + mirror['name'] + '.conf'
|
||||
assert mirror['provider'] in ('rsync', 'two-stage-rsync')
|
||||
assert upstream(mirror['upstream']) and sync_numbers(mirror)
|
||||
assert 'stage1_profile' not in mirror or mirror['stage1_profile'] == 'debian'
|
||||
|
||||
def validate(path, before, after):
|
||||
assert path == MAIN or (os.path.dirname(path) == INCLUDES and os.path.basename(path).endswith('.conf'))
|
||||
assert not os.path.islink(path)
|
||||
old = tomllib.loads(before) if before is not None else None
|
||||
new = tomllib.loads(after) if after is not None else None
|
||||
assert old is not None or new is not None
|
||||
if path == MAIN:
|
||||
assert old is not None and new is not None
|
||||
assert number(new['global'].get('concurrent'), 1, 64) and sync_numbers(new['global'])
|
||||
for doc in (old, new):
|
||||
for field in ('concurrent', 'interval', 'retry', 'timeout'):
|
||||
doc['global'].pop(field, None)
|
||||
assert old == new
|
||||
elif old is None or new is None:
|
||||
validate_new(new if new is not None else old, path)
|
||||
else:
|
||||
assert stripped_mirrors(old) == stripped_mirrors(new)
|
||||
for previous, current in zip(old.get('mirrors', []), new.get('mirrors', [])):
|
||||
if previous != current:
|
||||
assert upstream(current['upstream']) and sync_numbers(current)
|
||||
|
||||
def read(path):
|
||||
try:
|
||||
with open(path, encoding='utf-8', newline='') as stream:
|
||||
return stream.read()
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
|
||||
def atomic(path, value):
|
||||
if value is None:
|
||||
os.unlink(path)
|
||||
return
|
||||
mode = os.stat(path).st_mode & 0o777 if os.path.exists(path) else 0o644
|
||||
fd, temporary = tempfile.mkstemp(prefix='.mirrorouc-', dir=os.path.dirname(path))
|
||||
try:
|
||||
with os.fdopen(fd, 'w', encoding='utf-8', newline='') as stream:
|
||||
stream.write(value)
|
||||
stream.flush()
|
||||
os.fsync(stream.fileno())
|
||||
os.chmod(temporary, mode)
|
||||
os.replace(temporary, path)
|
||||
finally:
|
||||
if os.path.exists(temporary):
|
||||
os.unlink(temporary)
|
||||
|
||||
def reload_worker():
|
||||
worker = tomllib.loads(read(MAIN))['global']['name']
|
||||
body = json.dumps({'worker_id': worker, 'mirror_id': '', 'cmd': 'reload', 'args': [], 'options': {}}).encode()
|
||||
request = urllib.request.Request('http://127.0.0.1:12345/cmd', data=body, headers={'Content-Type': 'application/json'})
|
||||
with urllib.request.urlopen(request, timeout=10) as response:
|
||||
assert response.status == 200
|
||||
|
||||
def signature():
|
||||
pattern = tomllib.loads(read(MAIN)).get('include', {}).get('include_mirrors', '')
|
||||
assert pattern == INCLUDES + '/*.conf'
|
||||
names = sorted(set([MAIN] + glob.glob(pattern)))
|
||||
digest = hashlib.sha256()
|
||||
for path in names:
|
||||
assert not os.path.islink(path)
|
||||
raw = read(path).encode()
|
||||
digest.update(str(len(path.encode())).encode() + b':' + path.encode() + str(len(raw)).encode() + b':' + raw)
|
||||
return digest.hexdigest()
|
||||
|
||||
def restore(changes):
|
||||
for path, change in changes.items():
|
||||
if change['before'] is None and not os.path.exists(path):
|
||||
continue
|
||||
atomic(path, change['before'])
|
||||
reload_worker()
|
||||
|
||||
def apply(request):
|
||||
changes = request['files']
|
||||
assert isinstance(changes, dict) and 0 < len(changes) <= 20
|
||||
for path, change in changes.items():
|
||||
validate(path, change['before'], change['after'])
|
||||
if read(path) != change['before']:
|
||||
return 'CONFLICT'
|
||||
if signature() != request['baseHash']:
|
||||
return 'CONFLICT'
|
||||
# Write-ahead journal survives a helper/process restart during a multi-file update.
|
||||
atomic(JOURNAL, json.dumps(changes))
|
||||
try:
|
||||
for path, change in changes.items():
|
||||
atomic(path, change['after'])
|
||||
reload_worker()
|
||||
except Exception:
|
||||
try:
|
||||
restore(changes)
|
||||
except Exception:
|
||||
return 'CONFIG_ROLLBACK_FAILED'
|
||||
os.unlink(JOURNAL)
|
||||
return 'CONFIG_APPLY_FAILED'
|
||||
os.unlink(JOURNAL)
|
||||
return ''
|
||||
|
||||
class Handler(socketserver.StreamRequestHandler):
|
||||
def handle(self):
|
||||
self.connection.settimeout(5)
|
||||
try:
|
||||
raw = self.rfile.readline(2 * 1024 * 1024 + 1)
|
||||
assert len(raw) <= 2 * 1024 * 1024 and raw.endswith(b'\n')
|
||||
if os.path.exists(JOURNAL):
|
||||
code = 'CONFIG_ROLLBACK_FAILED'
|
||||
else:
|
||||
code = apply(json.loads(raw))
|
||||
except (AssertionError, KeyError, TypeError, ValueError):
|
||||
code = 'INVALID_INPUT'
|
||||
except Exception:
|
||||
code = 'CONFIG_APPLY_FAILED'
|
||||
self.wfile.write(json.dumps({'code': code}).encode() + b'\n')
|
||||
|
||||
if __name__ == '__main__':
|
||||
if os.path.exists(JOURNAL):
|
||||
with open(JOURNAL) as stream:
|
||||
restore(json.load(stream))
|
||||
os.unlink(JOURNAL)
|
||||
if os.path.exists(SOCKET):
|
||||
os.unlink(SOCKET)
|
||||
with socketserver.UnixStreamServer(SOCKET, Handler) as server:
|
||||
os.chown(SOCKET, 0, grp.getgrnam('mirrorouc').gr_gid)
|
||||
os.chmod(SOCKET, 0o660)
|
||||
server.serve_forever()
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
if ! /usr/sbin/nft list table inet mirrorouc_monitor >/dev/null 2>&1; then
|
||||
/usr/sbin/nft -f /etc/mirrorouc/protocol-counters.nft
|
||||
fi
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")"
|
||||
if ! id mirrorouc >/dev/null 2>&1; then
|
||||
useradd --system --home-dir /var/lib/mirrorouc --shell /usr/sbin/nologin mirrorouc
|
||||
fi
|
||||
install -d -o mirrorouc -g mirrorouc -m 0700 /var/lib/mirrorouc
|
||||
install -d -o root -g mirrorouc -m 0750 /var/lib/mirrorouc/metrics
|
||||
install -d -o root -g root -m 0755 /usr/local/lib/mirrorouc
|
||||
install -d -o root -g root -m 0755 /etc/mirrorouc
|
||||
install -m 0644 collect_metrics.py /usr/local/lib/mirrorouc/collect_metrics.py
|
||||
install -m 0644 collect_job_speeds.py /usr/local/lib/mirrorouc/collect_job_speeds.py
|
||||
install -m 0755 ensure-counters.sh /usr/local/lib/mirrorouc/ensure-counters.sh
|
||||
install -m 0644 protocol-counters.nft /etc/mirrorouc/protocol-counters.nft
|
||||
install -m 0644 mirrorouc-counters.service mirrorouc-metrics.service mirrorouc-metrics.timer mirrorouc-speeds.service /etc/systemd/system/
|
||||
if ! /usr/sbin/nft list table inet mirrorouc_monitor >/dev/null 2>&1; then
|
||||
/usr/sbin/nft -c -f /etc/mirrorouc/protocol-counters.nft
|
||||
fi
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now mirrorouc-counters.service mirrorouc-metrics.timer
|
||||
systemctl start mirrorouc-metrics.service
|
||||
systemctl enable --now mirrorouc-speeds.service
|
||||
systemctl restart mirrorouc-speeds.service
|
||||
@@ -0,0 +1,46 @@
|
||||
#!/bin/sh
|
||||
# Run as root: sh deploy/install-preview.sh /opt/mirrorouc/releases/<release>
|
||||
set -eu
|
||||
release=$(readlink -f "$1")
|
||||
case "$release" in /opt/mirrorouc/releases/*) ;; *) echo 'Invalid release path' >&2; exit 1;; esac
|
||||
test -x "$release/mirrorouc"
|
||||
test -f "$release/dist/index.html"
|
||||
deploy="$release/deploy"
|
||||
id mirrorouc >/dev/null 2>&1 || useradd --system --home-dir /var/lib/mirrorouc --shell /usr/sbin/nologin mirrorouc
|
||||
install -d -m 0700 -o mirrorouc -g mirrorouc /var/lib/mirrorouc
|
||||
install -d -m 0755 /etc/mirrorouc /usr/local/lib/mirrorouc
|
||||
install -d -m 0700 /var/lib/mirrorouc-config
|
||||
install -m 0755 "$deploy/config-helper.py" /usr/local/lib/mirrorouc/config-helper.py
|
||||
install -m 0644 "$deploy/mirrorouc.service" "$deploy/mirrorouc-config.service" /etc/systemd/system/
|
||||
if [ ! -f /etc/mirrorouc/mirrorouc.env ]; then
|
||||
umask 077
|
||||
cat > /etc/mirrorouc/mirrorouc.env <<'EOF'
|
||||
MIRROROUC_LISTEN=127.0.0.1:8081
|
||||
MIRROROUC_DATA=/var/lib/mirrorouc
|
||||
MIRROROUC_STATIC=/opt/mirrorouc/current/dist
|
||||
MIRROROUC_MANAGER=http://127.0.0.1:12345
|
||||
MIRROROUC_WORKER=srv-software-mirror
|
||||
MIRROROUC_WORKER_CONFIG=/etc/tunasync/worker.conf
|
||||
MIRROROUC_LOGS=/var/log/tunasync
|
||||
MIRROROUC_METRICS=/var/lib/mirrorouc/metrics/history.json
|
||||
MIRROROUC_CONFIG_SOCKET=/run/mirrorouc-config/control.sock
|
||||
MIRROROUC_SECURE_COOKIES=false
|
||||
EOF
|
||||
fi
|
||||
# Leave current tunasync configurations, ownership and permissions intact.
|
||||
if [ ! -s /var/lib/mirrorouc/mirrorouc.db ]; then
|
||||
umask 077
|
||||
runuser -u mirrorouc -- "$release/mirrorouc" -data /var/lib/mirrorouc -init-admin admin > /etc/mirrorouc/initial-admin-password
|
||||
fi
|
||||
ln -sfn "$release" /opt/mirrorouc/current.new
|
||||
mv -Tf /opt/mirrorouc/current.new /opt/mirrorouc/current
|
||||
install -m 0644 "$deploy/nginx-preview.conf" /etc/nginx/sites-available/mirrorouc-preview
|
||||
ln -sfn /etc/nginx/sites-available/mirrorouc-preview /etc/nginx/sites-enabled/mirrorouc-preview
|
||||
nginx -t
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now mirrorouc-config.service
|
||||
systemctl restart mirrorouc-config.service
|
||||
systemctl enable mirrorouc.service
|
||||
systemctl restart mirrorouc.service
|
||||
systemctl reload nginx
|
||||
echo 'MirrorOUC preview installed at http://100.69.2.2:8080'
|
||||
@@ -0,0 +1,25 @@
|
||||
[Unit]
|
||||
Description=MirrorOUC restricted tunasync configuration writer
|
||||
After=tunasync-manager.service
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/bin/python3 /usr/local/lib/mirrorouc/config-helper.py
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
RuntimeDirectory=mirrorouc-config
|
||||
RuntimeDirectoryMode=0755
|
||||
StateDirectory=mirrorouc-config
|
||||
StateDirectoryMode=0700
|
||||
UMask=0077
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
ReadWritePaths=/etc/tunasync /var/lib/mirrorouc-config /run/mirrorouc-config
|
||||
ProtectKernelTunables=true
|
||||
ProtectControlGroups=true
|
||||
RestrictSUIDSGID=true
|
||||
RestrictAddressFamilies=AF_UNIX AF_INET
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,9 @@
|
||||
[Unit]
|
||||
Description=MirrorOUC passive protocol byte counters
|
||||
After=nftables.service
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/usr/local/lib/mirrorouc/ensure-counters.sh
|
||||
RemainAfterExit=yes
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,13 @@
|
||||
[Unit]
|
||||
Description=MirrorOUC read-only host metrics collection
|
||||
[Service]
|
||||
Type=oneshot
|
||||
User=root
|
||||
Group=mirrorouc
|
||||
ExecStart=/usr/bin/python3 /usr/local/lib/mirrorouc/collect_metrics.py
|
||||
UMask=0027
|
||||
NoNewPrivileges=true
|
||||
ProtectSystem=strict
|
||||
ReadWritePaths=/var/lib/mirrorouc/metrics
|
||||
ProtectHome=true
|
||||
PrivateTmp=true
|
||||
@@ -0,0 +1,8 @@
|
||||
[Unit]
|
||||
Description=Collect MirrorOUC metrics every 10 seconds
|
||||
[Timer]
|
||||
OnBootSec=10
|
||||
OnUnitActiveSec=10
|
||||
AccuracySec=1
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
@@ -0,0 +1,22 @@
|
||||
[Unit]
|
||||
Description=MirrorOUC per-mirror upstream TCP receive rates
|
||||
After=tunasync-worker.service
|
||||
|
||||
[Service]
|
||||
User=root
|
||||
Group=mirrorouc
|
||||
ExecStart=/usr/bin/python3 /usr/local/lib/mirrorouc/collect_job_speeds.py
|
||||
Restart=on-failure
|
||||
RestartSec=3
|
||||
UMask=0027
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectHome=true
|
||||
ProtectSystem=strict
|
||||
ReadWritePaths=/var/lib/mirrorouc/metrics
|
||||
ProtectKernelTunables=true
|
||||
ProtectControlGroups=true
|
||||
RestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,26 @@
|
||||
[Unit]
|
||||
Description=MirrorOUC web and administration API
|
||||
After=network.target tunasync-manager.service mirrorouc-config.service
|
||||
Wants=mirrorouc-config.service
|
||||
|
||||
[Service]
|
||||
User=mirrorouc
|
||||
Group=mirrorouc
|
||||
WorkingDirectory=/opt/mirrorouc/current
|
||||
EnvironmentFile=/etc/mirrorouc/mirrorouc.env
|
||||
ExecStart=/opt/mirrorouc/current/mirrorouc
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
UMask=0077
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
ReadWritePaths=/var/lib/mirrorouc
|
||||
ProtectKernelTunables=true
|
||||
ProtectControlGroups=true
|
||||
RestrictSUIDSGID=true
|
||||
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,22 @@
|
||||
server {
|
||||
listen 100.69.2.2:8080;
|
||||
server_name 100.69.2.2;
|
||||
client_max_body_size 5m;
|
||||
|
||||
location /mirrors/ {
|
||||
alias /data/mirrors/;
|
||||
autoindex on;
|
||||
autoindex_exact_size off;
|
||||
autoindex_localtime on;
|
||||
add_header X-Content-Type-Options nosniff always;
|
||||
add_header Content-Security-Policy "sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src 'self' data:" always;
|
||||
}
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:8081;
|
||||
proxy_set_header Host $http_host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_read_timeout 35s;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
# Passive accounting only; no existing rules or tables are flushed.
|
||||
table inet mirrorouc_monitor {
|
||||
counter https4 { }
|
||||
counter https6 { }
|
||||
counter rsync4 { }
|
||||
counter rsync6 { }
|
||||
chain account {
|
||||
meta nfproto ipv4 tcp sport 443 counter name https4
|
||||
meta nfproto ipv4 tcp dport 443 tcp sport != 443 counter name https4
|
||||
meta nfproto ipv6 tcp sport 443 counter name https6
|
||||
meta nfproto ipv6 tcp dport 443 tcp sport != 443 counter name https6
|
||||
meta nfproto ipv4 tcp sport 873 counter name rsync4
|
||||
meta nfproto ipv4 tcp dport 873 tcp sport != 873 counter name rsync4
|
||||
meta nfproto ipv6 tcp sport 873 counter name rsync6
|
||||
meta nfproto ipv6 tcp dport 873 tcp sport != 873 counter name rsync6
|
||||
}
|
||||
chain input {
|
||||
type filter hook input priority 300; policy accept;
|
||||
iifname != "lo" jump account
|
||||
}
|
||||
chain output {
|
||||
type filter hook output priority 300; policy accept;
|
||||
oifname != "lo" jump account
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import copy
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
spec = importlib.util.spec_from_file_location('helper', os.path.join(os.path.dirname(__file__), 'config-helper.py'))
|
||||
helper = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(helper)
|
||||
|
||||
class ValidationTests(unittest.TestCase):
|
||||
def test_preserves_custom_commands(self):
|
||||
before = '[[mirrors]]\nname="debian"\nprovider="rsync"\nupstream="rsync://old/debian"\ncommand="/usr/local/bin/parallel-rsync"\n'
|
||||
after = before.replace('old/debian', 'new/debian') + 'interval=60\nretry=2\ntimeout=120\n'
|
||||
helper.validate(helper.INCLUDES + '/debian.conf', before, after)
|
||||
with self.assertRaises(AssertionError):
|
||||
helper.validate(helper.INCLUDES + '/debian.conf', before, after.replace('parallel-rsync', 'other-command'))
|
||||
|
||||
def test_blocks_other_global_fields_and_paths(self):
|
||||
before = '[global]\nname="worker"\nconcurrent=12\ninterval=60\nretry=2\ntimeout=120\n'
|
||||
helper.validate(helper.MAIN, before, before.replace('concurrent=12', 'concurrent=8'))
|
||||
for path, after in [('/etc/shadow', before), (helper.MAIN, before.replace('worker', 'other')), (helper.MAIN, before.replace('concurrent=12', 'concurrent=0'))]:
|
||||
with self.assertRaises(AssertionError):
|
||||
helper.validate(path, before, after)
|
||||
|
||||
def test_new_mirrors_and_injection(self):
|
||||
value = '[[mirrors]]\nname="test"\nprovider="rsync"\nupstream="rsync://mirror/test"\ninterval=60\nretry=0\ntimeout=120\n'
|
||||
helper.validate_new(helper.tomllib.loads(value), helper.INCLUDES + '/mirrorouc-test.conf')
|
||||
for changed in [value + 'command="evil"\n', value.replace('rsync://mirror/test', 'rsync://mirror/`cmd`'), value.replace('retry=0', 'retry=-1')]:
|
||||
with self.assertRaises(AssertionError):
|
||||
helper.validate_new(helper.tomllib.loads(changed), helper.INCLUDES + '/mirrorouc-test.conf')
|
||||
|
||||
def test_reload_failure_restores_files(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
path = root + '/worker.conf'
|
||||
before = '[global]\nname="worker"\nconcurrent=12\ninterval=60\nretry=2\ntimeout=120\n'
|
||||
helper.atomic(path, before)
|
||||
changes = {path: {'before': before, 'after': before.replace('concurrent=12', 'concurrent=8')}}
|
||||
with patch.object(helper, 'MAIN', path), patch.object(helper, 'JOURNAL', root + '/pending.json'), patch.object(helper, 'signature', return_value='base'), patch.object(helper, 'reload_worker', side_effect=[OSError('unavailable'), None]):
|
||||
self.assertEqual(helper.apply({'files': changes, 'baseHash': 'base'}), 'CONFIG_APPLY_FAILED')
|
||||
self.assertEqual(helper.read(path), before)
|
||||
self.assertFalse(os.path.exists(root + '/pending.json'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,40 @@
|
||||
import importlib.util
|
||||
import os
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
spec = importlib.util.spec_from_file_location('speeds', os.path.join(os.path.dirname(__file__), 'collect_job_speeds.py'))
|
||||
speeds = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(speeds)
|
||||
|
||||
class SpeedTests(unittest.TestCase):
|
||||
def test_sockets_deduplicate_shared_pids_and_separate_repos(self):
|
||||
data = '0 0 10.0.0.1:123 10.0.0.2:873 users:(("rsync",pid=1,fd=3),("rsync",pid=2,fd=3)) uid:9 ino:20 sk:abc <->\n\t cubic bytes_sent:4 bytes_received:1000\n0 0 10.0.0.1:124 10.0.0.2:873 users:(("rsync",pid=3,fd=3)) uid:9 ino:21 sk:abd <->\n\t cubic bytes_received:2000\n'
|
||||
current = speeds.parse_sockets(data, {1: 'debian', 2: 'debian', 3: 'ubuntu'})
|
||||
result = speeds.rates(current, {'20:abc': {'bytes': 400}, '21:abd': {'bytes': 1000}}, {'debian', 'ubuntu'}, 2)
|
||||
self.assertEqual(result['debian'], {'bytesPerSecond': 300, 'connections': 1})
|
||||
self.assertEqual(result['ubuntu']['bytesPerSecond'], 500)
|
||||
|
||||
def test_parallel_and_new_connections_add_without_startup_spike(self):
|
||||
current = {'1:a': {'bytes': 1000, 'job': 'debian'}, '2:b': {'bytes': 500, 'job': 'debian'}, '3:c': {'bytes': 9000, 'job': None}}
|
||||
self.assertEqual(speeds.rates(current, None, {'debian'}, 2), {})
|
||||
result = speeds.rates(current, {'1:a': {'bytes': 800}}, {'debian', 'ubuntu'}, 2)
|
||||
self.assertEqual(result['debian']['bytesPerSecond'], 350)
|
||||
self.assertEqual(result['ubuntu']['bytesPerSecond'], 0)
|
||||
self.assertEqual(speeds.rates(current, {}, {'debian'}, 20), {})
|
||||
|
||||
def test_process_ancestry_and_path_boundaries(self):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
for pid, parent, command in [(1, 0, ['/usr/bin/python', '/worker']), (2, 1, ['rsync', '/data/mirrors/debian/']), (3, 2, ['ssh', 'host']), (4, 0, ['rsync', '/data/mirrors/ubuntu/'])]:
|
||||
path = Path(root) / str(pid); path.mkdir()
|
||||
(path/'stat').write_text(f'{pid} (process) S {parent} 0 0')
|
||||
(path/'cgroup').write_text('0::/system.slice/tunasync-worker.service' if pid != 4 else '0::/system.slice/other.service')
|
||||
(path/'cmdline').write_bytes('\0'.join(command).encode())
|
||||
jobs = speeds.process_jobs(Path(root))
|
||||
self.assertEqual(jobs, {2: 'debian', 3: 'debian'})
|
||||
self.assertIsNone(speeds.mirror_name('/data/mirrors-other/secret'))
|
||||
self.assertIsNone(speeds.mirror_name('/data/mirrors/../secret'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Generated
+30
@@ -8,7 +8,9 @@
|
||||
"name": "mirrorouc-webui",
|
||||
"version": "0.1.0",
|
||||
"dependencies": {
|
||||
"dompurify": "^3.4.17",
|
||||
"lucide-vue-next": "^0.468.0",
|
||||
"marked": "^18.1.2",
|
||||
"vue": "^3.5.22",
|
||||
"vue-i18n": "^11.1.12"
|
||||
},
|
||||
@@ -1014,6 +1016,13 @@
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/trusted-types": {
|
||||
"version": "2.0.7",
|
||||
"resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz",
|
||||
"integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==",
|
||||
"license": "MIT",
|
||||
"optional": true
|
||||
},
|
||||
"node_modules/@vitejs/plugin-vue": {
|
||||
"version": "6.0.10",
|
||||
"resolved": "https://registry.npmjs.org/@vitejs/plugin-vue/-/plugin-vue-6.0.10.tgz",
|
||||
@@ -1223,6 +1232,15 @@
|
||||
"integrity": "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/dompurify": {
|
||||
"version": "3.4.17",
|
||||
"resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.4.17.tgz",
|
||||
"integrity": "sha512-B/pSlYVHQ58kqISD/yDjuJ1yfzI34m1gF4lAod2bwSRhiN4K5Cl3H8Af7hqB2PmTL911tdEudnnXn5FnhRoz0w==",
|
||||
"license": "(MPL-2.0 OR Apache-2.0)",
|
||||
"optionalDependencies": {
|
||||
"@types/trusted-types": "^2.0.7"
|
||||
}
|
||||
},
|
||||
"node_modules/entities": {
|
||||
"version": "7.0.1",
|
||||
"resolved": "https://registry.npmjs.org/entities/-/entities-7.0.1.tgz",
|
||||
@@ -1335,6 +1353,18 @@
|
||||
"@jridgewell/sourcemap-codec": "^1.5.5"
|
||||
}
|
||||
},
|
||||
"node_modules/marked": {
|
||||
"version": "18.1.2",
|
||||
"resolved": "https://registry.npmjs.org/marked/-/marked-18.1.2.tgz",
|
||||
"integrity": "sha512-DfOnvLAQ6WYeWxWO1u4RyFOeqyHup6dks5PPqnoFiooU7ipRvidO/2j8plhAHIiZg574E9ZlzSjUiPMLAzFkgg==",
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"marked": "bin/marked.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 20"
|
||||
}
|
||||
},
|
||||
"node_modules/muggle-string": {
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/muggle-string/-/muggle-string-0.4.1.tgz",
|
||||
|
||||
+5
-1
@@ -3,7 +3,9 @@
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"engines": { "node": ">=22.18.0" },
|
||||
"engines": {
|
||||
"node": ">=22.18.0"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "vite --host 127.0.0.1",
|
||||
"build": "vue-tsc -b && vite build",
|
||||
@@ -12,7 +14,9 @@
|
||||
"check:monitoring": "node scripts/check-monitoring.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"dompurify": "^3.4.17",
|
||||
"lucide-vue-next": "^0.468.0",
|
||||
"marked": "^18.1.2",
|
||||
"vue": "^3.5.22",
|
||||
"vue-i18n": "^11.1.12"
|
||||
},
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
## 已确定的方向
|
||||
|
||||
> 实现进度:前台、Go HTTP + SQLite 管理服务及 Vue 管理页面已接入。当前预览部署、使用方式与实际权限边界以 [README.md](README.md) 和 [deploy/README.md](deploy/README.md) 为准;下文保留原始方案。
|
||||
|
||||
- 使用 Vue 3、TypeScript 和 Vite 开发前端。
|
||||
- 使用 vue-i18n,所有界面文案通过语言键引用语言文件。
|
||||
- 第一版支持简体中文(`zh-CN`)和英文(`en-US`)。
|
||||
@@ -69,7 +71,7 @@ archlinux 58 分钟前 / 即将开始 1.3 TB / 等待同步│ 相关
|
||||
|
||||
| 模块 | 计划内容 |
|
||||
| --- | --- |
|
||||
| 公告与帮助 | Markdown 编辑、预览、草稿、发布、置顶及多语言内容 |
|
||||
| 公告与帮助 | Markdown 编辑、预览、草稿、发布、置顶;公告与镜像帮助统一中文内容 |
|
||||
| 镜像信息 | 名称、分类、描述、帮助关联、公开状态与下载入口 |
|
||||
| 同步配置 | 上游、同步方式、同步间隔、重试、超时及并发设置 |
|
||||
| 任务操作 | 手动触发、停止、执行结果、日志和同步历史 |
|
||||
@@ -162,7 +164,7 @@ export default {
|
||||
|
||||
### 动态内容
|
||||
|
||||
公告、帮助文档、镜像介绍和可编辑站点文案按语言保存于内容数据中,不写入前端语言包。缺少译文时展示原始语言内容,并标明内容语言。
|
||||
公告标题、正文和镜像帮助文档只维护一份中文,所有界面语言展示相同内容,正文节点标记 `lang="zh-CN"`。镜像介绍和可编辑站点文案仍按语言保存于内容数据中,不写入前端语言包。
|
||||
|
||||
## 建议技术架构
|
||||
|
||||
@@ -242,7 +244,7 @@ tunasync 的原生 `interval` 表示同步完成后等待指定分钟数再执
|
||||
- 核对服务器环境、tunasync 版本和现有配置。
|
||||
- 建立管理服务与状态适配层,接入真实镜像列表。
|
||||
- 完成公告、帮助、常用下载和站点设置。
|
||||
- 完成后台登录、内容编辑与多语言内容存储。
|
||||
- 完成后台登录、内容编辑;公告和镜像帮助统一中文,其他站点文案按需支持多语言。
|
||||
- 配置镜像文件服务、网站路由与部署流程。
|
||||
|
||||
### 第三阶段:运维管理
|
||||
|
||||
@@ -9,6 +9,7 @@ import monitorEn from '../src/locales/en-US/monitor.ts'
|
||||
import { datetimeFormats as datetimeZh, numberFormats as numbersZh } from '../src/locales/zh-CN/formats.ts'
|
||||
import { datetimeFormats as datetimeEn, numberFormats as numbersEn } from '../src/locales/en-US/formats.ts'
|
||||
import { createI18n } from 'vue-i18n'
|
||||
import { adminZh, adminEn } from '../src/locales/admin-messages.ts'
|
||||
|
||||
function flatten(value: Record<string, unknown>, prefix = ''): Record<string, string> {
|
||||
return Object.fromEntries(Object.entries(value).flatMap(([key, entry]) => {
|
||||
@@ -17,8 +18,8 @@ function flatten(value: Record<string, unknown>, prefix = ''): Record<string, st
|
||||
}))
|
||||
}
|
||||
|
||||
const zh = flatten({ common: commonZh, mirrors: mirrorsZh, announcements: announcementsZh, monitor: monitorZh })
|
||||
const en = flatten({ common: commonEn, mirrors: mirrorsEn, announcements: announcementsEn, monitor: monitorEn })
|
||||
const zh = flatten({ common: commonZh, mirrors: mirrorsZh, announcements: announcementsZh, monitor: monitorZh, admin: adminZh })
|
||||
const en = flatten({ common: commonEn, mirrors: mirrorsEn, announcements: announcementsEn, monitor: monitorEn, admin: adminEn })
|
||||
const errors: string[] = []
|
||||
const formats = {
|
||||
'zh-CN': { datetimeFormats: datetimeZh, numberFormats: numbersZh },
|
||||
|
||||
+44
-59
@@ -9,20 +9,29 @@ import {
|
||||
import RollingText from './components/RollingText.vue'
|
||||
import LanguagePicker from './components/LanguagePicker.vue'
|
||||
import StatusPage from './components/StatusPage.vue'
|
||||
import AdminPage from './components/AdminPage.vue'
|
||||
import MarkdownContent from './components/MarkdownContent.vue'
|
||||
import { content } from './api'
|
||||
import { usePublicData } from './usePublicData'
|
||||
import { useMirrorFormats } from './useMirrorFormats'
|
||||
import { announcements, demoMirrors, distributions, type Mirror } from './data'
|
||||
import { datetimeFormats, readPreference, savePreference, supportedLocales, type Locale } from './locales'
|
||||
import { type Mirror } from './data'
|
||||
import { datetimeFormats, readPreference, savePreference, type Locale } from './locales'
|
||||
|
||||
const { t, d, locale } = useI18n()
|
||||
const isAdminPage = window.location.pathname.startsWith('/admin')
|
||||
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 || [])
|
||||
const description = (mirror: Mirror) => content(mirror.description, locale.value)
|
||||
const language = computed(() => locale.value as Locale)
|
||||
const localeMessage = (key: string, selectedLocale: Locale = language.value) => t(key, {}, { locale: selectedLocale })
|
||||
type Theme = 'light' | 'dark' | 'system'
|
||||
const storedTheme = readPreference('mirrorouc.theme')
|
||||
const theme = ref<Theme>(storedTheme === 'light' || storedTheme === 'dark' ? storedTheme : 'system')
|
||||
const systemDark = window.matchMedia('(prefers-color-scheme: dark)')
|
||||
const prefersDark = ref(systemDark.matches)
|
||||
const dark = computed(() => theme.value === 'dark' || (theme.value === 'system' && prefersDark.value))
|
||||
const brandLogo = computed(() => dark.value ? '/assets/mirrorOUC.png' : '/assets/mirrorOUC_dark.png')
|
||||
const brandLogo = computed(() => dark.value ? site.value?.logoDark || '/assets/mirrorOUC.png' : site.value?.logoLight || '/assets/mirrorOUC_dark.png')
|
||||
watch(() => site.value?.favicon, value => { if (value) document.querySelector('link[rel="icon"]')?.setAttribute('href', value) })
|
||||
const themeOpen = ref(false)
|
||||
const mobileOpen = ref(false)
|
||||
const themeRoot = ref<HTMLElement>()
|
||||
@@ -30,7 +39,7 @@ const themes: Theme[] = ['light', 'dark', 'system']
|
||||
const themeIcons = { light: Sun, dark: Moon, system: Monitor }
|
||||
const isStatusPage = window.location.pathname.replace(/\/$/, '') === '/status'
|
||||
const navigation = [
|
||||
{ key: 'mirrors', href: '/', active: !isStatusPage },
|
||||
{ key: 'mirrors', href: '/', active: !isStatusPage && !isAdminPage },
|
||||
{ key: 'news', href: 'https://www.itouc.cn/announcements/', active: false },
|
||||
{ key: 'status', href: '/status', active: isStatusPage },
|
||||
{ key: 'feedback', href: 'https://git.itouc.cn/ITStudio_OUC/OUCMirror-WebUI/issues', active: false },
|
||||
@@ -46,16 +55,16 @@ watch(locale, (value) => {
|
||||
document.title = t('common.pageTitle')
|
||||
savePreference('mirrorouc.locale', value)
|
||||
}, { immediate: true })
|
||||
watch([locale, site], () => { document.title = site.value ? `MirrorOUC · ${content(site.value.name, locale.value)}` : t('common.pageTitle') })
|
||||
function selectTheme(value: Theme) { theme.value = value; themeOpen.value = false }
|
||||
|
||||
const search = ref('')
|
||||
const searchInput = ref<HTMLInputElement>()
|
||||
const statusPage = ref<InstanceType<typeof StatusPage>>()
|
||||
const mirrors = ref(demoMirrors.map(mirror => ({ ...mirror })))
|
||||
const filteredMirrors = computed(() => {
|
||||
const query = search.value.trim().toLowerCase()
|
||||
if (!query) return mirrors.value
|
||||
return mirrors.value.filter(mirror => [mirror.name, ...supportedLocales.map(value => localeMessage(mirror.descriptionKey, value))].join(' ').toLowerCase().includes(query))
|
||||
return mirrors.value.filter(mirror => [mirror.name, ...Object.values(mirror.description || {})].join(' ').toLowerCase().includes(query))
|
||||
})
|
||||
const now = ref(Date.now())
|
||||
const { mirrorSize, relativeTime, tableNextTime, fullTime } = useMirrorFormats(now)
|
||||
@@ -76,27 +85,16 @@ function nextLabel(mirror: Mirror) {
|
||||
? t(`mirrors.status.${mirror.status}`) : relativeTime(mirror.nextSync, true)
|
||||
}
|
||||
function resetSearch() { search.value = ''; searchInput.value?.focus() }
|
||||
let demoStep = 0
|
||||
function advanceDemo() {
|
||||
demoStep++
|
||||
const mirror = mirrors.value.find(item => item.name === (demoStep % 2 ? 'anaconda' : 'debian'))!
|
||||
mirror.status = mirror.status === 'syncing' ? 'success' : 'syncing'
|
||||
mirror.lastAttempt = Date.now()
|
||||
if (mirror.status === 'success') { mirror.lastSuccess = mirror.lastAttempt; mirror.nextSync = mirror.lastAttempt + 60 * 60_000; mirror.syncStarted = null }
|
||||
else { mirror.nextSync = null; mirror.syncStarted = mirror.lastAttempt }
|
||||
}
|
||||
|
||||
type Modal = 'help' | 'news' | 'about' | 'downloads' | 'mirror'
|
||||
const dialog = ref<HTMLDialogElement>()
|
||||
const modal = ref<Modal | null>(null)
|
||||
const selectedMirror = ref<Mirror | null>(null)
|
||||
const selectedNews = ref<string | null>(null)
|
||||
const copied = ref(false)
|
||||
const copyError = ref(false)
|
||||
const activeNews = computed(() => announcements.find(item => item.id === selectedNews.value))
|
||||
const activeNews = computed(() => announcements.value.find(item => item.id === selectedNews.value))
|
||||
const modalTitle = computed(() => {
|
||||
if (modal.value === 'mirror') return selectedMirror.value?.name || ''
|
||||
if (modal.value === 'news' && activeNews.value) return t(activeNews.value.titleKey)
|
||||
if (modal.value === 'news' && activeNews.value) return activeNews.value.title
|
||||
const keys = { help: 'mirrors.helpTitle', news: 'announcements.title', about: 'common.aboutTitle', downloads: 'announcements.downloadsTitle' }
|
||||
return modal.value && modal.value in keys ? t(keys[modal.value as keyof typeof keys]) : ''
|
||||
})
|
||||
@@ -104,8 +102,6 @@ async function openModal(value: Modal, mirror?: Mirror, news?: string) {
|
||||
modal.value = value
|
||||
selectedMirror.value = mirror || null
|
||||
selectedNews.value = news || null
|
||||
copied.value = false
|
||||
copyError.value = false
|
||||
mobileOpen.value = false
|
||||
themeOpen.value = false
|
||||
await nextTick()
|
||||
@@ -114,20 +110,6 @@ async function openModal(value: Modal, mirror?: Mirror, news?: string) {
|
||||
function closeModal() { dialog.value?.close() }
|
||||
function onDialogClose() { modal.value = null; selectedMirror.value = null }
|
||||
watch(modal, value => { document.body.classList.toggle('dialog-open', value !== null) })
|
||||
const configExample = computed(() => {
|
||||
switch (selectedMirror.value?.name) {
|
||||
case 'ubuntu': return 'deb https://example.invalid/ubuntu/ noble main universe'
|
||||
case 'debian': return 'deb https://example.invalid/debian/ stable main'
|
||||
case 'pypi': return 'pip config set global.index-url https://example.invalid/pypi/simple'
|
||||
default: return `https://example.invalid/${selectedMirror.value?.name || ''}/`
|
||||
}
|
||||
})
|
||||
async function copyConfig() {
|
||||
copied.value = false
|
||||
copyError.value = false
|
||||
try { await navigator.clipboard.writeText(configExample.value); copied.value = true }
|
||||
catch { copyError.value = true }
|
||||
}
|
||||
|
||||
function onKeydown(event: KeyboardEvent) {
|
||||
if (event.key === 'Escape') { themeOpen.value = false; mobileOpen.value = false }
|
||||
@@ -145,7 +127,6 @@ function outsideClick(event: MouseEvent) {
|
||||
function onSystemTheme(event: MediaQueryListEvent) { prefersDark.value = event.matches }
|
||||
let interval: ReturnType<typeof setInterval>
|
||||
let boardInterval: ReturnType<typeof setInterval>
|
||||
let elapsed = 0
|
||||
onMounted(() => {
|
||||
document.addEventListener('keydown', onKeydown)
|
||||
document.addEventListener('click', outsideClick)
|
||||
@@ -158,7 +139,6 @@ onMounted(() => {
|
||||
interval = setInterval(() => {
|
||||
if (document.visibilityState !== 'visible') return
|
||||
now.value = Date.now()
|
||||
if (++elapsed % 9 === 0) advanceDemo()
|
||||
}, 1000)
|
||||
})
|
||||
onBeforeUnmount(() => {
|
||||
@@ -172,9 +152,9 @@ onBeforeUnmount(() => {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<a class="skip-link" :href="isStatusPage ? '#site-status' : '#mirrors'">{{ t(isStatusPage ? 'common.skipStatus' : 'common.skip') }}</a>
|
||||
<a class="skip-link" :href="isAdminPage ? '#administration' : isStatusPage ? '#site-status' : '#mirrors'">{{ t(isAdminPage ? 'common.skipAdmin' : isStatusPage ? 'common.skipStatus' : 'common.skip') }}</a>
|
||||
<header class="site-header">
|
||||
<a href="/" class="brand" :aria-label="t('common.siteName')">
|
||||
<a href="/" class="brand" :aria-label="content(site?.name, locale) || t('common.siteName')">
|
||||
<span class="brand-logo"><img :src="brandLogo" alt="" width="2000" height="433" /></span>
|
||||
</a>
|
||||
<nav class="desktop-nav" :aria-label="t('common.menu')">
|
||||
@@ -197,7 +177,9 @@ onBeforeUnmount(() => {
|
||||
</header>
|
||||
|
||||
<main class="page-width">
|
||||
<StatusPage v-if="isStatusPage" ref="statusPage" :mirrors="mirrors" :now="now" :cycle="boardCycle" :show-next-sync="showNextSync" @mirror="openModal('mirror', $event)" />
|
||||
<p v-if="!isAdminPage && (loading || source.stale)" class="data-notice" role="status">{{ t(loading ? 'common.loading' : source.updatedAt ? 'common.stale' : 'common.unavailable') }}</p>
|
||||
<AdminPage v-if="isAdminPage" />
|
||||
<StatusPage v-else-if="isStatusPage" ref="statusPage" :mirrors="mirrors" :now="now" :cycle="boardCycle" :show-next-sync="showNextSync" @mirror="openModal('mirror', $event)" />
|
||||
<div v-else class="main-grid">
|
||||
<section id="mirrors" class="mirror-section" tabindex="-1">
|
||||
<div class="mirror-controls">
|
||||
@@ -216,7 +198,7 @@ onBeforeUnmount(() => {
|
||||
<tr v-for="mirror in filteredMirrors" :key="mirror.name" :data-status="mirror.status">
|
||||
<td class="name-cell">
|
||||
<div class="mirror-name-group">
|
||||
<button class="mirror-name-button" :aria-label="t('mirrors.browse', { name: mirror.name })" :title="t(mirror.descriptionKey)" @click="openModal('mirror', mirror)">{{ mirror.name }}</button>
|
||||
<a class="mirror-name-button" :aria-label="t('mirrors.browse', { name: mirror.name })" :title="description(mirror)" :href="mirror.browseUrl">{{ mirror.name }}</a>
|
||||
<button class="help-button" :aria-label="t('mirrors.helpFor', { name: mirror.name })" :title="t('mirrors.helpFor', { name: mirror.name })" @click="openModal('mirror', mirror)"><BookOpen :size="14" /></button>
|
||||
</div>
|
||||
</td>
|
||||
@@ -232,27 +214,29 @@ onBeforeUnmount(() => {
|
||||
<aside class="sidebar">
|
||||
<section class="side-section announcements">
|
||||
<div class="side-heading"><h2>{{ t('announcements.title') }}</h2><button class="sidebar-more" :aria-label="t('announcements.more')" @click="openModal('news')">{{ t('announcements.moreLabel') }}</button></div>
|
||||
<button v-for="item in announcements" :key="item.id" class="news-item" :title="t(item.titleKey)" :aria-label="`${announcementDate(item.date)} ${t(item.titleKey)}`" @click="openModal('news', undefined, item.id)">
|
||||
<button v-for="item in announcements" :key="item.id" class="news-item" :title="item.title" :aria-label="`${announcementDate(item.date)} ${item.title}`" @click="openModal('news', undefined, item.id)">
|
||||
<time :datetime="item.date">{{ announcementDate(item.date) }}</time>
|
||||
<span class="news-title">{{ t(item.titleKey) }}</span>
|
||||
<span class="news-title" lang="zh-CN">{{ item.title }}</span>
|
||||
</button>
|
||||
<p v-if="!announcements.length" class="dialog-hint">{{ t('common.noAnnouncements') }}</p>
|
||||
</section>
|
||||
|
||||
<section class="side-section downloads">
|
||||
<div class="side-heading"><h2>{{ t('announcements.downloads') }}</h2><button class="download-menu-button" aria-haspopup="dialog" aria-controls="content-dialog" @click="openModal('downloads')">{{ t('announcements.downloadMenu') }}</button></div>
|
||||
</section>
|
||||
|
||||
<section class="side-section useful-links">
|
||||
<section v-if="site?.relatedLinks?.length" class="side-section useful-links">
|
||||
<div class="side-heading"><h2>{{ t('announcements.links') }}</h2></div>
|
||||
<button @click="openModal('help')">{{ t('announcements.linkHelp') }}</button>
|
||||
<a href="https://www.itouc.cn/" target="_blank" rel="noopener noreferrer">{{ t('announcements.linkStudio') }}</a>
|
||||
<a href="https://www.ouc.edu.cn/" target="_blank" rel="noopener noreferrer">{{ t('announcements.linkUniversity') }}</a>
|
||||
<template v-for="(link, index) in site.relatedLinks" :key="index">
|
||||
<button v-if="link.kind === 'help'" @click="openModal('help')">{{ content(link.title, locale) }}</button>
|
||||
<a v-else :href="link.url" :target="link.url.startsWith('/') ? undefined : '_blank'" rel="noopener noreferrer">{{ content(link.title, locale) }}</a>
|
||||
</template>
|
||||
</section>
|
||||
</aside>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<div class="footer-affiliations">
|
||||
<div v-if="!isAdminPage" class="footer-affiliations">
|
||||
<a class="footer-affiliation footer-studio" href="https://www.itouc.cn/" target="_blank" rel="noopener noreferrer" :aria-label="t('common.studio')">
|
||||
<img src="/assets/itstudio.png" alt="" width="3849" height="990" />
|
||||
</a>
|
||||
@@ -260,18 +244,18 @@ onBeforeUnmount(() => {
|
||||
<img src="/assets/it.png" alt="" width="4384" height="1106" />
|
||||
</a>
|
||||
</div>
|
||||
<footer class="site-footer">
|
||||
<footer v-if="!isAdminPage" class="site-footer">
|
||||
<div class="page-width">
|
||||
<div class="footer-main">
|
||||
<div class="footer-identity">
|
||||
<a href="/" class="footer-brand" :aria-label="t('common.siteName')">
|
||||
<img :src="brandLogo" alt="" width="2000" height="433" />
|
||||
</a>
|
||||
<p>{{ t('common.footer') }}</p>
|
||||
<p>{{ content(site?.description, locale) || t('common.footer') }}</p>
|
||||
</div>
|
||||
<nav class="footer-links" :aria-label="t('common.footerLinks')">
|
||||
<h2>{{ t('common.footerLinks') }}</h2>
|
||||
<ul>
|
||||
<ul v-if="!site">
|
||||
<li><a href="https://www.ouc.edu.cn/" target="_blank" rel="noopener noreferrer">{{ t('common.university') }}</a></li>
|
||||
<li><a href="https://www.itouc.cn/" target="_blank" rel="noopener noreferrer">{{ t('common.studio') }}</a></li>
|
||||
<li><a href="https://it.ouc.edu.cn/" target="_blank" rel="noopener noreferrer">{{ t('common.faculty') }}</a></li>
|
||||
@@ -279,24 +263,25 @@ onBeforeUnmount(() => {
|
||||
<li><a href="https://git.itouc.cn/" target="_blank" rel="noopener noreferrer">{{ t('common.gitPlatform') }}</a></li>
|
||||
<li><a href="https://blog.itouc.cn/" target="_blank" rel="noopener noreferrer">{{ t('common.studioBlog') }}</a></li>
|
||||
</ul>
|
||||
<ul v-else><li v-for="link in site.links" :key="link.url"><a :href="link.url" target="_blank" rel="noopener noreferrer">{{ content(link.title, locale) }}</a></li></ul>
|
||||
</nav>
|
||||
<section class="footer-contact">
|
||||
<h2>{{ t('common.footerContact') }}</h2>
|
||||
<a href="mailto:contact@itstudio.club">{{ t('common.contactEmail') }}</a>
|
||||
<address>{{ t('common.studioAddress') }}</address>
|
||||
<a :href="`mailto:${site?.email || 'contact@itstudio.club'}`">{{ site?.email || t('common.contactEmail') }}</a>
|
||||
<address>{{ content(site?.address, locale) || t('common.studioAddress') }}</address>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<dialog id="content-dialog" ref="dialog" class="content-dialog" aria-labelledby="dialog-title" @close="onDialogClose" @click="($event.target === $event.currentTarget) && closeModal()">
|
||||
<div class="dialog-inner"><div class="dialog-heading"><h2 id="dialog-title">{{ modalTitle }}</h2><button class="icon-button" :aria-label="t('common.close')" autofocus @click="closeModal"><X :size="21" /></button></div>
|
||||
<div class="dialog-inner"><div class="dialog-heading"><h2 id="dialog-title" :lang="modal === 'news' && activeNews ? 'zh-CN' : undefined">{{ modalTitle }}</h2><button class="icon-button" :aria-label="t('common.close')" autofocus @click="closeModal"><X :size="21" /></button></div>
|
||||
<div class="dialog-content">
|
||||
<template v-if="modal === 'mirror' && selectedMirror"><p class="dialog-lead">{{ t(selectedMirror.descriptionKey) }}</p><div class="mirror-facts"><span>{{ t('mirrors.columns.status') }}</span><span class="status-chip" :class="selectedMirror.status"><span class="status-dot" />{{ t(`mirrors.status.${selectedMirror.status}`) }}</span><span>{{ t('mirrors.columns.updated') }}</span><span>{{ relativeTime(selectedMirror.lastSuccess) }}</span><span>{{ t('mirrors.columns.next') }}</span><span>{{ nextLabel(selectedMirror) }}</span></div><div class="directory-preview"><BookOpen :size="22" /><div><h3>{{ t('mirrors.directoryPending') }}</h3><p>{{ t('mirrors.directoryHint') }}</p></div></div><h3>{{ t('mirrors.exampleConfig') }}</h3><p class="dialog-hint">{{ t('mirrors.exampleHint') }}</p><pre><code>{{ configExample }}</code></pre><div class="dialog-actions"><button class="secondary-button" @click="copyConfig"><Check v-if="copied" :size="15" /><BookOpen v-else :size="15" />{{ copied ? t('common.copied') : t('common.copy') }}</button><a :href="selectedMirror.website" target="_blank" rel="noopener noreferrer" class="text-button">{{ t('mirrors.official') }}<ExternalLink :size="14" /></a></div><p v-if="copyError" role="status">{{ t('common.copyFailed') }}</p></template>
|
||||
<template v-if="modal === 'mirror' && selectedMirror"><p class="dialog-lead">{{ description(selectedMirror) }}</p><div class="mirror-facts"><span>{{ t('mirrors.columns.status') }}</span><span>{{ t(`mirrors.status.${selectedMirror.status}`) }}</span><span>{{ t('mirrors.columns.updated') }}</span><span>{{ relativeTime(selectedMirror.lastSuccess) }}</span><span>{{ t('mirrors.columns.next') }}</span><span>{{ nextLabel(selectedMirror) }}</span></div><MarkdownContent v-if="selectedMirror.help" :source="selectedMirror.help" lang="zh-CN" /><p v-else class="dialog-hint">{{ t('common.noHelp') }}</p><div class="dialog-actions"><a class="text-button" :href="selectedMirror.browseUrl">{{ t('common.openDirectory') }}<ArrowRight :size="15" /></a><a v-if="selectedMirror.website" :href="selectedMirror.website" target="_blank" rel="noopener noreferrer" class="text-button">{{ t('mirrors.official') }}<ExternalLink :size="14" /></a></div></template>
|
||||
<template v-else-if="modal === 'help'"><p class="dialog-lead">{{ t('mirrors.helpIntro') }}</p><div class="help-grid"><button v-for="mirror in mirrors" :key="mirror.name" @click="openModal('mirror', mirror)"><span class="distro-mark" :style="{ '--distro-color': mirror.color }" aria-hidden="true">{{ mirror.mark }}</span><span>{{ mirror.name }}</span><ArrowRight :size="15" /></button></div></template>
|
||||
<template v-else-if="modal === 'news'"><template v-if="activeNews"><time class="article-date" :datetime="activeNews.date">{{ d(new Date(activeNews.date), 'article') }}</time><p class="article-body">{{ t(activeNews.bodyKey) }}</p></template><template v-else><p class="dialog-lead">{{ t('announcements.newsIntro') }}</p><button v-for="item in announcements" :key="item.id" class="article-link" @click="selectedNews = item.id"><span>{{ t(item.titleKey) }}</span><ArrowRight :size="17" /></button></template></template>
|
||||
<template v-else-if="modal === 'downloads'"><p class="dialog-lead">{{ t('announcements.downloadsIntro') }}</p><div class="modal-downloads"><a v-for="distro in distributions" :key="distro.name" :href="distro.url" target="_blank" rel="noopener noreferrer"><span class="distro-mark" :style="{ '--distro-color': distro.color }" aria-hidden="true">{{ distro.mark }}</span><span>{{ distro.name }}</span><ArrowUpRight :size="18" /></a></div></template>
|
||||
<template v-else-if="modal === 'about'"><img class="about-seal" src="/assets/ouc-seal.png" alt="" width="80" height="80" /><p class="article-body">{{ t('common.aboutBody') }}</p><p class="dialog-hint">{{ t('common.previewBody') }}</p><div class="about-links"><a href="https://www.itouc.cn/" target="_blank" rel="noopener noreferrer">{{ t('common.studio') }}<ArrowUpRight :size="15" /></a><a href="https://github.com/tuna/tunasync" target="_blank" rel="noopener noreferrer">{{ t('common.source') }} · tunasync<ArrowUpRight :size="15" /></a></div></template>
|
||||
<template v-else-if="modal === 'news'"><template v-if="activeNews"><time class="article-date" :datetime="activeNews.date">{{ d(new Date(activeNews.date), 'article') }}</time><MarkdownContent :source="activeNews.body" lang="zh-CN" /></template><template v-else><p class="dialog-lead">{{ t('announcements.newsIntro') }}</p><button v-for="item in announcements" :key="item.id" class="article-link" @click="selectedNews = item.id"><span lang="zh-CN">{{ item.title }}</span><ArrowRight :size="17" /></button></template></template>
|
||||
<template v-else-if="modal === 'downloads'"><p class="dialog-lead">{{ t('announcements.downloadsIntro') }}</p><p v-if="!distributions.length" class="dialog-hint">{{ t('common.noDownloads') }}</p><div class="modal-downloads"><a v-for="distro in distributions" :key="distro.name" :href="distro.url" target="_blank" rel="noopener noreferrer"><span>{{ distro.name }} {{ distro.version }}</span><ArrowUpRight :size="18" /></a></div></template>
|
||||
<template v-else-if="modal === 'about'"><img class="about-seal" :src="site?.avatar || '/assets/ouc-seal.png'" alt="" width="80" height="80" /><p class="article-body">{{ t('common.aboutBody') }}</p><div class="about-links"><a href="https://www.itouc.cn/" target="_blank" rel="noopener noreferrer">{{ t('common.studio') }}<ArrowUpRight :size="15" /></a><a href="https://github.com/tuna/tunasync" target="_blank" rel="noopener noreferrer">{{ t('common.source') }} · tunasync<ArrowUpRight :size="15" /></a></div></template>
|
||||
</div>
|
||||
</div>
|
||||
</dialog>
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
export type Localized = Record<string, string>
|
||||
export interface SiteLink { title: Localized; url: string }
|
||||
export interface RelatedLink extends SiteLink { kind: 'url' | 'help' }
|
||||
export interface SiteDownload { name: string; version: string; url: string }
|
||||
export interface SiteSettings {
|
||||
name: Localized; description: Localized; logoLight: string; logoDark: string; favicon: string; avatar: string
|
||||
email: string; address: Localized; links: SiteLink[]; relatedLinks: RelatedLink[]; downloads: SiteDownload[]
|
||||
}
|
||||
export interface AnnouncementContent { title: string; body: string; published: boolean; pinned: boolean; date: string }
|
||||
export interface MirrorMetadata { name: string; description: Localized; help: string; category: string; visible: boolean; website: string }
|
||||
export interface ContentRecord<T> { id: string; data: T; revision: number; updatedAt: number }
|
||||
export interface AdminSession { username: string; role: 'admin' | 'editor'; csrf: string }
|
||||
export interface SyncJob { name: string; worker: string; status: string; last_update: string; last_started: string; last_ended: string; next_schedule: string; upstream: string; size: string; error_msg: string }
|
||||
export interface Overview { jobs: SyncJob[]; workers: { id: string; last_online: string }[]; worker: string; updatedAt: number; code: string; configAvailable: boolean; logsAvailable: boolean }
|
||||
export interface SyncNumbers { concurrent: number; interval: number; retry: number; timeout: number }
|
||||
export interface SyncConfigMirror { name: string; provider: string; upstream: string; interval: number; retry: number; timeout: number; source?: string }
|
||||
export interface SyncConfig { worker: string; baseHash: string; global: SyncNumbers; mirrors: SyncConfigMirror[]; canAdd: boolean }
|
||||
export interface ConfigVersion { id: string; files: Record<string, { before: string | null; after: string | null }> | null; baseHash: string; status: string; created: number; username: string }
|
||||
export interface AuditEntry { id: number; username: string; action: string; target: string; result: string; created: number }
|
||||
export interface AdminUser { id: number; username: string; role: string; created: number }
|
||||
|
||||
let csrf = ''
|
||||
export class ApiError extends Error { constructor(public code: string, public status: number) { super(code) } }
|
||||
export function setSession(value: AdminSession | null) { csrf = value?.csrf ?? '' }
|
||||
export async function api<T>(path: string, options: RequestInit = {}): Promise<T> {
|
||||
const headers = new Headers(options.headers)
|
||||
if (options.body && !(options.body instanceof FormData)) headers.set('Content-Type', 'application/json')
|
||||
if (options.method && !['GET', 'HEAD'].includes(options.method)) headers.set('X-CSRF-Token', csrf)
|
||||
let response: Response
|
||||
try { response = await fetch(`/api/v1${path}`, { ...options, headers, credentials: 'same-origin' }) }
|
||||
catch (error) { if ((error as Error).name === 'AbortError') throw error; throw new ApiError('NETWORK_ERROR', 0) }
|
||||
const data = await response.json().catch(() => ({ code: 'INVALID_RESPONSE' }))
|
||||
if (!response.ok) throw new ApiError(data.code || 'INTERNAL_ERROR', response.status)
|
||||
return data as T
|
||||
}
|
||||
export const jsonBody = (value: unknown) => JSON.stringify(value)
|
||||
export function content(value: Localized | null | undefined, locale: string) { return value?.[locale] || value?.['zh-CN'] || Object.values(value || {}).find(Boolean) || '' }
|
||||
@@ -0,0 +1,252 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, onBeforeUnmount, onMounted, reactive, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { api, ApiError, jsonBody, setSession, type AdminSession, type AdminUser, type AnnouncementContent, type AuditEntry, type ConfigVersion, type ContentRecord, type MirrorMetadata, type Overview, type SiteSettings, type SyncConfig, type SyncConfigMirror, type SyncJob } from '../api'
|
||||
import { supportedLocales } from '../locales'
|
||||
import MarkdownContent from './MarkdownContent.vue'
|
||||
|
||||
const { t, te, d, n } = useI18n({ useScope: 'global' })
|
||||
const session = ref<AdminSession | null>(null)
|
||||
const checking = ref(true)
|
||||
const busy = ref(false)
|
||||
const error = ref('')
|
||||
const notice = ref('')
|
||||
const tab = ref('overview')
|
||||
const tabs = ['overview', 'mirrors', 'announcements', 'config', 'settings', 'users', 'audit']
|
||||
const allowedTabs = computed(() => session.value?.role === 'admin' ? tabs : ['announcements', 'users'])
|
||||
const credentials = reactive({ username: '', password: '' })
|
||||
const overview = ref<Overview | null>(null)
|
||||
const articles = ref<ContentRecord<AnnouncementContent>[]>([])
|
||||
const metadata = ref<ContentRecord<MirrorMetadata>[]>([])
|
||||
const settings = ref<ContentRecord<SiteSettings> | null>(null)
|
||||
const users = ref<AdminUser[]>([])
|
||||
const audit = ref<AuditEntry[]>([])
|
||||
const search = ref('')
|
||||
const jobs = computed(() => (overview.value?.jobs || []).filter(job => `${job.name} ${job.upstream}`.toLowerCase().includes(search.value.toLowerCase())).sort((a, b) => a.name.localeCompare(b.name, 'en')))
|
||||
const articlesEditor = ref<ContentRecord<AnnouncementContent> | null>(null)
|
||||
const mirrorEditor = ref<ContentRecord<MirrorMetadata> | null>(null)
|
||||
const editLocale = ref('zh-CN')
|
||||
const logs = ref<{ name: string; text: string } | null>(null)
|
||||
const config = ref<SyncConfig | null>(null)
|
||||
const configScope = ref('global')
|
||||
const selectedConfig = ref('')
|
||||
const mirrorConfig = ref<SyncConfigMirror | null>(null)
|
||||
const draft = ref<ConfigVersion | null>(null)
|
||||
const versions = ref<ConfigVersion[]>([])
|
||||
const newUser = reactive({ username: '', password: '', role: 'editor' })
|
||||
const passwords = reactive({ currentPassword: '', newPassword: '', confirm: '' })
|
||||
const confirmation = ref<{ message: string; action: () => Promise<void> } | null>(null)
|
||||
const confirmDialog = ref<HTMLDialogElement>()
|
||||
const syncFields = ['interval', 'retry', 'timeout'] as const
|
||||
const imageFields = ['logoLight', 'logoDark', 'favicon', 'avatar'] as const
|
||||
const localizedFields = ['name', 'description', 'address'] as const
|
||||
const clone = <T,>(value: T): T => JSON.parse(JSON.stringify(value))
|
||||
function date(value: number | string) { return d(new Date(value), 'monitorSample') }
|
||||
function message(code: string) { return te(`admin.errors.${code}`) ? t(`admin.errors.${code}`) : t('admin.errors.INTERNAL_ERROR') }
|
||||
function eventLabel(value: string) { return te(`admin.events.${value}`) ? t(`admin.events.${value}`) : te(`admin.errors.${value}`) ? t(`admin.errors.${value}`) : value }
|
||||
function jobStatus(value: string) { return ({ 'pre-syncing': 'waiting', disabled: 'paused', none: 'unknown' } as Record<string, string>)[value] || value }
|
||||
function jobSize(value: string) {
|
||||
const match = /^\s*(\d+(?:\.\d+)?)\s*([KMGTPE]?)(?:i?B)?\s*$/i.exec(value)
|
||||
if (!match) return t('common.emptyValue')
|
||||
const bytes = Number(match[1]) * 1024 ** (match[2] ? 'KMGTPE'.indexOf(match[2].toUpperCase()) + 1 : 0)
|
||||
const large = bytes >= 1e12
|
||||
return t(large ? 'mirrors.size.terabyte' : 'mirrors.size.gigabyte', { value: n(bytes / (large ? 1e12 : 1e9), 'size') })
|
||||
}
|
||||
async function run(action: () => Promise<void>) {
|
||||
if (busy.value) return
|
||||
busy.value = true; error.value = ''; notice.value = ''
|
||||
try { await action() }
|
||||
catch (e) {
|
||||
if (e instanceof ApiError && e.status === 401) { session.value = null; setSession(null) }
|
||||
error.value = message(e instanceof ApiError ? e.code : 'INTERNAL_ERROR')
|
||||
} finally { busy.value = false }
|
||||
}
|
||||
async function loadOverview() { overview.value = await api<Overview>('/admin/overview') }
|
||||
function moveRelatedLink(index: number, offset: number) {
|
||||
const links = settings.value?.data.relatedLinks
|
||||
const destination = index + offset
|
||||
if (!links || destination < 0 || destination >= links.length) return
|
||||
const [entry] = links.splice(index, 1)
|
||||
if (entry) links.splice(destination, 0, entry)
|
||||
}
|
||||
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 === 'settings') settings.value = (await api<ContentRecord<SiteSettings>[]>('/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')
|
||||
if (value === 'audit') audit.value = await api('/admin/audit')
|
||||
}
|
||||
function changeTab(value: string) { void run(async () => { tab.value = value; await loadTab(value) }) }
|
||||
async function login() {
|
||||
session.value = await api('/auth/login', { method: 'POST', body: jsonBody(credentials) }); setSession(session.value)
|
||||
credentials.password = ''; tab.value = session.value?.role === 'admin' ? 'overview' : 'announcements'
|
||||
await loadTab(tab.value)
|
||||
}
|
||||
async function logout() { await api('/auth/logout', { method: 'POST' }); session.value = null; setSession(null); mirrorEditor.value = null; articlesEditor.value = null; logs.value = null }
|
||||
async function saveRecord<T>(kind: string, value: ContentRecord<T>) {
|
||||
const result = await api<ContentRecord<T>>(`/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 ask(message: string, action: () => Promise<void>) {
|
||||
confirmation.value = { message, action }
|
||||
await nextTick()
|
||||
confirmDialog.value?.showModal()
|
||||
}
|
||||
function confirmed() { const action = confirmation.value?.action; confirmDialog.value?.close(); if (action) void run(action) }
|
||||
function newArticle() {
|
||||
const id = Array.from(crypto.getRandomValues(new Uint8Array(16)), byte => byte.toString(16).padStart(2, '0')).join('')
|
||||
articlesEditor.value = { id, revision: 0, updatedAt: 0, data: { title: '', body: '', published: false, pinned: false, date: new Date().toISOString() } }
|
||||
}
|
||||
function editArticle(record: ContentRecord<AnnouncementContent>) { articlesEditor.value = clone(record); articlesEditor.value.data.title ||= ''; articlesEditor.value.data.body ||= '' }
|
||||
async function saveArticle() { if (!articlesEditor.value) return; articlesEditor.value = await saveRecord('announcements', articlesEditor.value); articles.value = await api('/admin/records/announcements') }
|
||||
function deleteArticle(record: ContentRecord<AnnouncementContent>) {
|
||||
void ask(t('admin.deleteConfirm', { name: record.data.title }), async () => {
|
||||
await api(`/admin/records/announcements/${record.id}`, { method: 'DELETE', body: jsonBody({ revision: record.revision }) })
|
||||
articles.value = await api('/admin/records/announcements'); if (articlesEditor.value?.id === record.id) articlesEditor.value = null
|
||||
})
|
||||
}
|
||||
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 ||= ''
|
||||
}
|
||||
async function saveMirror() { if (!mirrorEditor.value) return; mirrorEditor.value = await saveRecord('mirrors', mirrorEditor.value); metadata.value = await api('/admin/records/mirrors') }
|
||||
function command(job: SyncJob, action: string) {
|
||||
void ask(t('admin.commandConfirm', { name: job.name, action: t(`admin.${action}`) }), async () => {
|
||||
await api(`/admin/jobs/${encodeURIComponent(job.name)}/${action}`, { method: 'POST' }); notice.value = t('admin.submitted'); await loadOverview()
|
||||
})
|
||||
}
|
||||
async function showLogs(name: string) { const data = await api<{ content: string }>(`/admin/jobs/${encodeURIComponent(name)}/logs`); logs.value = { name, text: data.content } }
|
||||
async function upload(event: Event, field: typeof imageFields[number]) {
|
||||
const input = event.target as HTMLInputElement, file = input.files?.[0]
|
||||
if (!file || !settings.value) return
|
||||
const form = new FormData(); form.append('file', file)
|
||||
const result = await api<{ url: string }>('/admin/uploads', { method: 'POST', body: form }); settings.value.data[field] = result.url; input.value = ''
|
||||
}
|
||||
function chooseMirrorConfig() {
|
||||
draft.value = null
|
||||
const entry = config.value?.mirrors.find(row => row.name === selectedConfig.value)
|
||||
mirrorConfig.value = entry ? clone(entry) : config.value ? { name: '', provider: 'rsync', upstream: '', interval: config.value.global.interval, retry: config.value.global.retry, timeout: config.value.global.timeout } : null
|
||||
}
|
||||
async function previewConfig() {
|
||||
if (!config.value) return
|
||||
const body = configScope.value === 'global' ? { baseHash: config.value.baseHash, global: config.value.global } : { baseHash: config.value.baseHash, mirror: mirrorConfig.value }
|
||||
draft.value = await api('/admin/config/preview', { method: 'POST', body: jsonBody(body) }); versions.value = await api('/admin/config/versions')
|
||||
}
|
||||
function applyConfig() { if (!draft.value) return; const id = draft.value.id; void ask(t('admin.applyConfirm'), async () => { await api('/admin/config/apply', { method: 'POST', body: jsonBody({ id }) }); await loadTab('config'); notice.value = t('admin.submitted') }) }
|
||||
function rollback(id: string) { void ask(t('admin.rollbackConfirm'), async () => { await api('/admin/config/rollback', { method: 'POST', body: jsonBody({ id }) }); await loadTab('config'); notice.value = t('admin.submitted') }) }
|
||||
async function createUser() { await api('/admin/users', { method: 'POST', body: jsonBody(newUser) }); newUser.password = ''; newUser.username = ''; users.value = await api('/admin/users'); notice.value = t('admin.saved') }
|
||||
async function changePassword() {
|
||||
if (passwords.newPassword !== passwords.confirm) { error.value = t('admin.passwordMismatch'); return }
|
||||
await api('/auth/password', { method: 'PUT', body: jsonBody({ current: passwords.currentPassword, password: passwords.newPassword }) })
|
||||
Object.assign(passwords, { currentPassword: '', newPassword: '', confirm: '' }); setSession(null); session.value = null; notice.value = t('admin.saved')
|
||||
}
|
||||
let poll: ReturnType<typeof setInterval>
|
||||
onMounted(async () => {
|
||||
try { session.value = await api('/auth/session'); setSession(session.value); tab.value = session.value?.role === 'admin' ? 'overview' : 'announcements'; await run(() => loadTab(tab.value)) }
|
||||
catch (e) { if (!(e instanceof ApiError && e.status === 401)) error.value = message(e instanceof ApiError ? e.code : 'INTERNAL_ERROR') }
|
||||
finally { checking.value = false }
|
||||
poll = setInterval(() => { if (session.value?.role === 'admin' && ['overview', 'mirrors'].includes(tab.value) && !document.hidden && !busy.value) void loadOverview().catch(() => {}) }, 15000)
|
||||
})
|
||||
onBeforeUnmount(() => clearInterval(poll))
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section id="administration" class="admin-page" tabindex="-1" :aria-busy="busy || checking">
|
||||
<div class="admin-top"><h1>{{ t('admin.title') }}</h1><div v-if="session" class="admin-actions"><span>{{ session.username }}</span><button @click="run(logout)" :disabled="busy">{{ t('admin.logout') }}</button></div></div>
|
||||
<p v-if="checking">{{ t('admin.checking') }}</p>
|
||||
<p v-if="error" role="alert" class="admin-error">{{ error }}</p><p v-if="notice" role="status" class="admin-notice">{{ notice }}</p>
|
||||
<form v-if="!checking && !session" class="admin-login admin-form" @submit.prevent="run(login)">
|
||||
<label>{{ t('admin.username') }}<input v-model="credentials.username" name="username" autocomplete="username" required maxlength="128" /></label>
|
||||
<label>{{ t('admin.password') }}<input v-model="credentials.password" name="password" type="password" autocomplete="current-password" required maxlength="256" /></label>
|
||||
<button class="primary" :disabled="busy">{{ t(busy ? 'admin.busy' : 'admin.login') }}</button>
|
||||
</form>
|
||||
<template v-if="session">
|
||||
<nav class="admin-tabs" :aria-label="t('admin.title')"><button v-for="item in allowedTabs" :key="item" :aria-current="tab === item ? 'page' : undefined" :disabled="busy" @click="changeTab(item)">{{ t(`admin.${item}`) }}</button></nav>
|
||||
<div class="admin-toolbar"><h2>{{ t(`admin.${tab}`) }}</h2><button :disabled="busy" @click="run(() => loadTab(tab))">{{ t('admin.refresh') }}</button></div>
|
||||
<template v-if="tab === 'overview' && overview">
|
||||
<p v-if="overview.code" class="admin-error">{{ t('admin.sourceError') }}</p>
|
||||
<dl class="admin-summary"><div><dt>{{ t('admin.worker') }}</dt><dd>{{ overview.worker }}</dd></div><div><dt>{{ t('admin.total') }}</dt><dd>{{ overview.jobs.length }}</dd></div><div><dt>{{ t('admin.active') }}</dt><dd>{{ overview.jobs.filter(row => row.status === 'syncing').length }}</dd></div><div><dt>{{ t('admin.failed') }}</dt><dd>{{ overview.jobs.filter(row => row.status === 'failed').length }}</dd></div><div><dt>{{ t('admin.updated') }}</dt><dd>{{ overview.updatedAt ? date(overview.updatedAt) : t('admin.empty') }}</dd></div></dl>
|
||||
</template>
|
||||
<template v-if="tab === 'mirrors'">
|
||||
<input v-model="search" class="admin-search" type="search" :placeholder="t('admin.search')" :aria-label="t('admin.search')" />
|
||||
<div class="admin-table-wrap jobs-table"><table><thead><tr><th>{{ t('admin.name') }}</th><th>{{ t('admin.status') }}</th><th>{{ t('admin.size') }}</th><th>{{ t('admin.actions') }}</th></tr></thead><tbody><tr v-for="job in jobs" :key="job.name" :data-status="jobStatus(job.status)"><th>{{ job.name }}</th><td>{{ t(`mirrors.status.${jobStatus(job.status)}`) }}</td><td>{{ jobSize(job.size) }}</td><td><div class="row-actions"><button @click="editMirror(job)">{{ t('admin.edit') }}</button><button :disabled="busy" @click="run(() => showLogs(job.name))">{{ t('admin.logs') }}</button><button v-for="action in ['start','stop','restart','disable']" :key="action" :disabled="busy" @click="command(job, action)">{{ t(`admin.${action}`) }}</button></div></td></tr></tbody></table></div>
|
||||
<form v-if="mirrorEditor" class="admin-editor admin-form" @submit.prevent="run(saveMirror)"><div class="admin-toolbar"><h3>{{ mirrorEditor.id }} · {{ t('admin.metadata') }}</h3><button type="button" @click="mirrorEditor = null">{{ t('admin.close') }}</button></div><label>{{ t('admin.descriptionLanguage') }}<select v-model="editLocale"><option v-for="language in supportedLocales" :key="language" :value="language">{{ t('common.languageName', {}, { locale: language }) }}</option></select></label><label>{{ t('admin.description') }}<textarea v-model="mirrorEditor.data.description[editLocale]" rows="2" /></label><label>{{ t('admin.helpChinese') }}<textarea v-model="mirrorEditor.data.help" lang="zh-CN" rows="10" /></label><MarkdownContent :source="mirrorEditor.data.help" lang="zh-CN" /><label>{{ t('admin.website') }}<input v-model="mirrorEditor.data.website" type="url" /></label><label>{{ t('admin.category') }}<select v-model="mirrorEditor.data.category"><option v-for="value in ['os','language','other']" :key="value" :value="value">{{ t(`admin.${value}`) }}</option></select></label><label class="check"><input v-model="mirrorEditor.data.visible" type="checkbox" />{{ t('admin.visible') }}</label><button class="primary" :disabled="busy">{{ t('admin.save') }}</button></form>
|
||||
<section v-if="logs" class="admin-editor"><div class="admin-toolbar"><h3>{{ logs.name }} · {{ t('admin.logs') }}</h3><div class="admin-actions"><button :disabled="busy" @click="run(() => showLogs(logs!.name))">{{ t('admin.refresh') }}</button><button @click="logs = null">{{ t('admin.close') }}</button></div></div><p>{{ t('admin.logTail') }}</p><pre class="admin-log">{{ logs.text }}</pre></section>
|
||||
</template>
|
||||
<template v-if="tab === 'announcements'">
|
||||
<button class="primary" @click="newArticle">{{ t('admin.newArticle') }}</button>
|
||||
<div class="admin-table-wrap"><table><thead><tr><th>{{ t('admin.articleTitle') }}</th><th>{{ t('admin.status') }}</th><th>{{ t('admin.date') }}</th><th>{{ t('admin.actions') }}</th></tr></thead><tbody><tr v-for="row in articles" :key="row.id"><td lang="zh-CN">{{ row.data.title }}</td><td>{{ t(row.data.published ? 'admin.published' : 'admin.draft') }}</td><td>{{ date(row.data.date) }}</td><td><div class="row-actions"><button @click="editArticle(row)">{{ t('admin.edit') }}</button><button @click="deleteArticle(row)">{{ t('admin.remove') }}</button></div></td></tr></tbody></table><p v-if="!articles.length">{{ t('admin.empty') }}</p></div>
|
||||
<form v-if="articlesEditor" class="admin-editor admin-form" @submit.prevent="run(saveArticle)"><div class="admin-toolbar"><h3>{{ t('admin.edit') }}</h3><button type="button" @click="articlesEditor = null">{{ t('admin.close') }}</button></div><label>{{ t('admin.articleTitle') }}<input v-model="articlesEditor.data.title" maxlength="300" lang="zh-CN" required /></label><label>{{ t('admin.body') }}<textarea v-model="articlesEditor.data.body" lang="zh-CN" rows="12" /></label><h3>{{ t('admin.preview') }}</h3><MarkdownContent :source="articlesEditor.data.body" lang="zh-CN" /><label>{{ t('admin.date') }}<input :value="articlesEditor.data.date.slice(0, 10)" type="date" required @input="articlesEditor.data.date = `${($event.target as HTMLInputElement).value}T00:00:00+08:00`" /></label><div class="admin-actions"><label class="check"><input v-model="articlesEditor.data.published" type="checkbox" />{{ t('admin.published') }}</label><label class="check"><input v-model="articlesEditor.data.pinned" type="checkbox" />{{ t('admin.pinned') }}</label></div><button class="primary" :disabled="busy">{{ t('admin.save') }}</button></form>
|
||||
</template>
|
||||
<form v-if="tab === 'settings' && settings" class="admin-form" @submit.prevent="run(async () => { settings = await saveRecord('settings', settings!) })">
|
||||
<label>{{ t('admin.contentLanguage') }}<select v-model="editLocale"><option v-for="language in supportedLocales" :key="language" :value="language">{{ t('common.languageName', {}, { locale: language }) }}</option></select></label>
|
||||
<label v-for="field in localizedFields" :key="field">{{ t(field === 'name' ? 'admin.siteName' : `admin.${field}`) }}<textarea v-model="settings.data[field][editLocale]" rows="2" /></label>
|
||||
<label>{{ t('admin.email') }}<input v-model="settings.data.email" type="email" /></label>
|
||||
<div class="admin-columns"><div v-for="field in imageFields" :key="field" class="image-field"><label>{{ t(`admin.${field}`) }}<input v-model="settings.data[field]" /></label><img :src="settings.data[field]" alt="" /><label class="upload-label">{{ t('admin.upload') }}<input type="file" accept="image/png,image/jpeg" :disabled="busy" @change="run(() => upload($event, field))" /></label></div></div>
|
||||
<section class="admin-related-links" aria-labelledby="related-links-settings">
|
||||
<h3 id="related-links-settings">{{ t('admin.relatedLinks') }}</h3>
|
||||
<div v-for="(link, index) in settings.data.relatedLinks" :key="index" class="admin-array" role="group" :aria-label="t('admin.linkEntry', { index: index + 1 })">
|
||||
<label>{{ t('admin.articleTitle') }}<input v-model="link.title[editLocale]" maxlength="200" /></label>
|
||||
<label>{{ t('admin.linkType') }}<select v-model="link.kind" @change="link.kind === 'help' && (link.url = '')"><option value="url">{{ t('admin.urlLink') }}</option><option value="help">{{ t('admin.helpLink') }}</option></select></label>
|
||||
<label v-if="link.kind === 'url'">{{ t('admin.url') }}<input v-model="link.url" required /></label>
|
||||
<div class="admin-actions"><button type="button" :disabled="index === 0" @click="moveRelatedLink(index, -1)">{{ t('admin.moveUp') }}</button><button type="button" :disabled="index === settings.data.relatedLinks.length - 1" @click="moveRelatedLink(index, 1)">{{ t('admin.moveDown') }}</button><button type="button" @click="settings.data.relatedLinks.splice(index, 1)">{{ t('admin.remove') }}</button></div>
|
||||
</div>
|
||||
<button type="button" @click="settings.data.relatedLinks.push({ title: { 'zh-CN': '', 'en-US': '' }, kind: 'url', url: '' })">{{ t('admin.add') }}</button>
|
||||
</section>
|
||||
<h3>{{ t('admin.links') }}</h3><div v-for="(link, index) in settings.data.links" :key="index" class="admin-array"><label>{{ t('admin.articleTitle') }}<input v-model="link.title[editLocale]" /></label><label>{{ t('admin.url') }}<input v-model="link.url" type="url" required /></label><button type="button" @click="settings.data.links.splice(index,1)">{{ t('admin.remove') }}</button></div><button type="button" @click="settings.data.links.push({ title: { 'zh-CN': '', 'en-US': '' }, url: '' })">{{ t('admin.add') }}</button>
|
||||
<h3>{{ t('admin.downloads') }}</h3><div v-for="(download,index) in settings.data.downloads" :key="index" class="admin-array"><label>{{ t('admin.name') }}<input v-model="download.name" required /></label><label>{{ t('admin.version') }}<input v-model="download.version" /></label><label>{{ t('admin.url') }}<input v-model="download.url" type="url" required /></label><button type="button" @click="settings.data.downloads.splice(index,1)">{{ t('admin.remove') }}</button></div><button type="button" @click="settings.data.downloads.push({ name: '', version: '', url: '' })">{{ t('admin.add') }}</button><button class="primary" :disabled="busy">{{ t('admin.save') }}</button>
|
||||
</form>
|
||||
<template v-if="tab === 'config' && config">
|
||||
<p>{{ t('admin.intervalHint') }}</p><form class="admin-form" @submit.prevent="run(previewConfig)"><label>{{ t('admin.config') }}<select v-model="configScope" @change="draft = null"><option value="global">{{ t('admin.global') }}</option><option value="mirror">{{ t('admin.mirrorConfig') }}</option></select></label>
|
||||
<div v-if="configScope === 'global'" class="admin-columns"><label>{{ t('admin.concurrent') }}<input v-model.number="config.global.concurrent" type="number" min="1" max="64" required /></label><label v-for="field in syncFields" :key="field">{{ t(`admin.${field}`) }}<input v-model.number="config.global[field]" type="number" :min="field === 'retry' ? 0 : field === 'timeout' ? 30 : 1" :max="field === 'retry' ? 20 : field === 'timeout' ? 1209600 : 525600" required /></label></div>
|
||||
<template v-else><label>{{ t('admin.mirrors') }}<select v-model="selectedConfig" @change="chooseMirrorConfig"><option value="" disabled>{{ t('admin.mirrors') }}</option><option v-for="row in config.mirrors" :key="row.name" :value="row.name">{{ row.name }}</option><option v-if="config.canAdd" value="__new">{{ t('admin.newMirror') }}</option></select></label><template v-if="mirrorConfig"><label>{{ t('admin.name') }}<input v-model="mirrorConfig.name" :readonly="selectedConfig !== '__new'" pattern="[a-zA-Z0-9][a-zA-Z0-9_.+\-]*" required /></label><label>{{ t('admin.provider') }}<select v-model="mirrorConfig.provider" :disabled="selectedConfig !== '__new'"><option value="rsync">rsync</option><option value="two-stage-rsync">two-stage-rsync</option><option v-if="!['rsync','two-stage-rsync'].includes(mirrorConfig.provider)" :value="mirrorConfig.provider">{{ mirrorConfig.provider }}</option></select></label><label>{{ t('admin.upstream') }}<input v-model="mirrorConfig.upstream" required /></label><div class="admin-columns"><label v-for="field in syncFields" :key="field">{{ t(`admin.${field}`) }}<input v-model.number="mirrorConfig[field]" type="number" :min="field === 'retry' ? 0 : field === 'timeout' ? 30 : 1" :max="field === 'retry' ? 20 : field === 'timeout' ? 1209600 : 525600" required /></label></div></template></template>
|
||||
<button class="primary" :disabled="busy || (configScope === 'mirror' && !mirrorConfig)">{{ t('admin.previewConfig') }}</button>
|
||||
</form>
|
||||
<section v-if="draft" class="admin-editor"><h3>{{ t('admin.preview') }}</h3><div v-for="(file,path) in draft.files" :key="path"><h4 class="file-name">{{ path }}</h4><div class="admin-columns diff"><div><h4>{{ t('admin.before') }}</h4><pre>{{ file.before }}</pre></div><div><h4>{{ t('admin.after') }}</h4><pre>{{ file.after }}</pre></div></div></div><button class="primary" :disabled="busy" @click="applyConfig">{{ t('admin.apply') }}</button></section>
|
||||
<h3>{{ t('admin.versions') }}</h3><div class="admin-table-wrap"><table><thead><tr><th>{{ t('admin.time') }}</th><th>{{ t('admin.actor') }}</th><th>{{ t('admin.status') }}</th><th>{{ t('admin.actions') }}</th></tr></thead><tbody><tr v-for="version in versions" :key="version.id"><td>{{ date(version.created) }}</td><td>{{ version.username }}</td><td>{{ eventLabel(version.status) }}</td><td><button v-if="version.status === 'applied'" :disabled="busy" @click="rollback(version.id)">{{ t('admin.rollback') }}</button></td></tr></tbody></table></div>
|
||||
</template>
|
||||
<template v-if="tab === 'users'">
|
||||
<h3>{{ t('admin.changePassword') }}</h3><p>{{ t('admin.passwordHint') }}</p><form class="admin-form admin-login" @submit.prevent="run(changePassword)"><label>{{ t('admin.currentPassword') }}<input v-model="passwords.currentPassword" type="password" autocomplete="current-password" required /></label><label>{{ t('admin.newPassword') }}<input v-model="passwords.newPassword" type="password" autocomplete="new-password" minlength="12" maxlength="256" required /></label><label>{{ t('admin.confirmPassword') }}<input v-model="passwords.confirm" type="password" autocomplete="new-password" minlength="12" maxlength="256" required /></label><button class="primary" :disabled="busy">{{ t('admin.changePassword') }}</button></form>
|
||||
<template v-if="session.role === 'admin'"><h3>{{ t('admin.users') }}</h3><div class="admin-table-wrap"><table><thead><tr><th>{{ t('admin.username') }}</th><th>{{ t('admin.role') }}</th></tr></thead><tbody><tr v-for="user in users" :key="user.id"><td>{{ user.username }}</td><td>{{ t(`admin.${user.role}`) }}</td></tr></tbody></table></div><h3>{{ t('admin.newUser') }}</h3><form class="admin-form admin-login" @submit.prevent="run(createUser)"><label>{{ t('admin.username') }}<input v-model="newUser.username" autocomplete="off" pattern="[a-zA-Z0-9][a-zA-Z0-9_.+\-]*" required /></label><label>{{ t('admin.password') }}<input v-model="newUser.password" type="password" autocomplete="new-password" minlength="12" maxlength="256" required /></label><label>{{ t('admin.role') }}<select v-model="newUser.role"><option value="admin">{{ t('admin.admin') }}</option><option value="editor">{{ t('admin.editor') }}</option></select></label><button class="primary" :disabled="busy">{{ t('admin.newUser') }}</button></form></template>
|
||||
</template>
|
||||
<div v-if="tab === 'audit'" class="admin-table-wrap"><table><thead><tr><th>{{ t('admin.time') }}</th><th>{{ t('admin.actor') }}</th><th>{{ t('admin.action') }}</th><th>{{ t('admin.target') }}</th><th>{{ t('admin.result') }}</th></tr></thead><tbody><tr v-for="entry in audit" :key="entry.id"><td>{{ date(entry.created) }}</td><td>{{ entry.username }}</td><td>{{ eventLabel(entry.action) }}</td><td class="file-name">{{ entry.target }}</td><td>{{ eventLabel(entry.result) }}</td></tr></tbody></table></div>
|
||||
</template>
|
||||
<dialog ref="confirmDialog" class="admin-confirm" :aria-label="t('admin.confirm')" @close="confirmation = null"><p>{{ confirmation?.message }}</p><div class="admin-actions"><button @click="confirmDialog?.close()">{{ t('admin.cancel') }}</button><button class="primary" @click="confirmed">{{ t('admin.confirm') }}</button></div></dialog>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.admin-page { min-width: 0; padding: 12px 0 40px; }
|
||||
.admin-top, .admin-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 22px; }
|
||||
h1 { margin: 0; font-size: 32px; } h2 { margin: 0; font-size: 25px; } h3 { margin: 24px 0 14px; font-size: 20px; } h4 { margin: 14px 0 8px; }
|
||||
.admin-actions, .row-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
|
||||
button { min-height: 38px; padding: 7px 13px; border: 1px solid var(--line); border-radius: 4px; color: var(--ink); background: var(--panel); cursor: pointer; font-size: 14px; }
|
||||
button:hover { border-color: var(--blue); color: var(--blue); } button:disabled { opacity: .5; cursor: wait; } .primary { background: var(--blue); color: white; border-color: var(--blue); } .primary:hover { color: white; filter: brightness(1.1); }
|
||||
.admin-tabs { display: flex; gap: 6px; overflow-x: auto; padding: 4px 0 18px; margin-bottom: 22px; }
|
||||
.admin-tabs button { flex-shrink: 0; border-color: transparent; background: transparent; } .admin-tabs [aria-current] { border-bottom: 2px solid var(--blue); color: var(--blue); border-radius: 0; font-weight: 700; }
|
||||
.admin-error, .admin-notice { padding: 12px 16px; border-left: 3px solid var(--blue); background: var(--panel); overflow-wrap: anywhere; } .admin-error { border-color: #d65252; }
|
||||
.admin-form { display: grid; gap: 18px; max-width: 1100px; } .admin-login { max-width: 420px; }
|
||||
label { display: flex; flex-direction: column; gap: 8px; min-width: 0; font-size: 14px; font-weight: 500; }
|
||||
input, textarea, select { width: 100%; min-width: 0; min-height: 40px; padding: 9px 11px; border: 1px solid var(--line); border-radius: 4px; background: var(--panel); color: var(--ink); font: inherit; }
|
||||
textarea { resize: vertical; line-height: 1.7; } .check { flex-direction: row; align-items: center; gap: 8px; } .check input { width: 18px; min-height: 18px; accent-color: var(--blue); }
|
||||
.admin-form > button { justify-self: start; } .admin-columns { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 18px; } .admin-array { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; padding: 14px 0; border-bottom: 1px solid var(--line); } .admin-array label { flex: 1; min-width: 160px; }
|
||||
.admin-related-links > button { margin-top: 16px; } .admin-related-links .admin-actions { gap: 6px; }
|
||||
.admin-table-wrap { max-width: 100%; overflow: auto; margin: 20px 0; } table { width: 100%; border-collapse: collapse; font-size: 14px; } th, td { padding: 12px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; } thead th { font-weight: 700; white-space: nowrap; } tbody th { font-weight: 500; }
|
||||
tr[data-status='syncing'] { background: color-mix(in srgb, var(--blue) 16%,transparent); } tr[data-status='failed'] { background: #df4d4d1c; } .row-actions { gap: 6px; min-width: 260px; } .row-actions button { font-size: 12px; padding: 4px 8px; min-height: 32px; }
|
||||
.admin-summary { display: flex; flex-wrap: wrap; gap: 28px 50px; } dt { color: var(--muted); font-size: 14px; } dd { margin: 10px 0 0; font-size: 24px; overflow-wrap: anywhere; }
|
||||
.admin-editor { margin: 30px 0; padding: 24px; border: 1px solid var(--line); border-radius: 5px; } .admin-editor h3 { margin-top: 0; } .admin-search { max-width: 360px; }
|
||||
.admin-log, .diff pre { padding: 16px; background: var(--panel); white-space: pre-wrap; overflow-wrap: anywhere; font: 12px/1.7 ui-monospace,monospace; max-height: 520px; overflow: auto; } .file-name { overflow-wrap: anywhere; }
|
||||
.image-field { display: grid; gap: 10px; align-content: start; } .image-field img { max-width: 230px; height: 65px; object-fit: contain; object-position: left; } .upload-label { font-size: 12px; } .upload-label input { font-size: 12px; }
|
||||
.admin-confirm { width: min(480px,calc(100% - 32px)); padding: 26px; border: 1px solid var(--line); border-radius: 6px; color: var(--ink); background: var(--panel); } .admin-confirm::backdrop { background: #0008; } .admin-confirm p { margin: 0 0 24px; line-height: 1.8; }
|
||||
@media(max-width:700px) {
|
||||
.jobs-table table, .jobs-table tbody { display: block; }
|
||||
.jobs-table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }
|
||||
.jobs-table tr { display: grid; grid-template-columns: minmax(0,1fr) auto auto; gap: 12px; padding: 16px 10px; border-bottom: 1px solid var(--line); }
|
||||
.jobs-table :is(td,th) { padding: 0; border: 0; overflow-wrap: anywhere; }
|
||||
.jobs-table td:last-child { grid-column: 1/-1; }
|
||||
.jobs-table .row-actions { min-width: 0; }
|
||||
.jobs-table .row-actions button { min-height: 40px; }
|
||||
h1 { font-size: 28px; } .admin-columns { grid-template-columns: 1fr; } .admin-editor { padding: 16px; } .admin-tabs { margin-right: -4px; } th, td { padding: 10px 8px; } .admin-summary { gap: 24px; } dd { font-size: 20px; } .admin-top { align-items: start; } }
|
||||
</style>
|
||||
@@ -0,0 +1,23 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import { marked } from 'marked'
|
||||
import DOMPurify from 'dompurify'
|
||||
const props = defineProps<{ source: string }>()
|
||||
const html = computed(() => DOMPurify.sanitize(marked.parse(props.source, { async: false, breaks: false }), {
|
||||
ALLOWED_TAGS: ['p', 'br', 'hr', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'strong', 'em', 'del', 'blockquote', 'pre', 'code', 'ul', 'ol', 'li', 'a', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'img'],
|
||||
ALLOWED_ATTR: ['href', 'src', 'alt', 'title', 'start'],
|
||||
ALLOW_DATA_ATTR: false,
|
||||
ALLOW_UNKNOWN_PROTOCOLS: false,
|
||||
}))
|
||||
</script>
|
||||
<template><div class="markdown-content" v-html="html" /></template>
|
||||
<style>
|
||||
.markdown-content { color: var(--ink); font-size: 14px; line-height: 1.85; overflow-wrap: anywhere; }
|
||||
.markdown-content > :first-child { margin-top: 0; }.markdown-content > :last-child { margin-bottom: 0; }
|
||||
.markdown-content :is(h1,h2,h3,h4,h5,h6) { margin: 1.4em 0 .6em; font-size: 1.15em; line-height: 1.4; }
|
||||
.markdown-content a { color: var(--blue); text-decoration: underline; text-underline-offset: 3px; }
|
||||
.markdown-content pre { max-width: 100%; overflow: auto; padding: 14px; border: 1px solid var(--line); border-radius: 5px; background: var(--surface); color: var(--ink); font: 12px/1.65 ui-monospace, Consolas, monospace; white-space: pre; }
|
||||
.markdown-content :not(pre)>code { padding: 2px 5px; border-radius: 3px; background: var(--surface); font: .9em ui-monospace, Consolas, monospace; }
|
||||
.markdown-content img { max-width: 100%; height: auto; }.markdown-content blockquote { margin-left: 0; padding-left: 16px; border-left: 3px solid var(--line); }
|
||||
.markdown-content table { display: block; max-width: 100%; overflow-x: auto; border-collapse: collapse; }.markdown-content th,.markdown-content td { padding: 6px 10px; border: 1px solid var(--line); }
|
||||
</style>
|
||||
@@ -48,7 +48,9 @@ const stats = computed(() => props.panel.series.map(series => ({
|
||||
mean: series.values.reduce((sum, value) => sum + value, 0) / Math.max(1, series.values.length),
|
||||
latest: series.values.at(-1) ?? 0,
|
||||
max: Math.max(0, ...series.values),
|
||||
...series.stats,
|
||||
})))
|
||||
const available = computed(() => props.panel.available !== false && props.timestamps.length > 1)
|
||||
const sampleIndex = computed(() => inspected.value === null ? null : Math.min(inspected.value, props.timestamps.length - 1))
|
||||
const tooltipLeft = computed(() => `${Math.max(4, Math.min(x(sampleIndex.value ?? 0) + 12, width.value - 206))}px`)
|
||||
function toggleSeries(id: string) {
|
||||
@@ -91,7 +93,8 @@ onBeforeUnmount(() => observer?.disconnect())
|
||||
<article class="monitor-chart" :data-panel="panel.id">
|
||||
<h3>{{ t(panel.titleKey) }}</h3>
|
||||
<div ref="plot" class="monitor-plot">
|
||||
<svg :viewBox="`0 0 ${width} ${height}`" role="img" :aria-label="t('monitor.chartHelp', { title: t(panel.titleKey) })" tabindex="0" @pointermove="inspectPointer" @pointerdown="inspectPointer" @pointerleave="leavePointer" @pointercancel="inspected = null" @blur="inspected = null" @keydown="onKeydown">
|
||||
<p v-if="!available" class="chart-empty">{{ t('monitor.collecting') }}</p>
|
||||
<svg v-else :viewBox="`0 0 ${width} ${height}`" role="img" :aria-label="t('monitor.chartHelp', { title: t(panel.titleKey) })" tabindex="0" @pointermove="inspectPointer" @pointerdown="inspectPointer" @pointerleave="leavePointer" @pointercancel="inspected = null" @blur="inspected = null" @keydown="onKeydown">
|
||||
<g v-for="tick in yTicks" :key="tick" class="chart-grid">
|
||||
<line :x1="left" :x2="width - right" :y1="y(tick)" :y2="y(tick)" />
|
||||
<text :x="left - 8" :y="y(tick) + 3" text-anchor="end">{{ format(tick) }}</text>
|
||||
@@ -109,13 +112,13 @@ onBeforeUnmount(() => observer?.disconnect())
|
||||
<circle v-for="series in paths" :key="series.id" :cx="x(sampleIndex)" :cy="y(series.plottedValues[sampleIndex]!)" r="3" :fill="series.color" />
|
||||
</g>
|
||||
</svg>
|
||||
<div v-if="sampleIndex !== null" class="chart-tooltip" :style="{ left: tooltipLeft }" aria-hidden="true">
|
||||
<div v-if="available && sampleIndex !== null" class="chart-tooltip" :style="{ left: tooltipLeft }" aria-hidden="true">
|
||||
<time :datetime="new Date(timestamps[sampleIndex]!).toISOString()">{{ d(timestamps[sampleIndex]!, 'monitorSample') }}</time>
|
||||
<div v-for="series in visible" :key="series.id"><span><i :style="{ background: series.color }" />{{ t(series.labelKey) }}</span><strong>{{ format(series.values[sampleIndex]!) }}</strong></div>
|
||||
</div>
|
||||
<p class="sr-only" aria-live="polite"><template v-if="keyboardInspection && sampleIndex !== null">{{ d(timestamps[sampleIndex]!, 'monitorSample') }}<span v-for="series in visible" :key="series.id"> {{ t(series.labelKey) }} {{ format(series.values[sampleIndex]!) }};</span></template></p>
|
||||
</div>
|
||||
<table class="chart-legend">
|
||||
<table v-if="available" class="chart-legend">
|
||||
<thead><tr><th scope="col">{{ t('monitor.legend.name') }}</th><th scope="col" class="chart-mean">{{ t('monitor.legend.mean') }}</th><th scope="col">{{ t('monitor.legend.latest') }}</th><th scope="col">{{ t('monitor.legend.max') }}</th></tr></thead>
|
||||
<tbody><tr v-for="series in stats" :key="series.id" :class="{ 'series-hidden': hidden.includes(series.id) }">
|
||||
<th scope="row"><button type="button" :aria-pressed="!hidden.includes(series.id)" :aria-label="t('monitor.toggleSeries', { name: t(series.labelKey) })" @click="toggleSeries(series.id)"><i :style="{ background: series.color }" />{{ t(series.labelKey) }}</button></th>
|
||||
@@ -129,6 +132,7 @@ onBeforeUnmount(() => observer?.disconnect())
|
||||
.monitor-chart { --chart-muted: color-mix(in srgb, var(--ink) 38%, var(--panel)); min-width: 0; padding: 16px 14px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); }
|
||||
h3 { margin: 0 0 10px; font-size: 14px; font-weight: 600; line-height: 1.5; }
|
||||
.monitor-plot { position: relative; height: 216px; }
|
||||
.chart-empty { height: 100%; display: grid; place-items: center; color: var(--ink-2); font-size: 13px; }
|
||||
svg { display: block; width: 100%; height: 100%; overflow: visible; touch-action: pan-y; }
|
||||
.chart-grid line { stroke: var(--line); stroke-opacity: .45; stroke-width: .5; }
|
||||
.chart-grid text { fill: color-mix(in srgb, var(--ink) 75%, var(--panel)); font-size: 10px; font-variant-numeric: tabular-nums; }
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, ref, toRef } from 'vue'
|
||||
import { computed, onBeforeUnmount, onMounted, ref, toRef, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { Pause, Play, Search, X } from 'lucide-vue-next'
|
||||
import type { Mirror } from '../data'
|
||||
import { createMonitoringSnapshot, demoDisks, type MonitoringRange } from '../monitoring'
|
||||
import { type MonitoringRange, type LiveMonitoringSnapshot } from '../monitoring'
|
||||
import { api } from '../api'
|
||||
import { useMirrorFormats } from '../useMirrorFormats'
|
||||
import MonitoringChart from './MonitoringChart.vue'
|
||||
import RollingText from './RollingText.vue'
|
||||
@@ -17,15 +18,49 @@ const ranges: { hours: MonitoringRange; key: string }[] = [
|
||||
{ hours: 1, key: 'one' }, { hours: 6, key: 'six' }, { hours: 24, key: 'day' },
|
||||
]
|
||||
const refreshing = ref(true)
|
||||
const snapshotAt = ref(Date.now())
|
||||
const snapshot = computed(() => createMonitoringSnapshot(snapshotAt.value, range.value))
|
||||
const snapshot = ref<LiveMonitoringSnapshot>({ timestamps: [], panels: [], disks: [], updatedAt: 0, stale: false })
|
||||
const snapshotAt = computed(() => snapshot.value.updatedAt)
|
||||
const loading = ref(true)
|
||||
const failed = ref(false)
|
||||
interface SpeedSnapshot { updatedAt: number; interval: number; stale: boolean; available: boolean; jobs: Record<string, { bytesPerSecond: number; connections: number }> }
|
||||
const speeds = ref<SpeedSnapshot | null>(null)
|
||||
const speedLoading = ref(true)
|
||||
let speedController: AbortController | undefined
|
||||
async function refreshSpeeds() {
|
||||
speedController?.abort(); speedController = new AbortController()
|
||||
try { speeds.value = await api<SpeedSnapshot>('/job-speeds', { signal: speedController.signal }) }
|
||||
catch (error) { if ((error as Error).name !== 'AbortError') speeds.value = null }
|
||||
finally { speedLoading.value = false }
|
||||
}
|
||||
function pullSpeed(mirror: Mirror) {
|
||||
if (mirror.status !== 'syncing') return t('common.emptyValue')
|
||||
if (speedLoading.value || (speeds.value && !speeds.value.available && !speeds.value.stale)) return t('monitor.speedCollecting')
|
||||
const sample = speeds.value?.jobs[mirror.name]
|
||||
if (!sample || speeds.value?.stale || props.now - speeds.value!.updatedAt > 15000) return t('common.emptyValue')
|
||||
const bytes = sample.bytesPerSecond
|
||||
const unit = bytes >= 1e9 ? 'gigabytes' : bytes >= 1e6 ? 'megabytes' : bytes >= 1e3 ? 'kilobytes' : 'bytes'
|
||||
const scale = { gigabytes: 1e9, megabytes: 1e6, kilobytes: 1e3, bytes: 1 }[unit]
|
||||
return t(`monitor.speedUnits.${unit}`, { value: n(bytes / scale, 'size') })
|
||||
}
|
||||
let controller: AbortController | undefined
|
||||
let sequence = 0
|
||||
async function refresh() {
|
||||
const request = ++sequence
|
||||
controller?.abort(); controller = new AbortController()
|
||||
try {
|
||||
const data = await api<LiveMonitoringSnapshot>(`/monitoring?hours=${range.value}`, { signal: controller.signal })
|
||||
if (request === sequence) { snapshot.value = data; failed.value = false }
|
||||
} catch (error) { if ((error as Error).name !== 'AbortError' && request === sequence) failed.value = true }
|
||||
finally { if (request === sequence) loading.value = false }
|
||||
}
|
||||
watch(range, () => { void refresh() })
|
||||
const search = ref('')
|
||||
const searchInput = ref<HTMLInputElement>()
|
||||
const filteredMirrors = computed(() => {
|
||||
const query = search.value.trim().toLowerCase()
|
||||
return props.mirrors.filter(mirror => `${mirror.name} ${mirror.upstream}`.toLowerCase().includes(query))
|
||||
})
|
||||
const capacity = (value: number) => t('monitor.units.capacity', { value: n(value, 'size') })
|
||||
const capacity = (value: number) => t(value < 1 ? 'monitor.capacityGB' : 'monitor.units.capacity', { value: n(value < 1 ? value * 1000 : value, 'size') })
|
||||
function syncInfo(mirror: Mirror, next: boolean) {
|
||||
if (next && mirror.nextSync !== null) return { key: 'next', timestamp: mirror.nextSync, next: true }
|
||||
if (mirror.status === 'syncing' && mirror.syncStarted !== null) return { key: 'started', timestamp: mirror.syncStarted, next: false }
|
||||
@@ -40,23 +75,28 @@ function syncTime(mirror: Mirror, next: boolean) {
|
||||
function focusSearch() { searchInput.value?.focus() }
|
||||
defineExpose({ focusSearch })
|
||||
let timer: ReturnType<typeof setInterval> | undefined
|
||||
let speedTimer: ReturnType<typeof setInterval> | undefined
|
||||
onMounted(() => {
|
||||
void refresh()
|
||||
void refreshSpeeds()
|
||||
speedTimer = setInterval(() => { if (refreshing.value && !document.hidden) void refreshSpeeds() }, 2000)
|
||||
timer = setInterval(() => {
|
||||
if (refreshing.value && !document.hidden) snapshotAt.value = Date.now()
|
||||
}, 5000)
|
||||
if (refreshing.value && !document.hidden) void refresh()
|
||||
}, 10000)
|
||||
})
|
||||
onBeforeUnmount(() => clearInterval(timer))
|
||||
onBeforeUnmount(() => { sequence++; controller?.abort(); speedController?.abort(); clearInterval(timer); clearInterval(speedTimer) })
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section id="site-status" class="status-page" tabindex="-1" :data-range="range" :data-updated="snapshotAt" :data-refreshing="refreshing">
|
||||
<div class="section-heading monitor-heading">
|
||||
<div class="monitor-title"><h1>{{ t('monitor.title') }}</h1><span class="monitor-demo">{{ t('monitor.demo') }}</span></div>
|
||||
<div class="monitor-title"><h1>{{ t('monitor.title') }}</h1><span class="monitor-demo">{{ t(failed || snapshot.stale ? 'monitor.stale' : 'monitor.live') }}</span></div>
|
||||
<div class="monitor-controls">
|
||||
<div class="monitor-ranges" role="group" :aria-label="t('monitor.range')"><button v-for="choice in ranges" :key="choice.hours" type="button" :aria-pressed="range === choice.hours" @click="range = choice.hours">{{ t(`monitor.ranges.${choice.key}`) }}</button></div>
|
||||
<button type="button" class="monitor-refresh" @click="refreshing = !refreshing; if (refreshing) snapshotAt = Date.now()"><Pause v-if="refreshing" :size="14" aria-hidden="true" /><Play v-else :size="14" aria-hidden="true" />{{ t(refreshing ? 'monitor.pauseRefresh' : 'monitor.resumeRefresh') }}</button>
|
||||
<button type="button" class="monitor-refresh" @click="refreshing = !refreshing; if (refreshing) refresh()"><Pause v-if="refreshing" :size="14" aria-hidden="true" /><Play v-else :size="14" aria-hidden="true" />{{ t(refreshing ? 'monitor.pauseRefresh' : 'monitor.resumeRefresh') }}</button>
|
||||
</div>
|
||||
</div>
|
||||
<p v-if="loading || failed" role="status">{{ t(loading ? 'monitor.loading' : 'monitor.unavailable') }}</p>
|
||||
|
||||
<section class="monitor-section" aria-labelledby="monitor-network">
|
||||
<div class="side-heading monitor-section-heading"><h2 id="monitor-network">{{ t('monitor.network') }}</h2></div>
|
||||
@@ -66,7 +106,7 @@ onBeforeUnmount(() => clearInterval(timer))
|
||||
<section class="monitor-section" aria-labelledby="monitor-disk">
|
||||
<div class="side-heading monitor-section-heading"><h2 id="monitor-disk">{{ t('monitor.disk') }}</h2></div>
|
||||
<div class="disk-capacities" role="group" :aria-label="t('monitor.capacity')">
|
||||
<div v-for="disk in demoDisks" :key="disk.name" class="disk-capacity">
|
||||
<div v-for="disk in snapshot.disks" :key="disk.name" class="disk-capacity">
|
||||
<div class="disk-caption"><span>{{ disk.name }}</span><span>{{ t('monitor.diskUsage', { used: capacity(disk.used), total: capacity(disk.total) }) }}</span></div>
|
||||
<div class="disk-track" role="meter" :aria-label="t('monitor.diskDescription', { name: disk.name, used: capacity(disk.used), total: capacity(disk.total) })" :aria-valuemin="0" :aria-valuemax="disk.total" :aria-valuenow="disk.used"><span :style="{ width: `${disk.used / disk.total * 100}%` }" /></div>
|
||||
</div>
|
||||
@@ -85,13 +125,14 @@ onBeforeUnmount(() => clearInterval(timer))
|
||||
<div class="search-box sync-search"><Search :size="17" aria-hidden="true" /><input ref="searchInput" v-model="search" type="search" :placeholder="t('monitor.search')" :aria-label="t('monitor.search')" autocomplete="off" /><button v-if="search" class="icon-button clear-search" :aria-label="t('mirrors.clear')" @click="search = ''; focusSearch()"><X :size="15" /></button></div>
|
||||
</div>
|
||||
<table class="sync-table" role="table" aria-labelledby="monitor-sync">
|
||||
<thead role="rowgroup"><tr role="row"><th v-for="column in ['name', 'update', 'upstream', 'status', 'size']" :key="column" role="columnheader" scope="col" :data-column="column">{{ t(`monitor.columns.${column}`) }}</th></tr></thead>
|
||||
<thead role="rowgroup"><tr role="row"><th v-for="column in ['name', 'update', 'upstream', 'status', 'size', 'speed']" :key="column" role="columnheader" scope="col" :data-column="column" :title="column === 'speed' ? t('monitor.speedDescription') : undefined">{{ t(`monitor.columns.${column}`) }}</th></tr></thead>
|
||||
<tbody role="rowgroup"><tr v-for="mirror in filteredMirrors" :key="mirror.name" role="row" :data-status="mirror.status">
|
||||
<td role="cell" data-column="name"><button class="sync-name" :aria-label="t('mirrors.browse', { name: mirror.name })" @click="emit('mirror', mirror)">{{ mirror.name }}</button></td>
|
||||
<td role="cell" data-column="update" :title="fullTime(syncInfo(mirror, showNextSync).timestamp)"><div class="sync-update"><RollingText :value="syncLabel(mirror, showNextSync)" :cycle="cycle" :reserve="[syncLabel(mirror, false), syncLabel(mirror, true)]" :shuffle="false" /><RollingText :value="syncTime(mirror, showNextSync)" :cycle="cycle" :reserve="[syncTime(mirror, false), syncTime(mirror, true)]" :shuffle="false" /></div></td>
|
||||
<td role="cell" data-column="upstream"><span class="sync-mobile-label" aria-hidden="true">{{ t('monitor.columns.upstream') }}</span><a :href="mirror.upstream" target="_blank" rel="noopener noreferrer">{{ mirror.upstream }}</a></td>
|
||||
<td role="cell" data-column="status">{{ t(`mirrors.status.${mirror.status}`) }}</td>
|
||||
<td role="cell" data-column="size"><span class="sync-mobile-label" aria-hidden="true">{{ t('monitor.columns.size') }}</span><span>{{ mirrorSize(mirror) }}</span></td>
|
||||
<td role="cell" data-column="speed" :title="t('monitor.speedDescription')"><span class="sync-mobile-label" aria-hidden="true">{{ t('monitor.columns.speed') }}</span><span>{{ pullSpeed(mirror) }}</span></td>
|
||||
</tr></tbody>
|
||||
</table>
|
||||
<div v-if="!filteredMirrors.length" class="sync-empty"><p>{{ t('mirrors.empty') }}</p><button class="text-button" @click="search = ''; focusSearch()">{{ t('mirrors.reset') }}</button></div>
|
||||
@@ -123,11 +164,12 @@ onBeforeUnmount(() => clearInterval(timer))
|
||||
.sync-search input { font-size: 12px; }
|
||||
.sync-table { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 13px; }
|
||||
.sync-table th { padding: 11px 10px; border-bottom: 2px solid var(--line); font-size: 14px; font-weight: 700; text-align: left; }
|
||||
.sync-table th[data-column='name'] { width: 15%; }
|
||||
.sync-table th[data-column='update'] { width: 29%; }
|
||||
.sync-table th[data-column='upstream'] { width: 36%; }
|
||||
.sync-table th[data-column='status'] { width: 12%; }
|
||||
.sync-table th[data-column='size'] { width: 8%; }
|
||||
.sync-table th[data-column='name'] { width: 14%; }
|
||||
.sync-table th[data-column='update'] { width: 26%; }
|
||||
.sync-table th[data-column='upstream'] { width: 29%; }
|
||||
.sync-table th[data-column='status'] { width: 10%; }
|
||||
.sync-table th[data-column='size'] { width: 9%; }
|
||||
.sync-table th[data-column='speed'] { width: 12%; }
|
||||
.sync-table td { padding: 7px 10px; height: 46px; border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }
|
||||
.sync-table :is(th, td):first-child { padding-left: 0; }
|
||||
.sync-table :is(th, td):last-child { padding-right: 0; }
|
||||
@@ -138,14 +180,16 @@ onBeforeUnmount(() => clearInterval(timer))
|
||||
.sync-update { display: grid; grid-template-columns: max-content minmax(0, 1fr); align-items: center; gap: 12px; font-size: 12px; font-variant-numeric: tabular-nums; }
|
||||
.sync-table td[data-column='upstream'] { font-size: 12px; }
|
||||
.sync-table td[data-column='size'] { font-variant-numeric: tabular-nums; white-space: nowrap; }
|
||||
.sync-table td[data-column='speed'] { font-variant-numeric: tabular-nums; white-space: nowrap; font-size: 12px; }
|
||||
.sync-mobile-label { display: none; }
|
||||
.sync-empty { padding: 32px 0; text-align: center; color: var(--ink-2); font-size: 14px; }
|
||||
@media (max-width: 980px) {
|
||||
.sync-table th[data-column='name'] { width: 16%; }
|
||||
.sync-table th[data-column='update'] { width: 26%; }
|
||||
.sync-table th[data-column='upstream'] { width: 34%; }
|
||||
.sync-table th[data-column='status'] { width: 14%; }
|
||||
.sync-table th[data-column='update'] { width: 22%; }
|
||||
.sync-table th[data-column='upstream'] { width: 27%; }
|
||||
.sync-table th[data-column='status'] { width: 12%; }
|
||||
.sync-table th[data-column='size'] { width: 10%; }
|
||||
.sync-table th[data-column='speed'] { width: 13%; }
|
||||
.sync-update { grid-template-columns: minmax(0, 1fr); gap: 2px; }
|
||||
}
|
||||
@media (max-width: 900px) { .monitor-grid { grid-template-columns: minmax(0, 1fr); gap: 18px; } }
|
||||
@@ -169,7 +213,7 @@ onBeforeUnmount(() => clearInterval(timer))
|
||||
.sync-table td[data-column='name'] { grid-column: 1; grid-row: 1; }
|
||||
.sync-table td[data-column='status'] { grid-column: 2; grid-row: 1; align-self: center; }
|
||||
.sync-table td[data-column='update'] { grid-column: 1 / -1; }
|
||||
.sync-table td[data-column='upstream'], .sync-table td[data-column='size'] { display: grid; grid-column: 1 / -1; grid-template-columns: 64px minmax(0, 1fr); align-items: baseline; gap: 10px; font-size: 12px; }
|
||||
.sync-table td[data-column='upstream'], .sync-table td[data-column='size'], .sync-table td[data-column='speed'] { display: grid; grid-column: 1 / -1; grid-template-columns: 64px minmax(0, 1fr); align-items: baseline; gap: 10px; font-size: 12px; }
|
||||
.sync-table td[data-column='upstream'] a { color: var(--blue); }
|
||||
.sync-name { min-height: 36px; font-size: 14px; }
|
||||
.sync-update { grid-template-columns: max-content minmax(0, 1fr); gap: 8px; }
|
||||
|
||||
@@ -6,6 +6,9 @@ export interface Mirror {
|
||||
color: string
|
||||
category: Exclude<Category, 'all'>
|
||||
descriptionKey: string
|
||||
description?: Record<string, string>
|
||||
help?: string
|
||||
browseUrl?: string
|
||||
status: MirrorStatus
|
||||
lastSuccess: number | null
|
||||
nextSync: number | null
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
const pairs = {
|
||||
relatedLinks: ['首页相关链接', 'Home page related links'], linkType: ['链接类型', 'Link type'], urlLink: ['网址', 'URL'], helpLink: ['软件源使用帮助', 'Mirror usage guides'], linkEntry: ['相关链接 {index}', 'Related link {index}'], moveUp: ['上移', 'Move up'], moveDown: ['下移', 'Move down'],
|
||||
helpChinese: ['使用说明(中文,Markdown)', 'Usage guide (Chinese, Markdown)'], descriptionLanguage: ['介绍语言', 'Description language'],
|
||||
contentLanguage: ['内容语言', 'Content language'],
|
||||
title: ['站点管理', 'Site administration'], login: ['登录', 'Sign in'], logout: ['退出登录', 'Sign out'], username: ['用户名', 'Username'], password: ['密码', 'Password'], checking: ['正在检查登录状态…', 'Checking session…'],
|
||||
overview: ['运行概况', 'Overview'], mirrors: ['镜像与任务', 'Mirrors & tasks'], announcements: ['公告', 'Announcements'], config: ['同步配置', 'Sync configuration'], settings: ['站点设置', 'Site settings'], users: ['账户', 'Accounts'], audit: ['操作记录', 'Audit log'],
|
||||
save: ['保存', 'Save'], saved: ['已保存', 'Saved'], cancel: ['取消', 'Cancel'], confirm: ['确认', 'Confirm'], close: ['关闭', 'Close'], edit: ['编辑', 'Edit'], remove: ['删除', 'Delete'], add: ['添加', 'Add'], refresh: ['刷新', 'Refresh'], loading: ['加载中…', 'Loading…'], empty: ['暂无内容', 'No entries'], busy: ['处理中…', 'Working…'],
|
||||
worker: ['同步节点', 'Worker'], total: ['镜像总数', 'Mirrors'], active: ['正在同步', 'Syncing'], failed: ['同步失败', 'Failed'], updated: ['最近采集', 'Last collected'], sourceError: ['同步节点暂时不可用,显示最近一次数据。', 'Worker unavailable. Showing the last collected data.'],
|
||||
name: ['名称', 'Name'], status: ['状态', 'Status'], upstream: ['上游地址', 'Upstream'], size: ['大小', 'Size'], actions: ['操作', 'Actions'], search: ['搜索镜像', 'Search mirrors'], metadata: ['展示信息', 'Display information'], description: ['介绍', 'Description'], help: ['使用说明(Markdown)', 'Help (Markdown)'], website: ['官方网站', 'Official website'], visible: ['在首页显示', 'Show on home page'], category: ['分类', 'Category'], os: ['操作系统', 'Operating system'], language: ['语言与工具', 'Languages & tools'], other: ['其他', 'Other'],
|
||||
start: ['开始同步', 'Start sync'], stop: ['停止同步', 'Stop sync'], restart: ['重新同步', 'Restart sync'], disable: ['禁用同步', 'Disable sync'], logs: ['日志', 'Logs'], logTail: ['显示日志末尾,最多 64 KiB。', 'Showing the log tail, up to 64 KiB.'], commandConfirm: ['确认对 {name} 执行“{action}”?这会影响实际同步任务。', 'Run “{action}” for {name}? This affects the live sync task.'], submitted: ['命令已提交,实际状态稍后更新。', 'Command submitted. The live status will update shortly.'],
|
||||
newArticle: ['新建公告', 'New announcement'], articleTitle: ['标题', 'Title'], body: ['正文(Markdown)', 'Body (Markdown)'], date: ['发布时间', 'Publication date'], published: ['发布', 'Published'], draft: ['草稿', 'Draft'], pinned: ['置顶', 'Pinned'], preview: ['预览', 'Preview'], deleteConfirm: ['确认删除 {name}?', 'Delete {name}?'],
|
||||
siteName: ['站点名称', 'Site name'], email: ['联系邮箱', 'Contact email'], address: ['地址', 'Address'], logoLight: ['亮色主题 Logo', 'Light theme logo'], logoDark: ['暗色主题 Logo', 'Dark theme logo'], favicon: ['网站图标', 'Favicon'], avatar: ['站点头像', 'Site avatar'], upload: ['上传 PNG / JPEG', 'Upload PNG / JPEG'], links: ['友情链接', 'Footer links'], downloads: ['常用下载', 'Downloads'], url: ['链接', 'URL'], version: ['版本', 'Version'],
|
||||
currentPassword: ['当前密码', 'Current password'], newPassword: ['新密码', 'New password'], confirmPassword: ['确认新密码', 'Confirm new password'], changePassword: ['修改密码', 'Change password'], passwordHint: ['密码至少 12 个字符。修改后需重新登录。', 'Use at least 12 characters. Changing your password signs out all your sessions.'], passwordMismatch: ['两次输入的密码不同。', 'Passwords do not match.'], newUser: ['创建账户', 'Create account'], role: ['权限', 'Role'], admin: ['管理员', 'Administrator'], editor: ['公告编辑', 'Announcement editor'],
|
||||
concurrent: ['最大并发任务', 'Maximum concurrent jobs'], interval: ['同步间隔(分钟)', 'Sync interval (minutes)'], retry: ['失败重试次数', 'Retries'], timeout: ['超时(秒)', 'Timeout (seconds)'], global: ['全局调度', 'Global schedule'], mirrorConfig: ['单个镜像', 'Individual mirror'], newMirror: ['新增镜像', 'New mirror'], provider: ['同步方式', 'Provider'], intervalHint: ['同步间隔从上次同步结束后计算。配置应用时将通知 Worker 重新加载。', 'The interval starts when the previous sync finishes. Applying configuration asks the worker to reload.'], previewConfig: ['生成变更预览', 'Preview changes'], apply: ['应用配置', 'Apply configuration'], applyConfirm: ['确认应用此配置并通知 Worker 重新加载?', 'Apply this configuration and ask the worker to reload?'], before: ['修改前', 'Before'], after: ['修改后', 'After'], versions: ['配置历史', 'Configuration history'], rollback: ['恢复此版本之前的配置', 'Restore configuration before this version'], rollbackConfirm: ['确认恢复此变更前的配置并重新加载?后续有冲突时会拒绝恢复。', 'Restore the configuration before this change and reload? Conflicting subsequent changes will block the restore.'], applied: ['已应用', 'Applied'],
|
||||
target: ['对象', 'Target'], action: ['动作', 'Action'], result: ['结果', 'Result'], time: ['时间', 'Time'], actor: ['操作人', 'User'], notice: ['提示', 'Notice'], conflictHint: ['数据已被其他操作修改,请重新加载后再编辑。', 'Another operation changed this data. Reload before editing.'],
|
||||
} as const
|
||||
const errors = {
|
||||
NETWORK_ERROR: ['网络连接失败。', 'Network connection failed.'], INVALID_RESPONSE: ['服务器响应无效。', 'Invalid server response.'], INTERNAL_ERROR: ['服务器处理失败,请稍后重试。', 'Server error. Try again shortly.'], INVALID_INPUT: ['请检查填写的内容和数值范围。', 'Check the form values and allowed ranges.'], UNAUTHORIZED: ['请重新登录。', 'Please sign in again.'], FORBIDDEN: ['没有执行此操作的权限。', 'You do not have permission for this action.'], CSRF_INVALID: ['登录状态已变化,请重新登录。', 'Your session has changed. Sign in again.'], INVALID_CREDENTIALS: ['用户名或密码不正确。', 'Incorrect username or password.'], RATE_LIMITED: ['登录尝试过于频繁,请稍后重试。', 'Too many sign-in attempts. Try again shortly.'], CONFLICT: ['内容已发生变化,请刷新后重新编辑。', 'The content has changed. Reload and edit again.'], NOT_FOUND: ['未找到对应记录。', 'The entry was not found.'], CONFIG_UNAVAILABLE: ['无法读取 Worker 配置。', 'Worker configuration is unavailable.'], NO_CHANGES: ['配置没有变化。', 'There are no configuration changes.'], CONFIG_APPLY_FAILED: ['配置应用失败,已尝试恢复原文件。请检查 Worker 状态。', 'Configuration failed. Original files were restored where possible. Check the worker status.'], CONFIG_ROLLBACK_FAILED: ['配置恢复未完成,需要检查服务器配置与 Worker 状态。', 'Configuration restore did not complete. Check the server files and worker.'], CONFIG_STATE_UNCERTAIN: ['文件可能已生效,但版本记录未完成,请检查配置。', 'Files may have been applied, but the version record failed. Check the configuration.'], COMMAND_UNCERTAIN: ['命令结果未确认,请检查节点状态后再操作。', 'Command delivery is unconfirmed. Check the worker before retrying.'], MANAGER_UNAVAILABLE: ['同步管理器暂时不可用。', 'The sync manager is unavailable.'], LOGS_UNAVAILABLE: ['日志暂时不可用。', 'Logs are unavailable.'], UPLOAD_INVALID: ['请选择不超过 4 MB 的 PNG 或 JPEG 图片。', 'Choose a PNG or JPEG image of at most 4 MB.'], INVALID_PASSWORD: ['密码需要 12 至 256 个字符。', 'The password must contain 12 to 256 characters.'], TUNASYNC_UNAVAILABLE: ['同步节点暂时不可用。', 'The sync worker is unavailable.'], LOG_UNAVAILABLE: ['日志暂时不可用。', 'Logs are unavailable.'], INVALID_IMAGE: ['图片无效,请使用不超过 4 MB 的 PNG 或 JPEG。', 'Use a valid PNG or JPEG image of at most 4 MB.'], PASSWORD_TOO_SHORT: ['密码至少需要 12 个字符。', 'Use at least 12 characters for the password.'],
|
||||
} as const
|
||||
const events = {
|
||||
login: ['登录', 'Sign in'], logout: ['退出', 'Sign out'], password_change: ['修改密码', 'Change password'], user_create: ['创建账户', 'Create account'], record_save: ['保存内容', 'Save content'], record_delete: ['删除内容', 'Delete content'], upload: ['上传图片', 'Upload image'], config_preview: ['预览配置', 'Preview configuration'], config_apply: ['应用配置', 'Apply configuration'], config_rollback: ['恢复配置', 'Restore configuration'], password: ['修改密码', 'Change password'], job_start: ['开始同步', 'Start sync'], job_stop: ['停止同步', 'Stop sync'], job_restart: ['重新同步', 'Restart sync'], job_disable: ['禁用同步', 'Disable sync'], denied: ['已拒绝', 'Denied'], uncertain: ['未确认', 'Unconfirmed'], command: ['任务命令', 'Task command'], success: ['成功', 'Success'], submitted: ['已提交', 'Submitted'], failed: ['失败', 'Failed'], draft: ['草稿', 'Draft'], applied: ['已应用', 'Applied'],
|
||||
} as const
|
||||
const translate = (source: Record<string, readonly [string, string]>, index: number) => Object.fromEntries(Object.entries(source).map(([key, value]) => [key, value[index]!]))
|
||||
export const adminZh = { ...translate(pairs, 0), errors: translate(errors, 0), events: translate(events, 0) }
|
||||
export const adminEn = { ...translate(pairs, 1), errors: translate(errors, 1), events: translate(events, 1) }
|
||||
@@ -1,4 +1,13 @@
|
||||
export default {
|
||||
skipAdmin: 'Skip to administration',
|
||||
loading: 'Loading mirrors…',
|
||||
stale: 'Connection interrupted. Showing the last collected data.',
|
||||
unavailable: 'Mirror data is unavailable. Refresh shortly.',
|
||||
noAnnouncements: 'No announcements',
|
||||
noDownloads: 'No downloads configured',
|
||||
noHelp: 'No usage guide has been added for this mirror.',
|
||||
openDirectory: 'Browse mirror directory',
|
||||
|
||||
"brand": "MirrorOUC",
|
||||
"university": "Ocean University of China",
|
||||
"siteName": "Ocean University of China Software Mirrors",
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
export default {
|
||||
speedCollecting: 'Sampling',
|
||||
speedDescription: 'TCP receive rate across this mirror’s parallel upstream connections, sampled about every 2 seconds.',
|
||||
speedUnits: { bytes: '{value} B/s', kilobytes: '{value} KB/s', megabytes: '{value} MB/s', gigabytes: '{value} GB/s' },
|
||||
live: 'Live monitoring',
|
||||
stale: 'Metrics collection interrupted',
|
||||
loading: 'Loading monitoring data…',
|
||||
unavailable: 'Monitoring data is unavailable.',
|
||||
collecting: 'Waiting for valid samples…',
|
||||
capacityGB: '{value} GB',
|
||||
|
||||
title: 'Server monitoring',
|
||||
demo: 'Demo data',
|
||||
range: 'Monitoring time range',
|
||||
@@ -33,6 +43,6 @@ export default {
|
||||
chartHelp: '{title}. Use Left / Right to inspect samples, Home / End to jump to the endpoints, and Esc to dismiss the tooltip.',
|
||||
toggleSeries: 'Show or hide {name}',
|
||||
search: 'Search mirrors or upstream URLs…',
|
||||
columns: { name: 'Mirror', update: 'Sync time', upstream: 'Upstream', status: 'Status', size: 'Size' },
|
||||
columns: { speed: 'Pull speed', name: 'Mirror', update: 'Sync time', upstream: 'Upstream', status: 'Status', size: 'Size' },
|
||||
syncTime: { success: 'Last successful sync', attempt: 'Last attempted sync', started: 'Sync started', next: 'Next scheduled sync' },
|
||||
}
|
||||
|
||||
@@ -7,12 +7,13 @@ import mirrorsEn from './en-US/mirrors'
|
||||
import announcementsEn from './en-US/announcements'
|
||||
import monitorZh from './zh-CN/monitor'
|
||||
import monitorEn from './en-US/monitor'
|
||||
import { adminZh, adminEn } from './admin-messages'
|
||||
import { datetimeFormats as datetimeZh, numberFormats as numbersZh } from './zh-CN/formats'
|
||||
import { datetimeFormats as datetimeEn, numberFormats as numbersEn } from './en-US/formats'
|
||||
|
||||
export const messages = {
|
||||
'zh-CN': { common: commonZh, mirrors: mirrorsZh, announcements: announcementsZh, monitor: monitorZh },
|
||||
'en-US': { common: commonEn, mirrors: mirrorsEn, announcements: announcementsEn, monitor: monitorEn },
|
||||
'zh-CN': { common: commonZh, mirrors: mirrorsZh, announcements: announcementsZh, monitor: monitorZh, admin: adminZh },
|
||||
'en-US': { common: commonEn, mirrors: mirrorsEn, announcements: announcementsEn, monitor: monitorEn, admin: adminEn },
|
||||
}
|
||||
export type Locale = keyof typeof messages
|
||||
export const supportedLocales = Object.keys(messages) as Locale[]
|
||||
|
||||
@@ -1,4 +1,13 @@
|
||||
export default {
|
||||
skipAdmin: '跳转到站点管理',
|
||||
loading: '正在加载镜像数据…',
|
||||
stale: '连接暂时中断,显示最近一次同步数据。',
|
||||
unavailable: '镜像数据暂时不可用,请稍后刷新。',
|
||||
noAnnouncements: '暂无公告',
|
||||
noDownloads: '暂无下载项',
|
||||
noHelp: '此镜像暂未添加使用说明。',
|
||||
openDirectory: '浏览镜像目录',
|
||||
|
||||
"brand": "MirrorOUC",
|
||||
"university": "中国海洋大学",
|
||||
"siteName": "中国海洋大学软件镜像源",
|
||||
|
||||
@@ -1,4 +1,14 @@
|
||||
export default {
|
||||
speedCollecting: '采集中',
|
||||
speedDescription: '约 2 秒采样窗口内,此仓库全部并行上游连接的 TCP 接收速率。',
|
||||
speedUnits: { bytes: '{value} B/s', kilobytes: '{value} KB/s', megabytes: '{value} MB/s', gigabytes: '{value} GB/s' },
|
||||
live: '实时监控',
|
||||
stale: '数据采集暂时中断',
|
||||
loading: '正在获取监控数据…',
|
||||
unavailable: '暂时无法获取监控数据。',
|
||||
collecting: '正在等待有效采样…',
|
||||
capacityGB: '{value} GB',
|
||||
|
||||
title: '服务器监控',
|
||||
demo: '演示数据',
|
||||
range: '监控时间范围',
|
||||
@@ -33,6 +43,6 @@ export default {
|
||||
chartHelp: '{title}。使用左右方向键查看时间点,Home / End 跳到首尾,Esc 关闭提示。',
|
||||
toggleSeries: '显示或隐藏 {name}',
|
||||
search: '搜索镜像名称或上游地址…',
|
||||
columns: { name: '镜像名称', update: '同步时间', upstream: '上游地址', status: '状态', size: '大小' },
|
||||
columns: { speed: '拉取速度', name: '镜像名称', update: '同步时间', upstream: '上游地址', status: '状态', size: '大小' },
|
||||
syncTime: { success: '最近成功同步', attempt: '最近尝试同步', started: '本次同步开始', next: '下次计划同步' },
|
||||
}
|
||||
|
||||
+3
-1
@@ -1,6 +1,6 @@
|
||||
export type MonitoringRange = 1 | 6 | 24
|
||||
export type MetricUnit = 'gigabits' | 'gigabytes' | 'operations' | 'percent' | 'memory'
|
||||
export interface MetricSeries { id: string; labelKey: string; color: string; values: number[] }
|
||||
export interface MetricSeries { id: string; labelKey: string; color: string; values: number[]; stats?: { mean: number; latest: number; max: number } }
|
||||
export interface MonitoringPanel {
|
||||
id: string
|
||||
titleKey: string
|
||||
@@ -8,8 +8,10 @@ export interface MonitoringPanel {
|
||||
maximum: number
|
||||
stacked: boolean
|
||||
series: MetricSeries[]
|
||||
available?: boolean
|
||||
}
|
||||
export interface MonitoringSnapshot { timestamps: number[]; panels: MonitoringPanel[] }
|
||||
export interface LiveMonitoringSnapshot extends MonitoringSnapshot { disks: { name: string; used: number; total: number }[]; updatedAt: number; stale: boolean }
|
||||
|
||||
export const demoDisks = [
|
||||
{ name: '/srv/mirrors', used: 43.2, total: 64 },
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
:root {
|
||||
--muted: var(--ink-2);
|
||||
/* Palette from itouc.cn; source values and component mapping are in DESIGN.md. */
|
||||
--blue: #0969da; --blue-bright: #58a6ff; --blue-soft: rgba(9, 105, 218, .08);
|
||||
--ink: #24292f; --ink-2: #343a40; --ink-3: var(--ink-2); --line: #d0d7de; --line-soft: #d8dee4;
|
||||
@@ -234,3 +235,6 @@ h2 { margin: 0; font-size: 19px; font-weight: 600; letter-spacing: -.04em; }
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; } }
|
||||
:focus-visible { outline: 2px solid var(--blue-bright); outline-offset: 3px; }
|
||||
|
||||
.data-notice { padding: 10px 0; color: var(--ink-2); font-size: 14px; }
|
||||
[data-theme='dark'] .admin-page :is(h1,h2,h3,thead th) { color: #fff; }
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
import { api, ApiError, type AnnouncementContent, type ContentRecord, type SiteSettings } from './api'
|
||||
import type { Mirror } from './data'
|
||||
|
||||
interface PublicPayload {
|
||||
site: SiteSettings
|
||||
mirrors: Omit<Mirror, 'mark' | 'color' | 'descriptionKey'>[]
|
||||
announcements: ContentRecord<AnnouncementContent>[]
|
||||
source: { updatedAt: number; stale: boolean; code: string }
|
||||
}
|
||||
export function usePublicData(enabled = true) {
|
||||
const site = ref<SiteSettings | null>(null)
|
||||
const mirrors = ref<Mirror[]>([])
|
||||
const announcements = ref<ContentRecord<AnnouncementContent>[]>([])
|
||||
const source = ref({ updatedAt: 0, stale: false, code: '' })
|
||||
const loading = ref(enabled)
|
||||
let controller: AbortController | undefined
|
||||
let timer: ReturnType<typeof setInterval> | undefined
|
||||
let disposed = false
|
||||
async function refresh() {
|
||||
if (!enabled || disposed) return
|
||||
controller?.abort(); controller = new AbortController()
|
||||
try {
|
||||
const data = await api<PublicPayload>('/public', { signal: controller.signal })
|
||||
if (disposed) return
|
||||
site.value = data.site
|
||||
mirrors.value = data.mirrors.map(mirror => ({ ...mirror, mark: mirror.name[0]?.toUpperCase() || '', color: '#004098', descriptionKey: '' }))
|
||||
announcements.value = data.announcements
|
||||
source.value = data.source
|
||||
} catch (error) {
|
||||
if ((error as Error).name !== 'AbortError') source.value = { ...source.value, stale: true, code: error instanceof ApiError ? error.code : 'NETWORK_ERROR' }
|
||||
} finally { if (!disposed) loading.value = false }
|
||||
}
|
||||
onMounted(() => { if (!enabled) return; void refresh(); timer = setInterval(() => { if (!document.hidden) void refresh() }, 15000) })
|
||||
onBeforeUnmount(() => { disposed = true; controller?.abort(); clearInterval(timer) })
|
||||
return { site, mirrors, announcements, source, loading, refresh }
|
||||
}
|
||||
+1
-1
@@ -3,5 +3,5 @@ import vue from '@vitejs/plugin-vue'
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [vue()],
|
||||
server: { port: 5173, strictPort: true },
|
||||
server: { port: 5173, strictPort: true, proxy: { '/api': 'http://127.0.0.1:8081', '/uploads': 'http://127.0.0.1:8081', '/mirrors/': 'http://127.0.0.1:8081' } },
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user