cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2021.10.5

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

edgediscovery/dial.go

55lines · modecode

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