rule_action.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. package rule
  2. import (
  3. "context"
  4. "errors"
  5. "net/netip"
  6. "strings"
  7. "sync"
  8. "time"
  9. "github.com/sagernet/sing-box/adapter"
  10. "github.com/sagernet/sing-box/common/dialer"
  11. "github.com/sagernet/sing-box/common/sniff"
  12. C "github.com/sagernet/sing-box/constant"
  13. "github.com/sagernet/sing-box/option"
  14. "github.com/sagernet/sing-tun"
  15. "github.com/sagernet/sing/common"
  16. E "github.com/sagernet/sing/common/exceptions"
  17. F "github.com/sagernet/sing/common/format"
  18. "github.com/sagernet/sing/common/logger"
  19. M "github.com/sagernet/sing/common/metadata"
  20. N "github.com/sagernet/sing/common/network"
  21. "github.com/miekg/dns"
  22. )
  23. func NewRuleAction(ctx context.Context, logger logger.ContextLogger, action option.RuleAction) (adapter.RuleAction, error) {
  24. switch action.Action {
  25. case "":
  26. return nil, nil
  27. case C.RuleActionTypeRoute:
  28. return &RuleActionRoute{
  29. Outbound: action.RouteOptions.Outbound,
  30. RuleActionRouteOptions: RuleActionRouteOptions{
  31. OverrideAddress: M.ParseSocksaddrHostPort(action.RouteOptions.OverrideAddress, 0),
  32. OverridePort: action.RouteOptions.OverridePort,
  33. NetworkStrategy: (*C.NetworkStrategy)(action.RouteOptions.NetworkStrategy),
  34. FallbackDelay: time.Duration(action.RouteOptions.FallbackDelay),
  35. UDPDisableDomainUnmapping: action.RouteOptions.UDPDisableDomainUnmapping,
  36. UDPConnect: action.RouteOptions.UDPConnect,
  37. TLSFragment: action.RouteOptions.TLSFragment,
  38. TLSFragmentFallbackDelay: time.Duration(action.RouteOptions.TLSFragmentFallbackDelay),
  39. TLSRecordFragment: action.RouteOptions.TLSRecordFragment,
  40. },
  41. }, nil
  42. case C.RuleActionTypeRouteOptions:
  43. return &RuleActionRouteOptions{
  44. OverrideAddress: M.ParseSocksaddrHostPort(action.RouteOptionsOptions.OverrideAddress, 0),
  45. OverridePort: action.RouteOptionsOptions.OverridePort,
  46. NetworkStrategy: (*C.NetworkStrategy)(action.RouteOptionsOptions.NetworkStrategy),
  47. FallbackDelay: time.Duration(action.RouteOptionsOptions.FallbackDelay),
  48. UDPDisableDomainUnmapping: action.RouteOptionsOptions.UDPDisableDomainUnmapping,
  49. UDPConnect: action.RouteOptionsOptions.UDPConnect,
  50. UDPTimeout: time.Duration(action.RouteOptionsOptions.UDPTimeout),
  51. TLSFragment: action.RouteOptionsOptions.TLSFragment,
  52. TLSFragmentFallbackDelay: time.Duration(action.RouteOptionsOptions.TLSFragmentFallbackDelay),
  53. TLSRecordFragment: action.RouteOptionsOptions.TLSRecordFragment,
  54. }, nil
  55. case C.RuleActionTypeDirect:
  56. directDialer, err := dialer.New(ctx, option.DialerOptions(action.DirectOptions), false)
  57. if err != nil {
  58. return nil, err
  59. }
  60. var description string
  61. descriptions := action.DirectOptions.Descriptions()
  62. switch len(descriptions) {
  63. case 0:
  64. case 1:
  65. description = F.ToString("(", descriptions[0], ")")
  66. case 2:
  67. description = F.ToString("(", descriptions[0], ",", descriptions[1], ")")
  68. default:
  69. description = F.ToString("(", descriptions[0], ",", descriptions[1], ",...)")
  70. }
  71. return &RuleActionDirect{
  72. Dialer: directDialer,
  73. description: description,
  74. }, nil
  75. case C.RuleActionTypeReject:
  76. return &RuleActionReject{
  77. Method: action.RejectOptions.Method,
  78. NoDrop: action.RejectOptions.NoDrop,
  79. logger: logger,
  80. }, nil
  81. case C.RuleActionTypeHijackDNS:
  82. return &RuleActionHijackDNS{}, nil
  83. case C.RuleActionTypeSniff:
  84. sniffAction := &RuleActionSniff{
  85. SnifferNames: action.SniffOptions.Sniffer,
  86. Timeout: time.Duration(action.SniffOptions.Timeout),
  87. }
  88. return sniffAction, sniffAction.build()
  89. case C.RuleActionTypeResolve:
  90. return &RuleActionResolve{
  91. Server: action.ResolveOptions.Server,
  92. Strategy: C.DomainStrategy(action.ResolveOptions.Strategy),
  93. DisableCache: action.ResolveOptions.DisableCache,
  94. RewriteTTL: action.ResolveOptions.RewriteTTL,
  95. ClientSubnet: action.ResolveOptions.ClientSubnet.Build(netip.Prefix{}),
  96. }, nil
  97. default:
  98. panic(F.ToString("unknown rule action: ", action.Action))
  99. }
  100. }
  101. func NewDNSRuleAction(logger logger.ContextLogger, action option.DNSRuleAction) adapter.RuleAction {
  102. switch action.Action {
  103. case "":
  104. return nil
  105. case C.RuleActionTypeRoute:
  106. return &RuleActionDNSRoute{
  107. Server: action.RouteOptions.Server,
  108. RuleActionDNSRouteOptions: RuleActionDNSRouteOptions{
  109. Strategy: C.DomainStrategy(action.RouteOptions.Strategy),
  110. DisableCache: action.RouteOptions.DisableCache,
  111. RewriteTTL: action.RouteOptions.RewriteTTL,
  112. ClientSubnet: netip.Prefix(common.PtrValueOrDefault(action.RouteOptions.ClientSubnet)),
  113. },
  114. }
  115. case C.RuleActionTypeRouteOptions:
  116. return &RuleActionDNSRouteOptions{
  117. Strategy: C.DomainStrategy(action.RouteOptionsOptions.Strategy),
  118. DisableCache: action.RouteOptionsOptions.DisableCache,
  119. RewriteTTL: action.RouteOptionsOptions.RewriteTTL,
  120. ClientSubnet: netip.Prefix(common.PtrValueOrDefault(action.RouteOptionsOptions.ClientSubnet)),
  121. }
  122. case C.RuleActionTypeReject:
  123. return &RuleActionReject{
  124. Method: action.RejectOptions.Method,
  125. NoDrop: action.RejectOptions.NoDrop,
  126. logger: logger,
  127. }
  128. case C.RuleActionTypePredefined:
  129. return &RuleActionPredefined{
  130. Rcode: action.PredefinedOptions.Rcode.Build(),
  131. Answer: common.Map(action.PredefinedOptions.Answer, option.DNSRecordOptions.Build),
  132. Ns: common.Map(action.PredefinedOptions.Ns, option.DNSRecordOptions.Build),
  133. Extra: common.Map(action.PredefinedOptions.Extra, option.DNSRecordOptions.Build),
  134. }
  135. default:
  136. panic(F.ToString("unknown rule action: ", action.Action))
  137. }
  138. }
  139. type RuleActionRoute struct {
  140. Outbound string
  141. RuleActionRouteOptions
  142. }
  143. func (r *RuleActionRoute) Type() string {
  144. return C.RuleActionTypeRoute
  145. }
  146. func (r *RuleActionRoute) String() string {
  147. var descriptions []string
  148. descriptions = append(descriptions, r.Outbound)
  149. descriptions = append(descriptions, r.Descriptions()...)
  150. return F.ToString("route(", strings.Join(descriptions, ","), ")")
  151. }
  152. type RuleActionRouteOptions struct {
  153. OverrideAddress M.Socksaddr
  154. OverridePort uint16
  155. NetworkStrategy *C.NetworkStrategy
  156. NetworkType []C.InterfaceType
  157. FallbackNetworkType []C.InterfaceType
  158. FallbackDelay time.Duration
  159. UDPDisableDomainUnmapping bool
  160. UDPConnect bool
  161. UDPTimeout time.Duration
  162. TLSFragment bool
  163. TLSFragmentFallbackDelay time.Duration
  164. TLSRecordFragment bool
  165. }
  166. func (r *RuleActionRouteOptions) Type() string {
  167. return C.RuleActionTypeRouteOptions
  168. }
  169. func (r *RuleActionRouteOptions) String() string {
  170. return F.ToString("route-options(", strings.Join(r.Descriptions(), ","), ")")
  171. }
  172. func (r *RuleActionRouteOptions) Descriptions() []string {
  173. var descriptions []string
  174. if r.OverrideAddress.IsValid() {
  175. descriptions = append(descriptions, F.ToString("override-address=", r.OverrideAddress.AddrString()))
  176. }
  177. if r.OverridePort > 0 {
  178. descriptions = append(descriptions, F.ToString("override-port=", r.OverridePort))
  179. }
  180. if r.NetworkStrategy != nil {
  181. descriptions = append(descriptions, F.ToString("network-strategy=", r.NetworkStrategy))
  182. }
  183. if r.NetworkType != nil {
  184. descriptions = append(descriptions, F.ToString("network-type=", strings.Join(common.Map(r.NetworkType, C.InterfaceType.String), ",")))
  185. }
  186. if r.FallbackNetworkType != nil {
  187. descriptions = append(descriptions, F.ToString("fallback-network-type="+strings.Join(common.Map(r.NetworkType, C.InterfaceType.String), ",")))
  188. }
  189. if r.FallbackDelay > 0 {
  190. descriptions = append(descriptions, F.ToString("fallback-delay=", r.FallbackDelay.String()))
  191. }
  192. if r.UDPDisableDomainUnmapping {
  193. descriptions = append(descriptions, "udp-disable-domain-unmapping")
  194. }
  195. if r.UDPConnect {
  196. descriptions = append(descriptions, "udp-connect")
  197. }
  198. if r.UDPTimeout > 0 {
  199. descriptions = append(descriptions, "udp-timeout")
  200. }
  201. if r.TLSFragment {
  202. descriptions = append(descriptions, "tls-fragment")
  203. }
  204. if r.TLSFragmentFallbackDelay > 0 {
  205. descriptions = append(descriptions, F.ToString("tls-fragment-fallback-delay=", r.TLSFragmentFallbackDelay.String()))
  206. }
  207. if r.TLSRecordFragment {
  208. descriptions = append(descriptions, "tls-record-fragment")
  209. }
  210. return descriptions
  211. }
  212. type RuleActionDNSRoute struct {
  213. Server string
  214. RuleActionDNSRouteOptions
  215. }
  216. func (r *RuleActionDNSRoute) Type() string {
  217. return C.RuleActionTypeRoute
  218. }
  219. func (r *RuleActionDNSRoute) String() string {
  220. var descriptions []string
  221. descriptions = append(descriptions, r.Server)
  222. if r.DisableCache {
  223. descriptions = append(descriptions, "disable-cache")
  224. }
  225. if r.RewriteTTL != nil {
  226. descriptions = append(descriptions, F.ToString("rewrite-ttl=", *r.RewriteTTL))
  227. }
  228. if r.ClientSubnet.IsValid() {
  229. descriptions = append(descriptions, F.ToString("client-subnet=", r.ClientSubnet))
  230. }
  231. return F.ToString("route(", strings.Join(descriptions, ","), ")")
  232. }
  233. type RuleActionDNSRouteOptions struct {
  234. Strategy C.DomainStrategy
  235. DisableCache bool
  236. RewriteTTL *uint32
  237. ClientSubnet netip.Prefix
  238. }
  239. func (r *RuleActionDNSRouteOptions) Type() string {
  240. return C.RuleActionTypeRouteOptions
  241. }
  242. func (r *RuleActionDNSRouteOptions) String() string {
  243. var descriptions []string
  244. if r.DisableCache {
  245. descriptions = append(descriptions, "disable-cache")
  246. }
  247. if r.RewriteTTL != nil {
  248. descriptions = append(descriptions, F.ToString("rewrite-ttl=", *r.RewriteTTL))
  249. }
  250. if r.ClientSubnet.IsValid() {
  251. descriptions = append(descriptions, F.ToString("client-subnet=", r.ClientSubnet))
  252. }
  253. return F.ToString("route-options(", strings.Join(descriptions, ","), ")")
  254. }
  255. type RuleActionDirect struct {
  256. Dialer N.Dialer
  257. description string
  258. }
  259. func (r *RuleActionDirect) Type() string {
  260. return C.RuleActionTypeDirect
  261. }
  262. func (r *RuleActionDirect) String() string {
  263. return "direct" + r.description
  264. }
  265. type RejectedError struct {
  266. Cause error
  267. }
  268. func (r *RejectedError) Error() string {
  269. return "rejected"
  270. }
  271. func (r *RejectedError) Unwrap() error {
  272. return r.Cause
  273. }
  274. func IsRejected(err error) bool {
  275. var rejected *RejectedError
  276. return errors.As(err, &rejected)
  277. }
  278. type RuleActionReject struct {
  279. Method string
  280. NoDrop bool
  281. logger logger.ContextLogger
  282. dropAccess sync.Mutex
  283. dropCounter []time.Time
  284. }
  285. func (r *RuleActionReject) Type() string {
  286. return C.RuleActionTypeReject
  287. }
  288. func (r *RuleActionReject) String() string {
  289. if r.Method == C.RuleActionRejectMethodDefault {
  290. return "reject"
  291. }
  292. return F.ToString("reject(", r.Method, ")")
  293. }
  294. func (r *RuleActionReject) Error(ctx context.Context) error {
  295. var returnErr error
  296. switch r.Method {
  297. case C.RuleActionRejectMethodDefault:
  298. returnErr = &RejectedError{tun.ErrReset}
  299. case C.RuleActionRejectMethodDrop:
  300. return &RejectedError{tun.ErrDrop}
  301. case C.RuleActionRejectMethodReply:
  302. return nil
  303. default:
  304. panic(F.ToString("unknown reject method: ", r.Method))
  305. }
  306. if r.NoDrop {
  307. return returnErr
  308. }
  309. r.dropAccess.Lock()
  310. defer r.dropAccess.Unlock()
  311. timeNow := time.Now()
  312. r.dropCounter = common.Filter(r.dropCounter, func(t time.Time) bool {
  313. return timeNow.Sub(t) <= 30*time.Second
  314. })
  315. r.dropCounter = append(r.dropCounter, timeNow)
  316. if len(r.dropCounter) > 50 {
  317. if ctx != nil {
  318. r.logger.DebugContext(ctx, "dropped due to flooding")
  319. }
  320. return &RejectedError{tun.ErrDrop}
  321. }
  322. return returnErr
  323. }
  324. type RuleActionHijackDNS struct{}
  325. func (r *RuleActionHijackDNS) Type() string {
  326. return C.RuleActionTypeHijackDNS
  327. }
  328. func (r *RuleActionHijackDNS) String() string {
  329. return "hijack-dns"
  330. }
  331. type RuleActionSniff struct {
  332. SnifferNames []string
  333. StreamSniffers []sniff.StreamSniffer
  334. PacketSniffers []sniff.PacketSniffer
  335. Timeout time.Duration
  336. // Deprecated
  337. OverrideDestination bool
  338. }
  339. func (r *RuleActionSniff) Type() string {
  340. return C.RuleActionTypeSniff
  341. }
  342. func (r *RuleActionSniff) build() error {
  343. for _, name := range r.SnifferNames {
  344. switch name {
  345. case C.ProtocolTLS:
  346. r.StreamSniffers = append(r.StreamSniffers, sniff.TLSClientHello)
  347. case C.ProtocolHTTP:
  348. r.StreamSniffers = append(r.StreamSniffers, sniff.HTTPHost)
  349. case C.ProtocolQUIC:
  350. r.PacketSniffers = append(r.PacketSniffers, sniff.QUICClientHello)
  351. case C.ProtocolDNS:
  352. r.StreamSniffers = append(r.StreamSniffers, sniff.StreamDomainNameQuery)
  353. r.PacketSniffers = append(r.PacketSniffers, sniff.DomainNameQuery)
  354. case C.ProtocolSTUN:
  355. r.PacketSniffers = append(r.PacketSniffers, sniff.STUNMessage)
  356. case C.ProtocolBitTorrent:
  357. r.StreamSniffers = append(r.StreamSniffers, sniff.BitTorrent)
  358. r.PacketSniffers = append(r.PacketSniffers, sniff.UTP)
  359. r.PacketSniffers = append(r.PacketSniffers, sniff.UDPTracker)
  360. case C.ProtocolDTLS:
  361. r.PacketSniffers = append(r.PacketSniffers, sniff.DTLSRecord)
  362. case C.ProtocolSSH:
  363. r.StreamSniffers = append(r.StreamSniffers, sniff.SSH)
  364. case C.ProtocolRDP:
  365. r.StreamSniffers = append(r.StreamSniffers, sniff.RDP)
  366. case C.ProtocolNTP:
  367. r.PacketSniffers = append(r.PacketSniffers, sniff.NTP)
  368. default:
  369. return E.New("unknown sniffer: ", name)
  370. }
  371. }
  372. return nil
  373. }
  374. func (r *RuleActionSniff) String() string {
  375. if len(r.SnifferNames) == 0 && r.Timeout == 0 {
  376. return "sniff"
  377. } else if len(r.SnifferNames) > 0 && r.Timeout == 0 {
  378. return F.ToString("sniff(", strings.Join(r.SnifferNames, ","), ")")
  379. } else if len(r.SnifferNames) == 0 && r.Timeout > 0 {
  380. return F.ToString("sniff(", r.Timeout.String(), ")")
  381. } else {
  382. return F.ToString("sniff(", strings.Join(r.SnifferNames, ","), ",", r.Timeout.String(), ")")
  383. }
  384. }
  385. type RuleActionResolve struct {
  386. Server string
  387. Strategy C.DomainStrategy
  388. DisableCache bool
  389. RewriteTTL *uint32
  390. ClientSubnet netip.Prefix
  391. }
  392. func (r *RuleActionResolve) Type() string {
  393. return C.RuleActionTypeResolve
  394. }
  395. func (r *RuleActionResolve) String() string {
  396. var options []string
  397. if r.Server != "" {
  398. options = append(options, r.Server)
  399. }
  400. if r.Strategy != C.DomainStrategyAsIS {
  401. options = append(options, F.ToString(option.DomainStrategy(r.Strategy)))
  402. }
  403. if r.DisableCache {
  404. options = append(options, "disable_cache")
  405. }
  406. if r.RewriteTTL != nil {
  407. options = append(options, F.ToString("rewrite_ttl=", *r.RewriteTTL))
  408. }
  409. if r.ClientSubnet.IsValid() {
  410. options = append(options, F.ToString("client_subnet=", r.ClientSubnet))
  411. }
  412. if len(options) == 0 {
  413. return "resolve"
  414. } else {
  415. return F.ToString("resolve(", strings.Join(options, ","), ")")
  416. }
  417. }
  418. type RuleActionPredefined struct {
  419. Rcode int
  420. Answer []dns.RR
  421. Ns []dns.RR
  422. Extra []dns.RR
  423. }
  424. func (r *RuleActionPredefined) Type() string {
  425. return C.RuleActionTypePredefined
  426. }
  427. func (r *RuleActionPredefined) String() string {
  428. var options []string
  429. options = append(options, dns.RcodeToString[r.Rcode])
  430. options = append(options, common.Map(r.Answer, dns.RR.String)...)
  431. options = append(options, common.Map(r.Ns, dns.RR.String)...)
  432. options = append(options, common.Map(r.Extra, dns.RR.String)...)
  433. return F.ToString("predefined(", strings.Join(options, ","), ")")
  434. }
  435. func (r *RuleActionPredefined) Response(request *dns.Msg) *dns.Msg {
  436. return &dns.Msg{
  437. MsgHdr: dns.MsgHdr{
  438. Id: request.Id,
  439. Response: true,
  440. Authoritative: true,
  441. RecursionDesired: true,
  442. RecursionAvailable: true,
  443. Rcode: r.Rcode,
  444. },
  445. Question: request.Question,
  446. Answer: rewriteRecords(r.Answer, request.Question[0]),
  447. Ns: rewriteRecords(r.Ns, request.Question[0]),
  448. Extra: rewriteRecords(r.Extra, request.Question[0]),
  449. }
  450. }
  451. func rewriteRecords(records []dns.RR, question dns.Question) []dns.RR {
  452. return common.Map(records, func(it dns.RR) dns.RR {
  453. if strings.HasPrefix(it.Header().Name, "*") {
  454. if strings.HasSuffix(question.Name, it.Header().Name[1:]) {
  455. it = dns.Copy(it)
  456. it.Header().Name = question.Name
  457. }
  458. }
  459. return it
  460. })
  461. }