sing-box/option/rule.go

161 lines
5.7 KiB
Go
Raw Normal View History

2023-06-07 12:28:21 +00:00
package option
import (
"reflect"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/json"
2024-11-01 16:39:02 +00:00
"github.com/sagernet/sing/common/json/badjson"
2024-11-07 13:44:04 +00:00
"github.com/sagernet/sing/common/json/badoption"
2023-06-07 12:28:21 +00:00
)
type _Rule struct {
Type string `json:"type,omitempty"`
DefaultOptions DefaultRule `json:"-"`
LogicalOptions LogicalRule `json:"-"`
}
type Rule _Rule
func (r Rule) 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-01 16:39:02 +00:00
return badjson.MarshallObjects((_Rule)(r), v)
2023-06-07 12:28:21 +00:00
}
func (r *Rule) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_Rule)(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-01 16:39:02 +00:00
err = badjson.UnmarshallExcluded(bytes, (*_Rule)(r), v)
2023-06-07 12:28:21 +00:00
if err != nil {
return err
2023-06-07 12:28:21 +00:00
}
return nil
}
2023-11-28 15:47:32 +00:00
func (r Rule) 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)
}
}
2024-10-21 15:38:34 +00:00
type RawDefaultRule struct {
2024-11-07 13:44:04 +00:00
Inbound badoption.Listable[string] `json:"inbound,omitempty"`
IPVersion int `json:"ip_version,omitempty"`
Network badoption.Listable[string] `json:"network,omitempty"`
AuthUser badoption.Listable[string] `json:"auth_user,omitempty"`
Protocol badoption.Listable[string] `json:"protocol,omitempty"`
Client badoption.Listable[string] `json:"client,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"`
Geosite badoption.Listable[string] `json:"geosite,omitempty"`
SourceGeoIP badoption.Listable[string] `json:"source_geoip,omitempty"`
GeoIP badoption.Listable[string] `json:"geoip,omitempty"`
SourceIPCIDR badoption.Listable[string] `json:"source_ip_cidr,omitempty"`
SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
IPCIDR badoption.Listable[string] `json:"ip_cidr,omitempty"`
IPIsPrivate bool `json:"ip_is_private,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"`
User badoption.Listable[string] `json:"user,omitempty"`
UserID badoption.Listable[int32] `json:"user_id,omitempty"`
ClashMode string `json:"clash_mode,omitempty"`
NetworkType badoption.Listable[string] `json:"network_type,omitempty"`
NetworkIsExpensive bool `json:"network_is_expensive,omitempty"`
NetworkIsConstrained bool `json:"network_is_constrained,omitempty"`
2024-11-07 13:44:04 +00:00
WIFISSID badoption.Listable[string] `json:"wifi_ssid,omitempty"`
WIFIBSSID badoption.Listable[string] `json:"wifi_bssid,omitempty"`
RuleSet badoption.Listable[string] `json:"rule_set,omitempty"`
RuleSetIPCIDRMatchSource bool `json:"rule_set_ip_cidr_match_source,omitempty"`
Invert bool `json:"invert,omitempty"`
2024-06-24 01:41:00 +00:00
// Deprecated: renamed to rule_set_ip_cidr_match_source
Deprecated_RulesetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
}
2024-10-21 15:38:34 +00:00
type DefaultRule struct {
RawDefaultRule
RuleAction
}
2024-11-06 09:30:40 +00:00
func (r DefaultRule) MarshalJSON() ([]byte, error) {
2024-11-01 16:39:02 +00:00
return badjson.MarshallObjects(r.RawDefaultRule, r.RuleAction)
2024-10-21 15:38:34 +00:00
}
func (r *DefaultRule) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, &r.RawDefaultRule)
if err != nil {
return err
}
2024-11-01 16:39:02 +00:00
return badjson.UnmarshallExcluded(data, &r.RawDefaultRule, &r.RuleAction)
2024-10-21 15:38:34 +00:00
}
2024-06-24 01:41:00 +00:00
func (r *DefaultRule) IsValid() bool {
2023-06-07 12:28:21 +00:00
var defaultValue DefaultRule
defaultValue.Invert = r.Invert
2024-10-21 15:38:34 +00:00
defaultValue.Action = r.Action
2023-06-07 12:28:21 +00:00
return !reflect.DeepEqual(r, defaultValue)
}
2024-11-06 09:30:40 +00:00
type RawLogicalRule struct {
2024-10-21 15:38:34 +00:00
Mode string `json:"mode"`
Rules []Rule `json:"rules,omitempty"`
Invert bool `json:"invert,omitempty"`
}
2023-06-07 12:28:21 +00:00
type LogicalRule struct {
2024-11-06 09:30:40 +00:00
RawLogicalRule
2024-10-21 15:38:34 +00:00
RuleAction
}
2024-11-06 09:30:40 +00:00
func (r LogicalRule) MarshalJSON() ([]byte, error) {
return badjson.MarshallObjects(r.RawLogicalRule, r.RuleAction)
2024-10-21 15:38:34 +00:00
}
func (r *LogicalRule) UnmarshalJSON(data []byte) error {
2024-11-06 09:30:40 +00:00
err := json.Unmarshal(data, &r.RawLogicalRule)
2024-10-21 15:38:34 +00:00
if err != nil {
return err
}
2024-11-06 09:30:40 +00:00
return badjson.UnmarshallExcluded(data, &r.RawLogicalRule, &r.RuleAction)
2023-06-07 12:28:21 +00:00
}
2024-10-21 15:38:34 +00:00
func (r *LogicalRule) IsValid() bool {
2023-11-28 15:47:32 +00:00
return len(r.Rules) > 0 && common.All(r.Rules, Rule.IsValid)
2023-06-07 12:28:21 +00:00
}