rule_dns.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package option
  2. import (
  3. "context"
  4. "reflect"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing/common"
  7. E "github.com/sagernet/sing/common/exceptions"
  8. "github.com/sagernet/sing/common/json"
  9. "github.com/sagernet/sing/common/json/badjson"
  10. "github.com/sagernet/sing/common/json/badoption"
  11. )
  12. type _DNSRule struct {
  13. Type string `json:"type,omitempty"`
  14. DefaultOptions DefaultDNSRule `json:"-"`
  15. LogicalOptions LogicalDNSRule `json:"-"`
  16. }
  17. type DNSRule _DNSRule
  18. func (r DNSRule) MarshalJSON() ([]byte, error) {
  19. var v any
  20. switch r.Type {
  21. case C.RuleTypeDefault:
  22. r.Type = ""
  23. v = r.DefaultOptions
  24. case C.RuleTypeLogical:
  25. v = r.LogicalOptions
  26. default:
  27. return nil, E.New("unknown rule type: " + r.Type)
  28. }
  29. return badjson.MarshallObjects((_DNSRule)(r), v)
  30. }
  31. func (r *DNSRule) UnmarshalJSONContext(ctx context.Context, bytes []byte) error {
  32. err := json.Unmarshal(bytes, (*_DNSRule)(r))
  33. if err != nil {
  34. return err
  35. }
  36. var v any
  37. switch r.Type {
  38. case "", C.RuleTypeDefault:
  39. r.Type = C.RuleTypeDefault
  40. v = &r.DefaultOptions
  41. case C.RuleTypeLogical:
  42. v = &r.LogicalOptions
  43. default:
  44. return E.New("unknown rule type: " + r.Type)
  45. }
  46. err = badjson.UnmarshallExcludedContext(ctx, bytes, (*_DNSRule)(r), v)
  47. if err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. func (r DNSRule) IsValid() bool {
  53. switch r.Type {
  54. case C.RuleTypeDefault:
  55. return r.DefaultOptions.IsValid()
  56. case C.RuleTypeLogical:
  57. return r.LogicalOptions.IsValid()
  58. default:
  59. panic("unknown DNS rule type: " + r.Type)
  60. }
  61. }
  62. type RawDefaultDNSRule struct {
  63. Inbound badoption.Listable[string] `json:"inbound,omitempty"`
  64. IPVersion int `json:"ip_version,omitempty"`
  65. QueryType badoption.Listable[DNSQueryType] `json:"query_type,omitempty"`
  66. Network badoption.Listable[string] `json:"network,omitempty"`
  67. AuthUser badoption.Listable[string] `json:"auth_user,omitempty"`
  68. Protocol badoption.Listable[string] `json:"protocol,omitempty"`
  69. Domain badoption.Listable[string] `json:"domain,omitempty"`
  70. DomainSuffix badoption.Listable[string] `json:"domain_suffix,omitempty"`
  71. DomainKeyword badoption.Listable[string] `json:"domain_keyword,omitempty"`
  72. DomainRegex badoption.Listable[string] `json:"domain_regex,omitempty"`
  73. Geosite badoption.Listable[string] `json:"geosite,omitempty"`
  74. SourceGeoIP badoption.Listable[string] `json:"source_geoip,omitempty"`
  75. GeoIP badoption.Listable[string] `json:"geoip,omitempty"`
  76. IPCIDR badoption.Listable[string] `json:"ip_cidr,omitempty"`
  77. IPIsPrivate bool `json:"ip_is_private,omitempty"`
  78. IPAcceptAny bool `json:"ip_accept_any,omitempty"`
  79. SourceIPCIDR badoption.Listable[string] `json:"source_ip_cidr,omitempty"`
  80. SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
  81. SourcePort badoption.Listable[uint16] `json:"source_port,omitempty"`
  82. SourcePortRange badoption.Listable[string] `json:"source_port_range,omitempty"`
  83. Port badoption.Listable[uint16] `json:"port,omitempty"`
  84. PortRange badoption.Listable[string] `json:"port_range,omitempty"`
  85. ProcessName badoption.Listable[string] `json:"process_name,omitempty"`
  86. ProcessPath badoption.Listable[string] `json:"process_path,omitempty"`
  87. ProcessPathRegex badoption.Listable[string] `json:"process_path_regex,omitempty"`
  88. PackageName badoption.Listable[string] `json:"package_name,omitempty"`
  89. User badoption.Listable[string] `json:"user,omitempty"`
  90. UserID badoption.Listable[int32] `json:"user_id,omitempty"`
  91. Outbound badoption.Listable[string] `json:"outbound,omitempty"`
  92. ClashMode string `json:"clash_mode,omitempty"`
  93. NetworkType badoption.Listable[InterfaceType] `json:"network_type,omitempty"`
  94. NetworkIsExpensive bool `json:"network_is_expensive,omitempty"`
  95. NetworkIsConstrained bool `json:"network_is_constrained,omitempty"`
  96. WIFISSID badoption.Listable[string] `json:"wifi_ssid,omitempty"`
  97. WIFIBSSID badoption.Listable[string] `json:"wifi_bssid,omitempty"`
  98. InterfaceAddress *badjson.TypedMap[string, badoption.Listable[*badoption.Prefixable]] `json:"interface_address,omitempty"`
  99. NetworkInterfaceAddress *badjson.TypedMap[InterfaceType, badoption.Listable[*badoption.Prefixable]] `json:"network_interface_address,omitempty"`
  100. DefaultInterfaceAddress badoption.Listable[*badoption.Prefixable] `json:"default_interface_address,omitempty"`
  101. RuleSet badoption.Listable[string] `json:"rule_set,omitempty"`
  102. RuleSetIPCIDRMatchSource bool `json:"rule_set_ip_cidr_match_source,omitempty"`
  103. RuleSetIPCIDRAcceptEmpty bool `json:"rule_set_ip_cidr_accept_empty,omitempty"`
  104. Invert bool `json:"invert,omitempty"`
  105. // Deprecated: renamed to rule_set_ip_cidr_match_source
  106. Deprecated_RulesetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
  107. }
  108. type DefaultDNSRule struct {
  109. RawDefaultDNSRule
  110. DNSRuleAction
  111. }
  112. func (r DefaultDNSRule) MarshalJSON() ([]byte, error) {
  113. return badjson.MarshallObjects(r.RawDefaultDNSRule, r.DNSRuleAction)
  114. }
  115. func (r *DefaultDNSRule) UnmarshalJSONContext(ctx context.Context, data []byte) error {
  116. err := json.UnmarshalContext(ctx, data, &r.RawDefaultDNSRule)
  117. if err != nil {
  118. return err
  119. }
  120. return badjson.UnmarshallExcludedContext(ctx, data, &r.RawDefaultDNSRule, &r.DNSRuleAction)
  121. }
  122. func (r DefaultDNSRule) IsValid() bool {
  123. var defaultValue DefaultDNSRule
  124. defaultValue.Invert = r.Invert
  125. return !reflect.DeepEqual(r, defaultValue)
  126. }
  127. type RawLogicalDNSRule struct {
  128. Mode string `json:"mode"`
  129. Rules []DNSRule `json:"rules,omitempty"`
  130. Invert bool `json:"invert,omitempty"`
  131. }
  132. type LogicalDNSRule struct {
  133. RawLogicalDNSRule
  134. DNSRuleAction
  135. }
  136. func (r LogicalDNSRule) MarshalJSON() ([]byte, error) {
  137. return badjson.MarshallObjects(r.RawLogicalDNSRule, r.DNSRuleAction)
  138. }
  139. func (r *LogicalDNSRule) UnmarshalJSONContext(ctx context.Context, data []byte) error {
  140. err := json.Unmarshal(data, &r.RawLogicalDNSRule)
  141. if err != nil {
  142. return err
  143. }
  144. return badjson.UnmarshallExcludedContext(ctx, data, &r.RawLogicalDNSRule, &r.DNSRuleAction)
  145. }
  146. func (r *LogicalDNSRule) IsValid() bool {
  147. return len(r.Rules) > 0 && common.All(r.Rules, DNSRule.IsValid)
  148. }