2024-09-18 18:29:21 +08:00
|
|
|
package js
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/dop251/goja"
|
|
|
|
"github.com/ssgo/u"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RequireUtil() map[string]any {
|
|
|
|
return map[string]any{
|
2024-09-23 18:15:02 +08:00
|
|
|
"json": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"jsonP": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"unJson": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"yaml": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"unYaml": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"base64": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
2024-09-18 18:29:21 +08:00
|
|
|
return vm.ToValue(u.Base64(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"unBase64": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
|
|
|
return vm.ToValue(u.UnBase64String(args.Str(0)))
|
2024-09-18 18:29:21 +08:00
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"urlBase64": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
|
|
|
return vm.ToValue(u.UrlBase64String(args.Str(0)))
|
2024-09-18 18:29:21 +08:00
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"unUrlBase64": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
|
|
|
return vm.ToValue(u.UnUrlBase64String(args.Str(0)))
|
2024-09-18 18:29:21 +08:00
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"hex": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
2024-09-18 18:29:21 +08:00
|
|
|
return vm.ToValue(hex.EncodeToString(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"unHex": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
|
|
|
if r, err := hex.DecodeString(args.Str(0)); err == nil {
|
2024-09-18 18:29:21 +08:00
|
|
|
return vm.ToValue(string(r))
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"aes": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(3); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"unAes": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(3); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"gzip": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"gunzip": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"id": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
2024-09-18 18:29:21 +08:00
|
|
|
return vm.ToValue(u.Id12())
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"uniqueId": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
2024-09-18 18:29:21 +08:00
|
|
|
return vm.ToValue(u.UniqueId())
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"token": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
2024-09-18 18:29:21 +08:00
|
|
|
size := 20
|
2024-09-23 18:15:02 +08:00
|
|
|
if len(argsIn.Arguments) > 0 {
|
|
|
|
size = u.Int(argsIn.Arguments[0].Export())
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
return vm.ToValue(u.MakeToken(size))
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"md5": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
2024-09-18 18:29:21 +08:00
|
|
|
return vm.ToValue(u.MD5(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"sha1": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
2024-09-18 18:29:21 +08:00
|
|
|
return vm.ToValue(u.Sha1(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"sha256": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
2024-09-18 18:29:21 +08:00
|
|
|
return vm.ToValue(u.Sha256(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"sha512": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
2024-09-18 18:29:21 +08:00
|
|
|
return vm.ToValue(u.Sha512(u.Bytes(args.Arguments[0].Export())))
|
|
|
|
},
|
2024-09-23 18:15:02 +08:00
|
|
|
"tpl": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(2); err != nil {
|
|
|
|
return err
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
|
|
|
var functions = map[string]any{}
|
|
|
|
if len(argsIn.Arguments) > 2 {
|
|
|
|
obj := argsIn.Arguments[2].ToObject(vm)
|
|
|
|
for _, k := range obj.Keys() {
|
|
|
|
v := obj.Get(k)
|
|
|
|
if cb, ok := goja.AssertFunction(v); ok {
|
|
|
|
functions[k] = func(in string) string {
|
|
|
|
if r, err := cb(argsIn.This, vm.ToValue(in)); err == nil {
|
|
|
|
return r.String()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-18 18:29:21 +08:00
|
|
|
buf := bytes.NewBuffer(make([]byte, 0))
|
|
|
|
var err error
|
2024-09-23 18:15:02 +08:00
|
|
|
tpl := template.New("tpl")
|
|
|
|
if len(functions) > 0 {
|
|
|
|
tpl.Funcs(functions)
|
|
|
|
}
|
|
|
|
if tpl, err = tpl.Parse(args.Str(0)); err == nil {
|
2024-09-20 16:50:35 +08:00
|
|
|
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-23 18:15:02 +08:00
|
|
|
"shell": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
|
|
args := MakeArgs(&argsIn, vm)
|
|
|
|
if err := args.Check(1); err != nil {
|
|
|
|
return err
|
2024-09-20 16:50:35 +08:00
|
|
|
}
|
2024-09-23 18:15:02 +08:00
|
|
|
|
2024-09-20 16:50:35 +08:00
|
|
|
a := make([]string, len(args.Arguments)-1)
|
|
|
|
for i := 1; i < len(args.Arguments); i++ {
|
2024-09-23 18:15:02 +08:00
|
|
|
a[i-1] = args.Str(i)
|
2024-09-20 16:50:35 +08:00
|
|
|
}
|
|
|
|
|
2024-09-23 18:15:02 +08:00
|
|
|
if r, err := u.RunCommand(args.Str(0), a...); err == nil {
|
2024-09-20 16:50:35 +08:00
|
|
|
return vm.ToValue(r)
|
|
|
|
} else {
|
|
|
|
return vm.NewGoError(err)
|
|
|
|
}
|
|
|
|
},
|
2024-09-18 18:29:21 +08:00
|
|
|
}
|
|
|
|
}
|