sing-box/option/rule_set.go

244 lines
6.8 KiB
Go
Raw Normal View History

2023-12-01 13:24:12 +08:00
package option
import (
"reflect"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/domain"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/json"
2024-11-02 00:39:02 +08:00
"github.com/sagernet/sing/common/json/badjson"
2024-11-07 21:44:04 +08:00
"github.com/sagernet/sing/common/json/badoption"
2023-12-01 13:24:12 +08:00
"go4.org/netipx"
)
type _RuleSet struct {
Type string `json:"type,omitempty"`
2023-12-01 13:24:12 +08:00
Tag string `json:"tag"`
Format string `json:"format,omitempty"`
InlineOptions PlainRuleSet `json:"-"`
2023-12-01 13:24:12 +08:00
LocalOptions LocalRuleSet `json:"-"`
RemoteOptions RemoteRuleSet `json:"-"`
}
type RuleSet _RuleSet
func (r RuleSet) MarshalJSON() ([]byte, error) {
var v any
switch r.Type {
case "", C.RuleSetTypeInline:
r.Type = ""
v = r.InlineOptions
2023-12-01 13:24:12 +08:00
case C.RuleSetTypeLocal:
v = r.LocalOptions
case C.RuleSetTypeRemote:
v = r.RemoteOptions
default:
2024-06-26 00:45:10 +08:00
return nil, E.New("unknown rule-set type: " + r.Type)
2023-12-01 13:24:12 +08:00
}
2024-11-02 00:39:02 +08:00
return badjson.MarshallObjects((_RuleSet)(r), v)
2023-12-01 13:24:12 +08:00
}
func (r *RuleSet) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_RuleSet)(r))
if err != nil {
return err
}
if r.Tag == "" {
return E.New("missing tag")
}
var v any
switch r.Type {
case "", C.RuleSetTypeInline:
r.Type = C.RuleSetTypeInline
v = &r.InlineOptions
2023-12-01 13:24:12 +08:00
case C.RuleSetTypeLocal:
v = &r.LocalOptions
case C.RuleSetTypeRemote:
v = &r.RemoteOptions
default:
2024-06-26 00:45:10 +08:00
return E.New("unknown rule-set type: " + r.Type)
2023-12-01 13:24:12 +08:00
}
2024-10-19 21:07:43 +08:00
if r.Type != C.RuleSetTypeInline {
switch r.Format {
case "":
return E.New("missing format")
case C.RuleSetFormatSource, C.RuleSetFormatBinary:
default:
return E.New("unknown rule-set format: " + r.Format)
}
} else {
r.Format = ""
}
2024-11-02 00:39:02 +08:00
err = badjson.UnmarshallExcluded(bytes, (*_RuleSet)(r), v)
2023-12-01 13:24:12 +08:00
if err != nil {
return err
2023-12-01 13:24:12 +08:00
}
return nil
}
type LocalRuleSet struct {
Path string `json:"path,omitempty"`
}
type RemoteRuleSet struct {
2024-11-07 21:44:04 +08:00
URL string `json:"url"`
DownloadDetour string `json:"download_detour,omitempty"`
UpdateInterval badoption.Duration `json:"update_interval,omitempty"`
2023-12-01 13:24:12 +08:00
}
type _HeadlessRule struct {
Type string `json:"type,omitempty"`
DefaultOptions DefaultHeadlessRule `json:"-"`
LogicalOptions LogicalHeadlessRule `json:"-"`
}
type HeadlessRule _HeadlessRule
func (r HeadlessRule) MarshalJSON() ([]byte, error) {
var v any
switch r.Type {
case C.RuleTypeDefault:
r.Type = ""
v = r.DefaultOptions
case C.RuleTypeLogical:
v = r.LogicalOptions
default:
return nil, E.New("unknown rule type: " + r.Type)
}
2024-11-02 00:39:02 +08:00
return badjson.MarshallObjects((_HeadlessRule)(r), v)
2023-12-01 13:24:12 +08:00
}
func (r *HeadlessRule) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_HeadlessRule)(r))
if err != nil {
return err
}
var v any
switch r.Type {
case "", C.RuleTypeDefault:
r.Type = C.RuleTypeDefault
v = &r.DefaultOptions
case C.RuleTypeLogical:
v = &r.LogicalOptions
default:
return E.New("unknown rule type: " + r.Type)
}
2024-11-02 00:39:02 +08:00
err = badjson.UnmarshallExcluded(bytes, (*_HeadlessRule)(r), v)
2023-12-01 13:24:12 +08:00
if err != nil {
return err
2023-12-01 13:24:12 +08:00
}
return nil
}
func (r HeadlessRule) IsValid() bool {
switch r.Type {
case C.RuleTypeDefault, "":
return r.DefaultOptions.IsValid()
case C.RuleTypeLogical:
return r.LogicalOptions.IsValid()
default:
panic("unknown rule type: " + r.Type)
}
}
type DefaultHeadlessRule struct {
2024-11-07 21:44:04 +08:00
QueryType badoption.Listable[DNSQueryType] `json:"query_type,omitempty"`
Network badoption.Listable[string] `json:"network,omitempty"`
Domain badoption.Listable[string] `json:"domain,omitempty"`
DomainSuffix badoption.Listable[string] `json:"domain_suffix,omitempty"`
DomainKeyword badoption.Listable[string] `json:"domain_keyword,omitempty"`
DomainRegex badoption.Listable[string] `json:"domain_regex,omitempty"`
SourceIPCIDR badoption.Listable[string] `json:"source_ip_cidr,omitempty"`
IPCIDR badoption.Listable[string] `json:"ip_cidr,omitempty"`
SourcePort badoption.Listable[uint16] `json:"source_port,omitempty"`
SourcePortRange badoption.Listable[string] `json:"source_port_range,omitempty"`
Port badoption.Listable[uint16] `json:"port,omitempty"`
PortRange badoption.Listable[string] `json:"port_range,omitempty"`
ProcessName badoption.Listable[string] `json:"process_name,omitempty"`
ProcessPath badoption.Listable[string] `json:"process_path,omitempty"`
ProcessPathRegex badoption.Listable[string] `json:"process_path_regex,omitempty"`
PackageName badoption.Listable[string] `json:"package_name,omitempty"`
WIFISSID badoption.Listable[string] `json:"wifi_ssid,omitempty"`
WIFIBSSID badoption.Listable[string] `json:"wifi_bssid,omitempty"`
Invert bool `json:"invert,omitempty"`
2023-12-01 13:24:12 +08:00
DomainMatcher *domain.Matcher `json:"-"`
SourceIPSet *netipx.IPSet `json:"-"`
IPSet *netipx.IPSet `json:"-"`
2024-07-26 08:03:08 +08:00
2024-11-07 21:44:04 +08:00
AdGuardDomain badoption.Listable[string] `json:"-"`
AdGuardDomainMatcher *domain.AdGuardMatcher `json:"-"`
2023-12-01 13:24:12 +08:00
}
func (r DefaultHeadlessRule) IsValid() bool {
var defaultValue DefaultHeadlessRule
defaultValue.Invert = r.Invert
return !reflect.DeepEqual(r, defaultValue)
}
type LogicalHeadlessRule struct {
Mode string `json:"mode"`
Rules []HeadlessRule `json:"rules,omitempty"`
Invert bool `json:"invert,omitempty"`
}
func (r LogicalHeadlessRule) IsValid() bool {
return len(r.Rules) > 0 && common.All(r.Rules, HeadlessRule.IsValid)
}
type _PlainRuleSetCompat struct {
Version int `json:"version"`
Options PlainRuleSet `json:"-"`
}
type PlainRuleSetCompat _PlainRuleSetCompat
func (r PlainRuleSetCompat) MarshalJSON() ([]byte, error) {
var v any
switch r.Version {
2024-07-17 17:57:35 +08:00
case C.RuleSetVersion1, C.RuleSetVersion2:
2023-12-01 13:24:12 +08:00
v = r.Options
default:
2024-06-26 00:45:10 +08:00
return nil, E.New("unknown rule-set version: ", r.Version)
2023-12-01 13:24:12 +08:00
}
2024-11-02 00:39:02 +08:00
return badjson.MarshallObjects((_PlainRuleSetCompat)(r), v)
2023-12-01 13:24:12 +08:00
}
func (r *PlainRuleSetCompat) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_PlainRuleSetCompat)(r))
if err != nil {
return err
}
var v any
switch r.Version {
2024-07-17 17:57:35 +08:00
case C.RuleSetVersion1, C.RuleSetVersion2:
2023-12-01 13:24:12 +08:00
v = &r.Options
case 0:
2024-06-26 00:45:10 +08:00
return E.New("missing rule-set version")
2023-12-01 13:24:12 +08:00
default:
2024-06-26 00:45:10 +08:00
return E.New("unknown rule-set version: ", r.Version)
2023-12-01 13:24:12 +08:00
}
2024-11-02 00:39:02 +08:00
err = badjson.UnmarshallExcluded(bytes, (*_PlainRuleSetCompat)(r), v)
2023-12-01 13:24:12 +08:00
if err != nil {
return err
2023-12-01 13:24:12 +08:00
}
return nil
}
func (r PlainRuleSetCompat) Upgrade() (PlainRuleSet, error) {
2023-12-01 13:24:12 +08:00
switch r.Version {
2024-07-17 17:57:35 +08:00
case C.RuleSetVersion1, C.RuleSetVersion2:
2023-12-01 13:24:12 +08:00
default:
return PlainRuleSet{}, E.New("unknown rule-set version: " + F.ToString(r.Version))
2023-12-01 13:24:12 +08:00
}
return r.Options, nil
2023-12-01 13:24:12 +08:00
}
type PlainRuleSet struct {
Rules []HeadlessRule `json:"rules,omitempty"`
}