-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
301 lines (247 loc) · 6.68 KB
/
client.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package carrot
import (
"bytes"
"github.com/DataDog/datadog-go/statsd"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"math"
"net/http"
"sync"
"time"
)
const (
writeWaitSeconds = 10 * 10
pongWaitSeconds = 10 * 60
// time allowed to write a message to the websocket
writeWait = writeWaitSeconds * time.Second
// time allowed to read the next pong message from the websocket
pongWait = pongWaitSeconds * time.Second
// send pings to the websocket with this period, must be less than pongWait
pingPeriod = (pongWait * 9) / 10
// maximum message size allowed from the websocket
maxMessageSize = 8192
// toggle to require a client secret token on WS upgrade request
clientSecretRequired = false
// size of client send channel
sendMsgBufferSize = 8192
// size of sendToken channel
sendTokenBufferSize = 1
sendMsgBufferWarningTrigger = 0.9
)
var (
newline = []byte{'\n'}
space = []byte{' '}
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
type Client struct {
session *Session
server *Server
// acts as a signal for when to start the go routines
start chan struct{}
open bool
conn *websocket.Conn
//buffered channel of outbound messages
send chan []byte
//send JSON detailing token, primary/secondary device status, etc
sendBeaconInfo chan []byte
openMutex *sync.RWMutex
logger *log.Entry
statsd *statsd.Client
}
func (c *Client) Open() bool {
c.openMutex.RLock()
status := c.open
c.openMutex.RUnlock()
return status
}
func (c *Client) softOpen() {
c.openMutex.Lock()
c.open = true
c.openMutex.Unlock()
}
func (c *Client) softClose() {
c.openMutex.Lock()
c.open = false
c.openMutex.Unlock()
}
func (c *Client) Expired() bool {
return !c.Open() && c.session.sessionDurationExpired()
}
func (c *Client) Full() bool {
// check for buffer full
if len(c.send) == sendMsgBufferSize {
return true
}
return false
}
func (c *Client) IsRecipient(recipientList []string) bool {
if InSlice(string(c.session.Token), recipientList) || len(recipientList) == 0 {
return true
}
return false
}
func (c *Client) Valid() bool {
// TODO: Specify criteria for what is a "valid" connection aside from existing
return !c.nil()
}
func (c *Client) nil() bool {
return c == nil
}
func (c *Client) checkBufferRedZone() bool {
// check for buffer warning
if len(c.send) > int(math.Floor(sendMsgBufferSize*sendMsgBufferWarningTrigger)) {
c.logger.WithFields(log.Fields{
"size": len(c.send),
"channel": "send"}).Error("input channel is 90% full!")
return true
}
return false
}
func (c *Client) checkBufferFull() bool {
if len(c.send) == sendMsgBufferSize {
c.logger.WithFields(log.Fields{
"size": len(c.send),
"channel": "send"}).Error("input channel is full!")
return true
}
return false
}
//readPump pumps messages from the websocket to the server
func (c *Client) readPump() {
defer func() {
c.server.unregister <- c
c.conn.Close()
}()
c.conn.SetReadLimit(maxMessageSize)
c.conn.SetReadDeadline(time.Now().Add(pongWait))
c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
for {
_, message, err := c.conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
log.Errorf("error: %v", err)
}
break
}
message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1))
req := NewRequest(c.session, message)
c.logger.WithField("session_token", c.session.Token).Debug("request being sent to middleware")
c.statsd.Incr("carrot.client.request_rate.total", nil, 100)
c.server.Middleware.In <- req
}
}
//writePump pumps messages from the hub to the websocket connection
func (c *Client) writePump() {
ticker := time.NewTicker(pingPeriod)
defer func() {
ticker.Stop()
c.conn.Close()
}()
for {
select {
case message, ok := <-c.send:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
if !ok {
c.softClose()
c.logger.Info("a connection has closed")
//the server closed the channel
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}
c.conn.WriteMessage(websocket.TextMessage, message)
/*
w, err := c.conn.NextWriter(websocket.TextMessage)
if err != nil {
return
}
w.Write(message)
// TODO: messages are having a \n in them, this is a problem
// add queued messages to the current websocket message
n := len(c.send)
for i := 0; i < n; i++ {
w.Write(newline)
w.Write(<-c.send)
}
if err := w.Close(); err != nil {
return
}
*/
case info, ok := <-c.sendBeaconInfo:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
if !ok {
//the server closed the channel
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}
w, err := c.conn.NextWriter(websocket.TextMessage)
if err != nil {
return
}
w.Write([]byte(info))
//add queued device handshake messages to the current websocket message
n := len(c.sendBeaconInfo)
for i := 0; i < n; i++ {
w.Write(newline)
w.Write([]byte(<-c.sendBeaconInfo))
}
if err := w.Close(); err != nil {
return
}
case <-ticker.C:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
if err := c.conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
return
}
}
}
}
// validate that the client should be allowed to connect
func validClientSecret(clientSecret string) bool {
if clientSecret != serverSecret {
log.WithField("attempted_secret", clientSecret).Error("client and server secrets do not match :(")
return false
}
return true
}
func serveWs(server *Server, w http.ResponseWriter, r *http.Request) {
_, clientSecret, _ := r.BasicAuth() //first underscore used to be sessionToken
//log.Printf("Session Token: %v | Client Secret: %v", SessionToken, clientSecret)
if clientSecretRequired && !validClientSecret(clientSecret) {
http.Error(w, "Not Authorized!", 403)
return
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
logger := log.WithField("module", "client")
c, err := statsd.New("127.0.0.1:8125")
if err != nil {
logger.Error(err)
}
client := &Client{
session: nil,
server: server,
conn: conn,
send: make(chan []byte, sendMsgBufferSize),
sendBeaconInfo: make(chan []byte, 1),
start: make(chan struct{}),
open: false,
openMutex: &sync.RWMutex{},
logger: logger,
statsd: c,
}
//client.sendToken <- SessionToken(sessionToken)
client.server.register <- client
func() {
// TODO: log session token used here
log.Debug("a new client has joined")
<-client.start
go client.writePump()
go client.readPump()
}()
}