forked from evolutek/cellaserv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcellaserv.go
325 lines (273 loc) · 8.22 KB
/
cellaserv.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"bitbucket.org/evolutek/cellaserv2-protobuf"
"github.com/golang/protobuf/proto"
"encoding/json"
"io/ioutil"
"net"
"os"
"path"
"path/filepath"
"strings"
)
const (
// This string will be replaced by the build system
csVersion = "git"
// Logs sent by cellaserv
logCloseConnection = "log.cellaserv.close-connection"
logConnRename = "log.cellaserv.connection-rename"
logLostService = "log.cellaserv.lost-service"
logLostSubscriber = "log.cellaserv.lost-subscriber"
logNewConnection = "log.cellaserv.new-connection"
logNewService = "log.cellaserv.new-service"
logNewSubscriber = "log.cellaserv.new-subscriber"
logNewLogSession = "log.cellaserv.new-log-session"
)
// Send conn data as this struct
type connNameJSON struct {
Addr string
Name string
}
/*
handleDescribeConn attaches a name to the connection that sent the request.
This information is normaly given when a service registers, but it can also be useful for other
clients.
*/
func handleDescribeConn(conn net.Conn, req *cellaserv.Request) {
var data struct {
Name string
}
if err := json.Unmarshal(req.Data, &data); err != nil {
log.Warning("[Cellaserv] Could not unmarshal describe-conn: %s, %s", req.Data, err)
sendReplyError(conn, req, cellaserv.Reply_Error_BadArguments)
return
}
connNameMap[conn] = data.Name
newName := connDescribe(conn)
pub_json, _ := json.Marshal(connNameJSON{conn.RemoteAddr().String(), newName})
cellaservPublish(logConnRename, pub_json)
log.Debug("[Cellaserv] Describe %s as %s", conn.RemoteAddr(), data.Name)
sendReply(conn, req, nil) // Empty reply
}
func handleListServices(conn net.Conn, req *cellaserv.Request) {
// Fix static empty slice that is "null" in JSON
// A dynamic empty slice is []
servicesList := make([]*ServiceJSON, 0)
for _, names := range services {
for _, s := range names {
servicesList = append(servicesList, s.JSONStruct())
}
}
data, err := json.Marshal(servicesList)
if err != nil {
log.Error("[Cellaserv] Could not marshal the services")
}
sendReply(conn, req, data)
}
// handleListConnections replies with the list of currently connected clients
func handleListConnections(conn net.Conn, req *cellaserv.Request) {
var conns []connNameJSON
for c := connList.Front(); c != nil; c = c.Next() {
connElt := c.Value.(net.Conn)
conns = append(conns,
connNameJSON{connElt.RemoteAddr().String(), connDescribe(connElt)})
}
data, err := json.Marshal(conns)
if err != nil {
log.Error("[Cellaserv] Could not marshal the connections list")
}
sendReply(conn, req, data)
}
// handleListEvents replies with the list of subscribers
func handleListEvents(conn net.Conn, req *cellaserv.Request) {
events := make(map[string][]string)
fillMap := func(subMap map[string][]net.Conn) {
for event, conns := range subMap {
var connSlice []string
for _, connItem := range conns {
connSlice = append(connSlice, connItem.RemoteAddr().String())
}
events[event] = connSlice
}
}
fillMap(subscriberMap)
fillMap(subscriberMatchMap)
data, err := json.Marshal(events)
if err != nil {
log.Error("[Cellaserv] Could not marshal the event list")
}
sendReply(conn, req, data)
}
/*
handleGetLogs replies with the content of log files.
Request format:
bytes
Examples:
cellaserv.new-connection
cellaserv.*
Reply format:
map[string]string
*/
func handleGetLogs(conn net.Conn, req *cellaserv.Request) {
if req.Data == nil {
log.Warning("[Cellaserv] Log request does not specify event")
sendReplyError(conn, req, cellaserv.Reply_Error_BadArguments)
return
}
event := string(req.Data)
pattern := path.Join(*logRootDirectory, logSubDir, event+".log")
if !strings.HasPrefix(pattern, path.Join(*logRootDirectory, logSubDir)) {
log.Warning("[Cellaserv] Don't try to do directory traversal")
sendReplyError(conn, req, cellaserv.Reply_Error_BadArguments)
return
}
// Globbing is allowed
filenames, err := filepath.Glob(pattern)
if err != nil {
log.Warning("[Cellaserv] Invalid log globbing : %s, %s", event, err)
sendReplyError(conn, req, cellaserv.Reply_Error_BadArguments)
return
}
if len(filenames) == 0 {
log.Warning("[Cellaserv] No such logs: %s", event)
sendReplyError(conn, req, cellaserv.Reply_Error_BadArguments)
return
}
logs := make(map[string]string)
for _, filename := range filenames {
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Warning("[Cellaserv] Could not open log: %s", filename)
sendReplyError(conn, req, cellaserv.Reply_Error_BadArguments)
return
}
logs[filename] = string(data)
}
logs_json, _ := json.Marshal(logs)
sendReply(conn, req, logs_json)
}
// handleLogRotate changes the current log environment
func handleLogRotate(conn net.Conn, req *cellaserv.Request) {
// Default to time
if req.Data == nil {
logRotateTimeNow()
} else {
var data struct {
Where string
}
err := json.Unmarshal(req.Data, &data)
if err != nil {
log.Warning("[Cellaserv] Could not rotate log, json error: %s", err)
sendReplyError(conn, req, cellaserv.Reply_Error_BadArguments)
return
}
logRotateName(data.Where)
}
sendReply(conn, req, nil)
}
// handleSession returns the current log sesion
func handleSession(conn net.Conn, req *cellaserv.Request) {
data, err := json.Marshal(logSubDir)
if err != nil {
log.Warning("[Cellaserv] Could not marshall log session, json error: %s", err)
sendReplyError(conn, req, cellaserv.Reply_Error_BadArguments)
return
}
sendReply(conn, req, data)
}
// handleShutdown quits cellaserv. Used for debug purposes
func handleShutdown() {
stopProfiling()
os.Exit(0)
}
// handleSpy registers the connection as a spy of a service
func handleSpy(conn net.Conn, req *cellaserv.Request) {
var data struct {
Service string
Identification string
}
err := json.Unmarshal(req.Data, &data)
if err != nil {
log.Warning("[Cellaserv] Could not spy, json error: %s", err)
sendReplyError(conn, req, cellaserv.Reply_Error_BadArguments)
return
}
srvc, ok := services[data.Service][data.Identification]
if !ok {
log.Warning("[Cellaserv] Could not spy, no such service: %s %s", data.Service,
data.Identification)
sendReplyError(conn, req, cellaserv.Reply_Error_BadArguments)
return
}
log.Debug("[Cellaserv] %s spies on %s/%s", connDescribe(conn), data.Service,
data.Identification)
srvc.Spies = append(srvc.Spies, conn)
connSpies[conn] = append(connSpies[conn], srvc)
sendReply(conn, req, nil)
}
// handleVersion return the version of cellaserv
func handleVersion(conn net.Conn, req *cellaserv.Request) {
data, err := json.Marshal(csVersion)
if err != nil {
log.Warning("[Cellaserv] Could not marshall version, json error: %s", err)
sendReplyError(conn, req, cellaserv.Reply_Error_BadArguments)
return
}
sendReply(conn, req, data)
}
func cellaservRequest(conn net.Conn, req *cellaserv.Request) {
switch *req.Method {
case "describe-conn", "describe_conn":
handleDescribeConn(conn, req)
case "get-logs", "get_logs":
handleGetLogs(conn, req)
case "list-connections", "list_connections":
handleListConnections(conn, req)
case "list-events", "list_events":
handleListEvents(conn, req)
case "list-services", "list_services":
handleListServices(conn, req)
case "log-rotate", "log_rotate":
handleLogRotate(conn, req)
case "session":
handleSession(conn, req)
case "shutdown":
handleShutdown()
case "spy":
handleSpy(conn, req)
case "version":
handleVersion(conn, req)
default:
sendReplyError(conn, req, cellaserv.Reply_Error_NoSuchMethod)
}
}
// cellaservLog logs a publish message to a file
func cellaservLog(pub *cellaserv.Publish) {
var data string
if pub.Data != nil {
data = string(pub.Data)
}
event := (*pub.Event)[4:] // Strip 'log.'
logEvent(event, data)
}
// cellaservPublish sends a publish message from cellaserv
func cellaservPublish(event string, data []byte) {
pub := &cellaserv.Publish{Event: &event}
if data != nil {
pub.Data = data
}
pubBytes, err := proto.Marshal(pub)
if err != nil {
log.Error("[Cellaserv] Could not marshal event")
return
}
msgType := cellaserv.Message_Publish
msg := &cellaserv.Message{Type: &msgType, Content: pubBytes}
msgBytes, err := proto.Marshal(msg)
if err != nil {
log.Error("[Cellaserv] Could not marshal event")
return
}
doPublish(msgBytes, pub)
}
// vim: set nowrap tw=100 noet sw=8: