outbound.go 9.4 KB

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