inbound.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. package tun
  2. import (
  3. "context"
  4. "net"
  5. "net/netip"
  6. "os"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/sagernet/sing-box/adapter"
  12. "github.com/sagernet/sing-box/adapter/inbound"
  13. "github.com/sagernet/sing-box/common/taskmonitor"
  14. C "github.com/sagernet/sing-box/constant"
  15. "github.com/sagernet/sing-box/log"
  16. "github.com/sagernet/sing-box/option"
  17. "github.com/sagernet/sing-box/route/rule"
  18. "github.com/sagernet/sing-tun"
  19. "github.com/sagernet/sing/common"
  20. E "github.com/sagernet/sing/common/exceptions"
  21. "github.com/sagernet/sing/common/json/badoption"
  22. M "github.com/sagernet/sing/common/metadata"
  23. N "github.com/sagernet/sing/common/network"
  24. "github.com/sagernet/sing/common/ranges"
  25. "github.com/sagernet/sing/common/x/list"
  26. "github.com/sagernet/sing/service"
  27. "go4.org/netipx"
  28. )
  29. func RegisterInbound(registry *inbound.Registry) {
  30. inbound.Register[option.TunInboundOptions](registry, C.TypeTun, NewInbound)
  31. }
  32. type Inbound struct {
  33. tag string
  34. ctx context.Context
  35. router adapter.Router
  36. networkManager adapter.NetworkManager
  37. logger log.ContextLogger
  38. tunOptions tun.Options
  39. udpTimeout time.Duration
  40. stack string
  41. tunIf tun.Tun
  42. tunStack tun.Stack
  43. platformInterface adapter.PlatformInterface
  44. platformOptions option.TunPlatformOptions
  45. autoRedirect tun.AutoRedirect
  46. routeRuleSet []adapter.RuleSet
  47. routeRuleSetCallback []*list.Element[adapter.RuleSetUpdateCallback]
  48. routeExcludeRuleSet []adapter.RuleSet
  49. routeExcludeRuleSetCallback []*list.Element[adapter.RuleSetUpdateCallback]
  50. routeAddressSet []*netipx.IPSet
  51. routeExcludeAddressSet []*netipx.IPSet
  52. }
  53. func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TunInboundOptions) (adapter.Inbound, error) {
  54. //nolint:staticcheck
  55. if len(options.Inet4Address) > 0 || len(options.Inet6Address) > 0 ||
  56. len(options.Inet4RouteAddress) > 0 || len(options.Inet6RouteAddress) > 0 ||
  57. len(options.Inet4RouteExcludeAddress) > 0 || len(options.Inet6RouteExcludeAddress) > 0 {
  58. return nil, E.New("legacy tun address fields are deprecated in sing-box 1.10.0 and removed in sing-box 1.12.0")
  59. }
  60. //nolint:staticcheck
  61. if options.GSO {
  62. return nil, E.New("GSO option in tun is deprecated in sing-box 1.11.0 and removed in sing-box 1.12.0")
  63. }
  64. //nolint:staticcheck
  65. if options.InboundOptions != (option.InboundOptions{}) {
  66. return nil, E.New("legacy inbound fields are deprecated in sing-box 1.11.0 and removed in sing-box 1.13.0, checkout migration: https://sing-box.sagernet.org/migration/#migrate-legacy-inbound-fields-to-rule-actions")
  67. }
  68. address := options.Address
  69. inet4Address := common.Filter(address, func(it netip.Prefix) bool {
  70. return it.Addr().Is4()
  71. })
  72. inet6Address := common.Filter(address, func(it netip.Prefix) bool {
  73. return it.Addr().Is6()
  74. })
  75. routeAddress := options.RouteAddress
  76. inet4RouteAddress := common.Filter(routeAddress, func(it netip.Prefix) bool {
  77. return it.Addr().Is4()
  78. })
  79. inet6RouteAddress := common.Filter(routeAddress, func(it netip.Prefix) bool {
  80. return it.Addr().Is6()
  81. })
  82. routeExcludeAddress := options.RouteExcludeAddress
  83. inet4RouteExcludeAddress := common.Filter(routeExcludeAddress, func(it netip.Prefix) bool {
  84. return it.Addr().Is4()
  85. })
  86. inet6RouteExcludeAddress := common.Filter(routeExcludeAddress, func(it netip.Prefix) bool {
  87. return it.Addr().Is6()
  88. })
  89. platformInterface := service.FromContext[adapter.PlatformInterface](ctx)
  90. tunMTU := options.MTU
  91. enableGSO := C.IsLinux && options.Stack == "gvisor" && platformInterface == nil && tunMTU > 0 && tunMTU < 49152
  92. if tunMTU == 0 {
  93. if platformInterface != nil && platformInterface.UnderNetworkExtension() {
  94. // In Network Extension, when MTU exceeds 4064 (4096-UTUN_IF_HEADROOM_SIZE), the performance of tun will drop significantly, which may be a system bug.
  95. tunMTU = 4064
  96. } else if C.IsAndroid {
  97. // Some Android devices report ENOBUFS when using MTU 65535
  98. tunMTU = 9000
  99. } else {
  100. tunMTU = 65535
  101. }
  102. }
  103. var udpTimeout time.Duration
  104. if options.UDPTimeout != 0 {
  105. udpTimeout = time.Duration(options.UDPTimeout)
  106. } else {
  107. udpTimeout = C.UDPTimeout
  108. }
  109. var err error
  110. includeUID := uidToRange(options.IncludeUID)
  111. if len(options.IncludeUIDRange) > 0 {
  112. includeUID, err = parseRange(includeUID, options.IncludeUIDRange)
  113. if err != nil {
  114. return nil, E.Cause(err, "parse include_uid_range")
  115. }
  116. }
  117. excludeUID := uidToRange(options.ExcludeUID)
  118. if len(options.ExcludeUIDRange) > 0 {
  119. excludeUID, err = parseRange(excludeUID, options.ExcludeUIDRange)
  120. if err != nil {
  121. return nil, E.Cause(err, "parse exclude_uid_range")
  122. }
  123. }
  124. tableIndex := options.IPRoute2TableIndex
  125. if tableIndex == 0 {
  126. tableIndex = tun.DefaultIPRoute2TableIndex
  127. }
  128. ruleIndex := options.IPRoute2RuleIndex
  129. if ruleIndex == 0 {
  130. ruleIndex = tun.DefaultIPRoute2RuleIndex
  131. }
  132. autoRedirectFallbackRuleIndex := options.AutoRedirectFallbackRuleIndex
  133. if autoRedirectFallbackRuleIndex == 0 {
  134. autoRedirectFallbackRuleIndex = tun.DefaultIPRoute2AutoRedirectFallbackRuleIndex
  135. }
  136. inputMark := uint32(options.AutoRedirectInputMark)
  137. if inputMark == 0 {
  138. inputMark = tun.DefaultAutoRedirectInputMark
  139. }
  140. outputMark := uint32(options.AutoRedirectOutputMark)
  141. if outputMark == 0 {
  142. outputMark = tun.DefaultAutoRedirectOutputMark
  143. }
  144. resetMark := uint32(options.AutoRedirectResetMark)
  145. if resetMark == 0 {
  146. resetMark = tun.DefaultAutoRedirectResetMark
  147. }
  148. nfQueue := options.AutoRedirectNFQueue
  149. if nfQueue == 0 {
  150. nfQueue = tun.DefaultAutoRedirectNFQueue
  151. }
  152. networkManager := service.FromContext[adapter.NetworkManager](ctx)
  153. multiPendingPackets := C.IsDarwin && ((options.Stack == "gvisor" && tunMTU < 32768) || (options.Stack != "gvisor" && options.MTU <= 9000))
  154. inbound := &Inbound{
  155. tag: tag,
  156. ctx: ctx,
  157. router: router,
  158. networkManager: networkManager,
  159. logger: logger,
  160. tunOptions: tun.Options{
  161. Name: options.InterfaceName,
  162. MTU: tunMTU,
  163. GSO: enableGSO,
  164. Inet4Address: inet4Address,
  165. Inet6Address: inet6Address,
  166. AutoRoute: options.AutoRoute,
  167. IPRoute2TableIndex: tableIndex,
  168. IPRoute2RuleIndex: ruleIndex,
  169. IPRoute2AutoRedirectFallbackRuleIndex: autoRedirectFallbackRuleIndex,
  170. AutoRedirectInputMark: inputMark,
  171. AutoRedirectOutputMark: outputMark,
  172. AutoRedirectResetMark: resetMark,
  173. AutoRedirectNFQueue: nfQueue,
  174. ExcludeMPTCP: options.ExcludeMPTCP,
  175. Inet4LoopbackAddress: common.Filter(options.LoopbackAddress, netip.Addr.Is4),
  176. Inet6LoopbackAddress: common.Filter(options.LoopbackAddress, netip.Addr.Is6),
  177. StrictRoute: options.StrictRoute,
  178. IncludeInterface: options.IncludeInterface,
  179. ExcludeInterface: options.ExcludeInterface,
  180. Inet4RouteAddress: inet4RouteAddress,
  181. Inet6RouteAddress: inet6RouteAddress,
  182. Inet4RouteExcludeAddress: inet4RouteExcludeAddress,
  183. Inet6RouteExcludeAddress: inet6RouteExcludeAddress,
  184. IncludeUID: includeUID,
  185. ExcludeUID: excludeUID,
  186. IncludeAndroidUser: options.IncludeAndroidUser,
  187. IncludePackage: options.IncludePackage,
  188. ExcludePackage: options.ExcludePackage,
  189. InterfaceMonitor: networkManager.InterfaceMonitor(),
  190. EXP_MultiPendingPackets: multiPendingPackets,
  191. },
  192. udpTimeout: udpTimeout,
  193. stack: options.Stack,
  194. platformInterface: platformInterface,
  195. platformOptions: common.PtrValueOrDefault(options.Platform),
  196. }
  197. for _, routeAddressSet := range options.RouteAddressSet {
  198. ruleSet, loaded := router.RuleSet(routeAddressSet)
  199. if !loaded {
  200. return nil, E.New("parse route_address_set: rule-set not found: ", routeAddressSet)
  201. }
  202. inbound.routeRuleSet = append(inbound.routeRuleSet, ruleSet)
  203. }
  204. for _, routeExcludeAddressSet := range options.RouteExcludeAddressSet {
  205. ruleSet, loaded := router.RuleSet(routeExcludeAddressSet)
  206. if !loaded {
  207. return nil, E.New("parse route_exclude_address_set: rule-set not found: ", routeExcludeAddressSet)
  208. }
  209. inbound.routeExcludeRuleSet = append(inbound.routeExcludeRuleSet, ruleSet)
  210. }
  211. if options.AutoRedirect {
  212. if !options.AutoRoute {
  213. return nil, E.New("`auto_route` is required by `auto_redirect`")
  214. }
  215. disableNFTables, dErr := strconv.ParseBool(os.Getenv("DISABLE_NFTABLES"))
  216. inbound.autoRedirect, err = tun.NewAutoRedirect(tun.AutoRedirectOptions{
  217. TunOptions: &inbound.tunOptions,
  218. Context: ctx,
  219. Handler: (*autoRedirectHandler)(inbound),
  220. Logger: logger,
  221. NetworkMonitor: networkManager.NetworkMonitor(),
  222. InterfaceFinder: networkManager.InterfaceFinder(),
  223. TableName: "sing-box",
  224. DisableNFTables: dErr == nil && disableNFTables,
  225. RouteAddressSet: &inbound.routeAddressSet,
  226. RouteExcludeAddressSet: &inbound.routeExcludeAddressSet,
  227. })
  228. if err != nil {
  229. return nil, E.Cause(err, "initialize auto-redirect")
  230. }
  231. if !C.IsAndroid {
  232. inbound.tunOptions.AutoRedirectMarkMode = true
  233. err = networkManager.RegisterAutoRedirectOutputMark(inbound.tunOptions.AutoRedirectOutputMark)
  234. if err != nil {
  235. return nil, err
  236. }
  237. }
  238. }
  239. return inbound, nil
  240. }
  241. func uidToRange(uidList badoption.Listable[uint32]) []ranges.Range[uint32] {
  242. return common.Map(uidList, func(uid uint32) ranges.Range[uint32] {
  243. return ranges.NewSingle(uid)
  244. })
  245. }
  246. func parseRange(uidRanges []ranges.Range[uint32], rangeList []string) ([]ranges.Range[uint32], error) {
  247. for _, uidRange := range rangeList {
  248. if !strings.Contains(uidRange, ":") {
  249. return nil, E.New("missing ':' in range: ", uidRange)
  250. }
  251. subIndex := strings.Index(uidRange, ":")
  252. if subIndex == 0 {
  253. return nil, E.New("missing range start: ", uidRange)
  254. } else if subIndex == len(uidRange)-1 {
  255. return nil, E.New("missing range end: ", uidRange)
  256. }
  257. var start, end uint64
  258. var err error
  259. start, err = strconv.ParseUint(uidRange[:subIndex], 0, 32)
  260. if err != nil {
  261. return nil, E.Cause(err, "parse range start")
  262. }
  263. end, err = strconv.ParseUint(uidRange[subIndex+1:], 0, 32)
  264. if err != nil {
  265. return nil, E.Cause(err, "parse range end")
  266. }
  267. uidRanges = append(uidRanges, ranges.New(uint32(start), uint32(end)))
  268. }
  269. return uidRanges, nil
  270. }
  271. func (t *Inbound) Type() string {
  272. return C.TypeTun
  273. }
  274. func (t *Inbound) Tag() string {
  275. return t.tag
  276. }
  277. func (t *Inbound) Start(stage adapter.StartStage) error {
  278. switch stage {
  279. case adapter.StartStateStart:
  280. if C.IsAndroid && t.platformInterface == nil {
  281. t.tunOptions.BuildAndroidRules(t.networkManager.PackageManager())
  282. }
  283. if t.tunOptions.Name == "" {
  284. t.tunOptions.Name = tun.CalculateInterfaceName("")
  285. }
  286. if t.platformInterface == nil {
  287. t.routeAddressSet = common.FlatMap(t.routeRuleSet, adapter.RuleSet.ExtractIPSet)
  288. for _, routeRuleSet := range t.routeRuleSet {
  289. ipSets := routeRuleSet.ExtractIPSet()
  290. if len(ipSets) == 0 {
  291. t.logger.Warn("route_address_set: no destination IP CIDR rules found in rule-set: ", routeRuleSet.Name())
  292. }
  293. routeRuleSet.IncRef()
  294. t.routeAddressSet = append(t.routeAddressSet, ipSets...)
  295. if t.autoRedirect != nil {
  296. t.routeRuleSetCallback = append(t.routeRuleSetCallback, routeRuleSet.RegisterCallback(t.updateRouteAddressSet))
  297. }
  298. }
  299. t.routeExcludeAddressSet = common.FlatMap(t.routeExcludeRuleSet, adapter.RuleSet.ExtractIPSet)
  300. for _, routeExcludeRuleSet := range t.routeExcludeRuleSet {
  301. ipSets := routeExcludeRuleSet.ExtractIPSet()
  302. if len(ipSets) == 0 {
  303. t.logger.Warn("route_address_set: no destination IP CIDR rules found in rule-set: ", routeExcludeRuleSet.Name())
  304. }
  305. routeExcludeRuleSet.IncRef()
  306. t.routeExcludeAddressSet = append(t.routeExcludeAddressSet, ipSets...)
  307. if t.autoRedirect != nil {
  308. t.routeExcludeRuleSetCallback = append(t.routeExcludeRuleSetCallback, routeExcludeRuleSet.RegisterCallback(t.updateRouteAddressSet))
  309. }
  310. }
  311. }
  312. var (
  313. tunInterface tun.Tun
  314. err error
  315. )
  316. monitor := taskmonitor.New(t.logger, C.StartTimeout)
  317. tunOptions := t.tunOptions
  318. if t.autoRedirect == nil && !(runtime.GOOS == "android" && t.platformInterface != nil) {
  319. for _, ipSet := range t.routeAddressSet {
  320. for _, prefix := range ipSet.Prefixes() {
  321. if prefix.Addr().Is4() {
  322. tunOptions.Inet4RouteAddress = append(tunOptions.Inet4RouteAddress, prefix)
  323. } else {
  324. tunOptions.Inet6RouteAddress = append(tunOptions.Inet6RouteAddress, prefix)
  325. }
  326. }
  327. }
  328. for _, ipSet := range t.routeExcludeAddressSet {
  329. for _, prefix := range ipSet.Prefixes() {
  330. if prefix.Addr().Is4() {
  331. tunOptions.Inet4RouteExcludeAddress = append(tunOptions.Inet4RouteExcludeAddress, prefix)
  332. } else {
  333. tunOptions.Inet6RouteExcludeAddress = append(tunOptions.Inet6RouteExcludeAddress, prefix)
  334. }
  335. }
  336. }
  337. }
  338. monitor.Start("open interface")
  339. if t.platformInterface != nil && t.platformInterface.UsePlatformInterface() {
  340. tunInterface, err = t.platformInterface.OpenInterface(&tunOptions, t.platformOptions)
  341. } else {
  342. if HookBeforeCreatePlatformInterface != nil {
  343. HookBeforeCreatePlatformInterface()
  344. }
  345. tunInterface, err = tun.New(tunOptions)
  346. }
  347. monitor.Finish()
  348. t.tunOptions.Name = tunOptions.Name
  349. if err != nil {
  350. return E.Cause(err, "configure tun interface")
  351. }
  352. t.logger.Trace("creating stack")
  353. t.tunIf = tunInterface
  354. var (
  355. forwarderBindInterface bool
  356. includeAllNetworks bool
  357. )
  358. if t.platformInterface != nil {
  359. forwarderBindInterface = true
  360. includeAllNetworks = t.platformInterface.NetworkExtensionIncludeAllNetworks()
  361. }
  362. tunStack, err := tun.NewStack(t.stack, tun.StackOptions{
  363. Context: t.ctx,
  364. Tun: tunInterface,
  365. TunOptions: t.tunOptions,
  366. UDPTimeout: t.udpTimeout,
  367. Handler: t,
  368. Logger: t.logger,
  369. ForwarderBindInterface: forwarderBindInterface,
  370. InterfaceFinder: t.networkManager.InterfaceFinder(),
  371. IncludeAllNetworks: includeAllNetworks,
  372. })
  373. if err != nil {
  374. return err
  375. }
  376. t.tunStack = tunStack
  377. t.logger.Info("started at ", t.tunOptions.Name)
  378. case adapter.StartStatePostStart:
  379. monitor := taskmonitor.New(t.logger, C.StartTimeout)
  380. monitor.Start("starting tun stack")
  381. err := t.tunStack.Start()
  382. monitor.Finish()
  383. if err != nil {
  384. return E.Cause(err, "starting tun stack")
  385. }
  386. monitor.Start("starting tun interface")
  387. err = t.tunIf.Start()
  388. monitor.Finish()
  389. if err != nil {
  390. return E.Cause(err, "starting TUN interface")
  391. }
  392. if t.autoRedirect != nil {
  393. monitor.Start("initialize auto-redirect")
  394. err := t.autoRedirect.Start()
  395. monitor.Finish()
  396. if err != nil {
  397. return E.Cause(err, "auto-redirect")
  398. }
  399. }
  400. t.routeAddressSet = nil
  401. t.routeExcludeAddressSet = nil
  402. }
  403. return nil
  404. }
  405. func (t *Inbound) updateRouteAddressSet(it adapter.RuleSet) {
  406. t.routeAddressSet = common.FlatMap(t.routeRuleSet, adapter.RuleSet.ExtractIPSet)
  407. t.routeExcludeAddressSet = common.FlatMap(t.routeExcludeRuleSet, adapter.RuleSet.ExtractIPSet)
  408. t.autoRedirect.UpdateRouteAddressSet()
  409. t.routeAddressSet = nil
  410. t.routeExcludeAddressSet = nil
  411. }
  412. func (t *Inbound) Close() error {
  413. return common.Close(
  414. t.tunStack,
  415. t.tunIf,
  416. t.autoRedirect,
  417. )
  418. }
  419. func (t *Inbound) PrepareConnection(network string, source M.Socksaddr, destination M.Socksaddr, routeContext tun.DirectRouteContext, timeout time.Duration) (tun.DirectRouteDestination, error) {
  420. var ipVersion uint8
  421. if !destination.IsIPv6() {
  422. ipVersion = 4
  423. } else {
  424. ipVersion = 6
  425. }
  426. routeDestination, err := t.router.PreMatch(adapter.InboundContext{
  427. Inbound: t.tag,
  428. InboundType: C.TypeTun,
  429. IPVersion: ipVersion,
  430. Network: network,
  431. Source: source,
  432. Destination: destination,
  433. }, routeContext, timeout, false)
  434. if err != nil {
  435. switch {
  436. case rule.IsBypassed(err):
  437. err = nil
  438. case rule.IsRejected(err):
  439. t.logger.Trace("reject ", network, " connection from ", source.AddrString(), " to ", destination.AddrString())
  440. default:
  441. if network == N.NetworkICMP {
  442. t.logger.Warn(E.Cause(err, "link ", network, " connection from ", source.AddrString(), " to ", destination.AddrString()))
  443. }
  444. }
  445. }
  446. return routeDestination, err
  447. }
  448. func (t *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
  449. ctx = log.ContextWithNewID(ctx)
  450. var metadata adapter.InboundContext
  451. metadata.Inbound = t.tag
  452. metadata.InboundType = C.TypeTun
  453. metadata.Source = source
  454. metadata.Destination = destination
  455. t.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
  456. t.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  457. t.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  458. }
  459. func (t *Inbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
  460. ctx = log.ContextWithNewID(ctx)
  461. var metadata adapter.InboundContext
  462. metadata.Inbound = t.tag
  463. metadata.InboundType = C.TypeTun
  464. metadata.Source = source
  465. metadata.Destination = destination
  466. t.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
  467. t.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  468. t.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
  469. }
  470. type autoRedirectHandler Inbound
  471. func (t *autoRedirectHandler) PrepareConnection(network string, source M.Socksaddr, destination M.Socksaddr, routeContext tun.DirectRouteContext, timeout time.Duration) (tun.DirectRouteDestination, error) {
  472. var ipVersion uint8
  473. if !destination.IsIPv6() {
  474. ipVersion = 4
  475. } else {
  476. ipVersion = 6
  477. }
  478. routeDestination, err := t.router.PreMatch(adapter.InboundContext{
  479. Inbound: t.tag,
  480. InboundType: C.TypeTun,
  481. IPVersion: ipVersion,
  482. Network: network,
  483. Source: source,
  484. Destination: destination,
  485. }, routeContext, timeout, true)
  486. if err != nil {
  487. switch {
  488. case rule.IsBypassed(err):
  489. t.logger.Trace("bypass ", network, " connection from ", source.AddrString(), " to ", destination.AddrString())
  490. case rule.IsRejected(err):
  491. t.logger.Trace("reject ", network, " connection from ", source.AddrString(), " to ", destination.AddrString())
  492. default:
  493. if network == N.NetworkICMP {
  494. t.logger.Warn(E.Cause(err, "link ", network, " connection from ", source.AddrString(), " to ", destination.AddrString()))
  495. }
  496. }
  497. }
  498. return routeDestination, err
  499. }
  500. func (t *autoRedirectHandler) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
  501. ctx = log.ContextWithNewID(ctx)
  502. var metadata adapter.InboundContext
  503. metadata.Inbound = t.tag
  504. metadata.InboundType = C.TypeTun
  505. metadata.Source = source
  506. metadata.Destination = destination
  507. t.logger.InfoContext(ctx, "inbound redirect connection from ", metadata.Source)
  508. t.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  509. t.router.RouteConnectionEx(ctx, conn, metadata, onClose)
  510. }
  511. func (t *autoRedirectHandler) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
  512. panic("unexcepted")
  513. }