outbound.go 9.3 KB

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