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
+79 -1
View File
@@ -9,6 +9,7 @@ import (
"net/http"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"time"
@@ -32,6 +33,13 @@ import (
//go:embed config.json
var xrayTemplateConfig string
const (
DefaultSubClashUserAgentRegex = `(?i)(clash|mihomo)`
DefaultSubJsonUserAgentRegex = ``
DefaultRemarkTemplate = "{{INBOUND}}-{{EMAIL}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D"
maxRegexLength = 2048
)
var defaultValueMap = map[string]string{
"xrayTemplateConfig": xrayTemplateConfig,
"webListen": "",
@@ -57,7 +65,7 @@ var defaultValueMap = map[string]string{
"pageSize": "25",
"expireDiff": "0",
"trafficDiff": "0",
"remarkTemplate": "{{INBOUND}}-{{EMAIL}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D",
"remarkTemplate": DefaultRemarkTemplate,
"timeLocation": "Local",
"tgBotEnable": "false",
"tgBotToken": "",
@@ -73,6 +81,11 @@ var defaultValueMap = map[string]string{
"twoFactorToken": "",
"subEnable": "true",
"subJsonEnable": "false",
"subJsonAutoDetect": "false",
"subJsonAlwaysArray": "false",
"subJsonUserAgentRegex": "",
"subClashAutoDetect": "false",
"subClashUserAgentRegex": "",
"subTitle": "",
"subSupportUrl": "",
"subProfileUrl": "",
@@ -707,6 +720,26 @@ func (s *SettingService) GetSubJsonEnable() (bool, error) {
return s.getBool("subJsonEnable")
}
func (s *SettingService) GetSubJsonAutoDetect() (bool, error) {
return s.getBool("subJsonAutoDetect")
}
func (s *SettingService) GetSubJsonAlwaysArray() (bool, error) {
return s.getBool("subJsonAlwaysArray")
}
func (s *SettingService) GetSubJsonUserAgentRegex() (string, error) {
return s.getString("subJsonUserAgentRegex")
}
func (s *SettingService) GetSubClashAutoDetect() (bool, error) {
return s.getBool("subClashAutoDetect")
}
func (s *SettingService) GetSubClashUserAgentRegex() (string, error) {
return s.getString("subClashUserAgentRegex")
}
func (s *SettingService) GetSubTitle() (string, error) {
return s.getString("subTitle")
}
@@ -1119,6 +1152,9 @@ func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting, clears
if err := validateSettingsURLs(allSetting); err != nil {
return err
}
if err := validateSubUserAgentRegexes(allSetting); err != nil {
return err
}
if err := allSetting.CheckValid(); err != nil {
return err
}
@@ -1159,6 +1195,48 @@ func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting, clears
})
}
func validateSubUserAgentRegexes(allSetting *entity.AllSetting) error {
jsonPattern, err := validateSubUserAgentRegex("Xray JSON", allSetting.SubJsonUserAgentRegex, DefaultSubJsonUserAgentRegex)
if err != nil {
return err
}
clashPattern, err := validateSubUserAgentRegex("Clash/Mihomo", allSetting.SubClashUserAgentRegex, DefaultSubClashUserAgentRegex)
if err != nil {
return err
}
allSetting.SubJsonUserAgentRegex = jsonPattern
allSetting.SubClashUserAgentRegex = clashPattern
return nil
}
func validateSubUserAgentRegex(name, pattern, defaultPattern string) (string, error) {
pattern = strings.TrimSpace(pattern)
effectivePattern := pattern
if effectivePattern == "" {
effectivePattern = defaultPattern
}
if len(effectivePattern) > maxRegexLength {
return "", common.NewErrorf("%s User-Agent regex must not exceed %d characters", name, maxRegexLength)
}
if _, err := regexp.Compile(effectivePattern); err != nil {
return "", common.NewErrorf("%s User-Agent regex is invalid: %v", name, err)
}
// Return the original pattern (empty string if cleared) so the caller
// can distinguish "user explicitly set empty" from "user set a value".
// The empty value is stored in the DB and inherited as runtime default.
return pattern, nil
}
func ValidateRegex(pattern string) error {
if len(pattern) > maxRegexLength {
return common.NewErrorf("Regular expression must not exceed %d characters", maxRegexLength)
}
if _, err := regexp.Compile(pattern); err != nil {
return common.NewError("Regular expression is invalid:", err)
}
return nil
}
func (s *SettingService) preserveRedactedSecrets(allSetting *entity.AllSetting, clears SecretClears) error {
if !clears.TgBotToken && strings.TrimSpace(allSetting.TgBotToken) == "" {
value, err := s.GetTgBotToken()