mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-16 15:17:14 +00:00
feat(sub): bake Happ/INCY routing profiles into the JSON subscription (#6402)
* feat(sub): parse generic Happ/INCY routing payloads for the JSON subscription Accepts the routing-rules format emitted for Happ and INCY (inline JSON, happ:// or incy:// deeplink, or a remote https:// URL resolved through the existing remote routing cache). The JSON subscription will bake these rules into its documents so header-ignoring clients still get routing. * feat(sub): bake Happ/INCY routing profiles into JSON subscription documents When subJsonRoutingRules is set, every emitted document (per-inbound and balancer alike) carries the profile's dns and routing rules baked in, so header-ignoring clients like Happ and INCY still get routing; the legacy simple-rules merge only applies when no profile is set. The balancer document builder keeps rewriting proxy-tag rules to the balancer. * feat(sub): add the subJsonRoutingRules setting Plumbed from the settings store through the subscription server into SubJsonService, so admins can set a routing profile once and every JSON subscription document carries it. * chore(api): regenerate OpenAPI artifacts for subJsonRoutingRules * feat(web): routing profile editor for the JSON subscription A textarea inside the JSON card accepts the routing profile (inline JSON, happ/incy deeplink, or https URL) with a remote-source badge; the badge helper moves to a shared module. Keys added to all 13 locales. * fix(sub): warm and lazily resolve the baked JSON routing source The routing profile was resolved once at service construction: a remote URL that was cold at that moment baked default routing forever, and the cron job never warmed it. The job now warms the subJsonRoutingRules URL, and the profile resolves per request with an in-memory memo (a failed resolve is not cached), so a warmed cache takes effect without a restart. * feat(sub): fall back to the JSON routing profile for the Routing header Happ and INCY download the geo files a routing profile references through the Routing response header. When the Happ header setting was blank the header stayed unset, and clients fetched no geo files even though a JSON routing profile was configured. A blank setting now falls back to the JSON profile: happ/incy deeplinks pass through, inline JSON and remote URLs are normalized to a happ:// deeplink; an unusable or oversized value leaves the header unset. Locale captions mention the fallback. * fix(sub): pass routingRules arg at call sites added by main Main gained four NewSubJsonService call sites after this branch forked; update them to the five-arg signature so internal/sub builds again. * fix(sub): address code review findings on the baked JSON routing The memoised baked template never invalidated, so an edited remote profile kept serving the superseded dns/routing subtrees until a panel restart; bakedTemplate now re-resolves the spec per request and rebuilds only when the payload actually changed (regression-tested). subJsonRoutingRules shared the happ persistence row with subRoutingRules, so only the last-written setting survived a restart; it now resolves under its own jsonhapp kind with the same validation and size caps. The setting also joins validateSettingsURLs, so remote values are canonicalised and bad URLs are rejected on save. Also: drop the unreachable half of the remote-source guard, cut the overlong comment blocks to the two-line convention, and deduplicate remoteSourceBadge in the General tab. Merges upstream/main (call sites for the widened NewSubJsonService signature). * style(sub): gofumpt the json_routing imports * fix(sub): accept happ add/ deeplinks and bound the routing warning The baked-JSON routing parser only recognised happ://routing/onadd/, but normalizeHappRouting treats happ://routing/add/ as an equally valid routing deeplink. An operator pasting the add/ form got the Routing header set, so the panel looked configured, while every JSON subscription document silently carried the default routing instead of their profile. resolveJsonRoutingSpec logged one warning per call and bakedTemplate calls it once per emitted document, so a single fetch of an unusable profile wrote one identical warning per document. On the public subscription server that floods the 10240-entry buffer the panel's log view reads, evicting real entries. Log only when the message changes, and reset on a successful resolve so a profile that recovers and fails again is still reported. Also resolve the template once in buildBalancerConfig: two resolves could straddle a profile refresh and pair one revision's dns with the other's routing.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
||||
@@ -30,11 +31,22 @@ type SubJsonService struct {
|
||||
mux string
|
||||
observatory subBalancerObservatoryConfig
|
||||
|
||||
// bakedRouting is re-resolved per request: a remote URL may be cold at
|
||||
// construction time and warm up later via the cron job.
|
||||
routingRules string
|
||||
bakedRoutingMu sync.Mutex
|
||||
bakedRouting *bakedRoutingState
|
||||
|
||||
SubService *SubService
|
||||
}
|
||||
|
||||
type bakedRoutingState struct {
|
||||
spec jsonRoutingSpec
|
||||
configJson map[string]any
|
||||
}
|
||||
|
||||
// NewSubJsonService creates a new JSON subscription service with the given configuration.
|
||||
func NewSubJsonService(mux string, rules string, finalMask string, subService *SubService) *SubJsonService {
|
||||
func NewSubJsonService(mux string, rules string, finalMask string, routingRules string, subService *SubService) *SubJsonService {
|
||||
var configJson map[string]any
|
||||
var defaultOutbounds []json_util.RawMessage
|
||||
_ = json.Unmarshal([]byte(defaultJson), &configJson)
|
||||
@@ -45,7 +57,9 @@ func NewSubJsonService(mux string, rules string, finalMask string, subService *S
|
||||
}
|
||||
}
|
||||
|
||||
if rules != "" {
|
||||
// A baked routing profile replaces the template's dns and routing subtrees
|
||||
// outright; the legacy simple-rules setting only applies without a profile.
|
||||
if routingRules == "" && rules != "" {
|
||||
var newRules []any
|
||||
routing, _ := configJson["routing"].(map[string]any)
|
||||
defaultRules, _ := routing["rules"].([]any)
|
||||
@@ -60,11 +74,35 @@ func NewSubJsonService(mux string, rules string, finalMask string, subService *S
|
||||
defaultOutbounds: defaultOutbounds,
|
||||
finalMask: finalMask,
|
||||
mux: mux,
|
||||
routingRules: routingRules,
|
||||
observatory: defaultSubBalancerObservatoryConfig(),
|
||||
SubService: subService,
|
||||
}
|
||||
}
|
||||
|
||||
// Re-resolved per call so an upstream edit reaches the documents without a
|
||||
// restart; a failed resolve keeps the last good template.
|
||||
func (s *SubJsonService) bakedTemplate() map[string]any {
|
||||
if s.routingRules == "" {
|
||||
return s.configJson
|
||||
}
|
||||
spec := resolveJsonRoutingSpec(s.routingRules)
|
||||
s.bakedRoutingMu.Lock()
|
||||
defer s.bakedRoutingMu.Unlock()
|
||||
if s.bakedRouting != nil {
|
||||
if spec.empty() || spec.equal(s.bakedRouting.spec) {
|
||||
return s.bakedRouting.configJson
|
||||
}
|
||||
} else if spec.empty() {
|
||||
return s.configJson
|
||||
}
|
||||
template := make(map[string]any, len(s.configJson)+2)
|
||||
maps.Copy(template, s.configJson)
|
||||
applyJsonRouting(template, spec)
|
||||
s.bakedRouting = &bakedRoutingState{spec: spec, configJson: template}
|
||||
return template
|
||||
}
|
||||
|
||||
// GetJson generates a JSON subscription configuration for the given subscription ID and host.
|
||||
func (s *SubJsonService) GetJson(subId string, host string, alwaysReturnArray bool) (string, string, error) {
|
||||
subReq := s.SubService.ForRequest(host)
|
||||
@@ -153,7 +191,7 @@ func (s *SubJsonService) GetJson(subId string, host string, alwaysReturnArray bo
|
||||
newOutbounds := []json_util.RawMessage{outbound}
|
||||
newOutbounds = append(newOutbounds, s.defaultOutbounds...)
|
||||
newConfigJson := make(map[string]any)
|
||||
maps.Copy(newConfigJson, s.configJson)
|
||||
maps.Copy(newConfigJson, s.bakedTemplate())
|
||||
newConfigJson["outbounds"] = newOutbounds
|
||||
newConfigJson["remarks"] = remark
|
||||
newConfig, _ := json.MarshalIndent(newConfigJson, "", " ")
|
||||
@@ -455,9 +493,12 @@ func (s *SubJsonService) buildBalancerConfig(balancer *model.SubBalancer, entrie
|
||||
outbounds := append([]json_util.RawMessage{}, proxies...)
|
||||
outbounds = append(outbounds, s.defaultOutbounds...)
|
||||
|
||||
// The routing subtree in s.configJson is shared by every emitted document;
|
||||
// clone it (and each rule map) before pointing rules at the balancer.
|
||||
baseRouting, _ := s.configJson["routing"].(map[string]any)
|
||||
// One template per document: two resolves could straddle a profile refresh
|
||||
// and pair this document's dns with the other revision's routing.
|
||||
template := s.bakedTemplate()
|
||||
// Clone the shared routing subtree (and each rule map) before pointing
|
||||
// rules at the balancer.
|
||||
baseRouting, _ := template["routing"].(map[string]any)
|
||||
routing := make(map[string]any, len(baseRouting)+1)
|
||||
maps.Copy(routing, baseRouting)
|
||||
baseRules, _ := baseRouting["rules"].([]any)
|
||||
@@ -493,8 +534,8 @@ func (s *SubJsonService) buildBalancerConfig(balancer *model.SubBalancer, entrie
|
||||
}
|
||||
routing["balancers"] = []any{balancerEntry}
|
||||
|
||||
newConfigJson := make(map[string]any, len(s.configJson)+2)
|
||||
maps.Copy(newConfigJson, s.configJson)
|
||||
newConfigJson := make(map[string]any, len(template)+2)
|
||||
maps.Copy(newConfigJson, template)
|
||||
newConfigJson["outbounds"] = outbounds
|
||||
newConfigJson["remarks"] = balancer.Remark
|
||||
newConfigJson["routing"] = routing
|
||||
@@ -614,7 +655,7 @@ func (s *SubJsonService) getConfig(subReq *SubService, inbound *model.Inbound, c
|
||||
|
||||
newOutbounds = append(newOutbounds, s.defaultOutbounds...)
|
||||
newConfigJson := make(map[string]any)
|
||||
maps.Copy(newConfigJson, s.configJson)
|
||||
maps.Copy(newConfigJson, s.bakedTemplate())
|
||||
|
||||
transport, _ := newStream["network"].(string)
|
||||
newConfigJson["outbounds"] = newOutbounds
|
||||
|
||||
Reference in New Issue
Block a user