-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlsdial.go
75 lines (61 loc) · 1.67 KB
/
tlsdial.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package utils
import (
"context"
"net"
"strings"
"crypto/tls"
"github.com/gravitational/trace"
)
// DialWithContext dials with context
type DialWithContextFunc func(ctx context.Context, network, addr string) (net.Conn, error)
// TLSDial dials and establishes TLS connection using custom dialer
// is similar to tls.DialWithDialer
func TLSDial(ctx context.Context, dial DialWithContextFunc, network, addr string, tlsConfig *tls.Config) (*tls.Conn, error) {
if tlsConfig == nil {
tlsConfig = &tls.Config{}
}
plainConn, err := dial(ctx, network, addr)
if err != nil {
return nil, trace.Wrap(err)
}
colonPos := strings.LastIndex(addr, ":")
if colonPos == -1 {
colonPos = len(addr)
}
hostname := addr[:colonPos]
// If no ServerName is set, infer the ServerName
// from the hostname we're connecting to.
if tlsConfig.ServerName == "" {
// Make a copy to avoid polluting argument or default.
c := tlsConfig.Clone()
c.ServerName = hostname
tlsConfig = c
}
conn := tls.Client(plainConn, tlsConfig)
errC := make(chan error, 1)
go func() {
err := conn.Handshake()
errC <- err
}()
select {
case err := <-errC:
if err != nil {
plainConn.Close()
return nil, trace.Wrap(err)
}
case <-ctx.Done():
plainConn.Close()
return nil, trace.BadParameter("tls handshake has been cancelled due to timeout")
}
if tlsConfig.InsecureSkipVerify {
return conn, nil
}
if err := conn.VerifyHostname(tlsConfig.ServerName); err != nil {
plainConn.Close()
return nil, trace.Wrap(err)
}
return conn, nil
}