dialer.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package internet
  2. import (
  3. "context"
  4. "github.com/v2fly/v2ray-core/v4/transport/internet/tagged"
  5. "github.com/v2fly/v2ray-core/v4/common/net"
  6. "github.com/v2fly/v2ray-core/v4/common/session"
  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 (
  18. transportDialerCache = make(map[string]dialFunc)
  19. )
  20. // RegisterTransportDialer registers a Dialer with given name.
  21. func RegisterTransportDialer(protocol string, dialer dialFunc) error {
  22. if _, found := transportDialerCache[protocol]; found {
  23. return newError(protocol, " dialer already registered").AtError()
  24. }
  25. transportDialerCache[protocol] = dialer
  26. return nil
  27. }
  28. // Dial dials a internet connection towards the given destination.
  29. func Dial(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error) {
  30. if dest.Network == net.Network_TCP {
  31. if streamSettings == nil {
  32. s, err := ToMemoryStreamConfig(nil)
  33. if err != nil {
  34. return nil, newError("failed to create default stream settings").Base(err)
  35. }
  36. streamSettings = s
  37. }
  38. protocol := streamSettings.ProtocolName
  39. dialer := transportDialerCache[protocol]
  40. if dialer == nil {
  41. return nil, newError(protocol, " dialer not registered").AtError()
  42. }
  43. return dialer(ctx, dest, streamSettings)
  44. }
  45. if dest.Network == net.Network_UDP {
  46. udpDialer := transportDialerCache["udp"]
  47. if udpDialer == nil {
  48. return nil, newError("UDP dialer not registered").AtError()
  49. }
  50. return udpDialer(ctx, dest, streamSettings)
  51. }
  52. return nil, newError("unknown network ", dest.Network)
  53. }
  54. // DialSystem calls system dialer to create a network connection.
  55. func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
  56. var src net.Address
  57. if outbound := session.OutboundFromContext(ctx); outbound != nil {
  58. src = outbound.Gateway
  59. }
  60. if transportLayerOutgoingTag := session.GetTransportLayerProxyTagFromContext(ctx); transportLayerOutgoingTag != "" {
  61. return DialTaggedOutbound(ctx, dest, transportLayerOutgoingTag)
  62. }
  63. return effectiveSystemDialer.Dial(ctx, src, dest, sockopt)
  64. }
  65. func DialTaggedOutbound(ctx context.Context, dest net.Destination, tag string) (net.Conn, error) {
  66. if tagged.Dialer == nil {
  67. return nil, newError("tagged dial not enabled")
  68. }
  69. return tagged.Dialer(ctx, dest, tag)
  70. }