mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
`Server/go.mod` declared `github.com/owncord/server` while the public repository is `github.com/J3vb/OwnCord`. Nothing resolves that path — there is no `owncord` GitHub org and no vanity-import host serving go-import metadata for it — so every import line in the tree named a location that does not exist. It compiles because a main module's own path is never fetched, which is exactly why it went unnoticed. The obvious fix — an AST-aware import rewriter (`gomvpkg`, `go mod edit`) — is wrong here, and provably so. Six of the 722 occurrences are not imports at all: `api/main_test.go:20` (a goleak `IgnoreTopFunction` pattern), `telemetry/metrics.go:17-19` (three OTel instrumentation-scope names), `invariants/syncutil_locks.go:73` (a diagnostic message), and `invariants/syncutil_locks_test.go:56` (an import line inside a raw-string Go fixture). An import rewriter touches none of them, and the compiler cannot see any of them either. Done as one scripted substitution over `git ls-files`, anchored on the full `github.com/owncord/server` string. The anchor matters: `owncord-server` is a different identifier — the OTel `service.name` (`config/config.go`, `telemetry/telemetry_otel.go`) and the GHCR image name (`.github/workflows/release.yml`, `docker-compose.yml`) — and a looser pattern would have moved it. It is untouched: 10 occurrences across 9 files, before and after. 350 files, 728 insertions, 728 deletions. 722 occurrences in 344 Go files, plus `go.mod:1`, the `sed` at `Makefile:67`, `Server/CLAUDE.md:3`, `docs/architecture/server.md:5`, and the ledger pair (`findings-ledger.json:3758` plus a `render-ledger.mjs` re-render of `FINDINGS.md`). Zero in any workflow, zero in the Dockerfile, zero in `Server/.golangci.yml` (no `local-prefixes`, `gci`, `importas` or `depguard` rule keys on the module path, so import grouping is not configured anywhere). The plan's blast-radius estimate missed one thing, and it is the one that would have gone red: **gofmt**. `J` (0x4A) sorts before every lowercase letter, so in the 36 files where a module-local import shares a contiguous group with a third-party one, the module's imports must move above `github.com/go-chi/...`. `gofmt -l` was clean before the substitution and listed exactly 36 files after it; `gofmt -w` on those 36 restores it to clean. `gofmt` is an enforced gate — the `formatters` block in `Server/.golangci.yml`, which is S-05 — so a substitution-only commit fails Lint. Verified: both directions, and the line accounting is exact. Every added line in this diff contains the new module path (728) and every removed line contains the old one (728); the count of changed lines containing neither is **zero**, so the gofmt re-sort moved module-path lines only and touched no third-party import. The residual check (`git ls-files -z | xargs -0 grep -n 'github\.com/owncord/server'`) returns exactly two hits, both deliberately out of scope: the RL-13 row in `docs/audit-2026-08-23-repository-layout.md` and the measurement row in this phase's own plan. The compiler-invisible half was proven by reverting *only* `api/main_test.go:20` to the old path on the otherwise-renamed tree: `go build ./...` and `go vet ./api/` both still pass — they see nothing wrong — while `go test ./api/` FAILS, because the runtime function name now carries the new path and goleak stops ignoring `ws.(*Hub).Run.func1`. Restoring the line makes it pass. `go.sum` is byte-identical (no `go mod tidy` was run and none was needed). All four build-tag variants compile; `go vet ./...`, `go vet -tags otel,wazero ./...` and `go vet -tags deadlock ./...` pass; `go test -race ./...` is 16/16 packages green; `go test -tags deadlock ./...` passes; the tag-gated `./plugin/...` (wazero) and `./telemetry/...` (otel) runs pass. `golangci-lint` v2.11.3 — the pinned CI version, rebuilt locally against Go 1.26 because the packaged binary cannot load a 1.26 config — reports **0 issues**. `go run ./cmd/genprotocol` leaves `git diff --exit-code ws/message_types.go ../Client/src/lib/protocolTypes.ts` clean, so the rename does not reach the generated protocol constants. `npx prettier --check .` and `node .superpowers/render-ledger.mjs --check` pass. Not included: `docs/audit-2026-08-23-repository-layout.md` and `docs/plans/b1-repository-foundation-2026-08-25.md` keep the old path — they are the audit row and the measurement that motivated this change, and rewriting them would erase the record of what was measured. They are why the residual check needs a two-path allowance rather than being empty; that allowance is stated above rather than hidden in a pathspec. `telemetry/metrics.go:19` declares `scopeVoice` for a `Server/voice` package that does not exist; the substitution carried the dead path forward verbatim as `github.com/J3vb/OwnCord/Server/voice` rather than fixing it, because correcting a real observability bug inside a mechanical rename would hide it in a 350-file diff. It needs its own item. No `go.work`, no second module, and no vanity-import host was set up — the new path resolves against the real repository, but nothing imports this module as a library, so `go get` reachability was not exercised either way. Refs RL-13, L-12
361 lines
12 KiB
Go
361 lines
12 KiB
Go
package admin
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/J3vb/OwnCord/Server/config"
|
|
"github.com/J3vb/OwnCord/Server/db"
|
|
"github.com/J3vb/OwnCord/Server/service"
|
|
)
|
|
|
|
// ─── SetupOptions ────────────────────────────────────────────────────────────
|
|
|
|
// SetupOptions wires the first-run setup wizard to the running server. The
|
|
// zero value disables everything beyond legacy owner-account creation, which
|
|
// keeps every existing NewAdminAPI/NewHandler call site behaving as before.
|
|
type SetupOptions struct {
|
|
// ConfigPath is the config.yaml path the wizard patches. Empty disables
|
|
// config writing entirely.
|
|
ConfigPath string
|
|
// RunningCfg is the configuration the server booted with. It provides the
|
|
// wizard's prefill defaults, the values compared against to decide whether
|
|
// a restart is needed, and the generated LiveKit credentials to persist.
|
|
// Nil disables prefill and restarting.
|
|
RunningCfg *config.Config
|
|
// Restart replaces the process-restart hook (tests). Nil = requestRestart.
|
|
Restart func(reason string)
|
|
}
|
|
|
|
// ─── Wizard payload ──────────────────────────────────────────────────────────
|
|
|
|
// setupWizardRequest is the optional "wizard" object on POST /api/setup.
|
|
// Every field is a pointer: absent means "keep the current/default value".
|
|
type setupWizardRequest struct {
|
|
// Stored in the settings table (read live, no restart needed).
|
|
ServerName *string `json:"server_name"`
|
|
Motd *string `json:"motd"`
|
|
RegistrationOpen *bool `json:"registration_open"`
|
|
|
|
// Stored in config.yaml (consumed at startup — changes need a restart).
|
|
Port *int `json:"port"`
|
|
TLSMode *string `json:"tls_mode"`
|
|
TLSDomain *string `json:"tls_domain"`
|
|
UploadMaxSizeMB *int `json:"upload_max_size_mb"`
|
|
VoiceQuality *string `json:"voice_quality"`
|
|
// VoiceAutoDownload toggles voice.auto_download_livekit — download and
|
|
// run livekit-server automatically so voice works with zero setup.
|
|
VoiceAutoDownload *bool `json:"voice_auto_download"`
|
|
}
|
|
|
|
// setupDefaults is the prefill data the wizard shows. Exposed only while
|
|
// needs_setup is true, and deliberately free of secrets, filesystem paths and
|
|
// network ACLs.
|
|
type setupDefaults struct {
|
|
ServerName string `json:"server_name"`
|
|
Motd string `json:"motd"`
|
|
RegistrationOpen bool `json:"registration_open"`
|
|
Port int `json:"port"`
|
|
TLSMode string `json:"tls_mode"`
|
|
TLSDomain string `json:"tls_domain"`
|
|
UploadMaxSizeMB int `json:"upload_max_size_mb"`
|
|
VoiceQuality string `json:"voice_quality"`
|
|
VoiceAutoDownload bool `json:"voice_auto_download"`
|
|
}
|
|
|
|
// ─── Validation ──────────────────────────────────────────────────────────────
|
|
|
|
const (
|
|
maxServerNameLen = 100
|
|
maxMotdLen = 500
|
|
maxUploadSizeMB = 10240 // 10 GiB
|
|
)
|
|
|
|
var validTLSModes = map[string]struct{}{
|
|
"self_signed": {}, "acme": {}, "manual": {}, "off": {},
|
|
}
|
|
|
|
var validVoiceQualities = map[string]struct{}{
|
|
"low": {}, "medium": {}, "high": {},
|
|
}
|
|
|
|
// validateWizard checks and normalises the wizard payload in place. It must
|
|
// be called BEFORE the owner account is created so a bad payload rejects the
|
|
// whole request instead of leaving a half-configured server.
|
|
func validateWizard(wr *setupWizardRequest) error {
|
|
if err := wizardValidateIdentity(wr); err != nil {
|
|
return err
|
|
}
|
|
if err := wizardValidateNetwork(wr); err != nil {
|
|
return err
|
|
}
|
|
if err := wizardValidateMedia(wr); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// wizardValidateIdentity checks and normalises the settings-table fields the
|
|
// server reads live: the display name and the message of the day.
|
|
//
|
|
// It uses the fixpoint sanitizer (service.SanitizeText), not a bare
|
|
// bluemonday.StrictPolicy().Sanitize call: bluemonday's bare Sanitize HTML-escapes
|
|
// survivors (' -> ', & -> &, " -> "), which would store these
|
|
// fields differently from how the admin Settings page's handlePatchSettings
|
|
// stores the exact same keys (no sanitizer at all). See setup_handler.go's
|
|
// identical treatment of the username field, and service.SanitizeText's doc
|
|
// comment.
|
|
func wizardValidateIdentity(wr *setupWizardRequest) error {
|
|
if wr.ServerName != nil {
|
|
name := strings.TrimSpace(service.SanitizeText(*wr.ServerName))
|
|
if name == "" {
|
|
return fmt.Errorf("server_name cannot be empty")
|
|
}
|
|
if len(name) > maxServerNameLen {
|
|
return fmt.Errorf("server_name must be at most %d characters", maxServerNameLen)
|
|
}
|
|
*wr.ServerName = name
|
|
}
|
|
if wr.Motd != nil {
|
|
motd := strings.TrimSpace(service.SanitizeText(*wr.Motd))
|
|
if len(motd) > maxMotdLen {
|
|
return fmt.Errorf("motd must be at most %d characters", maxMotdLen)
|
|
}
|
|
*wr.Motd = motd
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// wizardValidateNetwork checks and normalises the listener and TLS fields,
|
|
// including the cross-field rule that ACME issuance needs a domain.
|
|
func wizardValidateNetwork(wr *setupWizardRequest) error {
|
|
if wr.Port != nil && (*wr.Port < 1 || *wr.Port > 65535) {
|
|
return fmt.Errorf("port must be between 1 and 65535")
|
|
}
|
|
if wr.TLSMode != nil {
|
|
mode := strings.ToLower(strings.TrimSpace(*wr.TLSMode))
|
|
if _, ok := validTLSModes[mode]; !ok {
|
|
return fmt.Errorf("tls_mode must be one of: self_signed, acme, manual, off")
|
|
}
|
|
*wr.TLSMode = mode
|
|
}
|
|
if wr.TLSDomain != nil {
|
|
domain := strings.ToLower(strings.TrimSpace(*wr.TLSDomain))
|
|
if domain != "" {
|
|
if err := validateHostname(domain); err != nil {
|
|
return fmt.Errorf("tls_domain: %w", err)
|
|
}
|
|
}
|
|
*wr.TLSDomain = domain
|
|
}
|
|
if wr.TLSMode != nil && *wr.TLSMode == "acme" &&
|
|
(wr.TLSDomain == nil || *wr.TLSDomain == "") {
|
|
return fmt.Errorf("tls_domain is required when tls_mode is acme")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// wizardValidateMedia checks and normalises the upload-size cap and the voice
|
|
// quality preset.
|
|
func wizardValidateMedia(wr *setupWizardRequest) error {
|
|
if wr.UploadMaxSizeMB != nil && (*wr.UploadMaxSizeMB < 1 || *wr.UploadMaxSizeMB > maxUploadSizeMB) {
|
|
return fmt.Errorf("upload_max_size_mb must be between 1 and %d", maxUploadSizeMB)
|
|
}
|
|
if wr.VoiceQuality != nil {
|
|
q := strings.ToLower(strings.TrimSpace(*wr.VoiceQuality))
|
|
if _, ok := validVoiceQualities[q]; !ok {
|
|
return fmt.Errorf("voice_quality must be one of: low, medium, high")
|
|
}
|
|
*wr.VoiceQuality = q
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// validateHostname checks an LDH (letters-digits-hyphen) DNS name suitable
|
|
// for ACME issuance: dotted, each label 1-63 chars, no leading/trailing
|
|
// hyphen, 253 chars max. Input is expected lowercase.
|
|
func validateHostname(h string) error {
|
|
if len(h) > 253 {
|
|
return fmt.Errorf("hostname too long")
|
|
}
|
|
labels := strings.Split(h, ".")
|
|
if len(labels) < 2 {
|
|
return fmt.Errorf("must be a fully qualified domain name (e.g. chat.example.com)")
|
|
}
|
|
for _, label := range labels {
|
|
if label == "" || len(label) > 63 {
|
|
return fmt.Errorf("invalid hostname label")
|
|
}
|
|
if label[0] == '-' || label[len(label)-1] == '-' {
|
|
return fmt.Errorf("hostname labels cannot start or end with a hyphen")
|
|
}
|
|
for _, c := range label {
|
|
if (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '-' {
|
|
return fmt.Errorf("hostname contains invalid characters")
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ─── Applying the wizard ─────────────────────────────────────────────────────
|
|
|
|
// applyWizardSettings persists the wizard's DB-backed settings atomically.
|
|
// server_name, motd and registration_open are read live by the server;
|
|
// max_upload_bytes and voice_quality are written so the Settings page shows
|
|
// values consistent with what the wizard put in config.yaml.
|
|
func applyWizardSettings(ctx context.Context, database *db.DB, wr *setupWizardRequest) error {
|
|
updates := map[string]string{}
|
|
if wr.ServerName != nil {
|
|
updates["server_name"] = *wr.ServerName
|
|
}
|
|
if wr.Motd != nil {
|
|
updates["motd"] = *wr.Motd
|
|
}
|
|
if wr.RegistrationOpen != nil {
|
|
if *wr.RegistrationOpen {
|
|
updates["registration_open"] = "1"
|
|
} else {
|
|
updates["registration_open"] = "0"
|
|
}
|
|
}
|
|
if wr.UploadMaxSizeMB != nil {
|
|
updates["max_upload_bytes"] = strconv.Itoa(*wr.UploadMaxSizeMB * 1024 * 1024)
|
|
}
|
|
if wr.VoiceQuality != nil {
|
|
updates["voice_quality"] = *wr.VoiceQuality
|
|
}
|
|
if len(updates) == 0 {
|
|
return nil
|
|
}
|
|
|
|
tx, err := database.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("starting transaction: %w", err)
|
|
}
|
|
for key, value := range updates {
|
|
if _, txErr := tx.ExecContext(ctx,
|
|
`INSERT INTO settings (key, value) VALUES (?, ?)
|
|
ON CONFLICT(key) DO UPDATE SET value = excluded.value`,
|
|
key, value,
|
|
); txErr != nil {
|
|
_ = tx.Rollback()
|
|
return fmt.Errorf("writing setting %s: %w", key, txErr)
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// buildConfigPatch maps the wizard payload onto config.yaml keys. When the
|
|
// running config is known, the LiveKit credentials it booted with are included
|
|
// so config.Save can persist them if the file has none — stabilising voice
|
|
// tokens across restarts (they are otherwise regenerated randomly each boot).
|
|
func buildConfigPatch(wr *setupWizardRequest, running *config.Config) config.Patch {
|
|
p := config.Patch{
|
|
ServerPort: wr.Port,
|
|
ServerName: wr.ServerName,
|
|
TLSMode: wr.TLSMode,
|
|
TLSDomain: wr.TLSDomain,
|
|
UploadMaxSizeMB: wr.UploadMaxSizeMB,
|
|
VoiceQuality: wr.VoiceQuality,
|
|
VoiceAutoDownload: wr.VoiceAutoDownload,
|
|
}
|
|
if running != nil {
|
|
if key := running.Voice.LiveKitAPIKey; key != "" {
|
|
p.VoiceAPIKey = &key
|
|
}
|
|
if secret := running.Voice.LiveKitAPISecret; secret != "" {
|
|
p.VoiceAPISecret = &secret
|
|
}
|
|
}
|
|
return p
|
|
}
|
|
|
|
// patchedConfigKeys summarises which config.yaml keys a patch touches, for
|
|
// the audit log. Secrets are named, never valued.
|
|
func patchedConfigKeys(wr *setupWizardRequest) string {
|
|
var keys []string
|
|
if wr.Port != nil {
|
|
keys = append(keys, "server.port")
|
|
}
|
|
if wr.ServerName != nil {
|
|
keys = append(keys, "server.name")
|
|
}
|
|
if wr.TLSMode != nil {
|
|
keys = append(keys, "tls.mode")
|
|
}
|
|
if wr.TLSDomain != nil {
|
|
keys = append(keys, "tls.domain")
|
|
}
|
|
if wr.UploadMaxSizeMB != nil {
|
|
keys = append(keys, "upload.max_size_mb")
|
|
}
|
|
if wr.VoiceQuality != nil {
|
|
keys = append(keys, "voice.quality")
|
|
}
|
|
if wr.VoiceAutoDownload != nil {
|
|
keys = append(keys, "voice.auto_download_livekit")
|
|
}
|
|
keys = append(keys, "voice credentials (persisted if unset)")
|
|
return strings.Join(keys, ", ")
|
|
}
|
|
|
|
// ─── Restart decision ────────────────────────────────────────────────────────
|
|
|
|
// wizardChangesRunningConfig reports whether the wizard set any startup-only
|
|
// value to something different from what this process booted with. Only those
|
|
// changes justify a restart; server.name and the persisted voice credentials
|
|
// match the running state by construction.
|
|
func wizardChangesRunningConfig(wr *setupWizardRequest, running *config.Config) bool {
|
|
if wr.Port != nil && *wr.Port != running.Server.Port {
|
|
return true
|
|
}
|
|
if wr.TLSMode != nil && *wr.TLSMode != running.TLS.Mode {
|
|
return true
|
|
}
|
|
if wr.UploadMaxSizeMB != nil && *wr.UploadMaxSizeMB != running.Upload.MaxSizeMB {
|
|
return true
|
|
}
|
|
if wr.VoiceQuality != nil && *wr.VoiceQuality != running.Voice.Quality {
|
|
return true
|
|
}
|
|
if wr.VoiceAutoDownload != nil && *wr.VoiceAutoDownload != running.Voice.AutoDownloadLiveKit {
|
|
return true
|
|
}
|
|
// A domain change only matters when certificates come from ACME.
|
|
effMode := running.TLS.Mode
|
|
if wr.TLSMode != nil {
|
|
effMode = *wr.TLSMode
|
|
}
|
|
if effMode == "acme" && wr.TLSDomain != nil && *wr.TLSDomain != running.TLS.Domain {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// computeRestartURL builds the admin-panel URL the server will be reachable
|
|
// at after restarting with the wizard's values. host is the request's Host
|
|
// header (the address the user's browser is already using).
|
|
func computeRestartURL(host string, wr *setupWizardRequest, running *config.Config) string {
|
|
h := host
|
|
if hh, _, err := net.SplitHostPort(host); err == nil {
|
|
h = hh
|
|
}
|
|
effPort := running.Server.Port
|
|
if wr.Port != nil {
|
|
effPort = *wr.Port
|
|
}
|
|
effMode := running.TLS.Mode
|
|
if wr.TLSMode != nil {
|
|
effMode = *wr.TLSMode
|
|
}
|
|
scheme := "https"
|
|
if effMode == "off" {
|
|
scheme = "http"
|
|
}
|
|
return scheme + "://" + net.JoinHostPort(h, strconv.Itoa(effPort)) + "/admin"
|
|
}
|