rule_dns.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. GeoIP Listable[string] `json:"geoip,omitempty"`
  73. IPCIDR Listable[string] `json:"ip_cidr,omitempty"`
  74. IPIsPrivate bool `json:"ip_is_private,omitempty"`
  75. SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
  76. SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
  77. SourcePort Listable[uint16] `json:"source_port,omitempty"`
  78. SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
  79. Port Listable[uint16] `json:"port,omitempty"`
  80. PortRange Listable[string] `json:"port_range,omitempty"`
  81. ProcessName Listable[string] `json:"process_name,omitempty"`
  82. ProcessPath Listable[string] `json:"process_path,omitempty"`
  83. PackageName Listable[string] `json:"package_name,omitempty"`
  84. User Listable[string] `json:"user,omitempty"`
  85. UserID Listable[int32] `json:"user_id,omitempty"`
  86. Outbound Listable[string] `json:"outbound,omitempty"`
  87. ClashMode string `json:"clash_mode,omitempty"`
  88. WIFISSID Listable[string] `json:"wifi_ssid,omitempty"`
  89. WIFIBSSID Listable[string] `json:"wifi_bssid,omitempty"`
  90. RuleSet Listable[string] `json:"rule_set,omitempty"`
  91. Invert bool `json:"invert,omitempty"`
  92. Server string `json:"server,omitempty"`
  93. DisableCache bool `json:"disable_cache,omitempty"`
  94. RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"`
  95. ClientSubnet *ListenAddress `json:"client_subnet,omitempty"`
  96. }
  97. func (r DefaultDNSRule) IsValid() bool {
  98. var defaultValue DefaultDNSRule
  99. defaultValue.Invert = r.Invert
  100. defaultValue.Server = r.Server
  101. defaultValue.DisableCache = r.DisableCache
  102. defaultValue.RewriteTTL = r.RewriteTTL
  103. defaultValue.ClientSubnet = r.ClientSubnet
  104. return !reflect.DeepEqual(r, defaultValue)
  105. }
  106. type LogicalDNSRule struct {
  107. Mode string `json:"mode"`
  108. Rules []DNSRule `json:"rules,omitempty"`
  109. Invert bool `json:"invert,omitempty"`
  110. Server string `json:"server,omitempty"`
  111. DisableCache bool `json:"disable_cache,omitempty"`
  112. RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"`
  113. ClientSubnet *ListenAddress `json:"client_subnet,omitempty"`
  114. }
  115. func (r LogicalDNSRule) IsValid() bool {
  116. return len(r.Rules) > 0 && common.All(r.Rules, DNSRule.IsValid)
  117. }