rule_dns.go 4.3 KB

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