dialer.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. dialer := transportDialerCache[protocol]
  38. if dialer == nil {
  39. return nil, newError(protocol, " dialer not registered").AtError()
  40. }
  41. return dialer(ctx, dest, streamSettings)
  42. }
  43. if dest.Network == net.Network_UDP {
  44. udpDialer := transportDialerCache["udp"]
  45. if udpDialer == nil {
  46. return nil, newError("UDP dialer not registered").AtError()
  47. }
  48. return udpDialer(ctx, dest, streamSettings)
  49. }
  50. return nil, newError("unknown network ", dest.Network)
  51. }
  52. // DialSystem calls system dialer to create a network connection.
  53. func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
  54. var src net.Address
  55. if outbound := session.OutboundFromContext(ctx); outbound != nil {
  56. src = outbound.Gateway
  57. }
  58. if transportLayerOutgoingTag := session.GetTransportLayerProxyTagFromContext(ctx); transportLayerOutgoingTag != "" {
  59. return DialTaggedOutbound(ctx, dest, transportLayerOutgoingTag)
  60. }
  61. return effectiveSystemDialer.Dial(ctx, src, dest, sockopt)
  62. }
  63. func DialTaggedOutbound(ctx context.Context, dest net.Destination, tag string) (net.Conn, error) {
  64. if tagged.Dialer == nil {
  65. return nil, newError("tagged dial not enabled")
  66. }
  67. return tagged.Dialer(ctx, dest, tag)
  68. }