forked from mirror/serenity
Fix rule-set template
This commit is contained in:
parent
6da3a8eaee
commit
6dcfa706eb
@ -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/<geoip/geosite>`.
|
||||
|
||||
File prefix, `geoip-` or `geosite-`.
|
||||
|
||||
#### rule-set
|
||||
#### rule_set
|
||||
|
||||
RuleSet name list.
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user