outbound.go 9.3 KB

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