1
0

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