outbound.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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, []string{N.NetworkTCP, N.NetworkUDP}, tag, 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. if options.ProxyProtocol != 0 {
  61. return nil, E.New("Proxy Protocol is deprecated and removed in sing-box 1.6.0")
  62. }
  63. if options.OverrideAddress != "" && options.OverridePort != 0 {
  64. outbound.overrideOption = 1
  65. outbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  66. } else if options.OverrideAddress != "" {
  67. outbound.overrideOption = 2
  68. outbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  69. } else if options.OverridePort != 0 {
  70. outbound.overrideOption = 3
  71. outbound.overrideDestination = M.Socksaddr{Port: options.OverridePort}
  72. }
  73. return outbound, nil
  74. }
  75. func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  76. ctx, metadata := adapter.ExtendContext(ctx)
  77. metadata.Outbound = h.Tag()
  78. metadata.Destination = destination
  79. switch h.overrideOption {
  80. case 1:
  81. destination = h.overrideDestination
  82. case 2:
  83. newDestination := h.overrideDestination
  84. newDestination.Port = destination.Port
  85. destination = newDestination
  86. case 3:
  87. destination.Port = h.overrideDestination.Port
  88. }
  89. network = N.NetworkName(network)
  90. switch network {
  91. case N.NetworkTCP:
  92. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  93. case N.NetworkUDP:
  94. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  95. }
  96. /*conn, err := h.dialer.DialContext(ctx, network, destination)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return h.loopBack.NewConn(conn), nil*/
  101. return h.dialer.DialContext(ctx, network, destination)
  102. }
  103. func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  104. ctx, metadata := adapter.ExtendContext(ctx)
  105. metadata.Outbound = h.Tag()
  106. metadata.Destination = destination
  107. originDestination := destination
  108. switch h.overrideOption {
  109. case 1:
  110. destination = h.overrideDestination
  111. case 2:
  112. newDestination := h.overrideDestination
  113. newDestination.Port = destination.Port
  114. destination = newDestination
  115. case 3:
  116. destination.Port = h.overrideDestination.Port
  117. }
  118. if h.overrideOption == 0 {
  119. h.logger.InfoContext(ctx, "outbound packet connection")
  120. } else {
  121. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  122. }
  123. conn, err := h.dialer.ListenPacket(ctx, destination)
  124. if err != nil {
  125. return nil, err
  126. }
  127. // conn = h.loopBack.NewPacketConn(bufio.NewPacketConn(conn), destination)
  128. if originDestination != destination {
  129. conn = bufio.NewNATPacketConn(bufio.NewPacketConn(conn), destination, originDestination)
  130. }
  131. return conn, nil
  132. }
  133. func (h *Outbound) DialParallel(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr) (net.Conn, error) {
  134. ctx, metadata := adapter.ExtendContext(ctx)
  135. metadata.Outbound = h.Tag()
  136. metadata.Destination = destination
  137. switch h.overrideOption {
  138. case 1, 2:
  139. // override address
  140. return h.DialContext(ctx, network, destination)
  141. case 3:
  142. destination.Port = h.overrideDestination.Port
  143. }
  144. network = N.NetworkName(network)
  145. switch network {
  146. case N.NetworkTCP:
  147. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  148. case N.NetworkUDP:
  149. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  150. }
  151. var domainStrategy dns.DomainStrategy
  152. if h.domainStrategy != dns.DomainStrategyAsIS {
  153. domainStrategy = h.domainStrategy
  154. } else {
  155. domainStrategy = dns.DomainStrategy(metadata.InboundOptions.DomainStrategy)
  156. }
  157. switch domainStrategy {
  158. case dns.DomainStrategyUseIPv4:
  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 dns.DomainStrategyUseIPv6:
  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 == dns.DomainStrategyPreferIPv6, h.networkStrategy, h.networkType, h.fallbackNetworkType, 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 dns.DomainStrategy
  190. if h.domainStrategy != dns.DomainStrategyAsIS {
  191. domainStrategy = h.domainStrategy
  192. } else {
  193. domainStrategy = dns.DomainStrategy(metadata.InboundOptions.DomainStrategy)
  194. }
  195. switch domainStrategy {
  196. case dns.DomainStrategyUseIPv4:
  197. destinationAddresses = common.Filter(destinationAddresses, netip.Addr.Is4)
  198. if len(destinationAddresses) == 0 {
  199. return nil, E.New("no IPv4 address available for ", destination)
  200. }
  201. case dns.DomainStrategyUseIPv6:
  202. destinationAddresses = common.Filter(destinationAddresses, netip.Addr.Is6)
  203. if len(destinationAddresses) == 0 {
  204. return nil, E.New("no IPv6 address available for ", destination)
  205. }
  206. }
  207. return dialer.DialParallelNetwork(ctx, h.dialer, network, destination, destinationAddresses, domainStrategy == dns.DomainStrategyPreferIPv6, networkStrategy, networkType, fallbackNetworkType, fallbackDelay)
  208. }
  209. 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) {
  210. ctx, metadata := adapter.ExtendContext(ctx)
  211. metadata.Outbound = h.Tag()
  212. metadata.Destination = destination
  213. switch h.overrideOption {
  214. case 1:
  215. destination = h.overrideDestination
  216. case 2:
  217. newDestination := h.overrideDestination
  218. newDestination.Port = destination.Port
  219. destination = newDestination
  220. case 3:
  221. destination.Port = h.overrideDestination.Port
  222. }
  223. if h.overrideOption == 0 {
  224. h.logger.InfoContext(ctx, "outbound packet connection")
  225. } else {
  226. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  227. }
  228. conn, newDestination, err := dialer.ListenSerialNetworkPacket(ctx, h.dialer, destination, destinationAddresses, networkStrategy, networkType, fallbackNetworkType, fallbackDelay)
  229. if err != nil {
  230. return nil, netip.Addr{}, err
  231. }
  232. return conn, newDestination, nil
  233. }
  234. /*func (h *Outbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  235. if h.loopBack.CheckConn(metadata.Source.AddrPort(), M.AddrPortFromNet(conn.LocalAddr())) {
  236. return E.New("reject loopback connection to ", metadata.Destination)
  237. }
  238. return NewConnection(ctx, h, conn, metadata)
  239. }
  240. func (h *Outbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  241. if h.loopBack.CheckPacketConn(metadata.Source.AddrPort(), M.AddrPortFromNet(conn.LocalAddr())) {
  242. return E.New("reject loopback packet connection to ", metadata.Destination)
  243. }
  244. return NewPacketConnection(ctx, h, conn, metadata)
  245. }
  246. */