dialer.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. conn = tls.Client(conn, config.GetTLSConfig(tls.WithDestination(dest), tls.WithNextProto("h2")))
  20. }
  21. tcpSettings := streamSettings.ProtocolSettings.(*Config)
  22. if tcpSettings.HeaderSettings != nil {
  23. headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
  24. if err != nil {
  25. return nil, newError("failed to get header settings").Base(err).AtError()
  26. }
  27. auth, err := internet.CreateConnectionAuthenticator(headerConfig)
  28. if err != nil {
  29. return nil, newError("failed to create header authenticator").Base(err).AtError()
  30. }
  31. conn = auth.Client(conn)
  32. }
  33. return internet.Connection(conn), nil
  34. }
  35. func init() {
  36. common.Must(internet.RegisterTransportDialer(protocolName, Dial))
  37. }