rule_dns.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package option
  2. import (
  3. "reflect"
  4. "github.com/sagernet/sing-box/common/json"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing/common"
  7. E "github.com/sagernet/sing/common/exceptions"
  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 E.Cause(err, "dns route rule")
  46. }
  47. return nil
  48. }
  49. type DefaultDNSRule struct {
  50. Inbound Listable[string] `json:"inbound,omitempty"`
  51. IPVersion int `json:"ip_version,omitempty"`
  52. QueryType Listable[DNSQueryType] `json:"query_type,omitempty"`
  53. Network Listable[string] `json:"network,omitempty"`
  54. AuthUser Listable[string] `json:"auth_user,omitempty"`
  55. Protocol Listable[string] `json:"protocol,omitempty"`
  56. Domain Listable[string] `json:"domain,omitempty"`
  57. DomainSuffix Listable[string] `json:"domain_suffix,omitempty"`
  58. DomainKeyword Listable[string] `json:"domain_keyword,omitempty"`
  59. DomainRegex Listable[string] `json:"domain_regex,omitempty"`
  60. Geosite Listable[string] `json:"geosite,omitempty"`
  61. SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
  62. SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
  63. SourcePort Listable[uint16] `json:"source_port,omitempty"`
  64. SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
  65. Port Listable[uint16] `json:"port,omitempty"`
  66. PortRange Listable[string] `json:"port_range,omitempty"`
  67. ProcessName Listable[string] `json:"process_name,omitempty"`
  68. ProcessPath Listable[string] `json:"process_path,omitempty"`
  69. PackageName Listable[string] `json:"package_name,omitempty"`
  70. User Listable[string] `json:"user,omitempty"`
  71. UserID Listable[int32] `json:"user_id,omitempty"`
  72. Outbound Listable[string] `json:"outbound,omitempty"`
  73. ClashMode string `json:"clash_mode,omitempty"`
  74. Invert bool `json:"invert,omitempty"`
  75. Server string `json:"server,omitempty"`
  76. DisableCache bool `json:"disable_cache,omitempty"`
  77. }
  78. func (r DefaultDNSRule) IsValid() bool {
  79. var defaultValue DefaultDNSRule
  80. defaultValue.Invert = r.Invert
  81. defaultValue.Server = r.Server
  82. defaultValue.DisableCache = r.DisableCache
  83. return !reflect.DeepEqual(r, defaultValue)
  84. }
  85. type LogicalDNSRule struct {
  86. Mode string `json:"mode"`
  87. Rules []DefaultDNSRule `json:"rules,omitempty"`
  88. Invert bool `json:"invert,omitempty"`
  89. Server string `json:"server,omitempty"`
  90. DisableCache bool `json:"disable_cache,omitempty"`
  91. }
  92. func (r LogicalDNSRule) IsValid() bool {
  93. return len(r.Rules) > 0 && common.All(r.Rules, DefaultDNSRule.IsValid)
  94. }