dns.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. var v any
  53. switch r.Type {
  54. case "", C.RuleTypeDefault:
  55. r.Type = C.RuleTypeDefault
  56. v = &r.DefaultOptions
  57. case C.RuleTypeLogical:
  58. v = &r.LogicalOptions
  59. default:
  60. return E.New("unknown rule type: " + r.Type)
  61. }
  62. err = UnmarshallExcluded(bytes, (*_DNSRule)(r), v)
  63. if err != nil {
  64. return E.Cause(err, "dns route rule")
  65. }
  66. return nil
  67. }
  68. type DefaultDNSRule struct {
  69. Inbound Listable[string] `json:"inbound,omitempty"`
  70. Network string `json:"network,omitempty"`
  71. AuthUser Listable[string] `json:"auth_user,omitempty"`
  72. Protocol Listable[string] `json:"protocol,omitempty"`
  73. Domain Listable[string] `json:"domain,omitempty"`
  74. DomainSuffix Listable[string] `json:"domain_suffix,omitempty"`
  75. DomainKeyword Listable[string] `json:"domain_keyword,omitempty"`
  76. DomainRegex Listable[string] `json:"domain_regex,omitempty"`
  77. Geosite Listable[string] `json:"geosite,omitempty"`
  78. SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
  79. SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
  80. SourcePort Listable[uint16] `json:"source_port,omitempty"`
  81. SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
  82. Port Listable[uint16] `json:"port,omitempty"`
  83. PortRange Listable[string] `json:"port_range,omitempty"`
  84. ProcessName Listable[string] `json:"process_name,omitempty"`
  85. PackageName Listable[string] `json:"package_name,omitempty"`
  86. User Listable[string] `json:"user,omitempty"`
  87. UserID Listable[int32] `json:"user_id,omitempty"`
  88. Outbound Listable[string] `json:"outbound,omitempty"`
  89. Invert bool `json:"invert,omitempty"`
  90. Server string `json:"server,omitempty"`
  91. DisableCache bool `json:"disable_cache,omitempty"`
  92. }
  93. func (r DefaultDNSRule) IsValid() bool {
  94. var defaultValue DefaultDNSRule
  95. defaultValue.Invert = r.Invert
  96. defaultValue.Server = r.Server
  97. defaultValue.DisableCache = r.DisableCache
  98. return !reflect.DeepEqual(r, defaultValue)
  99. }
  100. type LogicalDNSRule struct {
  101. Mode string `json:"mode"`
  102. Rules []DefaultDNSRule `json:"rules,omitempty"`
  103. Invert bool `json:"invert,omitempty"`
  104. Server string `json:"server,omitempty"`
  105. DisableCache bool `json:"disable_cache,omitempty"`
  106. }
  107. func (r LogicalDNSRule) IsValid() bool {
  108. return len(r.Rules) > 0 && common.All(r.Rules, DefaultDNSRule.IsValid)
  109. }