dialer.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package internet
  2. import (
  3. "context"
  4. core "github.com/v2fly/v2ray-core/v4"
  5. "github.com/v2fly/v2ray-core/v4/features/routing"
  6. "github.com/v2fly/v2ray-core/v4/common/net"
  7. "github.com/v2fly/v2ray-core/v4/common/session"
  8. )
  9. // Dialer is the interface for dialing outbound connections.
  10. type Dialer interface {
  11. // Dial dials a system connection to the given destination.
  12. Dial(ctx context.Context, destination net.Destination) (Connection, error)
  13. // Address returns the address used by this Dialer. Maybe nil if not known.
  14. Address() net.Address
  15. }
  16. // dialFunc is an interface to dial network connection to a specific destination.
  17. type dialFunc func(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error)
  18. var (
  19. transportDialerCache = make(map[string]dialFunc)
  20. )
  21. // RegisterTransportDialer registers a Dialer with given name.
  22. func RegisterTransportDialer(protocol string, dialer dialFunc) error {
  23. if _, found := transportDialerCache[protocol]; found {
  24. return newError(protocol, " dialer already registered").AtError()
  25. }
  26. transportDialerCache[protocol] = dialer
  27. return nil
  28. }
  29. // Dial dials a internet connection towards the given destination.
  30. func Dial(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error) {
  31. if dest.Network == net.Network_TCP {
  32. if streamSettings == nil {
  33. s, err := ToMemoryStreamConfig(nil)
  34. if err != nil {
  35. return nil, newError("failed to create default stream settings").Base(err)
  36. }
  37. streamSettings = s
  38. }
  39. protocol := streamSettings.ProtocolName
  40. dialer := transportDialerCache[protocol]
  41. if dialer == nil {
  42. return nil, newError(protocol, " dialer not registered").AtError()
  43. }
  44. return dialer(ctx, dest, streamSettings)
  45. }
  46. if dest.Network == net.Network_UDP {
  47. udpDialer := transportDialerCache["udp"]
  48. if udpDialer == nil {
  49. return nil, newError("UDP dialer not registered").AtError()
  50. }
  51. return udpDialer(ctx, dest, streamSettings)
  52. }
  53. return nil, newError("unknown network ", dest.Network)
  54. }
  55. // DialSystem calls system dialer to create a network connection.
  56. func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
  57. var src net.Address
  58. if outbound := session.OutboundFromContext(ctx); outbound != nil {
  59. src = outbound.Gateway
  60. }
  61. if transportLayerOutgoingTag := session.GetTransportLayerProxyTagFromContext(ctx); transportLayerOutgoingTag != "" {
  62. return DialTaggedOutbound(ctx, dest, transportLayerOutgoingTag)
  63. }
  64. return effectiveSystemDialer.Dial(ctx, src, dest, sockopt)
  65. }
  66. func DialTaggedOutbound(ctx context.Context, dest net.Destination, tag string) (net.Conn, error) {
  67. var dispatcher routing.Dispatcher
  68. if err := core.RequireFeatures(ctx, func(dispatcherInstance routing.Dispatcher) {
  69. dispatcher = dispatcherInstance
  70. }); err != nil {
  71. return nil, newError("Required Feature dispatcher not resolved").Base(err)
  72. }
  73. content := new(session.Content)
  74. content.SkipDNSResolve = true
  75. session.SetForcedOutboundTagToContext(ctx, tag)
  76. ctx = session.ContextWithContent(ctx, content)
  77. r, err := dispatcher.Dispatch(ctx, dest)
  78. if err != nil {
  79. return nil, err
  80. }
  81. var readerOpt net.ConnectionOption
  82. if dest.Network == net.Network_TCP {
  83. readerOpt = net.ConnectionOutputMulti(r.Reader)
  84. } else {
  85. readerOpt = net.ConnectionOutputMultiUDP(r.Reader)
  86. }
  87. return net.NewConnection(net.ConnectionInputMulti(r.Writer), readerOpt), nil
  88. }