dial.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package connection
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net"
  6. "time"
  7. "github.com/pkg/errors"
  8. )
  9. // DialEdge makes a TLS connection to a Cloudflare edge node
  10. func DialEdge(
  11. ctx context.Context,
  12. timeout time.Duration,
  13. tlsConfig *tls.Config,
  14. edgeTCPAddr *net.TCPAddr,
  15. ) (net.Conn, error) {
  16. // Inherit from parent context so we can cancel (Ctrl-C) while dialing
  17. dialCtx, dialCancel := context.WithTimeout(ctx, timeout)
  18. defer dialCancel()
  19. dialer := net.Dialer{}
  20. edgeConn, err := dialer.DialContext(dialCtx, "tcp", edgeTCPAddr.String())
  21. if err != nil {
  22. return nil, newDialError(err, "DialContext error")
  23. }
  24. tlsEdgeConn := tls.Client(edgeConn, tlsConfig)
  25. tlsEdgeConn.SetDeadline(time.Now().Add(timeout))
  26. if err = tlsEdgeConn.Handshake(); err != nil {
  27. return nil, newDialError(err, "TLS handshake with edge error")
  28. }
  29. // clear the deadline on the conn; h2mux has its own timeouts
  30. tlsEdgeConn.SetDeadline(time.Time{})
  31. return tlsEdgeConn, nil
  32. }
  33. // DialError is an error returned from DialEdge
  34. type DialError struct {
  35. cause error
  36. }
  37. func newDialError(err error, message string) error {
  38. return DialError{cause: errors.Wrap(err, message)}
  39. }
  40. func (e DialError) Error() string {
  41. return e.cause.Error()
  42. }
  43. func (e DialError) Cause() error {
  44. return e.cause
  45. }