outbound.go 9.6 KB

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