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:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user