diff options
Diffstat (limited to 'web/service/xray_setting.go')
| -rw-r--r-- | web/service/xray_setting.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/web/service/xray_setting.go b/web/service/xray_setting.go index 5df8a211..4c3892e4 100644 --- a/web/service/xray_setting.go +++ b/web/service/xray_setting.go @@ -15,6 +15,12 @@ type XraySettingService struct { } func (s *XraySettingService) SaveXraySetting(newXraySettings string) error { + // The frontend round-trips the whole getXraySetting response back + // through the textarea, so if it has ever received a wrapped + // payload (see UnwrapXrayTemplateConfig) it sends that same wrapper + // back here. Strip it before validation/storage, otherwise we save + // garbage the next read can't recover from without this same call. + newXraySettings = UnwrapXrayTemplateConfig(newXraySettings) if err := s.CheckXrayConfig(newXraySettings); err != nil { return err } @@ -29,3 +35,51 @@ func (s *XraySettingService) CheckXrayConfig(XrayTemplateConfig string) error { } return nil } + +// UnwrapXrayTemplateConfig returns the raw xray config JSON from `raw`, +// peeling off any number of `{ "inboundTags": ..., "outboundTestUrl": ..., +// "xraySetting": <real config> }` response-shaped wrappers that may have +// ended up in the database. +// +// How it got there: getXraySetting used to embed the raw DB value as +// `xraySetting` in its response without checking whether the stored +// value was already that exact response shape. If the frontend then +// saved it verbatim (the textarea is a round-trip of the JSON it was +// handed), the wrapper got persisted — and each subsequent save nested +// another layer, producing the blank Xray Settings page reported in +// issue #4059. +// +// If `raw` does not look like a wrapper, it is returned unchanged. +func UnwrapXrayTemplateConfig(raw string) string { + const maxDepth = 8 // defensive cap against pathological multi-nest values + for i := 0; i < maxDepth; i++ { + var top map[string]json.RawMessage + if err := json.Unmarshal([]byte(raw), &top); err != nil { + return raw + } + inner, ok := top["xraySetting"] + if !ok { + return raw + } + // Real xray configs never contain a top-level "xraySetting" key, + // but they do contain things like "inbounds"/"outbounds"/"api". + // If any of those are present, we're already at the real config + // and the "xraySetting" field is either user data or coincidence + // — don't touch it. + for _, k := range []string{"inbounds", "outbounds", "routing", "api", "dns", "log", "policy", "stats"} { + if _, hit := top[k]; hit { + return raw + } + } + // Peel off one layer. + unwrapped := string(inner) + // `xraySetting` may be stored either as a JSON object or as a + // JSON-encoded string of an object. Handle both. + var asStr string + if err := json.Unmarshal(inner, &asStr); err == nil { + unwrapped = asStr + } + raw = unwrapped + } + return raw +} |
