dns.go 4.2 KB

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