forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport.go
142 lines (121 loc) · 3.19 KB
/
transport.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package logstash
import (
"crypto/tls"
"fmt"
"math/rand"
"net"
"strings"
"time"
"github.com/elastic/beats/libbeat/logp"
)
// TransportClient interfaces adds (re-)connect support to net.Conn.
type TransportClient interface {
net.Conn
Connect(timeout time.Duration) error
IsConnected() bool
}
type tcpClient struct {
hostport string
connected bool
net.Conn
}
type tlsClient struct {
tcpClient
tls tls.Config
}
func newTCPClient(host string, defaultPort int) (*tcpClient, error) {
return &tcpClient{hostport: fullAddress(host, defaultPort)}, nil
}
func (c *tcpClient) Connect(timeout time.Duration) error {
if c.IsConnected() {
_ = c.Close()
}
host, port, err := net.SplitHostPort(c.hostport)
if err != nil {
return err
}
// TODO: address lookup copied from logstash-forwarded. Really required?
addresses, err := net.LookupHost(host)
c.Conn = nil
if err != nil {
logp.Warn("DNS lookup failure \"%s\": %s", host, err)
return err
}
// connect to random address
// Use randomization on DNS reported addresses combined with timeout and ACKs
// to spread potential load when starting up large number of beats using
// lumberjack.
//
// RFCs discussing reasons for ignoring order of DNS records:
// http://www.ietf.org/rfc/rfc3484.txt
// > is specific to locality-based address selection for multiple dns
// > records, but exists as prior art in "Choose some different ordering for
// > the dns records" done by a client
//
// https://tools.ietf.org/html/rfc1794
// > "Clients, of course, may reorder this information" - with respect to
// > handling order of dns records in a response. address :=
address := addresses[rand.Int()%len(addresses)]
addressport := net.JoinHostPort(address, port)
conn, err := net.DialTimeout("tcp", addressport, timeout)
if err != nil {
return err
}
c.Conn = conn
c.connected = true
return nil
}
func (c *tcpClient) IsConnected() bool {
return c.connected
}
func (c *tcpClient) Close() error {
err := c.Conn.Close()
c.connected = false
return err
}
func newTLSClient(host string, defaultPort int, tls *tls.Config) (*tlsClient, error) {
c := tlsClient{}
c.hostport = fullAddress(host, defaultPort)
c.tls = *tls
return &c, nil
}
func (c *tlsClient) Connect(timeout time.Duration) error {
host, _, err := net.SplitHostPort(c.hostport)
if err != nil {
return err
}
if err := c.tcpClient.Connect(timeout); err != nil {
return c.onFail(err)
}
tlsconfig := c.tls
tlsconfig.ServerName = host
socket := tls.Client(c.Conn, &tlsconfig)
if err := socket.SetDeadline(time.Now().Add(timeout)); err != nil {
_ = socket.Close()
return c.onFail(err)
}
if err := socket.Handshake(); err != nil {
_ = socket.Close()
return c.onFail(err)
}
c.Conn = socket
c.connected = true
return nil
}
func (c *tlsClient) onFail(err error) error {
logp.Err("SSL client failed to connect with: %v", err)
c.Conn = nil
c.connected = false
return err
}
func fullAddress(host string, defaultPort int) string {
if _, _, err := net.SplitHostPort(host); err == nil {
return host
}
idx := strings.Index(host, ":")
if idx >= 0 {
// IPv6 address detected
return fmt.Sprintf("[%v]:%v", host, defaultPort)
}
return fmt.Sprintf("%v:%v", host, defaultPort)
}