/
routingSockets.go
218 lines (198 loc) · 5.81 KB
/
routingSockets.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
/*************************************************************************
* Copyright 2021 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package client
import (
"errors"
"fmt"
"github.com/gravwell/gravwell/v3/client/websocketRouter"
)
const (
// Websocket subprotocols
PROTO_PING string = "ping"
PROTO_IDX string = "idxStats"
PROTO_SYS string = "sysStats"
PROTO_DESC string = "sysDesc"
PROTO_IGST string = "igstStats"
PROTO_PONG string = `PONG`
PROTO_PARSE string = `parse`
PROTO_SEARCH string = `search`
PROTO_ATTACH string = `attach`
STAT_R_SIZE = 1024
STAT_W_SIZE = 1024
jwtWebsocketHeader string = `Sec-Websocket-Protocol`
)
type sockParams struct {
subProtos []string
negSubProtos []string
uri string
}
func (c *Client) websocketHeaderMap() (m map[string]string) {
m = c.hm.dump()
m[jwtWebsocketHeader] = c.sessionData.JWT
return
}
func (c *Client) getSockets(p sockParams) ([]*websocketRouter.SubProtoConn, *websocketRouter.SubProtoClient, error) {
c.mtx.Lock()
defer c.mtx.Unlock()
if c.state != STATE_AUTHED {
return nil, nil, ErrNoLogin
}
spc, err := websocketRouter.NewSubProtoClient(p.uri, c.websocketHeaderMap(), STAT_R_SIZE, STAT_W_SIZE, c.enforceCert, p.negSubProtos, c.objLog)
if err != nil {
return nil, nil, err
}
//kill off all but the subproto we want
subs, err := spc.SubProtocols()
if err != nil {
spc.Close()
return nil, nil, err
}
for i := range subs {
killSub := true
//check if this sub is in the list we want
for j := range p.subProtos {
if subs[i] == p.subProtos[j] {
killSub = false
break
}
}
if !killSub {
continue
}
//wasn't in the list, delete it
if err = spc.CloseSubProtoConn(subs[i]); err != nil {
spc.Close()
return nil, nil, err
}
}
err = spc.Start()
if err != nil {
spc.Close()
return nil, nil, err
}
var retSubProtoConns []*websocketRouter.SubProtoConn
for i := range p.subProtos {
subProtoConn, err := spc.GetSubProtoConn(p.subProtos[i])
if err != nil {
spc.Close()
return nil, nil, err
}
if c.timeout > 0 {
if err := subProtoConn.SetTimeout(c.timeout); err != nil {
return nil, nil, err
}
}
retSubProtoConns = append(retSubProtoConns, subProtoConn)
}
return retSubProtoConns, spc, nil
}
// GetStatSocket will connect to the websocketRouter and get the subProto client for
// the stats socket only.
func (c *Client) GetStatSocket(subProto string) (*websocketRouter.SubProtoConn, *websocketRouter.SubProtoClient, error) {
subs := []string{PROTO_PING, PROTO_IDX, PROTO_SYS, PROTO_DESC} //PROTO_IGST is optional for the websocket
//ensure the subprotocol being requested is provided by the server
switch subProto {
case PROTO_PING:
case PROTO_IDX:
case PROTO_IGST: //because its optional, we only add it if we request it
subs = append(subs, PROTO_IGST)
case PROTO_SYS:
default:
return nil, nil, errors.New("Invalid subprotocol")
}
//get our param list rolling
params := sockParams{
subProtos: []string{subProto},
negSubProtos: subs,
uri: fmt.Sprintf("%s://%s%s", c.wsScheme, c.serverURL.Host, WS_STAT_URL),
}
s, p, err := c.getSockets(params)
if err != nil {
return nil, nil, err
}
if c.timeout > 0 {
if err := s[0].SetTimeout(c.timeout); err != nil {
return nil, nil, err
}
}
return s[0], p, nil
}
// SearchSockets wraps up several different websocket subprotocols. Depending on the
// function used to obtain the SearchSockets object, not all subprotocols may be
// populated--refer to the individual function's documentation.
type SearchSockets struct {
Parse *websocketRouter.SubProtoConn
Search *websocketRouter.SubProtoConn
Attach *websocketRouter.SubProtoConn
Pong *websocketRouter.SubProtoConn
Client *websocketRouter.SubProtoClient
}
// GetSearchSockets will hit the search routing websocket page and pull back
// the parse, search, and attach subprotocols.
func (c *Client) GetSearchSockets() (*SearchSockets, error) {
subs := []string{PROTO_PONG, PROTO_PARSE, PROTO_SEARCH, PROTO_ATTACH}
//get our param list rolling
params := sockParams{
subProtos: []string{PROTO_PARSE, PROTO_SEARCH, PROTO_ATTACH, PROTO_PONG},
negSubProtos: subs,
uri: fmt.Sprintf("%s://%s%s", c.wsScheme, c.serverURL.Host, WS_SEARCH_URL),
}
subConns, p, err := c.getSockets(params)
if err != nil {
return nil, err
}
if len(subConns) != len(params.subProtos) {
return nil, errors.New("Invalid response protocol count")
}
if c.timeout > 0 {
for i := range subConns {
if err := subConns[i].SetTimeout(c.timeout); err != nil {
return nil, err
}
}
}
//the subs come back in the same order we asked for them
return &SearchSockets{
Parse: subConns[0],
Search: subConns[1],
Attach: subConns[2],
Pong: subConns[3],
Client: p,
}, nil
}
// GetAttachSockets will hit the search routing websocket page and pull back only the attach
// socket.
func (c *Client) GetAttachSockets() (*SearchSockets, error) {
subs := []string{PROTO_ATTACH, PROTO_PONG}
//get our param list rolling
params := sockParams{
subProtos: subs,
negSubProtos: subs,
uri: fmt.Sprintf("%s://%s%s", c.wsScheme, c.serverURL.Host, WS_SEARCH_URL),
}
subConns, p, err := c.getSockets(params)
if err != nil {
return nil, err
}
if len(subConns) != len(params.subProtos) {
return nil, errors.New("Invalid response protocol count")
}
if c.timeout > 0 {
for i := range subConns {
if err := subConns[i].SetTimeout(c.timeout); err != nil {
return nil, err
}
}
}
//the subs come back in the same order we asked for them
return &SearchSockets{
Attach: subConns[0],
Pong: subConns[1],
Client: p,
}, nil
}