2023-06-07 20:28:21 +08:00
|
|
|
package option
|
|
|
|
|
|
|
|
import (
|
2024-11-07 12:02:36 +08:00
|
|
|
"context"
|
2023-06-07 20:28:21 +08:00
|
|
|
"reflect"
|
|
|
|
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2023-12-01 20:15:11 +08:00
|
|
|
"github.com/sagernet/sing/common/json"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing/common/json/badjson"
|
2023-06-07 20:28:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type _DNSRule struct {
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
DefaultOptions DefaultDNSRule `json:"-"`
|
|
|
|
LogicalOptions LogicalDNSRule `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DNSRule _DNSRule
|
|
|
|
|
|
|
|
func (r DNSRule) 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((_DNSRule)(r), v)
|
2023-06-07 20:28:21 +08:00
|
|
|
}
|
|
|
|
|
2024-11-07 12:02:36 +08:00
|
|
|
func (r *DNSRule) UnmarshalJSONContext(ctx context.Context, bytes []byte) error {
|
2023-06-07 20:28:21 +08:00
|
|
|
err := json.Unmarshal(bytes, (*_DNSRule)(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-07 12:02:36 +08:00
|
|
|
err = badjson.UnmarshallExcludedContext(ctx, bytes, (*_DNSRule)(r), v)
|
2023-06-07 20:28:21 +08:00
|
|
|
if err != nil {
|
2023-12-01 20:15:11 +08:00
|
|
|
return err
|
2023-06-07 20:28:21 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-28 23:47:32 +08:00
|
|
|
func (r DNSRule) IsValid() bool {
|
|
|
|
switch r.Type {
|
|
|
|
case C.RuleTypeDefault:
|
|
|
|
return r.DefaultOptions.IsValid()
|
|
|
|
case C.RuleTypeLogical:
|
|
|
|
return r.LogicalOptions.IsValid()
|
|
|
|
default:
|
|
|
|
panic("unknown DNS rule type: " + r.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
type RawDefaultDNSRule struct {
|
2024-02-21 14:27:55 +08:00
|
|
|
Inbound Listable[string] `json:"inbound,omitempty"`
|
|
|
|
IPVersion int `json:"ip_version,omitempty"`
|
|
|
|
QueryType Listable[DNSQueryType] `json:"query_type,omitempty"`
|
|
|
|
Network Listable[string] `json:"network,omitempty"`
|
|
|
|
AuthUser Listable[string] `json:"auth_user,omitempty"`
|
|
|
|
Protocol Listable[string] `json:"protocol,omitempty"`
|
|
|
|
Domain Listable[string] `json:"domain,omitempty"`
|
|
|
|
DomainSuffix Listable[string] `json:"domain_suffix,omitempty"`
|
|
|
|
DomainKeyword Listable[string] `json:"domain_keyword,omitempty"`
|
|
|
|
DomainRegex Listable[string] `json:"domain_regex,omitempty"`
|
|
|
|
Geosite Listable[string] `json:"geosite,omitempty"`
|
|
|
|
SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
|
|
|
|
GeoIP Listable[string] `json:"geoip,omitempty"`
|
|
|
|
IPCIDR Listable[string] `json:"ip_cidr,omitempty"`
|
|
|
|
IPIsPrivate bool `json:"ip_is_private,omitempty"`
|
|
|
|
SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
|
|
|
|
SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
|
|
|
|
SourcePort Listable[uint16] `json:"source_port,omitempty"`
|
|
|
|
SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
|
|
|
|
Port Listable[uint16] `json:"port,omitempty"`
|
|
|
|
PortRange Listable[string] `json:"port_range,omitempty"`
|
|
|
|
ProcessName Listable[string] `json:"process_name,omitempty"`
|
|
|
|
ProcessPath Listable[string] `json:"process_path,omitempty"`
|
2024-09-15 11:42:57 +08:00
|
|
|
ProcessPathRegex Listable[string] `json:"process_path_regex,omitempty"`
|
2024-02-21 14:27:55 +08:00
|
|
|
PackageName Listable[string] `json:"package_name,omitempty"`
|
|
|
|
User Listable[string] `json:"user,omitempty"`
|
|
|
|
UserID Listable[int32] `json:"user_id,omitempty"`
|
|
|
|
Outbound Listable[string] `json:"outbound,omitempty"`
|
|
|
|
ClashMode string `json:"clash_mode,omitempty"`
|
|
|
|
WIFISSID Listable[string] `json:"wifi_ssid,omitempty"`
|
|
|
|
WIFIBSSID Listable[string] `json:"wifi_bssid,omitempty"`
|
|
|
|
RuleSet Listable[string] `json:"rule_set,omitempty"`
|
2024-06-24 09:41:00 +08:00
|
|
|
RuleSetIPCIDRMatchSource bool `json:"rule_set_ip_cidr_match_source,omitempty"`
|
|
|
|
RuleSetIPCIDRAcceptEmpty bool `json:"rule_set_ip_cidr_accept_empty,omitempty"`
|
2024-02-21 14:27:55 +08:00
|
|
|
Invert bool `json:"invert,omitempty"`
|
2024-06-24 09:41:00 +08:00
|
|
|
|
|
|
|
// Deprecated: renamed to rule_set_ip_cidr_match_source
|
|
|
|
Deprecated_RulesetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
type DefaultDNSRule struct {
|
|
|
|
RawDefaultDNSRule
|
|
|
|
DNSRuleAction
|
|
|
|
}
|
|
|
|
|
2024-11-06 17:30:40 +08:00
|
|
|
func (r DefaultDNSRule) MarshalJSON() ([]byte, error) {
|
2024-11-02 00:39:02 +08:00
|
|
|
return badjson.MarshallObjects(r.RawDefaultDNSRule, r.DNSRuleAction)
|
2024-10-21 23:38:34 +08:00
|
|
|
}
|
|
|
|
|
2024-11-07 12:02:36 +08:00
|
|
|
func (r *DefaultDNSRule) UnmarshalJSONContext(ctx context.Context, data []byte) error {
|
|
|
|
err := json.UnmarshalContext(ctx, data, &r.RawDefaultDNSRule)
|
2024-10-21 23:38:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-07 12:02:36 +08:00
|
|
|
return badjson.UnmarshallExcludedContext(ctx, data, &r.RawDefaultDNSRule, &r.DNSRuleAction)
|
2024-10-21 23:38:34 +08:00
|
|
|
}
|
|
|
|
|
2024-11-06 17:30:40 +08:00
|
|
|
func (r DefaultDNSRule) IsValid() bool {
|
2023-06-07 20:28:21 +08:00
|
|
|
var defaultValue DefaultDNSRule
|
|
|
|
defaultValue.Invert = r.Invert
|
2024-10-21 23:38:34 +08:00
|
|
|
defaultValue.DNSRuleAction = r.DNSRuleAction
|
2023-06-07 20:28:21 +08:00
|
|
|
return !reflect.DeepEqual(r, defaultValue)
|
|
|
|
}
|
|
|
|
|
2024-11-06 17:30:40 +08:00
|
|
|
type RawLogicalDNSRule struct {
|
2024-10-21 23:38:34 +08:00
|
|
|
Mode string `json:"mode"`
|
|
|
|
Rules []DNSRule `json:"rules,omitempty"`
|
|
|
|
Invert bool `json:"invert,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-06-07 20:28:21 +08:00
|
|
|
type LogicalDNSRule struct {
|
2024-11-06 17:30:40 +08:00
|
|
|
RawLogicalDNSRule
|
2024-10-21 23:38:34 +08:00
|
|
|
DNSRuleAction
|
|
|
|
}
|
|
|
|
|
2024-11-06 17:30:40 +08:00
|
|
|
func (r LogicalDNSRule) MarshalJSON() ([]byte, error) {
|
|
|
|
return badjson.MarshallObjects(r.RawLogicalDNSRule, r.DNSRuleAction)
|
2024-10-21 23:38:34 +08:00
|
|
|
}
|
|
|
|
|
2024-11-07 12:02:36 +08:00
|
|
|
func (r *LogicalDNSRule) UnmarshalJSONContext(ctx context.Context, data []byte) error {
|
2024-11-06 17:30:40 +08:00
|
|
|
err := json.Unmarshal(data, &r.RawLogicalDNSRule)
|
2024-10-21 23:38:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-07 12:02:36 +08:00
|
|
|
return badjson.UnmarshallExcludedContext(ctx, data, &r.RawLogicalDNSRule, &r.DNSRuleAction)
|
2023-06-07 20:28:21 +08:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
func (r *LogicalDNSRule) IsValid() bool {
|
2023-11-28 23:47:32 +08:00
|
|
|
return len(r.Rules) > 0 && common.All(r.Rules, DNSRule.IsValid)
|
2023-06-07 20:28:21 +08:00
|
|
|
}
|