outbound.go 9.7 KB

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