rule_dns.go 5.6 KB

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