outbound.go 9.9 KB

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