-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjs.go
652 lines (525 loc) · 14.2 KB
/
js.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*********************************************
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 永无BUG
心外无法 法外无心
三宝弟子 飞猪宏愿
*********************************************/
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net"
"net/smtp"
"os/exec"
"strconv"
"strings"
"github.com/jmoiron/sqlx"
"github.com/dop251/goja"
"gopkg.in/gomail.v2"
"gopkg.in/resty.v1"
)
// initJSVM - creates a new javascript virtual machine
func initJSVM(ctx map[string]interface{}) *goja.Runtime {
vm := goja.New()
for k, v := range ctx {
vm.Set(k, v)
}
vm.Set("fetch", jsFetchfunc)
vm.Set("call_api", jsJWTFetchfunc)
vm.Set("jwt_token", jsJWTTokenfunc)
vm.Set("exec_sql", jsExecSQLFunc)
vm.Set("exec_cmd", jsExecCommandFunc)
vm.Set("log", log.Println)
vm.Set("emit_msg", jsExecEmitMessage)
vm.Set("call_macro", jsExecCallMacro)
vm.Set("send_udp", jsExecSendUDP)
vm.Set("send_mail", jsSendMail)
vm.Set("ws_broacast", jsExecWebsocketBroacastMessage)
vm.Set("ws_send", jsExecWebsocketSendMessage)
return vm
}
// jsFetchfunc - the fetch function used inside the js vm
func jsFetchfunc(url string, options ...map[string]interface{}) (map[string]interface{}, error) {
var option map[string]interface{}
var method string
var headers map[string]string
var body interface{}
if len(options) > 0 {
option = options[0]
}
if nil != option["method"] {
method, _ = option["method"].(string)
}
if nil != option["headers"] {
hdrs, _ := option["headers"].(map[string]interface{})
headers = make(map[string]string)
for k, v := range hdrs {
headers[k], _ = v.(string)
}
}
var req *resty.Request
if nil != option["body"] {
body, _ = option["body"]
}
switch {
case option["format"] == "form":
req = resty.R().SetHeaders(headers).SetFormData(convertBodyToMap(body))
case option["format"] == "query":
req = resty.R().SetHeaders(headers).SetQueryParams(convertBodyToMap(body))
default:
req = resty.R().SetHeaders(headers).SetBody(body)
}
resp, err := req.Execute(method, url)
if err != nil {
return nil, err
}
rspHdrs := resp.Header()
rspHdrsNormalized := map[string]string{}
for k, v := range rspHdrs {
rspHdrsNormalized[strings.ToLower(k)] = v[0]
}
return map[string]interface{}{
"status": resp.Status(),
"statusCode": resp.StatusCode(),
"headers": rspHdrsNormalized,
"body": string(resp.Body()),
}, nil
}
func convertBodyToMap(body interface{}) map[string]string {
switch body.(type) {
case map[string]interface{}:
out := map[string]string{}
for k, v := range body.(map[string]interface{}) {
out[k] = fmt.Sprintf("%v", v)
}
return out
default:
return map[string]string{}
}
}
func jsJWTFetchfunc(url string, options ...map[string]interface{}) (map[string]interface{}, error) {
var option map[string]interface{}
var method string
var headers map[string]string
var body interface{}
if len(options) > 0 {
option = options[0]
}
if nil != option["method"] {
method, _ = option["method"].(string)
}
if nil != option["headers"] {
hdrs, _ := option["headers"].(map[string]interface{})
headers = make(map[string]string)
for k, v := range hdrs {
headers[k], _ = v.(string)
}
}
requestToken, err := macrosManager.JwtIdentityConfig().CreateRequestToken()
if err == nil {
if headers == nil {
headers = make(map[string]string)
}
headers["Authorization"] = "Bearer " + requestToken
}
if headers == nil {
headers = make(map[string]string)
}
if headers["Content-Type"] == "" {
headers["Content-Type"] = "application/json; charset=UTF-8"
}
var req *resty.Request
if nil != option["body"] {
body, _ = option["body"]
}
switch {
case option["format"] == "form":
req = resty.R().SetHeaders(headers).SetFormData(convertBodyToMap(body))
case option["format"] == "query":
req = resty.R().SetHeaders(headers).SetQueryParams(convertBodyToMap(body))
default:
req = resty.R().SetHeaders(headers).SetBody(body)
}
resp, err := req.Execute(method, url)
if err != nil {
return map[string]interface{}{
"code": 5000,
"message": err.Error(),
}, nil
}
rspHdrs := resp.Header()
rspHdrsNormalized := map[string]string{}
for k, v := range rspHdrs {
rspHdrsNormalized[strings.ToLower(k)] = v[0]
}
var respData map[string]interface{}
respCode := resp.StatusCode()
if respCode >= 200 && respCode < 400 {
respCode = 0
}
if nil != json.Unmarshal(resp.Body(), &respData) {
return map[string]interface{}{
"code": respCode,
"message": "操作成功!",
"data": string(resp.Body()),
"__header__": rspHdrsNormalized,
}, nil
}
respData["__header__"] = rspHdrsNormalized
return respData, nil
}
func jsJWTTokenfunc() (string, error) {
if !macrosManager.JwtIdentityConfig().IsEnabled() {
return "", fmt.Errorf("jwt not enabled")
}
requestToken, err := macrosManager.JwtIdentityConfig().CreateRequestToken()
if err != nil {
return "", err
}
return requestToken, nil
}
//jsExecSQL - 执行SQL
func jsExecSQLFunc(sql string, args ...map[string]interface{}) (interface{}, error) {
var arg map[string]interface{}
if len(args) > 0 {
arg = args[0]
}
ret, err := jsExecSQLQuery(strings.Split(strings.TrimSpace(sql), "---"), arg)
if err != nil {
return nil, err
}
if *flagDebug > 2 {
log.Printf("jsExecSQL return %v\n", ret)
}
return ret, nil
}
// execSQLQuery - execute the specified sql query
func jsExecSQLQuery(sqls []string, args map[string]interface{}) (interface{}, error) {
if !macrosManager.DatabaseConfig().IsDatabaseEnabled() {
return nil, fmt.Errorf("Database forget enable??")
}
conn, err := sqlx.Open(macrosManager.DatabaseConfig().Driver, macrosManager.DatabaseConfig().Dsn)
if err != nil {
if *flagDebug > 0 {
log.Printf("jsExecSQL open database error: %v\n", err)
}
return nil, err
}
defer conn.Close()
for i, sql := range sqls {
if strings.TrimSpace(sql) == "" {
sqls = append(sqls[0:i], sqls[i+1:]...)
}
}
//先执行前面的SQL
for i, sql := range sqls[0 : len(sqls)-1] {
sql = strings.TrimSpace(sql)
if "" == sql {
continue
}
if *flagDebug > 2 {
log.Printf("jsExecSQL exec sql%d:\n==sql==\n%s\n==args==\n%v\n", i, sql, args)
}
if _, err := conn.NamedExec(sql, args); err != nil {
if *flagDebug > 0 {
log.Printf("jsExecSQL exec sql%d error: %v\n==sql==\n%s\n", i, err, sql)
}
return nil, fmt.Errorf("jsExecSQL exec sql%d error: %v", i, err)
}
}
if *flagDebug > 2 {
log.Printf("jsExecSQL exec sql%d:\n==sql==\n%s\n==args==\n%v\n", len(sqls)-1, sqls[len(sqls)-1], args)
}
//最后一个用于返回数据
rows, err := conn.NamedQuery(sqls[len(sqls)-1], args)
if err != nil {
if *flagDebug > 1 {
log.Printf("jsExecSQL exec sql%d error: %v\n==sql==\n%s\n", len(sqls)-1, err, sqls[len(sqls)-1])
}
return nil, fmt.Errorf("jsExecSQL exec sql%d error: %v", len(sqls)-1, err)
}
defer rows.Close()
ret := []map[string]interface{}{}
for rows.Next() {
row, err := jsScanSQLRow(rows)
if err != nil {
if *flagDebug > 1 {
log.Printf("jsExecSQL sql%d fetch rows error:\n%v\n==sql==\n%s\n==rows==\n%v\n",
len(sqls)-1, err, sqls[len(sqls)-1], rows)
}
continue
}
ret = append(ret, row)
}
return interface{}(ret), nil
}
// jsScanSQLRow - return values
func jsScanSQLRow(rows *sqlx.Rows) (map[string]interface{}, error) {
row := make(map[string]interface{})
if err := rows.MapScan(row); err != nil {
return nil, err
}
for k, v := range row {
if nil == v {
continue
}
switch v.(type) {
case []uint8:
v = []byte(v.([]uint8))
default:
v, _ = json.Marshal(v)
}
var d interface{}
if nil == json.Unmarshal(v.([]byte), &d) {
row[k] = d
} else {
row[k] = string(v.([]byte))
}
}
return row, nil
}
// jsExecCommandFunc - js execute the command line
func jsExecCommandFunc(cmdline string, args ...string) (interface{}, error) {
cmdExecute, inputArgs := getCommandDefines(cmdline)
for _, v := range args {
inputArgs = append(inputArgs, v)
}
cmd := exec.Command(cmdExecute, inputArgs[0:]...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
if *flagDebug > 0 {
log.Printf("jsExecCommand error: %v\n==cmd==\n%s", err, cmdline)
}
return nil, err
}
outStr := out.String()
var outData interface{}
err = json.Unmarshal([]byte(outStr), &outData)
if err != nil {
return outStr, nil
}
return outData, nil
}
// jsExecCallMacro - 调用其他定义
func jsExecCallMacro(macroName string, args ...map[string]interface{}) (interface{}, error) {
var arg map[string]interface{}
if len(args) > 0 {
arg = args[0]
} else {
arg = map[string]interface{}{}
}
m := macrosManager.Get(macroName)
if m == nil {
return nil, fmt.Errorf("not found %s macro", macroName)
}
out, err := m.Call(nil, arg, nil)
if err != nil {
if m.Format == "nil" || m.Format == "origin" {
return nil, err
}
return map[string]interface{}{
"code": 500,
"message": err.Error(),
}, nil
}
if m.Format == "origin" {
return out, nil
}
if m.Format == "nil" {
return nil, nil
}
return map[string]interface{}{
"code": 0,
"message": "操作成功!",
"data": out,
}, nil
}
// jsExecEmitMessage - 发送消息
func jsExecEmitMessage(dest string, inMsg interface{}, args ...map[string]interface{}) (bool, error) {
var arg map[string]interface{}
if len(args) > 0 {
arg = args[0]
} else {
arg = map[string]interface{}{}
}
sender, err := macrosManager.MessageSendProvider()
if err != nil {
return false, err
}
var msg string
switch inMsg.(type) {
case string:
msg = inMsg.(string)
default:
msgBytes, _ := json.Marshal(inMsg)
msg = string(msgBytes)
}
if err := sender.EmitMessage(dest, msg, arg); err != nil {
return false, err
}
return true, nil
}
// jsExecWebsocketBroacastMessage -
func jsExecWebsocketBroacastMessage(ch string, args ...interface{}) (bool, error) {
var msg interface{}
if len(args) == 1 {
msg = args[0]
} else {
msg = args
}
r := GetWSClientRegistry(ch)
if r == nil {
return false, fmt.Errorf("not found endpoint %s", ch)
}
r.BroacastWebsocketMessage(msg)
return true, nil
}
// jsExecWebsocketSendMessage -
func jsExecWebsocketSendMessage(ch string, cid string, args ...interface{}) (bool, error) {
var msg interface{}
if len(args) == 1 {
msg = args[0]
} else {
msg = args
}
r := GetWSClientRegistry(ch)
if r == nil {
return false, nil
}
err := r.SendWebsocketMessage(cid, msg)
return err == nil, err
}
// jsExecSendUDP - 发送UDP
func jsExecSendUDP(rAddr string, msg string, args ...interface{}) (outMsg string, err error) {
recvData := false
bufSiz := 8192
if len(args) >= 1 {
recvData = args[0] == true
}
if len(args) >= 2 {
bufSiz = args[1].(int)
}
addrs := strings.Split(rAddr, ":")
if len(addrs) != 2 {
return "", fmt.Errorf("rAddr must be ip:port, not %s", rAddr)
}
port, err := strconv.Atoi(addrs[1])
if err != nil {
return "", err
}
socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
IP: net.ParseIP(addrs[0]),
Port: port,
})
defer socket.Close()
_, err = socket.Write([]byte(msg))
if err != nil {
return "", err
}
if !recvData {
return "", nil
}
outData := make([]byte, bufSiz)
outSiz, _, err := socket.ReadFromUDP(outData)
if err != nil {
return "", err
}
outMsg = string(outData[:outSiz])
return outMsg, err
}
type unencryptedAuth struct {
smtp.Auth
}
type loginAuth struct {
username, password string
}
func LoginAuth(username, password string) smtp.Auth {
return &loginAuth{username, password}
}
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", nil, nil
}
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
command := string(fromServer)
command = strings.TrimSpace(command)
command = strings.TrimSuffix(command, ":")
command = strings.ToLower(command)
if more {
if command == "username" {
return []byte(fmt.Sprintf("%s", a.username)), nil
} else if command == "password" {
return []byte(fmt.Sprintf("%s", a.password)), nil
} else {
// We've already sent everything.
return nil, fmt.Errorf("unexpected server challenge: %s", command)
}
}
return nil, nil
}
// jsSendMail - 发送邮件
func jsSendMail(to string, cc string, bcc string, subject string, body string) (bool, error) {
if macrosManager.SmtpConfig() == nil {
return false, fmt.Errorf("未配置SMTP")
}
m := gomail.NewMessage(
//发送文本时设置编码,防止乱码。 如果txt文本设置了之后还是乱码,那可以将原txt文本在保存时
//就选择utf-8格式保存
gomail.SetEncoding(gomail.Base64),
)
m.SetHeader(
"From",
m.FormatAddress(
macrosManager.SmtpConfig().Address,
macrosManager.SmtpConfig().Name,
),
)
toAddrs := strings.Split(strings.ReplaceAll(to, ";", ","), ",")
m.SetHeader("To", toAddrs...) // 发送给用户(可以多个)
m.SetHeader("Subject", subject) // 设置邮件主题
m.SetBody("text/html", body)
if cc != "" && cc != "undefined" {
ccAddrs := strings.Split(strings.ReplaceAll(cc, ";", ","), ",")
m.SetHeader("Cc", ccAddrs...)
}
if bcc != "" && bcc != "undefined" {
bccAddrs := strings.Split(strings.ReplaceAll(bcc, ";", ","), ",")
m.SetHeader("Bcc", bccAddrs...)
}
d := gomail.NewDialer(macrosManager.SmtpConfig().Host,
macrosManager.SmtpConfig().Port, macrosManager.SmtpConfig().Username, macrosManager.SmtpConfig().Password,
)
if macrosManager.SmtpConfig().SSL {
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
d.SSL = true
} else {
d.Auth = LoginAuth(macrosManager.SmtpConfig().Username, macrosManager.SmtpConfig().Password)
}
err := d.DialAndSend(m)
if err != nil {
return false, err
}
return true, nil
}