diff --git a/docs/configuration/shared/rule-set.md b/docs/configuration/shared/rule-set.md index 51230d0..9c495ff 100644 --- a/docs/configuration/shared/rule-set.md +++ b/docs/configuration/shared/rule-set.md @@ -4,13 +4,13 @@ RuleSet generate configuration. ### Structure -=== "Default" +=== "Original" ```json { - "type": "", // optional + "type": "remote", // or local - ... // Default Fields + ... // Original Fields } ``` @@ -21,7 +21,7 @@ RuleSet generate configuration. "type": "github", "repository": "", "path": "", - "rule-set": [] + "rule_set": [] } ``` @@ -33,7 +33,7 @@ RuleSet generate configuration. "repository": "SagerNet/sing-geosite", "path": "rule-set", "prefix": "geosite-", - "rule-set": [ + "rule_set": [ "apple", "microsoft", "openai" @@ -49,7 +49,7 @@ RuleSet generate configuration. "repository": "MetaCubeX/meta-rules-dat", "path": "sing/geo/geosite", "prefix": "geosite-", - "rule-set": [ + "rule_set": [ "apple", "microsoft", "openai" @@ -57,7 +57,7 @@ RuleSet generate configuration. } ``` -### Default Fields +### Original Fields See [RuleSet](https://sing-box.sagernet.org/configuration/rule-set/). @@ -75,6 +75,6 @@ Branch and directory path, `rule-set` or `sing/geo/`. File prefix, `geoip-` or `geosite-`. -#### rule-set +#### rule_set RuleSet name list. diff --git a/option/template.go b/option/template.go index 81c8ffa..90b1782 100644 --- a/option/template.go +++ b/option/template.go @@ -4,7 +4,6 @@ import ( C "github.com/sagernet/serenity/constant" "github.com/sagernet/sing-box/option" "github.com/sagernet/sing-dns" - E "github.com/sagernet/sing/common/exceptions" "github.com/sagernet/sing/common/json" ) @@ -80,24 +79,12 @@ type _RuleSet struct { type RuleSet _RuleSet -func (r *RuleSet) RawOptions() (any, error) { - switch r.Type { - case C.RuleSetTypeDefault, "": - r.Type = "" - return &r.DefaultOptions, nil - case C.RuleSetTypeGitHub: - return &r.GitHubOptions, nil - default: - return nil, E.New("unknown rule set type", r.Type) - } -} - func (r *RuleSet) MarshalJSON() ([]byte, error) { - rawOptions, err := r.RawOptions() - if err != nil { - return nil, err + if r.Type == C.RuleSetTypeGitHub { + return option.MarshallObjects((*_RuleSet)(r), r.GitHubOptions) + } else { + return json.Marshal(r.DefaultOptions) } - return option.MarshallObjects((*_RuleSet)(r), rawOptions) } func (r *RuleSet) UnmarshalJSON(bytes []byte) error { @@ -105,11 +92,11 @@ func (r *RuleSet) UnmarshalJSON(bytes []byte) error { if err != nil { return err } - rawOptions, err := r.RawOptions() - if err != nil { - return err + if r.Type == C.RuleSetTypeGitHub { + return option.UnmarshallExcluded(bytes, (*_RuleSet)(r), &r.GitHubOptions) + } else { + return json.Unmarshal(bytes, &r.DefaultOptions) } - return option.UnmarshallExcluded(bytes, (*_RuleSet)(r), rawOptions) } type GitHubRuleSetOptions struct { diff --git a/template/render_geo_resources.go b/template/render_geo_resources.go index b15d08e..b1eec86 100644 --- a/template/render_geo_resources.go +++ b/template/render_geo_resources.go @@ -96,10 +96,7 @@ func (t *Template) renderGeoResources(metadata M.Metadata, options *boxOption.Op func (t *Template) renderRuleSet(ruleSets []option.RuleSet) []boxOption.RuleSet { var result []boxOption.RuleSet for _, ruleSet := range ruleSets { - switch ruleSet.Type { - case constant.RuleSetTypeDefault, "": - result = append(result, ruleSet.DefaultOptions) - case constant.RuleSetTypeGitHub: + if ruleSet.Type == constant.RuleSetTypeGitHub { var ( downloadURL string downloadDetour string @@ -133,6 +130,8 @@ func (t *Template) renderRuleSet(ruleSets []option.RuleSet) []boxOption.RuleSet }, }) } + } else { + result = append(result, ruleSet.DefaultOptions) } } return result