mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-07-02 17:58:46 +00:00
Config: Support env XRAY_JSON_STRICT=true (#6053)
https://github.com/XTLS/Xray-core/pull/6053#issuecomment-4363840170 --------- Co-authored-by: RPRX <63339210+RPRX@users.noreply.github.com>
This commit is contained in:
@@ -5,12 +5,22 @@ import (
|
||||
"io"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/platform"
|
||||
creflect "github.com/xtls/xray-core/common/reflect"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
"github.com/xtls/xray-core/main/confloader"
|
||||
)
|
||||
|
||||
// UseStrictJSON, when true, makes JSON config decoders skip the custom
|
||||
// comment-stripping reader and parse input as strict RFC 8259 JSON.
|
||||
//
|
||||
// Enabled by setting the env variable xray.json.strict=true (or its normalized
|
||||
// form XRAY_JSON_STRICT=true). Default false preserves backward-compatible
|
||||
// behavior for human-edited configs that may contain comments or other
|
||||
// JSON5/JSONC syntax.
|
||||
var UseStrictJSON = platform.NewEnvFlag(platform.UseStrictJSON).GetValue(func() string { return "" }) == "true"
|
||||
|
||||
func MergeConfigFromFiles(files []*core.ConfigSource) (string, error) {
|
||||
c, err := mergeConfigs(files)
|
||||
if err != nil {
|
||||
@@ -31,7 +41,11 @@ func mergeConfigs(files []*core.ConfigSource) (*conf.Config, error) {
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to read config: ", file).Base(err)
|
||||
}
|
||||
c, err := ReaderDecoderByFormat[file.Format](r)
|
||||
decoder := ReaderDecoderByFormat[file.Format]
|
||||
if file.Format == "json" && UseStrictJSON {
|
||||
decoder = DecodeJSONConfigStrict
|
||||
}
|
||||
c, err := decoder(r)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to decode config: ", file).Base(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user