mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-05-24 07:56:34 +00:00

Some more legacy h2mux code to be cleaned up and moved out of the way. The h2mux.Header used in the serialization for http2 proxied headers is moved to connection module. Additionally, the booleanfuse structure is also moved to supervisor as it is also needed. Both of these structures could be evaluated later for removal/updates, however, the intent of the proposed changes here is to remove the dependencies on the h2mux code and removal. Approved-by: Chung-Ting Huang <chungting@cloudflare.com> Approved-by: Luis Neto <lneto@cloudflare.com> Approved-by: Gonçalo Garcia <ggarcia@cloudflare.com> MR: https://gitlab.cfdata.org/cloudflare/tun/cloudflared/-/merge_requests/1576
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package edgediscovery
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// DialEdge makes a TLS connection to a Cloudflare edge node
|
|
func DialEdge(
|
|
ctx context.Context,
|
|
timeout time.Duration,
|
|
tlsConfig *tls.Config,
|
|
edgeTCPAddr *net.TCPAddr,
|
|
localIP net.IP,
|
|
) (net.Conn, error) {
|
|
// Inherit from parent context so we can cancel (Ctrl-C) while dialing
|
|
dialCtx, dialCancel := context.WithTimeout(ctx, timeout)
|
|
defer dialCancel()
|
|
|
|
dialer := net.Dialer{}
|
|
if localIP != nil {
|
|
dialer.LocalAddr = &net.TCPAddr{IP: localIP, Port: 0}
|
|
}
|
|
edgeConn, err := dialer.DialContext(dialCtx, "tcp", edgeTCPAddr.String())
|
|
if err != nil {
|
|
return nil, newDialError(err, "DialContext error")
|
|
}
|
|
|
|
tlsEdgeConn := tls.Client(edgeConn, tlsConfig)
|
|
tlsEdgeConn.SetDeadline(time.Now().Add(timeout))
|
|
|
|
if err = tlsEdgeConn.Handshake(); err != nil {
|
|
return nil, newDialError(err, "TLS handshake with edge error")
|
|
}
|
|
// clear the deadline on the conn; http2 has its own timeouts
|
|
tlsEdgeConn.SetDeadline(time.Time{})
|
|
return tlsEdgeConn, nil
|
|
}
|
|
|
|
// DialError is an error returned from DialEdge
|
|
type DialError struct {
|
|
cause error
|
|
}
|
|
|
|
func newDialError(err error, message string) error {
|
|
return DialError{cause: errors.Wrap(err, message)}
|
|
}
|
|
|
|
func (e DialError) Error() string {
|
|
return e.cause.Error()
|
|
}
|
|
|
|
func (e DialError) Cause() error {
|
|
return e.cause
|
|
}
|