dialer.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package internet
  2. import (
  3. "context"
  4. "github.com/v2fly/v2ray-core/v5/common/net"
  5. "github.com/v2fly/v2ray-core/v5/common/session"
  6. "github.com/v2fly/v2ray-core/v5/transport/internet/tagged"
  7. )
  8. // Dialer is the interface for dialing outbound connections.
  9. type Dialer interface {
  10. // Dial dials a system connection to the given destination.
  11. Dial(ctx context.Context, destination net.Destination) (Connection, error)
  12. // Address returns the address used by this Dialer. Maybe nil if not known.
  13. Address() net.Address
  14. }
  15. // dialFunc is an interface to dial network connection to a specific destination.
  16. type dialFunc func(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error)
  17. var transportDialerCache = make(map[string]dialFunc)
  18. // RegisterTransportDialer registers a Dialer with given name.
  19. func RegisterTransportDialer(protocol string, dialer dialFunc) error {
  20. if _, found := transportDialerCache[protocol]; found {
  21. return newError(protocol, " dialer already registered").AtError()
  22. }
  23. transportDialerCache[protocol] = dialer
  24. return nil
  25. }
  26. // Dial dials a internet connection towards the given destination.
  27. func Dial(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error) {
  28. if dest.Network == net.Network_TCP {
  29. if streamSettings == nil {
  30. s, err := ToMemoryStreamConfig(nil)
  31. if err != nil {
  32. return nil, newError("failed to create default stream settings").Base(err)
  33. }
  34. streamSettings = s
  35. }
  36. protocol := streamSettings.ProtocolName
  37. if originalProtocolName := getOriginalMessageName(streamSettings); originalProtocolName != "" {
  38. protocol = originalProtocolName
  39. }
  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. outbound := session.OutboundFromContext(ctx)
  58. var src net.Address
  59. if outbound != nil {
  60. src = outbound.Gateway
  61. }
  62. if transportLayerOutgoingTag := session.GetTransportLayerProxyTagFromContext(ctx); transportLayerOutgoingTag != "" {
  63. return DialTaggedOutbound(ctx, dest, transportLayerOutgoingTag)
  64. }
  65. originalAddr := dest.Address
  66. if outbound != nil && outbound.Resolver != nil && dest.Address.Family().IsDomain() {
  67. if addr := outbound.Resolver(ctx, dest.Address.Domain()); addr != nil {
  68. dest.Address = addr
  69. }
  70. }
  71. switch {
  72. case src != nil && dest.Address != originalAddr:
  73. newError("dialing to ", dest, " resolved from ", originalAddr, " via ", src).WriteToLog(session.ExportIDToError(ctx))
  74. case src != nil:
  75. newError("dialing to ", dest, " via ", src).WriteToLog(session.ExportIDToError(ctx))
  76. case dest.Address != originalAddr:
  77. newError("dialing to ", dest, " resolved from ", originalAddr).WriteToLog(session.ExportIDToError(ctx))
  78. }
  79. return effectiveSystemDialer.Dial(ctx, src, dest, sockopt)
  80. }
  81. func DialTaggedOutbound(ctx context.Context, dest net.Destination, tag string) (net.Conn, error) {
  82. if tagged.Dialer == nil {
  83. return nil, newError("tagged dial not enabled")
  84. }
  85. return tagged.Dialer(ctx, dest, tag)
  86. }