dialer.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package internet
  2. import (
  3. "context"
  4. "github.com/v2fly/v2ray-core/v4/common/net"
  5. "github.com/v2fly/v2ray-core/v4/common/session"
  6. "github.com/v2fly/v2ray-core/v4/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. 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. if tagged.Dialer == nil {
  68. return nil, newError("tagged dial not enabled")
  69. }
  70. return tagged.Dialer(ctx, dest, tag)
  71. }