dialer.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package dialer
  2. import (
  3. "context"
  4. "net"
  5. "net/netip"
  6. "time"
  7. "github.com/sagernet/sing-box/adapter"
  8. C "github.com/sagernet/sing-box/constant"
  9. "github.com/sagernet/sing-box/option"
  10. "github.com/sagernet/sing-dns"
  11. E "github.com/sagernet/sing/common/exceptions"
  12. M "github.com/sagernet/sing/common/metadata"
  13. N "github.com/sagernet/sing/common/network"
  14. "github.com/sagernet/sing/service"
  15. )
  16. func New(ctx context.Context, options option.DialerOptions) (N.Dialer, error) {
  17. if options.IsWireGuardListener {
  18. return NewDefault(ctx, options)
  19. }
  20. var (
  21. dialer N.Dialer
  22. err error
  23. )
  24. if options.Detour == "" {
  25. dialer, err = NewDefault(ctx, options)
  26. if err != nil {
  27. return nil, err
  28. }
  29. } else {
  30. outboundManager := service.FromContext[adapter.OutboundManager](ctx)
  31. if outboundManager == nil {
  32. return nil, E.New("missing outbound manager")
  33. }
  34. dialer = NewDetour(outboundManager, options.Detour)
  35. }
  36. if options.Detour == "" {
  37. router := service.FromContext[adapter.Router](ctx)
  38. if router != nil {
  39. dialer = NewResolveDialer(
  40. router,
  41. dialer,
  42. options.Detour == "" && !options.TCPFastOpen,
  43. dns.DomainStrategy(options.DomainStrategy),
  44. time.Duration(options.FallbackDelay))
  45. }
  46. }
  47. return dialer, nil
  48. }
  49. func NewDirect(ctx context.Context, options option.DialerOptions) (ParallelInterfaceDialer, error) {
  50. if options.Detour != "" {
  51. return nil, E.New("`detour` is not supported in direct context")
  52. }
  53. if options.IsWireGuardListener {
  54. return NewDefault(ctx, options)
  55. }
  56. dialer, err := NewDefault(ctx, options)
  57. if err != nil {
  58. return nil, err
  59. }
  60. return NewResolveParallelInterfaceDialer(
  61. service.FromContext[adapter.Router](ctx),
  62. dialer,
  63. true,
  64. dns.DomainStrategy(options.DomainStrategy),
  65. time.Duration(options.FallbackDelay),
  66. ), nil
  67. }
  68. type ParallelInterfaceDialer interface {
  69. N.Dialer
  70. DialParallelInterface(ctx context.Context, network string, destination M.Socksaddr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.Conn, error)
  71. ListenSerialInterfacePacket(ctx context.Context, destination M.Socksaddr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.PacketConn, error)
  72. }
  73. type ParallelNetworkDialer interface {
  74. DialParallelNetwork(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.Conn, error)
  75. ListenSerialNetworkPacket(ctx context.Context, destination M.Socksaddr, destinationAddresses []netip.Addr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.PacketConn, netip.Addr, error)
  76. }