-
Notifications
You must be signed in to change notification settings - Fork 11
/
tcp.go
201 lines (179 loc) · 4.29 KB
/
tcp.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package riemanngo
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/binary"
"io"
"io/ioutil"
"net"
"time"
"github.com/riemann/riemann-go-client/proto"
pb "google.golang.org/protobuf/proto"
"gopkg.in/tomb.v2"
)
// TCPClient is a type that implements the Client interface
type TCPClient struct {
tls bool
addr string
conn net.Conn
requestQueue chan request
timeout time.Duration
tlsConfig *tls.Config
t tomb.Tomb
}
// NewTLSClient - Factory
func NewTLSClient(addr string, tlsConfig *tls.Config, timeout time.Duration) (*TCPClient, error) {
t := &TCPClient{
tls: true,
addr: addr,
tlsConfig: tlsConfig,
requestQueue: make(chan request),
timeout: timeout,
}
return t, nil
}
// GetTLSConfig returns a *tls.Config
func GetTLSConfig(serverName string, certPath string, keyPath string, insecure bool) (*tls.Config, error) {
certFile, err := ioutil.ReadFile(certPath)
if err != nil {
return nil, err
}
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, err
}
clientCertPool := x509.NewCertPool()
clientCertPool.AppendCertsFromPEM(certFile)
config := tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: clientCertPool,
InsecureSkipVerify: insecure}
if !insecure {
config.ServerName = serverName
}
return &config, nil
}
// NewTCPClient - Factory
func NewTCPClient(addr string, timeout time.Duration) *TCPClient {
t := &TCPClient{
tls: false,
addr: addr,
requestQueue: make(chan request),
timeout: timeout,
}
return t
}
// Connect the tcp client
func (c *TCPClient) Connect() error {
c.t.Go(func() error {
return c.runRequestQueue()
})
connection, err := net.DialTimeout("tcp", c.addr, c.timeout)
if err != nil {
return err
}
if c.tls {
tlsConn := tls.Client(connection, c.tlsConfig)
err = tlsConn.Handshake()
if err != nil {
return err
}
c.conn = tlsConn
} else {
c.conn = connection
}
return nil
}
// Close will close the TCPClient
func (c *TCPClient) Close() error {
c.t.Kill(nil)
_ = c.t.Wait()
close(c.requestQueue)
err := c.conn.Close()
return err
}
// Send queues a request to send a message to the server
func (c *TCPClient) Send(message *proto.Msg) (*proto.Msg, error) {
responseCh := make(chan response)
c.requestQueue <- request{message, responseCh}
r := <-responseCh
return r.message, r.err
}
// runRequestQueue services the TCPClient request queue
func (c *TCPClient) runRequestQueue() error {
for {
select {
case <-c.t.Dying():
return nil
case req := <-c.requestQueue:
message := req.message
responseCh := req.responseCh
msg, err := c.execRequest(message)
responseCh <- response{msg, err}
}
}
}
// execRequest will send a TCP message to Riemann
func (c *TCPClient) execRequest(message *proto.Msg) (*proto.Msg, error) {
err := c.conn.SetDeadline(time.Now().Add(c.timeout))
if err != nil {
return nil, err
}
msg := &proto.Msg{}
data, err := pb.Marshal(message)
if err != nil {
return msg, err
}
b := new(bytes.Buffer)
if err = binary.Write(b, binary.BigEndian, uint32(len(data))); err != nil {
return msg, err
}
// send the msg length
if _, err = c.conn.Write(b.Bytes()); err != nil {
return msg, err
}
// send the msg
if _, err = c.conn.Write(data); err != nil {
return msg, err
}
var header uint32
if err = binary.Read(c.conn, binary.BigEndian, &header); err != nil {
return msg, err
}
response := make([]byte, header)
if err = readMessages(c.conn, response); err != nil {
return msg, err
}
if err = pb.Unmarshal(response, msg); err != nil {
return msg, err
}
return msg, nil
}
// readMessages will read Riemann messages from the TCP connection
func readMessages(r io.Reader, p []byte) error {
for len(p) > 0 {
n, err := r.Read(p)
p = p[n:]
if err != nil {
return err
}
}
return nil
}
// QueryIndex query the server for events using the client
func (c *TCPClient) QueryIndex(q string) ([]Event, error) {
err := c.conn.SetDeadline(time.Now().Add(c.timeout))
if err != nil {
return nil, err
}
query := &proto.Query{}
query.String_ = pb.String(q)
message := &proto.Msg{}
message.Query = query
response, err := c.Send(message)
if err != nil {
return nil, err
}
return ProtocolBuffersToEvents(response.GetEvents()), nil
}