2024-09-18 18:29:21 +08:00
|
|
|
package js
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"github.com/dop251/goja"
|
|
|
|
"github.com/ssgo/u"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RequireUtil() map[string]any {
|
|
|
|
return map[string]any{
|
|
|
|
"json": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
if r, err := json.Marshal(args.Arguments[0].Export()); err == nil {
|
|
|
|
return vm.ToValue(string(r))
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"jsonP": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
if r, err := json.Marshal(args.Arguments[0].Export()); err == nil {
|
|
|
|
r1 := bytes.Buffer{}
|
|
|
|
if err2 := json.Indent(&r1, r, "", " "); err2 == nil {
|
|
|
|
return vm.ToValue(string(r))
|
|
|
|
} else {
|
|
|
|
return vm.ToValue(string(r))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"unJson": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
var r any
|
|
|
|
if err := json.Unmarshal(u.Bytes(args.Arguments[0].Export()), &r); err == nil {
|
|
|
|
return vm.ToValue(r)
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"yaml": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
if r, err := yaml.Marshal(args.Arguments[0].Export()); err == nil {
|
|
|
|
return vm.ToValue(string(r))
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"unYaml": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
var r any
|
|
|
|
if err := yaml.Unmarshal(u.Bytes(args.Arguments[0].Export()), &r); err == nil {
|
|
|
|
return vm.ToValue(r)
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"base64": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
return vm.ToValue(u.Base64(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
|
|
|
"unBase64": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
return vm.ToValue(u.UnBase64String(u.String(args.Arguments[0].Export())))
|
|
|
|
},
|
|
|
|
"urlBase64": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
return vm.ToValue(u.UrlBase64String(u.String(args.Arguments[0].Export())))
|
|
|
|
},
|
|
|
|
"unUrlBase64": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
return vm.ToValue(u.UnUrlBase64String(u.String(args.Arguments[0].Export())))
|
|
|
|
},
|
|
|
|
"hex": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
return vm.ToValue(hex.EncodeToString(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
|
|
|
"unHex": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
if r, err := hex.DecodeString(u.String(args.Arguments[0].Export())); err == nil {
|
|
|
|
return vm.ToValue(string(r))
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"aes": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 3 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 3, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
if r, err := u.EncryptAesBytes(u.Bytes(args.Arguments[0].Export()), u.Bytes(args.Arguments[1].Export()), u.Bytes(args.Arguments[2].Export())); err == nil {
|
|
|
|
return vm.ToValue(string(r))
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"unAes": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 3 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 3, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
if r, err := u.DecryptAesBytes(u.Bytes(args.Arguments[0].Export()), u.Bytes(args.Arguments[1].Export()), u.Bytes(args.Arguments[2].Export())); err == nil {
|
|
|
|
return vm.ToValue(string(r))
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"gzip": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
if r, err := u.Gzip(u.Bytes(args.Arguments[0].Export())); err == nil {
|
|
|
|
return vm.ToValue(string(r))
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"gunzip": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
if r, err := u.Gunzip(u.Bytes(args.Arguments[0].Export())); err == nil {
|
|
|
|
return vm.ToValue(string(r))
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"id": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
return vm.ToValue(u.Id12())
|
|
|
|
},
|
|
|
|
"uniqueId": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
return vm.ToValue(u.UniqueId())
|
|
|
|
},
|
|
|
|
"token": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
size := 20
|
|
|
|
if len(args.Arguments) > 0 {
|
|
|
|
size = u.Int(args.Arguments[0].Export())
|
|
|
|
}
|
|
|
|
return vm.ToValue(u.MakeToken(size))
|
|
|
|
},
|
|
|
|
"md5": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
return vm.ToValue(u.MD5(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
|
|
|
"sha1": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
return vm.ToValue(u.Sha1(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
|
|
|
"sha256": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
return vm.ToValue(u.Sha256(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
|
|
|
"sha512": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 1, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
return vm.ToValue(u.Sha512(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
|
|
|
"tpl": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 2 {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.NewGoError(errors.New("arguments need 2, but given " + u.String(len(args.Arguments))))
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
var tpl *template.Template
|
|
|
|
buf := bytes.NewBuffer(make([]byte, 0))
|
|
|
|
var err error
|
2024-09-20 16:50:35 +08:00
|
|
|
if tpl, err = template.New("tpl").Parse(u.String(args.Arguments[0].Export())); err == nil {
|
|
|
|
err = tpl.Execute(buf, args.Arguments[1].Export())
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
return vm.ToValue(buf.String())
|
|
|
|
},
|
2024-09-20 16:50:35 +08:00
|
|
|
"shell": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
if len(args.Arguments) < 1 {
|
|
|
|
return vm.NewGoError(errors.New("arguments need 1 or more, but given " + u.String(len(args.Arguments))))
|
|
|
|
}
|
|
|
|
a := make([]string, len(args.Arguments)-1)
|
|
|
|
for i := 1; i < len(args.Arguments); i++ {
|
|
|
|
a[i-1] = u.String(args.Arguments[i].Export())
|
|
|
|
}
|
|
|
|
|
|
|
|
if r, err := u.RunCommand(u.String(args.Arguments[0].Export()), a...); err == nil {
|
|
|
|
return vm.ToValue(r)
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
}
|