DialEdge makes a TLS connection to a Cloudflare edge node
( ctx context.Context, timeout time.Duration, tlsConfig *tls.Config, edgeTCPAddr *net.TCPAddr, localIP net.IP, )
| 11 | |
| 12 | // DialEdge makes a TLS connection to a Cloudflare edge node |
| 13 | func DialEdge( |
| 14 | ctx context.Context, |
| 15 | timeout time.Duration, |
| 16 | tlsConfig *tls.Config, |
| 17 | edgeTCPAddr *net.TCPAddr, |
| 18 | localIP net.IP, |
| 19 | ) (net.Conn, error) { |
| 20 | // Inherit from parent context so we can cancel (Ctrl-C) while dialing |
| 21 | dialCtx, dialCancel := context.WithTimeout(ctx, timeout) |
| 22 | defer dialCancel() |
| 23 | |
| 24 | dialer := net.Dialer{} |
| 25 | if localIP != nil { |
| 26 | dialer.LocalAddr = &net.TCPAddr{IP: localIP, Port: 0} |
| 27 | } |
| 28 | edgeConn, err := dialer.DialContext(dialCtx, "tcp", edgeTCPAddr.String()) |
| 29 | if err != nil { |
| 30 | return nil, newDialError(err, "DialContext error") |
| 31 | } |
| 32 | |
| 33 | tlsEdgeConn := tls.Client(edgeConn, tlsConfig) |
| 34 | tlsEdgeConn.SetDeadline(time.Now().Add(timeout)) |
| 35 | |
| 36 | if err = tlsEdgeConn.Handshake(); err != nil { |
| 37 | return nil, newDialError(err, "TLS handshake with edge error") |
| 38 | } |
| 39 | // clear the deadline on the conn; http2 has its own timeouts |
| 40 | tlsEdgeConn.SetDeadline(time.Time{}) |
| 41 | return tlsEdgeConn, nil |
| 42 | } |
| 43 | |
| 44 | // DialError is an error returned from DialEdge |
| 45 | type DialError struct { |
no test coverage detected