Improve configuration merge

This commit is contained in:
世界 2023-12-10 22:57:28 +08:00
parent 7396c30dfd
commit 28ca8c3de5
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
4 changed files with 13 additions and 5 deletions

View File

@ -17,6 +17,7 @@ import (
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/json"
"github.com/sagernet/sing/common/json/badjson"
"github.com/spf13/cobra"
@ -107,13 +108,18 @@ func readConfigAndMerge() (option.Options, error) {
if len(optionsList) == 1 {
return optionsList[0].options, nil
}
var mergedOptions option.Options
var mergedMessage json.RawMessage
for _, options := range optionsList {
mergedOptions, err = badjson.Merge(options.options, mergedOptions)
mergedMessage, err = badjson.MergeJSON(options.options.RawMessage, mergedMessage)
if err != nil {
return option.Options{}, E.Cause(err, "merge config at ", options.path)
}
}
var mergedOptions option.Options
err = mergedOptions.UnmarshalJSON(mergedMessage)
if err != nil {
return option.Options{}, E.Cause(err, "unmarshal merged config")
}
return mergedOptions, nil
}

2
go.mod
View File

@ -26,7 +26,7 @@ require (
github.com/sagernet/gvisor v0.0.0-20231209105102-8d27a30e436e
github.com/sagernet/quic-go v0.40.0
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691
github.com/sagernet/sing v0.2.19-0.20231209022445-766839c00099
github.com/sagernet/sing v0.2.19-0.20231211084415-35e7014b0898
github.com/sagernet/sing-dns v0.1.11
github.com/sagernet/sing-mux v0.1.6-0.20231207143704-9f6c20fb5266
github.com/sagernet/sing-quic v0.1.6-0.20231207143711-eb3cbf9ed054

4
go.sum
View File

@ -109,8 +109,8 @@ github.com/sagernet/quic-go v0.40.0/go.mod h1:VqtdhlbkeeG5Okhb3eDMb/9o0EoglReHun
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byLGkEnIYp6grlXfo1QYUfiYFGjewIdc=
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU=
github.com/sagernet/sing v0.2.18/go.mod h1:OL6k2F0vHmEzXz2KW19qQzu172FDgSbUSODylighuVo=
github.com/sagernet/sing v0.2.19-0.20231209022445-766839c00099 h1:q/efB3NAhVolSoBe0oDc7eZ7/72+OZITWVwn0o92Hp0=
github.com/sagernet/sing v0.2.19-0.20231209022445-766839c00099/go.mod h1:Ce5LNojQOgOiWhiD8pPD6E9H7e2KgtOe3Zxx4Ou5u80=
github.com/sagernet/sing v0.2.19-0.20231211084415-35e7014b0898 h1:UsIwOvI9JAsUeRBFv6joJUW2CvH7yTzwr86OF2+hBCg=
github.com/sagernet/sing v0.2.19-0.20231211084415-35e7014b0898/go.mod h1:Ce5LNojQOgOiWhiD8pPD6E9H7e2KgtOe3Zxx4Ou5u80=
github.com/sagernet/sing-dns v0.1.11 h1:PPrMCVVrAeR3f5X23I+cmvacXJ+kzuyAsBiWyUKhGSE=
github.com/sagernet/sing-dns v0.1.11/go.mod h1:zJ/YjnYB61SYE+ubMcMqVdpaSvsyQ2iShQGO3vuLvvE=
github.com/sagernet/sing-mux v0.1.6-0.20231207143704-9f6c20fb5266 h1:QqwwUyEfmOuoGVTZ2cYvUJEeSWlzunvQLRmv+9B41uk=

View File

@ -9,6 +9,7 @@ import (
)
type _Options struct {
RawMessage json.RawMessage `json:"-"`
Schema string `json:"$schema,omitempty"`
Log *LogOptions `json:"log,omitempty"`
DNS *DNSOptions `json:"dns,omitempty"`
@ -26,6 +27,7 @@ func (o *Options) UnmarshalJSON(content []byte) error {
decoder.DisallowUnknownFields()
err := decoder.Decode((*_Options)(o))
if err == nil {
o.RawMessage = content
return nil
}
if syntaxError, isSyntaxError := err.(*json.SyntaxError); isSyntaxError {