dns.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. Detour string `json:"detour,omitempty"`
  31. }
  32. type _DNSRule struct {
  33. Type string `json:"type,omitempty"`
  34. DefaultOptions DefaultDNSRule `json:"-"`
  35. LogicalOptions LogicalDNSRule `json:"-"`
  36. }
  37. type DNSRule _DNSRule
  38. func (r DNSRule) Equals(other DNSRule) bool {
  39. return r.Type == other.Type &&
  40. r.DefaultOptions.Equals(other.DefaultOptions) &&
  41. r.LogicalOptions.Equals(other.LogicalOptions)
  42. }
  43. func (r DNSRule) MarshalJSON() ([]byte, error) {
  44. var v any
  45. switch r.Type {
  46. case C.RuleTypeDefault:
  47. v = r.DefaultOptions
  48. case C.RuleTypeLogical:
  49. v = r.LogicalOptions
  50. default:
  51. return nil, E.New("unknown rule type: " + r.Type)
  52. }
  53. return MarshallObjects((_DNSRule)(r), v)
  54. }
  55. func (r *DNSRule) UnmarshalJSON(bytes []byte) error {
  56. err := json.Unmarshal(bytes, (*_DNSRule)(r))
  57. if err != nil {
  58. return err
  59. }
  60. if r.Type == "" {
  61. r.Type = C.RuleTypeDefault
  62. }
  63. var v any
  64. switch r.Type {
  65. case C.RuleTypeDefault:
  66. v = &r.DefaultOptions
  67. case C.RuleTypeLogical:
  68. v = &r.LogicalOptions
  69. default:
  70. return E.New("unknown rule type: " + r.Type)
  71. }
  72. err = UnmarshallExcluded(bytes, (*_DNSRule)(r), v)
  73. if err != nil {
  74. return E.Cause(err, "dns route rule")
  75. }
  76. return nil
  77. }
  78. type DefaultDNSRule struct {
  79. Inbound Listable[string] `json:"inbound,omitempty"`
  80. Network string `json:"network,omitempty"`
  81. Protocol Listable[string] `json:"protocol,omitempty"`
  82. Domain Listable[string] `json:"domain,omitempty"`
  83. DomainSuffix Listable[string] `json:"domain_suffix,omitempty"`
  84. DomainKeyword Listable[string] `json:"domain_keyword,omitempty"`
  85. DomainRegex Listable[string] `json:"domain_regex,omitempty"`
  86. Geosite Listable[string] `json:"geosite,omitempty"`
  87. SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
  88. SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
  89. SourcePort Listable[uint16] `json:"source_port,omitempty"`
  90. Port Listable[uint16] `json:"port,omitempty"`
  91. Outbound Listable[string] `json:"outbound,omitempty"`
  92. Server string `json:"server,omitempty"`
  93. }
  94. func (r DefaultDNSRule) IsValid() bool {
  95. var defaultValue DefaultDNSRule
  96. defaultValue.Server = r.Server
  97. return !r.Equals(defaultValue)
  98. }
  99. func (r DefaultDNSRule) Equals(other DefaultDNSRule) bool {
  100. return common.ComparableSliceEquals(r.Inbound, other.Inbound) &&
  101. r.Network == other.Network &&
  102. common.ComparableSliceEquals(r.Protocol, other.Protocol) &&
  103. common.ComparableSliceEquals(r.Domain, other.Domain) &&
  104. common.ComparableSliceEquals(r.DomainSuffix, other.DomainSuffix) &&
  105. common.ComparableSliceEquals(r.DomainKeyword, other.DomainKeyword) &&
  106. common.ComparableSliceEquals(r.DomainRegex, other.DomainRegex) &&
  107. common.ComparableSliceEquals(r.Geosite, other.Geosite) &&
  108. common.ComparableSliceEquals(r.SourceGeoIP, other.SourceGeoIP) &&
  109. common.ComparableSliceEquals(r.SourceIPCIDR, other.SourceIPCIDR) &&
  110. common.ComparableSliceEquals(r.SourcePort, other.SourcePort) &&
  111. common.ComparableSliceEquals(r.Port, other.Port) &&
  112. common.ComparableSliceEquals(r.Outbound, other.Outbound) &&
  113. r.Server == other.Server
  114. }
  115. type LogicalDNSRule struct {
  116. Mode string `json:"mode"`
  117. Rules []DefaultDNSRule `json:"rules,omitempty"`
  118. Server string `json:"server,omitempty"`
  119. }
  120. func (r LogicalDNSRule) IsValid() bool {
  121. return len(r.Rules) > 0 && common.All(r.Rules, DefaultDNSRule.IsValid)
  122. }
  123. func (r LogicalDNSRule) Equals(other LogicalDNSRule) bool {
  124. return r.Mode == other.Mode &&
  125. common.SliceEquals(r.Rules, other.Rules) &&
  126. r.Server == other.Server
  127. }