-
Notifications
You must be signed in to change notification settings - Fork 0
/
dial.go
97 lines (87 loc) · 1.97 KB
/
dial.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package fleet
import (
"context"
"crypto/tls"
"errors"
"net"
"strconv"
"strings"
"time"
)
func formatAltAddrs(host string, addrs []string, port int) []string {
// transform CIDR addrs into addr:port
res := make([]string, 0, len(addrs)+1)
addrs = append(addrs, "!"+host) // initial ! means we need to wait a bit before connecting
for _, a := range addrs {
pos := strings.LastIndexByte(a, '/')
if pos != -1 {
a = a[:pos]
}
a += ":" + strconv.FormatInt(int64(port), 10)
res = append(res, a)
}
return res
}
// tlsDialAll will dial all the provided hosts at the same time, and return the
// the first one that was successful from the list. If everything fails then
// the last error will be returned, unless everything timed out, in which case
// a timeout error will be returned.
func tlsDialAll(ctx context.Context, timeout time.Duration, addr []string, cfg *tls.Config) (*tls.Conn, error) {
if len(addr) == 0 {
return nil, errors.New("no target to connect to")
}
// perform dial on as many addrs as passed and return the first one to connect
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
ctx, cancel = context.WithCancel(ctx)
defer cancel()
var d net.Dialer
ch := make(chan *tls.Conn)
che := make(chan error)
for _, a := range addr {
go func(a string) {
if len(a) > 0 && a[0] == '!' {
a = a[1:]
time.Sleep(time.Second) // sleep 1s
}
c, err := d.DialContext(ctx, "tcp", a)
if err != nil {
select {
case che <- err:
case <-ctx.Done():
}
return
}
tc := tls.Client(c, cfg)
err = tc.Handshake()
if err != nil {
c.Close()
select {
case che <- err:
case <-ctx.Done():
}
return
}
select {
case ch <- tc:
case <-ctx.Done():
tc.Close()
}
}(a)
}
var err error
for {
select {
case c := <-ch:
// got a connection!
return c, nil
case e := <-che:
err = e
case <-ctx.Done():
if err != nil {
return nil, err
}
return nil, ctx.Err()
}
}
}