Skip to content

Commit

Permalink
Fix rule-set template
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed May 19, 2024
1 parent 6da3a8e commit 6dcfa70
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
16 changes: 8 additions & 8 deletions docs/configuration/shared/rule-set.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ RuleSet generate configuration.

### Structure

=== "Default"
=== "Original"

```json
{
"type": "", // optional
"type": "remote", // or local
... // Default Fields
... // Original Fields
}
```

Expand All @@ -21,7 +21,7 @@ RuleSet generate configuration.
"type": "github",
"repository": "",
"path": "",
"rule-set": []
"rule_set": []
}
```

Expand All @@ -33,7 +33,7 @@ RuleSet generate configuration.
"repository": "SagerNet/sing-geosite",
"path": "rule-set",
"prefix": "geosite-",
"rule-set": [
"rule_set": [
"apple",
"microsoft",
"openai"
Expand All @@ -49,15 +49,15 @@ RuleSet generate configuration.
"repository": "MetaCubeX/meta-rules-dat",
"path": "sing/geo/geosite",
"prefix": "geosite-",
"rule-set": [
"rule_set": [
"apple",
"microsoft",
"openai"
]
}
```

### Default Fields
### Original Fields

See [RuleSet](https://sing-box.sagernet.org/configuration/rule-set/).

Expand All @@ -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.
29 changes: 8 additions & 21 deletions option/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -80,36 +79,24 @@ 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 {
err := json.Unmarshal(bytes, (*_RuleSet)(r))
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 {
Expand Down
7 changes: 3 additions & 4 deletions template/render_geo_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -133,6 +130,8 @@ func (t *Template) renderRuleSet(ruleSets []option.RuleSet) []boxOption.RuleSet
},
})
}
} else {
result = append(result, ruleSet.DefaultOptions)
}
}
return result
Expand Down

0 comments on commit 6dcfa70

Please sign in to comment.