feat(sub): auto-detect subscription format by User-Agent (Updated) (#5826)

* feat(settings): add subscription format controls

* feat(sub): auto-detect subscription formats

* fix(xray): validate balancer regexes before save

* Revert "fix(xray): validate balancer regexes before save"

This reverts commit 8a208ce71b.

* doc(endpoints): align indent spaces

* doc(settings): improve error message formatting in validateSubUserAgentRegex

- Use NewErrorf with proper formatting instead of NewError with string concatenation
- Add comment explaining the rationale for returning original pattern value
- This preserves the intentional design where empty input is stored as empty
  in the DB and inherited as the runtime default at read time

---------

Co-authored-by: Tomilla <5007859+Tomilla@users.noreply.github.com>
Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
This commit is contained in:
Tomi lla
2026-07-14 19:01:40 +08:00
committed by GitHub
parent f2b17397f4
commit 129f50d92a
40 changed files with 1588 additions and 89 deletions
+19
View File
@@ -2,6 +2,7 @@ package controller
import (
"errors"
"net/http"
"strconv"
"time"
@@ -38,6 +39,10 @@ type updateSettingForm struct {
ClearSmtpPassword bool `json:"clearSmtpPassword" form:"clearSmtpPassword"`
}
type validateRegexForm struct {
Regex string `json:"regex" form:"regex"`
}
// SettingController handles settings and user management operations.
type SettingController struct {
settingService service.SettingService
@@ -61,6 +66,7 @@ func (a *SettingController) initRouter(g *gin.RouterGroup) {
g.POST("/all", a.getAllSetting)
g.POST("/defaultSettings", a.getDefaultSettings)
g.POST("/update", a.updateSetting)
g.POST("/validateRegex", a.validateRegex)
g.POST("/updateUser", a.updateUser)
g.POST("/restartPanel", a.restartPanel)
g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig)
@@ -72,6 +78,19 @@ func (a *SettingController) initRouter(g *gin.RouterGroup) {
g.POST("/testTgBot", a.testTgBot)
}
func (a *SettingController) validateRegex(c *gin.Context) {
form := &validateRegexForm{}
if err := c.ShouldBind(form); err != nil {
pureJsonMsg(c, http.StatusOK, false, err.Error())
return
}
if err := service.ValidateRegex(form.Regex); err != nil {
pureJsonMsg(c, http.StatusOK, false, err.Error())
return
}
pureJsonMsg(c, http.StatusOK, true, "")
}
// getAllSetting retrieves all current settings as the browser-safe view:
// secret values are redacted and surfaced as has* presence flags instead.
func (a *SettingController) getAllSetting(c *gin.Context) {
+46
View File
@@ -0,0 +1,46 @@
package controller
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
func TestValidateRegex(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
NewSettingController(router.Group("/panel/api"))
tests := []struct {
name string
body string
success bool
}{
{name: "Go RE2 inline flag", body: `{"regex":"(?m)^general-purpose$"}`, success: true},
{name: "invalid expression", body: `{"regex":"["}`, success: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/panel/api/setting/validateRegex", strings.NewReader(tt.body))
req.Header.Set("Content-Type", "application/json")
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body=%s", resp.Code, resp.Body.String())
}
needle := `"success":true`
if !tt.success {
needle = `"success":false`
}
if !strings.Contains(resp.Body.String(), needle) {
t.Fatalf("body = %s, want %s", resp.Body.String(), needle)
}
})
}
}