forked from centrifugal/centrifugo
/
admin.go
214 lines (181 loc) · 4.64 KB
/
admin.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
package libcentrifugo
import (
"encoding/json"
"sync"
"github.com/FZambia/go-logger"
"github.com/gorilla/websocket"
"github.com/satori/go.uuid"
)
// use interface to mimic websocket connection write method we use here
type adminSession interface {
WriteMessage(int, []byte) error
}
// adminClient is a wrapper over admin websocket connection
type adminClient struct {
sync.RWMutex
app *Application
UID ConnID
sess adminSession
watch bool
authenticated bool
writeChan chan []byte
closeChan chan struct{}
}
func newAdminClient(app *Application, sess adminSession) (*adminClient, error) {
c := &adminClient{
UID: ConnID(uuid.NewV4().String()),
app: app,
sess: sess,
watch: false,
authenticated: false,
writeChan: make(chan []byte, 256),
closeChan: make(chan struct{}),
}
app.RLock()
insecure := app.config.InsecureAdmin
app.RUnlock()
if insecure {
err := app.addAdminConn(c)
if err != nil {
return nil, err
}
c.authenticated = true
}
return c, nil
}
func (c *adminClient) uid() ConnID {
return c.UID
}
func (c *adminClient) send(message []byte) error {
if !c.watch {
// At moment we only use this method to send asynchronous
// messages to admin client when new message published into channel with
// watch option enabled and admin watch option was set to true in connection
// params. If we introduce another types of asynchronous admin messages coming
// from admin hub broadcast then we will need to refactor this – it seems that
// we then need to know a type of message coming to decide what to do with it
// on broadcast level, for example 1-byte prefix meaning message type (what
// @banks actually suggested).
return nil
}
return c.write(message)
}
func (c *adminClient) write(message []byte) error {
// TODO: introduce queue here - similar to client's queue.
select {
case c.writeChan <- message:
default:
logger.ERROR.Println("can't write into admin ws connection write channel")
return ErrInternalServerError
}
return nil
}
// writer reads from buffered channel and sends received messages to admin.
func (c *adminClient) writer() {
for {
select {
case message := <-c.writeChan:
err := c.sess.WriteMessage(websocket.TextMessage, message)
if err != nil {
return
}
case <-c.closeChan:
return
}
}
}
// message handles message received from admin connection
func (c *adminClient) message(msg []byte) error {
commands, err := cmdFromRequestMsg(msg)
if err != nil {
logger.ERROR.Println(err)
return ErrInvalidMessage
}
if len(commands) == 0 {
return nil
}
var mr multiResponse
for _, command := range commands {
c.Lock()
if command.Method != "connect" && !c.authenticated {
c.Unlock()
return ErrUnauthorized
}
var resp *response
switch command.Method {
case "connect":
var cmd connectAdminCommand
err = json.Unmarshal(command.Params, &cmd)
if err != nil {
c.Unlock()
logger.ERROR.Println(err)
return ErrInvalidMessage
}
resp, err = c.connectCmd(&cmd)
case "ping":
resp, err = c.pingCmd()
case "info":
resp, err = c.infoCmd()
default:
resp, err = c.app.apiCmd(command)
}
if err != nil {
c.Unlock()
logger.ERROR.Println(err)
return err
}
c.Unlock()
resp.UID = command.UID
mr = append(mr, resp)
}
respBytes, err := json.Marshal(mr)
if err != nil {
logger.ERROR.Println(err)
return ErrInternalServerError
}
err = c.write(respBytes)
if err != nil {
return ErrInternalServerError
}
return nil
}
// connectCmd checks provided token and adds admin connection into application
// registry if token correct
func (c *adminClient) connectCmd(cmd *connectAdminCommand) (*response, error) {
err := c.app.checkAdminAuthToken(cmd.Token)
if err != nil {
return nil, ErrUnauthorized
}
err = c.app.addAdminConn(c)
if err != nil {
logger.ERROR.Println(err)
return nil, ErrInternalServerError
}
c.authenticated = true
if cmd.Watch {
c.watch = true
}
resp := newResponse("connect")
resp.Body = true
return resp, nil
}
// infoCmd handles info command from admin client.
// TODO: make this a proper API method with strictly defined response body.
func (c *adminClient) infoCmd() (*response, error) {
c.app.nodesMu.Lock()
defer c.app.nodesMu.Unlock()
c.app.RLock()
defer c.app.RUnlock()
resp := newResponse("info")
resp.Body = map[string]interface{}{
"engine": c.app.engine.name(),
"config": c.app.config,
}
return resp, nil
}
// pingCmd handles ping command from admin client.
func (c *adminClient) pingCmd() (*response, error) {
resp := newResponse("ping")
resp.Body = "pong"
return resp, nil
}