outbound.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package direct
  2. import (
  3. "context"
  4. "net"
  5. "net/netip"
  6. "reflect"
  7. "time"
  8. "github.com/sagernet/sing-box/adapter"
  9. "github.com/sagernet/sing-box/adapter/outbound"
  10. "github.com/sagernet/sing-box/common/dialer"
  11. C "github.com/sagernet/sing-box/constant"
  12. "github.com/sagernet/sing-box/log"
  13. "github.com/sagernet/sing-box/option"
  14. "github.com/sagernet/sing-tun"
  15. "github.com/sagernet/sing-tun/ping"
  16. "github.com/sagernet/sing/common"
  17. E "github.com/sagernet/sing/common/exceptions"
  18. "github.com/sagernet/sing/common/logger"
  19. M "github.com/sagernet/sing/common/metadata"
  20. N "github.com/sagernet/sing/common/network"
  21. )
  22. func RegisterOutbound(registry *outbound.Registry) {
  23. outbound.Register[option.DirectOutboundOptions](registry, C.TypeDirect, NewOutbound)
  24. }
  25. var (
  26. _ N.ParallelDialer = (*Outbound)(nil)
  27. _ dialer.ParallelNetworkDialer = (*Outbound)(nil)
  28. _ dialer.DirectDialer = (*Outbound)(nil)
  29. _ adapter.DirectRouteOutbound = (*Outbound)(nil)
  30. )
  31. type Outbound struct {
  32. outbound.Adapter
  33. ctx context.Context
  34. logger logger.ContextLogger
  35. dialer dialer.ParallelInterfaceDialer
  36. domainStrategy C.DomainStrategy
  37. fallbackDelay time.Duration
  38. isEmpty bool
  39. // loopBack *loopBackDetector
  40. }
  41. func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.DirectOutboundOptions) (adapter.Outbound, error) {
  42. options.UDPFragmentDefault = true
  43. if options.Detour != "" {
  44. return nil, E.New("`detour` is not supported in direct context")
  45. }
  46. outboundDialer, err := dialer.NewWithOptions(dialer.Options{
  47. Context: ctx,
  48. Options: options.DialerOptions,
  49. RemoteIsDomain: true,
  50. DirectOutbound: true,
  51. })
  52. if err != nil {
  53. return nil, err
  54. }
  55. outbound := &Outbound{
  56. Adapter: outbound.NewAdapterWithDialerOptions(C.TypeDirect, tag, []string{N.NetworkTCP, N.NetworkUDP, N.NetworkICMP}, options.DialerOptions),
  57. ctx: ctx,
  58. logger: logger,
  59. //nolint:staticcheck
  60. domainStrategy: C.DomainStrategy(options.DomainStrategy),
  61. fallbackDelay: time.Duration(options.FallbackDelay),
  62. dialer: outboundDialer.(dialer.ParallelInterfaceDialer),
  63. isEmpty: reflect.DeepEqual(options.DialerOptions, option.DialerOptions{UDPFragmentDefault: true}),
  64. // loopBack: newLoopBackDetector(router),
  65. }
  66. //nolint:staticcheck
  67. if options.ProxyProtocol != 0 {
  68. return nil, E.New("Proxy Protocol is deprecated and removed in sing-box 1.6.0")
  69. }
  70. return outbound, nil
  71. }
  72. func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  73. ctx, metadata := adapter.ExtendContext(ctx)
  74. metadata.Outbound = h.Tag()
  75. metadata.Destination = destination
  76. network = N.NetworkName(network)
  77. switch network {
  78. case N.NetworkTCP:
  79. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  80. case N.NetworkUDP:
  81. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  82. }
  83. /*conn, err := h.dialer.DialContext(ctx, network, destination)
  84. if err != nil {
  85. return nil, err
  86. }
  87. return h.loopBack.NewConn(conn), nil*/
  88. return h.dialer.DialContext(ctx, network, destination)
  89. }
  90. func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  91. ctx, metadata := adapter.ExtendContext(ctx)
  92. metadata.Outbound = h.Tag()
  93. metadata.Destination = destination
  94. h.logger.InfoContext(ctx, "outbound packet connection")
  95. conn, err := h.dialer.ListenPacket(ctx, destination)
  96. if err != nil {
  97. return nil, err
  98. }
  99. // conn = h.loopBack.NewPacketConn(bufio.NewPacketConn(conn), destination)
  100. return conn, nil
  101. }
  102. func (h *Outbound) NewDirectRouteConnection(metadata adapter.InboundContext, routeContext tun.DirectRouteContext, timeout time.Duration) (tun.DirectRouteDestination, error) {
  103. ctx := log.ContextWithNewID(h.ctx)
  104. destination, err := ping.ConnectDestination(ctx, h.logger, common.MustCast[*dialer.DefaultDialer](h.dialer).DialerForICMPDestination(metadata.Destination.Addr).Control, metadata.Destination.Addr, routeContext, timeout)
  105. if err != nil {
  106. return nil, err
  107. }
  108. h.logger.InfoContext(ctx, "linked ", metadata.Network, " connection from ", metadata.Source.AddrString(), " to ", metadata.Destination.AddrString())
  109. return destination, nil
  110. }
  111. func (h *Outbound) DialParallel(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr) (net.Conn, error) {
  112. ctx, metadata := adapter.ExtendContext(ctx)
  113. metadata.Outbound = h.Tag()
  114. metadata.Destination = destination
  115. network = N.NetworkName(network)
  116. switch network {
  117. case N.NetworkTCP:
  118. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  119. case N.NetworkUDP:
  120. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  121. }
  122. return dialer.DialParallelNetwork(ctx, h.dialer, network, destination, destinationAddresses, len(destinationAddresses) > 0 && destinationAddresses[0].Is6(), nil, nil, nil, h.fallbackDelay)
  123. }
  124. func (h *Outbound) DialParallelNetwork(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr, networkStrategy *C.NetworkStrategy, networkType []C.InterfaceType, fallbackNetworkType []C.InterfaceType, fallbackDelay time.Duration) (net.Conn, error) {
  125. ctx, metadata := adapter.ExtendContext(ctx)
  126. metadata.Outbound = h.Tag()
  127. metadata.Destination = destination
  128. network = N.NetworkName(network)
  129. switch network {
  130. case N.NetworkTCP:
  131. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  132. case N.NetworkUDP:
  133. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  134. }
  135. return dialer.DialParallelNetwork(ctx, h.dialer, network, destination, destinationAddresses, len(destinationAddresses) > 0 && destinationAddresses[0].Is6(), networkStrategy, networkType, fallbackNetworkType, fallbackDelay)
  136. }
  137. func (h *Outbound) ListenSerialNetworkPacket(ctx context.Context, destination M.Socksaddr, destinationAddresses []netip.Addr, networkStrategy *C.NetworkStrategy, networkType []C.InterfaceType, fallbackNetworkType []C.InterfaceType, fallbackDelay time.Duration) (net.PacketConn, netip.Addr, error) {
  138. ctx, metadata := adapter.ExtendContext(ctx)
  139. metadata.Outbound = h.Tag()
  140. metadata.Destination = destination
  141. h.logger.InfoContext(ctx, "outbound packet connection")
  142. conn, newDestination, err := dialer.ListenSerialNetworkPacket(ctx, h.dialer, destination, destinationAddresses, networkStrategy, networkType, fallbackNetworkType, fallbackDelay)
  143. if err != nil {
  144. return nil, netip.Addr{}, err
  145. }
  146. return conn, newDestination, nil
  147. }
  148. func (h *Outbound) IsEmpty() bool {
  149. return h.isEmpty
  150. }
  151. /*func (h *Outbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  152. if h.loopBack.CheckConn(metadata.Source.AddrPort(), M.AddrPortFromNet(conn.LocalAddr())) {
  153. return E.New("reject loopback connection to ", metadata.Destination)
  154. }
  155. return NewConnection(ctx, h, conn, metadata)
  156. }
  157. func (h *Outbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  158. if h.loopBack.CheckPacketConn(metadata.Source.AddrPort(), M.AddrPortFromNet(conn.LocalAddr())) {
  159. return E.New("reject loopback packet connection to ", metadata.Destination)
  160. }
  161. return NewPacketConnection(ctx, h, conn, metadata)
  162. }
  163. */