rule_dns.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. SourceIPCIDR badoption.Listable[string] `json:"source_ip_cidr,omitempty"`
  79. SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
  80. SourcePort badoption.Listable[uint16] `json:"source_port,omitempty"`
  81. SourcePortRange badoption.Listable[string] `json:"source_port_range,omitempty"`
  82. Port badoption.Listable[uint16] `json:"port,omitempty"`
  83. PortRange badoption.Listable[string] `json:"port_range,omitempty"`
  84. ProcessName badoption.Listable[string] `json:"process_name,omitempty"`
  85. ProcessPath badoption.Listable[string] `json:"process_path,omitempty"`
  86. ProcessPathRegex badoption.Listable[string] `json:"process_path_regex,omitempty"`
  87. PackageName badoption.Listable[string] `json:"package_name,omitempty"`
  88. User badoption.Listable[string] `json:"user,omitempty"`
  89. UserID badoption.Listable[int32] `json:"user_id,omitempty"`
  90. Outbound badoption.Listable[string] `json:"outbound,omitempty"`
  91. ClashMode string `json:"clash_mode,omitempty"`
  92. WIFISSID badoption.Listable[string] `json:"wifi_ssid,omitempty"`
  93. WIFIBSSID badoption.Listable[string] `json:"wifi_bssid,omitempty"`
  94. RuleSet badoption.Listable[string] `json:"rule_set,omitempty"`
  95. RuleSetIPCIDRMatchSource bool `json:"rule_set_ip_cidr_match_source,omitempty"`
  96. RuleSetIPCIDRAcceptEmpty bool `json:"rule_set_ip_cidr_accept_empty,omitempty"`
  97. Invert bool `json:"invert,omitempty"`
  98. // Deprecated: renamed to rule_set_ip_cidr_match_source
  99. Deprecated_RulesetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
  100. }
  101. type DefaultDNSRule struct {
  102. RawDefaultDNSRule
  103. DNSRuleAction
  104. }
  105. func (r DefaultDNSRule) MarshalJSON() ([]byte, error) {
  106. return badjson.MarshallObjects(r.RawDefaultDNSRule, r.DNSRuleAction)
  107. }
  108. func (r *DefaultDNSRule) UnmarshalJSONContext(ctx context.Context, data []byte) error {
  109. err := json.UnmarshalContext(ctx, data, &r.RawDefaultDNSRule)
  110. if err != nil {
  111. return err
  112. }
  113. return badjson.UnmarshallExcludedContext(ctx, data, &r.RawDefaultDNSRule, &r.DNSRuleAction)
  114. }
  115. func (r DefaultDNSRule) IsValid() bool {
  116. var defaultValue DefaultDNSRule
  117. defaultValue.Invert = r.Invert
  118. defaultValue.DNSRuleAction = r.DNSRuleAction
  119. return !reflect.DeepEqual(r, defaultValue)
  120. }
  121. type RawLogicalDNSRule struct {
  122. Mode string `json:"mode"`
  123. Rules []DNSRule `json:"rules,omitempty"`
  124. Invert bool `json:"invert,omitempty"`
  125. }
  126. type LogicalDNSRule struct {
  127. RawLogicalDNSRule
  128. DNSRuleAction
  129. }
  130. func (r LogicalDNSRule) MarshalJSON() ([]byte, error) {
  131. return badjson.MarshallObjects(r.RawLogicalDNSRule, r.DNSRuleAction)
  132. }
  133. func (r *LogicalDNSRule) UnmarshalJSONContext(ctx context.Context, data []byte) error {
  134. err := json.Unmarshal(data, &r.RawLogicalDNSRule)
  135. if err != nil {
  136. return err
  137. }
  138. return badjson.UnmarshallExcludedContext(ctx, data, &r.RawLogicalDNSRule, &r.DNSRuleAction)
  139. }
  140. func (r *LogicalDNSRule) IsValid() bool {
  141. return len(r.Rules) > 0 && common.All(r.Rules, DNSRule.IsValid)
  142. }