outbound.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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/common/bufio"
  15. E "github.com/sagernet/sing/common/exceptions"
  16. "github.com/sagernet/sing/common/logger"
  17. M "github.com/sagernet/sing/common/metadata"
  18. N "github.com/sagernet/sing/common/network"
  19. )
  20. func RegisterOutbound(registry *outbound.Registry) {
  21. outbound.Register[option.DirectOutboundOptions](registry, C.TypeDirect, NewOutbound)
  22. }
  23. var (
  24. _ N.ParallelDialer = (*Outbound)(nil)
  25. _ dialer.ParallelNetworkDialer = (*Outbound)(nil)
  26. _ dialer.DirectDialer = (*Outbound)(nil)
  27. )
  28. type Outbound struct {
  29. outbound.Adapter
  30. logger logger.ContextLogger
  31. dialer dialer.ParallelInterfaceDialer
  32. domainStrategy C.DomainStrategy
  33. fallbackDelay time.Duration
  34. overrideOption int
  35. overrideDestination M.Socksaddr
  36. isEmpty bool
  37. // loopBack *loopBackDetector
  38. }
  39. func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.DirectOutboundOptions) (adapter.Outbound, error) {
  40. options.UDPFragmentDefault = true
  41. if options.Detour != "" {
  42. return nil, E.New("`detour` is not supported in direct context")
  43. }
  44. outboundDialer, err := dialer.NewWithOptions(dialer.Options{
  45. Context: ctx,
  46. Options: options.DialerOptions,
  47. RemoteIsDomain: true,
  48. DirectOutbound: true,
  49. })
  50. if err != nil {
  51. return nil, err
  52. }
  53. outbound := &Outbound{
  54. Adapter: outbound.NewAdapterWithDialerOptions(C.TypeDirect, tag, []string{N.NetworkTCP, N.NetworkUDP}, options.DialerOptions),
  55. logger: logger,
  56. //nolint:staticcheck
  57. domainStrategy: C.DomainStrategy(options.DomainStrategy),
  58. fallbackDelay: time.Duration(options.FallbackDelay),
  59. dialer: outboundDialer.(dialer.ParallelInterfaceDialer),
  60. //nolint:staticcheck
  61. isEmpty: reflect.DeepEqual(options.DialerOptions, option.DialerOptions{UDPFragmentDefault: true}) && options.OverrideAddress == "" && options.OverridePort == 0,
  62. // loopBack: newLoopBackDetector(router),
  63. }
  64. //nolint:staticcheck
  65. if options.ProxyProtocol != 0 {
  66. return nil, E.New("Proxy Protocol is deprecated and removed in sing-box 1.6.0")
  67. }
  68. //nolint:staticcheck
  69. if options.OverrideAddress != "" && options.OverridePort != 0 {
  70. outbound.overrideOption = 1
  71. outbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  72. } else if options.OverrideAddress != "" {
  73. outbound.overrideOption = 2
  74. outbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  75. } else if options.OverridePort != 0 {
  76. outbound.overrideOption = 3
  77. outbound.overrideDestination = M.Socksaddr{Port: options.OverridePort}
  78. }
  79. return outbound, nil
  80. }
  81. func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  82. ctx, metadata := adapter.ExtendContext(ctx)
  83. metadata.Outbound = h.Tag()
  84. metadata.Destination = destination
  85. switch h.overrideOption {
  86. case 1:
  87. destination = h.overrideDestination
  88. case 2:
  89. newDestination := h.overrideDestination
  90. newDestination.Port = destination.Port
  91. destination = newDestination
  92. case 3:
  93. destination.Port = h.overrideDestination.Port
  94. }
  95. network = N.NetworkName(network)
  96. switch network {
  97. case N.NetworkTCP:
  98. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  99. case N.NetworkUDP:
  100. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  101. }
  102. /*conn, err := h.dialer.DialContext(ctx, network, destination)
  103. if err != nil {
  104. return nil, err
  105. }
  106. return h.loopBack.NewConn(conn), nil*/
  107. return h.dialer.DialContext(ctx, network, destination)
  108. }
  109. func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  110. ctx, metadata := adapter.ExtendContext(ctx)
  111. metadata.Outbound = h.Tag()
  112. metadata.Destination = destination
  113. originDestination := destination
  114. switch h.overrideOption {
  115. case 1:
  116. destination = h.overrideDestination
  117. case 2:
  118. newDestination := h.overrideDestination
  119. newDestination.Port = destination.Port
  120. destination = newDestination
  121. case 3:
  122. destination.Port = h.overrideDestination.Port
  123. }
  124. if h.overrideOption == 0 {
  125. h.logger.InfoContext(ctx, "outbound packet connection")
  126. } else {
  127. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  128. }
  129. conn, err := h.dialer.ListenPacket(ctx, destination)
  130. if err != nil {
  131. return nil, err
  132. }
  133. // conn = h.loopBack.NewPacketConn(bufio.NewPacketConn(conn), destination)
  134. if originDestination != destination {
  135. conn = bufio.NewNATPacketConn(bufio.NewPacketConn(conn), destination, originDestination)
  136. }
  137. return conn, nil
  138. }
  139. func (h *Outbound) DialParallel(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr) (net.Conn, error) {
  140. ctx, metadata := adapter.ExtendContext(ctx)
  141. metadata.Outbound = h.Tag()
  142. metadata.Destination = destination
  143. switch h.overrideOption {
  144. case 1, 2:
  145. // override address
  146. return h.DialContext(ctx, network, destination)
  147. case 3:
  148. destination.Port = h.overrideDestination.Port
  149. }
  150. network = N.NetworkName(network)
  151. switch network {
  152. case N.NetworkTCP:
  153. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  154. case N.NetworkUDP:
  155. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  156. }
  157. return dialer.DialParallelNetwork(ctx, h.dialer, network, destination, destinationAddresses, len(destinationAddresses) > 0 && destinationAddresses[0].Is6(), nil, nil, nil, h.fallbackDelay)
  158. }
  159. 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) {
  160. ctx, metadata := adapter.ExtendContext(ctx)
  161. metadata.Outbound = h.Tag()
  162. metadata.Destination = destination
  163. switch h.overrideOption {
  164. case 1, 2:
  165. // override address
  166. return h.DialContext(ctx, network, destination)
  167. case 3:
  168. destination.Port = h.overrideDestination.Port
  169. }
  170. network = N.NetworkName(network)
  171. switch network {
  172. case N.NetworkTCP:
  173. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  174. case N.NetworkUDP:
  175. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  176. }
  177. return dialer.DialParallelNetwork(ctx, h.dialer, network, destination, destinationAddresses, len(destinationAddresses) > 0 && destinationAddresses[0].Is6(), networkStrategy, networkType, fallbackNetworkType, fallbackDelay)
  178. }
  179. 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) {
  180. ctx, metadata := adapter.ExtendContext(ctx)
  181. metadata.Outbound = h.Tag()
  182. metadata.Destination = destination
  183. switch h.overrideOption {
  184. case 1:
  185. destination = h.overrideDestination
  186. case 2:
  187. newDestination := h.overrideDestination
  188. newDestination.Port = destination.Port
  189. destination = newDestination
  190. case 3:
  191. destination.Port = h.overrideDestination.Port
  192. }
  193. if h.overrideOption == 0 {
  194. h.logger.InfoContext(ctx, "outbound packet connection")
  195. } else {
  196. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  197. }
  198. conn, newDestination, err := dialer.ListenSerialNetworkPacket(ctx, h.dialer, destination, destinationAddresses, networkStrategy, networkType, fallbackNetworkType, fallbackDelay)
  199. if err != nil {
  200. return nil, netip.Addr{}, err
  201. }
  202. return conn, newDestination, nil
  203. }
  204. func (h *Outbound) IsEmpty() bool {
  205. return h.isEmpty
  206. }
  207. /*func (h *Outbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  208. if h.loopBack.CheckConn(metadata.Source.AddrPort(), M.AddrPortFromNet(conn.LocalAddr())) {
  209. return E.New("reject loopback connection to ", metadata.Destination)
  210. }
  211. return NewConnection(ctx, h, conn, metadata)
  212. }
  213. func (h *Outbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  214. if h.loopBack.CheckPacketConn(metadata.Source.AddrPort(), M.AddrPortFromNet(conn.LocalAddr())) {
  215. return E.New("reject loopback packet connection to ", metadata.Destination)
  216. }
  217. return NewPacketConnection(ctx, h, conn, metadata)
  218. }
  219. */