fix(sub): randomize fresh panel subscription paths (#6375)

* fix(sub): randomize fresh panel subscription paths

Seed distinct cryptographically random paths for base64, JSON, and Clash subscriptions when a panel database is first created. Persist them so restarts keep published URLs stable while upgrades preserve existing settings.

Generated-by: OpenCode:gpt-5.6-sol

* fix(sub): regenerate paths on settings reset

Keep subscription paths unpredictable after a factory reset, close the test database on failure, and update the builder, OpenAPI, and localized docs to describe panel-specific paths instead of obsolete fixed defaults.

Generated-by: OpenCode:gpt-5.6-sol
This commit is contained in:
ilyusha
2026-09-03 17:34:37 +03:00
committed by GitHub
parent ded2aa150c
commit f9898e0b24
20 changed files with 285 additions and 158 deletions
@@ -2,6 +2,7 @@ package service
import (
"path/filepath"
"regexp"
"testing"
"github.com/xlzd/gotp"
@@ -10,6 +11,44 @@ import (
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
func TestResetSettingsRegeneratesSubscriptionPaths(t *testing.T) {
setupSettingTestDB(t)
s := &SettingService{}
for key, value := range map[string]string{
"subPath": "/sub/",
"subJsonPath": "/json/",
"subClashPath": "/clash/",
"webPort": "8443",
} {
if err := s.saveSetting(key, value); err != nil {
t.Fatalf("save %s: %v", key, err)
}
}
if err := s.ResetSettings(); err != nil {
t.Fatalf("ResetSettings: %v", err)
}
pathPattern := regexp.MustCompile(`^/[0-9a-z]{16}/$`)
paths := map[string]string{}
for _, key := range []string{"subPath", "subJsonPath", "subClashPath"} {
value, err := s.getString(key)
if err != nil {
t.Fatalf("read %s: %v", key, err)
}
if !pathPattern.MatchString(value) {
t.Errorf("%s = %q, want /<16 lowercase alphanumeric characters>/", key, value)
}
paths[key] = value
}
if paths["subPath"] == paths["subJsonPath"] || paths["subPath"] == paths["subClashPath"] || paths["subJsonPath"] == paths["subClashPath"] {
t.Fatalf("subscription paths must be distinct: %v", paths)
}
if port, err := s.GetPort(); err != nil || port != 2053 {
t.Fatalf("web port after reset = %d, %v; want 2053", port, err)
}
}
func setupSettingTestDB(t *testing.T) {
t.Helper()
if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {