dns.go 4.1 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. Detour string `json:"detour,omitempty"`
  27. }
  28. type _DNSRule struct {
  29. Type string `json:"type,omitempty"`
  30. DefaultOptions DefaultDNSRule `json:"-"`
  31. LogicalOptions LogicalDNSRule `json:"-"`
  32. }
  33. type DNSRule _DNSRule
  34. func (r DNSRule) MarshalJSON() ([]byte, error) {
  35. var v any
  36. switch r.Type {
  37. case C.RuleTypeDefault:
  38. r.Type = ""
  39. v = r.DefaultOptions
  40. case C.RuleTypeLogical:
  41. v = r.LogicalOptions
  42. default:
  43. return nil, E.New("unknown rule type: " + r.Type)
  44. }
  45. return MarshallObjects((_DNSRule)(r), v)
  46. }
  47. func (r *DNSRule) UnmarshalJSON(bytes []byte) error {
  48. err := json.Unmarshal(bytes, (*_DNSRule)(r))
  49. if err != nil {
  50. return err
  51. }
  52. if r.Type == "" {
  53. r.Type = C.RuleTypeDefault
  54. }
  55. var v any
  56. switch r.Type {
  57. case C.RuleTypeDefault:
  58. v = &r.DefaultOptions
  59. case C.RuleTypeLogical:
  60. v = &r.LogicalOptions
  61. default:
  62. return E.New("unknown rule type: " + r.Type)
  63. }
  64. err = UnmarshallExcluded(bytes, (*_DNSRule)(r), v)
  65. if err != nil {
  66. return E.Cause(err, "dns route rule")
  67. }
  68. return nil
  69. }
  70. type DefaultDNSRule struct {
  71. Inbound Listable[string] `json:"inbound,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. }