github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/option/rule_dns.go (about) 1 package option 2 3 import ( 4 "reflect" 5 6 "github.com/inazumav/sing-box/common/json" 7 C "github.com/inazumav/sing-box/constant" 8 "github.com/sagernet/sing/common" 9 E "github.com/sagernet/sing/common/exceptions" 10 ) 11 12 type _DNSRule struct { 13 Type string `json:"type,omitempty"` 14 DefaultOptions DefaultDNSRule `json:"-"` 15 LogicalOptions LogicalDNSRule `json:"-"` 16 } 17 18 type DNSRule _DNSRule 19 20 func (r DNSRule) MarshalJSON() ([]byte, error) { 21 var v any 22 switch r.Type { 23 case C.RuleTypeDefault: 24 r.Type = "" 25 v = r.DefaultOptions 26 case C.RuleTypeLogical: 27 v = r.LogicalOptions 28 default: 29 return nil, E.New("unknown rule type: " + r.Type) 30 } 31 return MarshallObjects((_DNSRule)(r), v) 32 } 33 34 func (r *DNSRule) UnmarshalJSON(bytes []byte) error { 35 err := json.Unmarshal(bytes, (*_DNSRule)(r)) 36 if err != nil { 37 return err 38 } 39 var v any 40 switch r.Type { 41 case "", C.RuleTypeDefault: 42 r.Type = C.RuleTypeDefault 43 v = &r.DefaultOptions 44 case C.RuleTypeLogical: 45 v = &r.LogicalOptions 46 default: 47 return E.New("unknown rule type: " + r.Type) 48 } 49 err = UnmarshallExcluded(bytes, (*_DNSRule)(r), v) 50 if err != nil { 51 return E.Cause(err, "dns route rule") 52 } 53 return nil 54 } 55 56 type DefaultDNSRule struct { 57 Inbound Listable[string] `json:"inbound,omitempty"` 58 IPVersion int `json:"ip_version,omitempty"` 59 QueryType Listable[DNSQueryType] `json:"query_type,omitempty"` 60 Network Listable[string] `json:"network,omitempty"` 61 AuthUser Listable[string] `json:"auth_user,omitempty"` 62 Protocol Listable[string] `json:"protocol,omitempty"` 63 Domain Listable[string] `json:"domain,omitempty"` 64 DomainSuffix Listable[string] `json:"domain_suffix,omitempty"` 65 DomainKeyword Listable[string] `json:"domain_keyword,omitempty"` 66 DomainRegex Listable[string] `json:"domain_regex,omitempty"` 67 Geosite Listable[string] `json:"geosite,omitempty"` 68 SourceGeoIP Listable[string] `json:"source_geoip,omitempty"` 69 SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"` 70 SourcePort Listable[uint16] `json:"source_port,omitempty"` 71 SourcePortRange Listable[string] `json:"source_port_range,omitempty"` 72 Port Listable[uint16] `json:"port,omitempty"` 73 PortRange Listable[string] `json:"port_range,omitempty"` 74 ProcessName Listable[string] `json:"process_name,omitempty"` 75 ProcessPath Listable[string] `json:"process_path,omitempty"` 76 PackageName Listable[string] `json:"package_name,omitempty"` 77 User Listable[string] `json:"user,omitempty"` 78 UserID Listable[int32] `json:"user_id,omitempty"` 79 Outbound Listable[string] `json:"outbound,omitempty"` 80 ClashMode string `json:"clash_mode,omitempty"` 81 Invert bool `json:"invert,omitempty"` 82 Server string `json:"server,omitempty"` 83 DisableCache bool `json:"disable_cache,omitempty"` 84 RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"` 85 } 86 87 func (r DefaultDNSRule) IsValid() bool { 88 var defaultValue DefaultDNSRule 89 defaultValue.Invert = r.Invert 90 defaultValue.Server = r.Server 91 defaultValue.DisableCache = r.DisableCache 92 defaultValue.RewriteTTL = r.RewriteTTL 93 return !reflect.DeepEqual(r, defaultValue) 94 } 95 96 type LogicalDNSRule struct { 97 Mode string `json:"mode"` 98 Rules []DefaultDNSRule `json:"rules,omitempty"` 99 Invert bool `json:"invert,omitempty"` 100 Server string `json:"server,omitempty"` 101 DisableCache bool `json:"disable_cache,omitempty"` 102 RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"` 103 } 104 105 func (r LogicalDNSRule) IsValid() bool { 106 return len(r.Rules) > 0 && common.All(r.Rules, DefaultDNSRule.IsValid) 107 }