dialer.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // +build !confonly
  2. package tcp
  3. import (
  4. "context"
  5. "v2ray.com/core/common"
  6. "v2ray.com/core/common/net"
  7. "v2ray.com/core/common/session"
  8. "v2ray.com/core/transport/internet"
  9. "v2ray.com/core/transport/internet/tls"
  10. )
  11. // Dial dials a new TCP connection to the given destination.
  12. func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
  13. newError("dialing TCP to ", dest).WriteToLog(session.ExportIDToError(ctx))
  14. conn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
  15. if err != nil {
  16. return nil, err
  17. }
  18. if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
  19. tlsConfig := config.GetTLSConfig(tls.WithDestination(dest))
  20. if config.IsExperiment8357() {
  21. conn = tls.UClient(conn, tlsConfig)
  22. } else {
  23. conn = tls.Client(conn, tlsConfig)
  24. }
  25. }
  26. tcpSettings := streamSettings.ProtocolSettings.(*Config)
  27. if tcpSettings.HeaderSettings != nil {
  28. headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
  29. if err != nil {
  30. return nil, newError("failed to get header settings").Base(err).AtError()
  31. }
  32. auth, err := internet.CreateConnectionAuthenticator(headerConfig)
  33. if err != nil {
  34. return nil, newError("failed to create header authenticator").Base(err).AtError()
  35. }
  36. conn = auth.Client(conn)
  37. }
  38. return internet.Connection(conn), nil
  39. }
  40. func init() {
  41. common.Must(internet.RegisterTransportDialer(protocolName, Dial))
  42. }