use interceptor in SHMux

This commit is contained in:
kingecg 2025-09-21 19:03:29 +08:00
parent ae083e9d9d
commit 8fff0e8888
3 changed files with 27 additions and 23 deletions

18
gosh.go
View File

@ -3,8 +3,8 @@ package gosh
import (
"net/http"
"git.kingecg.top/kingecg/gosh/interceptor"
"github.com/mattn/anko/env"
"github.com/mattn/anko/vm"
)
const (
@ -26,15 +26,17 @@ type SHMux struct {
}
func NewSHMux() *SHMux {
return &SHMux{
s := &SHMux{
ServeMux: http.NewServeMux(),
ankoEnv: env.NewEnv(),
}
//s.executor = interceptor.NewAnkointerceptor(s.ankoEnv)
return s
}
func (s *SHMux) RegistFunction(name string, function interface{}) {
if s.ankoEnv != nil {
s.ankoEnv.Define(name, function)
_ = s.ankoEnv.Define(name, function)
}
}
@ -111,10 +113,12 @@ func (s *SHMux) handle(w http.ResponseWriter, r *http.Request, script string) {
resHelper := &ResponseHelper{
w: w,
}
env := s.ankoEnv.Copy()
env.Define("Req", reqHelper)
env.Define("Res", resHelper)
_, err := vm.Execute(env, nil, script)
renv := s.ankoEnv.Copy()
_ = renv.Define("Req", reqHelper)
_ = renv.Define("Res", resHelper)
rinterceptor := interceptor.NewAnkointerceptor(renv)
_, err := rinterceptor.ExecContent(script)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return

View File

@ -85,7 +85,7 @@ func (a *Ankointerceptor) loadLibrary(libPath string, libFuncMap map[string]func
if err != nil {
panic("Load library error")
}
a.libhabdles = append(a.libhabdles, lib)
a.liberalises = append(a.liberalises, lib)
fnmap := make(map[string]interface{})
fileds := make([]reflect.StructField, 0, len(fnmap))
for name, types := range libFuncMap {
@ -122,7 +122,7 @@ func (a *Ankointerceptor) loadLibrary(libPath string, libFuncMap map[string]func
// Close 关闭所有已加载的动态库句柄,释放资源。
// 应在不再需要使用动态库时调用此方法。
func (a *Ankointerceptor) Close() {
for _, lib := range a.libhabdles {
purego.Dlclose(lib)
for _, lib := range a.liberalises {
_ = purego.Dlclose(lib)
}
}

View File

@ -12,7 +12,7 @@ import (
type Ankointerceptor struct {
importMap map[string]interface{}
libMap map[string]interface{}
libhabdles []uintptr
liberalises []uintptr
ankoEnv *env.Env
}
@ -23,11 +23,11 @@ func NewAnkointerceptor(ankoEnv *env.Env) *Ankointerceptor {
a := &Ankointerceptor{
importMap: make(map[string]interface{}),
libMap: make(map[string]interface{}),
libhabdles: make([]uintptr, 0),
liberalises: make([]uintptr, 0),
ankoEnv: ankoEnv,
}
a.ankoEnv.Define("println", fmt.Println)
a.ankoEnv.Define("loadLibrary", func(libPath string, libFuncMap map[string]funcType) interface{} {
_ = a.ankoEnv.Define("println", fmt.Println)
_ = a.ankoEnv.Define("loadLibrary", func(libPath string, libFuncMap map[string]funcType) interface{} {
return a.loadLibrary(libPath, libFuncMap)
})
a.importMap["fs"] = &FileModule{}
@ -96,9 +96,9 @@ func (a *Ankointerceptor) exec(scriptContent string, scriptPath string) (interfa
cdir, _ := os.Getwd()
scriptPath = filepath.Join(cdir, "tmp")
}
e.Define("Require", a.genRequireMethod(scriptPath))
e.Define("__filename", scriptPath)
e.Define("__dirname", filepath.Dir(scriptPath))
_ = e.Define("Require", a.genRequireMethod(scriptPath))
_ = e.Define("__filename", scriptPath)
_ = e.Define("__dirname", filepath.Dir(scriptPath))
// Execute the script code in the prepared environment
result, err := vm.Execute(e, nil, scriptContent)