-
Notifications
You must be signed in to change notification settings - Fork 136
/
rpc.go
445 lines (399 loc) · 10.5 KB
/
rpc.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package main
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"os"
"regexp"
"strings"
"sync"
"time"
"github.com/acarl005/stripansi"
pb "devzat/plugin"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
var (
PluginCMDs = map[string]PluginCMD{}
RPCCMDs = []CMD{
{"plugins", pluginsCMD, "", "List plugin commands"},
}
RPCCMDsRest = []CMD{
{"lstokens", lsTokensCMD, "", "List plugin token hashes and their data (admin)"},
{"revoke", revokeTokenCMD, "<token hash>", "Revoke a plugin token (admin)"},
{"grant", grantTokenCMD, "[user] [data]", "Grant a token and optionally send it to a user (admin)"},
}
ListenersNonMiddleware = make([]chan pb.MiddlewareChannelMessage, 0, 4)
ListenersMiddleware = make([]chan pb.MiddlewareChannelMessage, 0, 4)
Tokens = make(map[string]string, 10)
)
type pluginServer struct {
pb.UnimplementedPluginServer
lock sync.Mutex
}
func (s *pluginServer) RegisterListener(stream pb.Plugin_RegisterListenerServer) error {
s.lock.Lock()
Log.Println("[gRPC] Registering event listener")
initialData, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
listener := initialData.GetListener()
if listener == nil {
return status.Error(codes.InvalidArgument, "First message must be a listener")
}
isMiddleware := listener.Middleware != nil && *listener.Middleware
isOnce := listener.Once != nil && *listener.Once
var regex *regexp.Regexp
if listener.Regex != nil {
regex, err = regexp.Compile(*listener.Regex)
if err != nil {
return status.Error(codes.InvalidArgument, "Invalid regex")
}
}
var listenerList *[]chan pb.MiddlewareChannelMessage
if isMiddleware {
listenerList = &ListenersMiddleware
} else {
listenerList = &ListenersNonMiddleware
}
c := make(chan pb.MiddlewareChannelMessage)
*listenerList = append(*listenerList, c)
s.lock.Unlock()
defer func() {
// Remove the channel from the list where the channel is equal to c
for i := range *listenerList {
if (*listenerList)[i] == c {
*listenerList = append((*listenerList)[:i], (*listenerList)[i+1:]...)
break
}
}
}()
for {
message := <-c
// If something goes wrong, make sure the goroutine sending the message doesn't block on waiting for a response
sendNilResponse := func() {
c <- &pb.ListenerClientData_Response{
Response: &pb.MiddlewareResponse{
Msg: nil,
},
}
}
// If there's a regex and it doesn't match, don't send the message to the plugin
if listener.Regex != nil && !regex.MatchString(message.(*pb.Event).Msg) {
if isMiddleware {
sendNilResponse()
}
continue
}
err = stream.Send(message.(*pb.Event))
if err != nil {
if isMiddleware {
sendNilResponse()
}
return err
}
if isMiddleware {
mwRes, err := stream.Recv()
if err != nil {
sendNilResponse()
return err
}
switch data := mwRes.Data.(type) {
case *pb.ListenerClientData_Listener:
sendNilResponse()
return status.Error(codes.InvalidArgument, "Middleware returned a listener instead of a response")
case *pb.ListenerClientData_Response:
c <- data
}
}
if isOnce {
break
}
}
return nil
}
type PluginCMD struct {
argsInfo string
info string
invocationChan chan *pb.CmdInvocation
}
func (s *pluginServer) RegisterCmd(def *pb.CmdDef, stream pb.Plugin_RegisterCmdServer) error {
s.lock.Lock()
Log.Print("[gRPC] Registering command with name " + def.Name)
PluginCMDs[def.Name] = PluginCMD{
argsInfo: def.ArgsInfo,
info: def.Info,
invocationChan: make(chan *pb.CmdInvocation),
}
s.lock.Unlock()
defer delete(PluginCMDs, def.Name)
for {
if err := stream.Send(<-PluginCMDs[def.Name].invocationChan); err != nil {
return err
}
}
}
func (s *pluginServer) SendMessage(ctx context.Context, msg *pb.Message) (*pb.MessageRes, error) {
s.lock.Lock()
defer s.lock.Unlock()
if msg.GetEphemeralTo() != "" {
u, success := findUserByName(Rooms[msg.Room], *msg.EphemeralTo)
if !success {
return nil, status.Error(codes.NotFound, "Could not find user "+*msg.EphemeralTo)
}
u.writeln(msg.GetFrom()+" -> ", msg.Msg)
} else {
r := Rooms[msg.Room]
if r == nil {
return nil, status.Error(codes.InvalidArgument, "Room does not exist")
}
r.broadcast(msg.GetFrom(), msg.Msg)
}
return &pb.MessageRes{}, nil
}
func authorize(ctx context.Context) error {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return status.Error(codes.Unauthenticated, "Missing metadata")
}
values := md["authorization"]
if len(values) == 0 {
return status.Error(codes.Unauthenticated, "Missing authorization header")
}
token := strings.TrimPrefix(values[0], "Bearer ")
if Integrations.RPC.Key != "" && token == Integrations.RPC.Key {
return nil
}
if _, ok = Tokens[token]; !ok {
return status.Error(codes.Unauthenticated, "Invalid authorization header")
}
return nil
}
func rpcInit() {
if Integrations.RPC == nil {
return
}
MainCMDs = append(MainCMDs, RPCCMDs...)
RestCMDs = append(RestCMDs, RPCCMDsRest...)
initTokens()
go func() {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", Integrations.RPC.Port))
if err != nil {
Log.Println("[gRPC] Failed to listen for plugin server:", err)
return
}
// TODO: add TLS if configured
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if err := authorize(ctx); err != nil {
return nil, err
}
return handler(ctx, req)
}),
grpc.StreamInterceptor(func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if err := authorize(stream.Context()); err != nil {
return err
}
return handler(srv, stream)
}),
grpc.KeepaliveParams(keepalive.ServerParameters{Time: time.Second * 10}),
)
pb.RegisterPluginServer(grpcServer, &pluginServer{})
Log.Printf("[gRPC] Plugin server started on port %d\n", Integrations.RPC.Port)
if err = grpcServer.Serve(lis); err != nil {
Log.Println("[gRPC] Failed to serve:", err)
}
}()
}
func runPluginCMDs(u *User, currCmd string, args string) (found bool) {
if pluginCmd, ok := PluginCMDs[currCmd]; ok {
pluginCmd.invocationChan <- &pb.CmdInvocation{
Room: u.room.name,
From: stripansi.Strip(u.Name),
Args: args,
}
return true
}
return false
}
// Hook that is called when a user sends a message (not private DMs)
func sendMessageToPlugins(line string, u *User) {
if len(ListenersNonMiddleware) > 0 {
for _, l := range ListenersNonMiddleware {
l <- &pb.Event{
Room: u.room.name,
From: stripansi.Strip(u.Name),
Msg: line,
}
}
}
}
var middlewareLock = new(sync.Mutex)
func getMiddlewareResult(u *User, line string) string {
if Integrations.RPC == nil {
return line
}
middlewareLock.Lock()
defer middlewareLock.Unlock()
// Middleware hook
for i := 0; i < len(ListenersMiddleware); i++ {
ListenersMiddleware[i] <- &pb.Event{
Room: u.room.name,
From: stripansi.Strip(u.Name),
Msg: line,
}
if res := (<-ListenersMiddleware[i]).(*pb.ListenerClientData_Response).Response.Msg; res != nil {
line = *res
}
}
return line
}
func pluginsCMD(_ string, u *User) {
plugins := make([]CMD, 0, len(PluginCMDs))
for n, c := range PluginCMDs {
plugins = append(plugins, CMD{
name: n,
info: c.info,
argsInfo: c.argsInfo,
})
}
autogenerated := autogenCommands(plugins)
if autogenerated == "" {
autogenerated = " (no plugin commands are loaded)"
}
u.room.broadcast("", "Plugin commands \n"+autogenerated)
}
func initTokens() {
f, err := os.Open(Config.DataDir + string(os.PathSeparator) + "tokens.json")
if err != nil {
if !os.IsNotExist(err) {
Log.Println("Error reading tokens file:", err)
}
return
}
defer f.Close()
j, err := io.ReadAll(f)
if err != nil {
Log.Println("Error reading tokens file:", err)
return
}
err = json.Unmarshal(j, &Tokens)
if err != nil {
var s []struct { // old format
Token string `json:"token"`
Data string `json:"data"`
}
err = json.Unmarshal(j, &s)
if err != nil {
Log.Println("Error decoding tokens file:", err)
return
}
Log.Println("Changing token file format")
for i := range s {
Tokens[s[i].Token] = s[i].Data
}
f.Close()
saveTokens()
}
}
func saveTokens() {
f, err := os.Create(Config.DataDir + string(os.PathSeparator) + "tokens.json")
if err != nil {
Log.Println(err)
}
defer f.Close()
data, err := json.Marshal(Tokens)
if err != nil {
Log.Println("Error encoding tokens file:", err)
}
_, err = f.Write(data)
if err != nil {
Log.Println("Error writing tokens file:", err)
}
}
func lsTokensCMD(_ string, u *User) {
if !auth(u) {
u.room.broadcast(Devbot, "Not authorized")
return
}
if len(Tokens) == 0 {
u.room.broadcast(Devbot, "No tokens found.")
return
}
msg := "Tokens: \n"
fmtString := "%" + fmt.Sprint(len(fmt.Sprint(len(Tokens)))) + "d"
i := 1
for t := range Tokens {
msg += Cyan.Cyan(fmt.Sprintf(fmtString, i)) + ". " + shasum(t) + "\t" + Tokens[t] + " \n"
i++
}
u.writeln(Devbot, msg)
}
func revokeTokenCMD(rest string, u *User) {
if !auth(u) {
u.room.broadcast(Devbot, "Not authorized")
return
}
if len(rest) == 0 {
u.room.broadcast(Devbot, "Please provide a sha256 hash of a token to revoke.")
return
}
for token := range Tokens {
if shasum(token) == rest {
delete(Tokens, token)
saveTokens()
u.room.broadcast(Devbot, "Token revoked!")
return
}
}
u.room.broadcast(Devbot, "Token not found.")
}
func grantTokenCMD(rest string, u *User) {
if !auth(u) {
u.room.broadcast(Devbot, "Not authorized")
return
}
token, err := generateToken()
if err != nil {
u.room.broadcast(Devbot, "Error generating token: "+err.Error())
Log.Println(err)
return
}
split := strings.Fields(rest)
if len(split) > 0 && len(split[0]) > 0 && split[0][0] == '@' {
toUser, ok := findUserByName(u.room, split[0][1:])
if ok {
toUser.writeln(Devbot, "You have been granted a token: "+token)
} else {
u.room.broadcast(Devbot, "Who's that?")
return
}
}
Tokens[token] = rest
u.writeln(Devbot, "Granted token: "+token)
saveTokens()
}
func generateToken() (string, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
return "", err
}
token := "dvz@" + base64.StdEncoding.EncodeToString(b)
// check if it's already in use
if _, ok := Tokens[token]; ok {
return generateToken()
}
return token, nil
}