rule_dns.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package option
  2. import (
  3. "context"
  4. "reflect"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing/common"
  7. E "github.com/sagernet/sing/common/exceptions"
  8. "github.com/sagernet/sing/common/json"
  9. "github.com/sagernet/sing/common/json/badjson"
  10. "github.com/sagernet/sing/common/json/badoption"
  11. )
  12. type _DNSRule struct {
  13. Type string `json:"type,omitempty"`
  14. DefaultOptions DefaultDNSRule `json:"-"`
  15. LogicalOptions LogicalDNSRule `json:"-"`
  16. }
  17. type DNSRule _DNSRule
  18. func (r DNSRule) MarshalJSON() ([]byte, error) {
  19. var v any
  20. switch r.Type {
  21. case C.RuleTypeDefault:
  22. r.Type = ""
  23. v = r.DefaultOptions
  24. case C.RuleTypeLogical:
  25. v = r.LogicalOptions
  26. default:
  27. return nil, E.New("unknown rule type: " + r.Type)
  28. }
  29. return badjson.MarshallObjects((_DNSRule)(r), v)
  30. }
  31. func (r *DNSRule) UnmarshalJSONContext(ctx context.Context, bytes []byte) error {
  32. err := json.UnmarshalContext(ctx, bytes, (*_DNSRule)(r))
  33. if err != nil {
  34. return err
  35. }
  36. var v any
  37. switch r.Type {
  38. case "", C.RuleTypeDefault:
  39. r.Type = C.RuleTypeDefault
  40. v = &r.DefaultOptions
  41. case C.RuleTypeLogical:
  42. v = &r.LogicalOptions
  43. default:
  44. return E.New("unknown rule type: " + r.Type)
  45. }
  46. err = badjson.UnmarshallExcludedContext(ctx, bytes, (*_DNSRule)(r), v)
  47. if err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. func (r DNSRule) IsValid() bool {
  53. switch r.Type {
  54. case C.RuleTypeDefault:
  55. return r.DefaultOptions.IsValid()
  56. case C.RuleTypeLogical:
  57. return r.LogicalOptions.IsValid()
  58. default:
  59. panic("unknown DNS rule type: " + r.Type)
  60. }
  61. }
  62. type RawDefaultDNSRule struct {
  63. Inbound badoption.Listable[string] `json:"inbound,omitempty"`
  64. IPVersion int `json:"ip_version,omitempty"`
  65. QueryType badoption.Listable[DNSQueryType] `json:"query_type,omitempty"`
  66. Network badoption.Listable[string] `json:"network,omitempty"`
  67. AuthUser badoption.Listable[string] `json:"auth_user,omitempty"`
  68. Protocol badoption.Listable[string] `json:"protocol,omitempty"`
  69. Domain badoption.Listable[string] `json:"domain,omitempty"`
  70. DomainSuffix badoption.Listable[string] `json:"domain_suffix,omitempty"`
  71. DomainKeyword badoption.Listable[string] `json:"domain_keyword,omitempty"`
  72. DomainRegex badoption.Listable[string] `json:"domain_regex,omitempty"`
  73. SourceIPCIDR badoption.Listable[string] `json:"source_ip_cidr,omitempty"`
  74. SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
  75. SourcePort badoption.Listable[uint16] `json:"source_port,omitempty"`
  76. SourcePortRange badoption.Listable[string] `json:"source_port_range,omitempty"`
  77. Port badoption.Listable[uint16] `json:"port,omitempty"`
  78. PortRange badoption.Listable[string] `json:"port_range,omitempty"`
  79. ProcessName badoption.Listable[string] `json:"process_name,omitempty"`
  80. ProcessPath badoption.Listable[string] `json:"process_path,omitempty"`
  81. ProcessPathRegex badoption.Listable[string] `json:"process_path_regex,omitempty"`
  82. PackageName badoption.Listable[string] `json:"package_name,omitempty"`
  83. PackageNameRegex badoption.Listable[string] `json:"package_name_regex,omitempty"`
  84. User badoption.Listable[string] `json:"user,omitempty"`
  85. UserID badoption.Listable[int32] `json:"user_id,omitempty"`
  86. Outbound badoption.Listable[string] `json:"outbound,omitempty"`
  87. ClashMode string `json:"clash_mode,omitempty"`
  88. NetworkType badoption.Listable[InterfaceType] `json:"network_type,omitempty"`
  89. NetworkIsExpensive bool `json:"network_is_expensive,omitempty"`
  90. NetworkIsConstrained bool `json:"network_is_constrained,omitempty"`
  91. WIFISSID badoption.Listable[string] `json:"wifi_ssid,omitempty"`
  92. WIFIBSSID badoption.Listable[string] `json:"wifi_bssid,omitempty"`
  93. InterfaceAddress *badjson.TypedMap[string, badoption.Listable[*badoption.Prefixable]] `json:"interface_address,omitempty"`
  94. NetworkInterfaceAddress *badjson.TypedMap[InterfaceType, badoption.Listable[*badoption.Prefixable]] `json:"network_interface_address,omitempty"`
  95. DefaultInterfaceAddress badoption.Listable[*badoption.Prefixable] `json:"default_interface_address,omitempty"`
  96. SourceMACAddress badoption.Listable[string] `json:"source_mac_address,omitempty"`
  97. SourceHostname badoption.Listable[string] `json:"source_hostname,omitempty"`
  98. RuleSet badoption.Listable[string] `json:"rule_set,omitempty"`
  99. RuleSetIPCIDRMatchSource bool `json:"rule_set_ip_cidr_match_source,omitempty"`
  100. MatchResponse bool `json:"match_response,omitempty"`
  101. IPCIDR badoption.Listable[string] `json:"ip_cidr,omitempty"`
  102. IPIsPrivate bool `json:"ip_is_private,omitempty"`
  103. IPAcceptAny bool `json:"ip_accept_any,omitempty"`
  104. ResponseRcode *DNSRCode `json:"response_rcode,omitempty"`
  105. ResponseAnswer badoption.Listable[DNSRecordOptions] `json:"response_answer,omitempty"`
  106. ResponseNs badoption.Listable[DNSRecordOptions] `json:"response_ns,omitempty"`
  107. ResponseExtra badoption.Listable[DNSRecordOptions] `json:"response_extra,omitempty"`
  108. Invert bool `json:"invert,omitempty"`
  109. // Deprecated: removed in sing-box 1.12.0
  110. Geosite badoption.Listable[string] `json:"geosite,omitempty"`
  111. SourceGeoIP badoption.Listable[string] `json:"source_geoip,omitempty"`
  112. GeoIP badoption.Listable[string] `json:"geoip,omitempty"`
  113. // Deprecated: removed in sing-box 1.11.0
  114. RuleSetIPCIDRAcceptEmpty bool `json:"rule_set_ip_cidr_accept_empty,omitempty"`
  115. // Deprecated: renamed to rule_set_ip_cidr_match_source
  116. Deprecated_RulesetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
  117. }
  118. type DefaultDNSRule struct {
  119. RawDefaultDNSRule
  120. DNSRuleAction
  121. }
  122. func (r DefaultDNSRule) MarshalJSON() ([]byte, error) {
  123. return badjson.MarshallObjects(r.RawDefaultDNSRule, r.DNSRuleAction)
  124. }
  125. func (r *DefaultDNSRule) UnmarshalJSONContext(ctx context.Context, data []byte) error {
  126. rawAction, routeOptions, err := inspectDNSRuleAction(ctx, data)
  127. if err != nil {
  128. return err
  129. }
  130. err = rejectNestedDNSRuleAction(ctx, data)
  131. if err != nil {
  132. return err
  133. }
  134. depth := nestedRuleDepth(ctx)
  135. err = json.UnmarshalContext(ctx, data, &r.RawDefaultDNSRule)
  136. if err != nil {
  137. return err
  138. }
  139. err = badjson.UnmarshallExcludedContext(ctx, data, &r.RawDefaultDNSRule, &r.DNSRuleAction)
  140. if err != nil {
  141. return err
  142. }
  143. if depth > 0 && rawAction == "" && routeOptions == (DNSRouteActionOptions{}) {
  144. r.DNSRuleAction = DNSRuleAction{}
  145. }
  146. return nil
  147. }
  148. func (r DefaultDNSRule) IsValid() bool {
  149. var defaultValue DefaultDNSRule
  150. defaultValue.Invert = r.Invert
  151. return !reflect.DeepEqual(r, defaultValue)
  152. }
  153. type RawLogicalDNSRule struct {
  154. Mode string `json:"mode"`
  155. Rules []DNSRule `json:"rules,omitempty"`
  156. Invert bool `json:"invert,omitempty"`
  157. }
  158. type LogicalDNSRule struct {
  159. RawLogicalDNSRule
  160. DNSRuleAction
  161. }
  162. func (r LogicalDNSRule) MarshalJSON() ([]byte, error) {
  163. return badjson.MarshallObjects(r.RawLogicalDNSRule, r.DNSRuleAction)
  164. }
  165. func (r *LogicalDNSRule) UnmarshalJSONContext(ctx context.Context, data []byte) error {
  166. rawAction, routeOptions, err := inspectDNSRuleAction(ctx, data)
  167. if err != nil {
  168. return err
  169. }
  170. err = rejectNestedDNSRuleAction(ctx, data)
  171. if err != nil {
  172. return err
  173. }
  174. depth := nestedRuleDepth(ctx)
  175. err = json.UnmarshalContext(nestedRuleChildContext(ctx), data, &r.RawLogicalDNSRule)
  176. if err != nil {
  177. return err
  178. }
  179. err = badjson.UnmarshallExcludedContext(ctx, data, &r.RawLogicalDNSRule, &r.DNSRuleAction)
  180. if err != nil {
  181. return err
  182. }
  183. if depth > 0 && rawAction == "" && routeOptions == (DNSRouteActionOptions{}) {
  184. r.DNSRuleAction = DNSRuleAction{}
  185. }
  186. return nil
  187. }
  188. func (r *LogicalDNSRule) IsValid() bool {
  189. return len(r.Rules) > 0 && common.All(r.Rules, DNSRule.IsValid)
  190. }