-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.socketio.go
585 lines (506 loc) · 14.8 KB
/
handler.socketio.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// +build plugin_socketio
package main
import (
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"strings"
"sync/atomic"
"time"
requ "plugins/request"
sktio "github.com/ambelovsky/gosf-socketio"
"github.com/ambelovsky/gosf-socketio/transport"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
ctyconvert "github.com/zclconf/go-cty/cty/convert"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/function/stdlib"
ctyjson "github.com/zclconf/go-cty/cty/json"
)
const socketioPluginName = "socketio"
func init() {
log.Println("[init] loading the socket.io plugin ...")
plugins[socketioPluginName] = new(socketioPlugin)
}
type socketioPlugin struct {
conn map[string]*sktio.Server
config map[string]socketioConfig
}
type socketioConfig struct {
Name string `hcl:"name,label"`
UUID string `hcl:"id,optional"`
}
type socketio struct {
Name string `hcl:"name,label"`
Event string `hcl:"event,label"`
Desc string `hcl:"_-,optional"`
Broadcast []struct {
Room string `hcl:"room,label"`
Event string `hcl:"event,label"`
Args hcl.Body `hcl:",remain"`
} `hcl:"broadcast,block"`
BroadcastAll []struct {
Event string `hcl:"event,label"`
Args hcl.Body `hcl:",remain"`
} `hcl:"broadcast_all,block"`
Emit []struct {
Event string `hcl:"event,label"`
Args hcl.Body `hcl:",remain"`
} `hcl:"emit,block"`
}
var cvt = new(converter)
var convertToJSON = func(body hcl.Body) map[string]interface{} {
jsonData, err := cvt.convertBody(body.(*hclsyntax.Body))
if log.OnErr(err).Printf("[socketio] failed json body convert: %v", err).HasErr() {
return nil
}
return jsonData
}
func (p *socketioPlugin) Setup() error {
log.Println("[socketio] setup plugin ...")
p.conn = make(map[string]*sktio.Server)
p.config = make(map[string]socketioConfig)
return nil
}
// Version takes in the max version and returns the version
// that this module supports
func (p *socketioPlugin) Version(int32) int32 { return 1 }
// Metadata returns the metadata of the plugin
func (p *socketioPlugin) Metadata() string {
return `
metadata {
version = "0.1.0"
author = "Nika Jones"
copyright = "Nika Jones - © 2021"
}
`
}
func (p *socketioPlugin) SetupConfig(svrName string, svrPlugins hcl.Body) error {
svrb, _, _ := svrPlugins.PartialContent(&hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: socketioPluginName,
LabelNames: []string{"name"},
},
},
})
if len(svrb.Blocks) == 0 {
return nil
}
for _, block := range svrb.Blocks {
var sioc socketioConfig
switch block.Type {
case socketioPluginName:
gohcl.DecodeBody(block.Body, nil, &sioc)
if len(block.Labels) > 0 {
sioc.Name = block.Labels[0] // the same index as the LabelNames above...
}
p.config[svrName] = sioc
}
}
p.conn[svrName] = sktio.NewServer(transport.GetDefaultWebsocketTransport())
return nil
}
func (p *socketioPlugin) SetupRoot(configPlugins hcl.Body) error {
cfgb, _, _ := configPlugins.PartialContent(&hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: socketioPluginName,
LabelNames: []string{"name"},
},
},
})
for _, block := range cfgb.Blocks {
var sio socketio
switch block.Type {
case socketioPluginName:
gohcl.DecodeBody(block.Body, nil, &sio)
if len(block.Labels) > 0 {
sio.Name = block.Labels[0] // the same index as the LabelNames above...
}
p.Subscribe(sio)
}
}
return nil
}
func (p *socketioPlugin) Subscribe(sio socketio) {
log.Printf("[socketio] subcribe to event %q ...", sio.Event)
p.conn[sio.Name].On(sio.Event, func(channel *sktio.Channel, args interface{}) {
for _, emit := range sio.Emit {
log.Println("[socketio] emit ...")
channel.Emit(emit.Event, convertToJSON(emit.Args))
}
for _, broadcast := range sio.Broadcast {
log.Println("[socketio] broadcast ...")
p.conn[sio.Name].BroadcastTo(broadcast.Room, broadcast.Event, convertToJSON(broadcast.Args))
}
for _, broadcast := range sio.BroadcastAll {
log.Println("[socketio] broadcast all ...")
p.conn[sio.Name].BroadcastToAll(broadcast.Event, convertToJSON(broadcast.Args))
}
})
}
func (p *socketioPlugin) MiddlewareHTTP(r Route, plugins hcl.Body, req requ.HTTP) (MiddlewareHTTP, bool) {
reqb, _, _ := plugins.PartialContent(&hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: socketioPluginName,
LabelNames: []string{"name"},
},
},
})
if len(reqb.Blocks) == 0 {
return nil, false
}
var reqSocketIO []socketio
for _, block := range reqb.Blocks {
var sio socketio
switch block.Type {
case socketioPluginName:
gohcl.DecodeBody(block.Body, nil, &sio)
if len(block.Labels) > 0 {
sio.Name = block.Labels[0]
}
reqSocketIO = append(reqSocketIO, sio)
}
}
if len(reqSocketIO) == 0 {
return nil, false
}
var idx = int64(-1)
var resps = reqSocketIO
if req.Order == "unordered" {
rand.Seed(time.Now().UnixNano()) // doesn't have to be crypto-quality random here...
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() { next.ServeHTTP(w, r) }()
go func() {
for {
var x int64
switch req.Order {
case "random":
x = rand.Int63n(int64(len(resps) * 2))
case "unordered":
x = atomic.AddInt64(&idx, 1)
if int(x)%len(resps) == 0 {
rand.Shuffle(len(resps), func(i, j int) { resps[i], resps[j] = resps[j], resps[i] })
}
default:
x = atomic.AddInt64(&idx, 1)
}
resp := resps[int(x)%len(resps)]
log.Println("[socketio] sending event ...")
if len(req.Delay) > 0 {
time.Sleep(delay(req.Delay))
}
for _, broadcast := range resp.Broadcast {
data := convertToJSON(broadcast.Args)
if data != nil {
log.Println("[socketio] http broadcast ...")
p.conn[resp.Name].BroadcastTo(broadcast.Room, broadcast.Event, data)
}
}
for _, broadcast := range resp.BroadcastAll {
data := convertToJSON(broadcast.Args)
if data != nil {
log.Println("[socketio] http broadcast all ...")
p.conn[resp.Name].BroadcastToAll(broadcast.Event, data)
}
}
if req.Ticker != nil && len(req.Ticker.Time) > 0 {
time.Sleep(delay(req.Ticker.Time))
continue
}
break
}
}()
})
}, true
}
type Options struct {
Simplify bool
}
type converter struct {
bytes []byte
options Options
file io.ReaderAt
}
type jsonObj map[string]interface{}
func (c *converter) convertBody(body *hclsyntax.Body) (out jsonObj, err error) {
out = make(jsonObj)
c.file, err = os.Open(body.SrcRange.Filename)
if err != nil {
log.Fatal("template file open:", err)
}
defer c.file.(*os.File).Close()
for _, block := range body.Blocks {
if err = c.convertBlock(block, out); err != nil {
return nil, fmt.Errorf("convert block: %w", err)
}
}
for key, value := range body.Attributes {
out[key], err = c.convertExpression(value.Expr)
if err != nil {
return nil, fmt.Errorf("convert expression: %w", err)
}
}
return out, nil
}
func (c *converter) rangeSource(r hcl.Range) string {
// for some reason the range doesn't include the ending paren, so
// check if the next character is an ending paren, and include it if it is.
end := r.End.Byte
if c.bytes[end] == ')' {
end++
}
return string(c.bytes[r.Start.Byte:end])
}
func (c *converter) convertBlock(block *hclsyntax.Block, out jsonObj) error {
key := block.Type
for _, label := range block.Labels {
// Labels represented in HCL are defined as quoted strings after the name of the block:
// block "label_one" "label_two"
//
// Labels represtend in JSON are nested one after the other:
// "label_one": {
// "label_two": {}
// }
//
// To create the JSON representation, check to see if the label exists in the current output:
//
// When the label exists, move onto the next label reference.
// When a label does not exist, create the label in the output and set that as the next label reference
// in order to append (potential) labels to it.
if _, exists := out[key]; exists {
var ok bool
out, ok = out[key].(jsonObj)
if !ok {
return fmt.Errorf("Unable to convert Block to JSON: %v.%v", block.Type, strings.Join(block.Labels, "."))
}
} else {
out[key] = make(jsonObj)
out = out[key].(jsonObj)
}
key = label
}
value, err := c.convertBody(block.Body)
if err != nil {
return fmt.Errorf("convert body: %w", err)
}
// Multiple blocks can exist with the same name, at the same
// level in the JSON document (e.g. locals).
//
// For consistency, always wrap the value in a collection.
// When multiple values are at the same key
if current, exists := out[key]; exists {
out[key] = append(current.([]interface{}), value)
} else {
out[key] = []interface{}{value}
}
return nil
}
func (c *converter) convertExpression(expr hclsyntax.Expression) (interface{}, error) {
if c.options.Simplify {
value, dia := expr.Value(&evalContext)
if !dia.HasErrors() {
return ctyjson.SimpleJSONValue{Value: value}, nil
}
}
// assume it is hcl syntax (because, um, it is)
switch value := expr.(type) {
case *hclsyntax.LiteralValueExpr:
return ctyjson.SimpleJSONValue{Value: value.Val}, nil
case *hclsyntax.UnaryOpExpr:
return c.convertUnary(value)
case *hclsyntax.TemplateExpr:
return c.convertTemplate(value)
case *hclsyntax.TemplateWrapExpr:
return c.convertExpression(value.Wrapped)
case *hclsyntax.TupleConsExpr:
list := make([]interface{}, 0)
for _, ex := range value.Exprs {
elem, err := c.convertExpression(ex)
if err != nil {
return nil, err
}
list = append(list, elem)
}
return list, nil
case *hclsyntax.ObjectConsExpr:
m := make(jsonObj)
for _, item := range value.Items {
key, err := c.convertKey(item.KeyExpr)
if err != nil {
return nil, err
}
m[key], err = c.convertExpression(item.ValueExpr)
if err != nil {
return nil, err
}
}
return m, nil
default:
return c.wrapExpr(expr), nil
}
}
func (c *converter) convertUnary(v *hclsyntax.UnaryOpExpr) (interface{}, error) {
_, isLiteral := v.Val.(*hclsyntax.LiteralValueExpr)
if !isLiteral {
// If the expression after the operator isn't a literal, fall back to
// wrapping the expression with ${...}
return c.wrapExpr(v), nil
}
val, dia := v.Value(nil)
if dia.HasErrors() {
return nil, dia
}
return ctyjson.SimpleJSONValue{Value: val}, nil
}
func (c *converter) convertTemplate(t *hclsyntax.TemplateExpr) (interface{}, error) {
if t.IsStringLiteral() {
// safe because the value is just the string
v, dia := t.Value(nil)
if dia.HasErrors() {
return "", dia
}
b := make([]byte, t.SrcRange.End.Column)
c.file.ReadAt(b, int64(t.SrcRange.End.Byte-t.SrcRange.End.Column))
if strings.TrimSpace(string(b)) == "_JSON_" {
m := make(map[string]interface{})
err := json.Unmarshal([]byte(v.AsString()), &m)
return m, err
}
return v.AsString(), nil
}
var builder strings.Builder
for _, part := range t.Parts {
s, err := c.convertStringPart(part)
if err != nil {
return "", err
}
builder.WriteString(s)
}
return builder.String(), nil
}
func (c *converter) convertStringPart(expr hclsyntax.Expression) (string, error) {
switch v := expr.(type) {
case *hclsyntax.LiteralValueExpr:
s, err := ctyconvert.Convert(v.Val, cty.String)
if err != nil {
return "", err
}
return s.AsString(), nil
case *hclsyntax.TemplateExpr:
str, err := c.convertTemplate(v)
return str.(string), err
case *hclsyntax.TemplateWrapExpr:
return c.convertStringPart(v.Wrapped)
case *hclsyntax.ConditionalExpr:
return c.convertTemplateConditional(v)
case *hclsyntax.TemplateJoinExpr:
return c.convertTemplateFor(v.Tuple.(*hclsyntax.ForExpr))
default:
// treating as an embedded expression
return c.wrapExpr(expr), nil
}
}
func (c *converter) convertKey(keyExpr hclsyntax.Expression) (string, error) {
// a key should never have dynamic input
if k, isKeyExpr := keyExpr.(*hclsyntax.ObjectConsKeyExpr); isKeyExpr {
keyExpr = k.Wrapped
if _, isTraversal := keyExpr.(*hclsyntax.ScopeTraversalExpr); isTraversal {
return c.rangeSource(keyExpr.Range()), nil
}
}
return c.convertStringPart(keyExpr)
}
func (c *converter) convertTemplateConditional(expr *hclsyntax.ConditionalExpr) (string, error) {
var builder strings.Builder
builder.WriteString("%{if ")
builder.WriteString(c.rangeSource(expr.Condition.Range()))
builder.WriteString("}")
trueResult, err := c.convertStringPart(expr.TrueResult)
if err != nil {
return "", nil
}
builder.WriteString(trueResult)
falseResult, err := c.convertStringPart(expr.FalseResult)
if len(falseResult) > 0 {
builder.WriteString("%{else}")
builder.WriteString(falseResult)
}
builder.WriteString("%{endif}")
return builder.String(), nil
}
func (c *converter) convertTemplateFor(expr *hclsyntax.ForExpr) (string, error) {
var builder strings.Builder
builder.WriteString("%{for ")
if len(expr.KeyVar) > 0 {
builder.WriteString(expr.KeyVar)
builder.WriteString(", ")
}
builder.WriteString(expr.ValVar)
builder.WriteString(" in ")
builder.WriteString(c.rangeSource(expr.CollExpr.Range()))
builder.WriteString("}")
templ, err := c.convertStringPart(expr.ValExpr)
if err != nil {
return "", err
}
builder.WriteString(templ)
builder.WriteString("%{endfor}")
return builder.String(), nil
}
func (c *converter) wrapExpr(expr hclsyntax.Expression) string {
return "${" + c.rangeSource(expr.Range()) + "}"
}
// a subset of functions used in terraform
// that can be used when simplifying during conversion
var evalContext = hcl.EvalContext{
Functions: map[string]function.Function{
// numeric
// "abs": stdlib.AbsoluteFunc,
// "ceil": stdlib.CeilFunc,
// "floor": stdlib.FloorFunc,
// "log": stdlib.LogFunc,
"max": stdlib.MaxFunc,
"min": stdlib.MinFunc,
// "parseint": stdlib.ParseIntFunc,
// "pow": stdlib.PowFunc,
// "signum": stdlib.SignumFunc,
// string
// "chomp": stdlib.ChompFunc,
"format": stdlib.FormatFunc,
"formatlist": stdlib.FormatListFunc,
// "indent": stdlib.IndentFunc,
// "join": stdlib.JoinFunc,
// "split": stdlib.SplitFunc,
// "strrev": stdlib.ReverseFunc,
// "trim": stdlib.TrimFunc,
// "trimprefix": stdlib.TrimPrefixFunc,
// "trimsuffix": stdlib.TrimSuffixFunc,
// "trimspace": stdlib.TrimSpaceFunc,
// collections
// "chunklist": stdlib.ChunklistFunc,
"concat": stdlib.ConcatFunc,
// "distinct": stdlib.DistinctFunc,
// "flatten": stdlib.FlattenFunc,
"length": stdlib.LengthFunc,
// "merge": stdlib.MergeFunc,
// "reverse": stdlib.ReverseListFunc,
// "sort": stdlib.SortFunc,
// encoding
"csvdecode": stdlib.CSVDecodeFunc,
"jsondecode": stdlib.JSONDecodeFunc,
"jsonencode": stdlib.JSONEncodeFunc,
// time
"formatdate": stdlib.FormatDateFunc,
// "timeadd": stdlib.TimeAddFunc,
},
}