outbound.go 9.3 KB

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