dns.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package option
  2. import (
  3. C "github.com/sagernet/sing-box/constant"
  4. "github.com/sagernet/sing/common"
  5. E "github.com/sagernet/sing/common/exceptions"
  6. "github.com/goccy/go-json"
  7. )
  8. type DNSOptions struct {
  9. Servers []DNSServerOptions `json:"servers,omitempty"`
  10. Rules []DNSRule `json:"rules,omitempty"`
  11. Final string `json:"final,omitempty"`
  12. DNSClientOptions
  13. }
  14. func (o DNSOptions) Equals(other DNSOptions) bool {
  15. return common.ComparableSliceEquals(o.Servers, other.Servers) &&
  16. common.SliceEquals(o.Rules, other.Rules) &&
  17. o.Final == other.Final &&
  18. o.DNSClientOptions == other.DNSClientOptions
  19. }
  20. type DNSClientOptions struct {
  21. Strategy DomainStrategy `json:"strategy,omitempty"`
  22. DisableCache bool `json:"disable_cache,omitempty"`
  23. DisableExpire bool `json:"disable_expire,omitempty"`
  24. }
  25. type DNSServerOptions struct {
  26. Tag string `json:"tag,omitempty"`
  27. Address string `json:"address"`
  28. AddressResolver string `json:"address_resolver,omitempty"`
  29. AddressStrategy DomainStrategy `json:"address_strategy,omitempty"`
  30. AddressFallbackDelay Duration `json:"address_fallback_delay,omitempty"`
  31. Detour string `json:"detour,omitempty"`
  32. }
  33. type _DNSRule struct {
  34. Type string `json:"type,omitempty"`
  35. DefaultOptions DefaultDNSRule `json:"-"`
  36. LogicalOptions LogicalDNSRule `json:"-"`
  37. }
  38. type DNSRule _DNSRule
  39. func (r DNSRule) Equals(other DNSRule) bool {
  40. return r.Type == other.Type &&
  41. r.DefaultOptions.Equals(other.DefaultOptions) &&
  42. r.LogicalOptions.Equals(other.LogicalOptions)
  43. }
  44. func (r DNSRule) MarshalJSON() ([]byte, error) {
  45. var v any
  46. switch r.Type {
  47. case C.RuleTypeDefault:
  48. v = r.DefaultOptions
  49. case C.RuleTypeLogical:
  50. v = r.LogicalOptions
  51. default:
  52. return nil, E.New("unknown rule type: " + r.Type)
  53. }
  54. return MarshallObjects((_DNSRule)(r), v)
  55. }
  56. func (r *DNSRule) UnmarshalJSON(bytes []byte) error {
  57. err := json.Unmarshal(bytes, (*_DNSRule)(r))
  58. if err != nil {
  59. return err
  60. }
  61. if r.Type == "" {
  62. r.Type = C.RuleTypeDefault
  63. }
  64. var v any
  65. switch r.Type {
  66. case C.RuleTypeDefault:
  67. v = &r.DefaultOptions
  68. case C.RuleTypeLogical:
  69. v = &r.LogicalOptions
  70. default:
  71. return E.New("unknown rule type: " + r.Type)
  72. }
  73. err = UnmarshallExcluded(bytes, (*_DNSRule)(r), v)
  74. if err != nil {
  75. return E.Cause(err, "dns route rule")
  76. }
  77. return nil
  78. }
  79. type DefaultDNSRule struct {
  80. Inbound Listable[string] `json:"inbound,omitempty"`
  81. Network string `json:"network,omitempty"`
  82. Protocol Listable[string] `json:"protocol,omitempty"`
  83. Domain Listable[string] `json:"domain,omitempty"`
  84. DomainSuffix Listable[string] `json:"domain_suffix,omitempty"`
  85. DomainKeyword Listable[string] `json:"domain_keyword,omitempty"`
  86. DomainRegex Listable[string] `json:"domain_regex,omitempty"`
  87. Geosite Listable[string] `json:"geosite,omitempty"`
  88. SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
  89. SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
  90. SourcePort Listable[uint16] `json:"source_port,omitempty"`
  91. Port Listable[uint16] `json:"port,omitempty"`
  92. Outbound Listable[string] `json:"outbound,omitempty"`
  93. Server string `json:"server,omitempty"`
  94. }
  95. func (r DefaultDNSRule) IsValid() bool {
  96. var defaultValue DefaultDNSRule
  97. defaultValue.Server = r.Server
  98. return !r.Equals(defaultValue)
  99. }
  100. func (r DefaultDNSRule) Equals(other DefaultDNSRule) bool {
  101. return common.ComparableSliceEquals(r.Inbound, other.Inbound) &&
  102. r.Network == other.Network &&
  103. common.ComparableSliceEquals(r.Protocol, other.Protocol) &&
  104. common.ComparableSliceEquals(r.Domain, other.Domain) &&
  105. common.ComparableSliceEquals(r.DomainSuffix, other.DomainSuffix) &&
  106. common.ComparableSliceEquals(r.DomainKeyword, other.DomainKeyword) &&
  107. common.ComparableSliceEquals(r.DomainRegex, other.DomainRegex) &&
  108. common.ComparableSliceEquals(r.Geosite, other.Geosite) &&
  109. common.ComparableSliceEquals(r.SourceGeoIP, other.SourceGeoIP) &&
  110. common.ComparableSliceEquals(r.SourceIPCIDR, other.SourceIPCIDR) &&
  111. common.ComparableSliceEquals(r.SourcePort, other.SourcePort) &&
  112. common.ComparableSliceEquals(r.Port, other.Port) &&
  113. common.ComparableSliceEquals(r.Outbound, other.Outbound) &&
  114. r.Server == other.Server
  115. }
  116. type LogicalDNSRule struct {
  117. Mode string `json:"mode"`
  118. Rules []DefaultDNSRule `json:"rules,omitempty"`
  119. Server string `json:"server,omitempty"`
  120. }
  121. func (r LogicalDNSRule) IsValid() bool {
  122. return len(r.Rules) > 0 && common.All(r.Rules, DefaultDNSRule.IsValid)
  123. }
  124. func (r LogicalDNSRule) Equals(other LogicalDNSRule) bool {
  125. return r.Mode == other.Mode &&
  126. common.SliceEquals(r.Rules, other.Rules) &&
  127. r.Server == other.Server
  128. }