Compare commits
4 commits
main
...
eh-steve/g
Author | SHA1 | Date | |
---|---|---|---|
1228feddc8 | |||
c879b68f06 | |||
e2557da13f | |||
31d3dadc52 |
8 changed files with 161 additions and 49 deletions
21
README.md
21
README.md
|
@ -1,2 +1,19 @@
|
||||||
tests for https://github.com/pkujhd/goloader and https://github.com/eh-steve/goloader.
|
This branch uses unofficial goloader implementation, see https://pkg.go.dev/github.com/eh-steve/goloader, https://github.com/eh-steve/goloader and https://git.aliberksandikci.com.tr/Liderahenk/ahenk-docs/src/branch/main/dev/resources.md#goloader-plugin-logic for more information
|
||||||
contains some parts from https://git.aliberksandikci.com.tr/Liderahenk/ahenk-go
|
|
||||||
|
|
||||||
|
---
|
||||||
|
#### When building for windows:
|
||||||
|
- change `.vscode/settings.json` to `"go.buildFlags": ["-tags=windows"],`
|
||||||
|
- change `cmd/go-loader-test/plugin-manager.go` to `BuildEnv: []string{"GOOS=windows"},`
|
||||||
|
|
||||||
|
---
|
||||||
|
# Pros
|
||||||
|
- Fast, Lightweight
|
||||||
|
- **Can be unload**
|
||||||
|
- **Can be use with windows**
|
||||||
|
- Can build go programs in runtime
|
||||||
|
# Cons
|
||||||
|
- Unofficial
|
||||||
|
- Changes default go configs and compiler settings !!
|
||||||
|
- Missing Security features ?
|
||||||
|
- Missing feature: use precompiled binaries
|
|
@ -6,13 +6,26 @@ import (
|
||||||
"github.com/eh-steve/goloader/jit"
|
"github.com/eh-steve/goloader/jit"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// plugins/addsymbol
|
||||||
|
type AddSymbol interface {
|
||||||
|
st() map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type myInt2 map[string]interface {
|
||||||
|
Symbol(a, b string) (interface{}, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type STR struct {
|
||||||
|
ress func(a string, b string) (interface{}, error)
|
||||||
|
}
|
||||||
|
|
||||||
func PluginManager(params ...string) {
|
func PluginManager(params ...string) {
|
||||||
conf := jit.BuildConfig{
|
conf := jit.BuildConfig{
|
||||||
KeepTempFiles: false, // Files are copied/written to a temp dir to ensure it is writable. This retains the temporary copies
|
KeepTempFiles: false, // Files are copied/written to a temp dir to ensure it is writable. This retains the temporary copies
|
||||||
ExtraBuildFlags: []string{"-x"}, // Flags passed to go build command
|
ExtraBuildFlags: []string{"-x"}, // Flags passed to go build command
|
||||||
BuildEnv: []string{"GOOS=windows"}, // Env vars to set for go build toolchain
|
BuildEnv: []string{"GOOS=linux"}, // Env vars to set for go build toolchain
|
||||||
TmpDir: "/tmp/inner", // To control where temporary files are copied
|
TmpDir: "/tmp/inner", // To control where temporary files are copied
|
||||||
DebugLog: true, //
|
DebugLog: false, //
|
||||||
}
|
}
|
||||||
|
|
||||||
loadable, err := jit.BuildGoPackage(conf, "plugins/tmptest/")
|
loadable, err := jit.BuildGoPackage(conf, "plugins/tmptest/")
|
||||||
|
@ -26,12 +39,6 @@ func PluginManager(params ...string) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
// defer func() {
|
|
||||||
// err = module.Unload()
|
|
||||||
// if err != nil {
|
|
||||||
// panic(err)
|
|
||||||
// }
|
|
||||||
// }()
|
|
||||||
switch f := symbols["MyFunc"].(type) {
|
switch f := symbols["MyFunc"].(type) {
|
||||||
case func([]byte) (interface{}, error):
|
case func([]byte) (interface{}, error):
|
||||||
result, err := f([]byte(`{"k":"v"}`))
|
result, err := f([]byte(`{"k":"v"}`))
|
||||||
|
@ -44,8 +51,99 @@ func PluginManager(params ...string) {
|
||||||
panic("Function signature was not what was expected")
|
panic("Function signature was not what was expected")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = module.Unload()
|
// err = module.Unload()
|
||||||
|
// if err != nil {
|
||||||
|
// panic(err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
loadable2, err := jit.BuildGoPackage(conf, "plugins/addsymbol/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module2, err := loadable2.Load()
|
||||||
|
// module.SymbolsByPkg is a map[string]map[string]interface{} of all packages and their exported functions and global vars
|
||||||
|
symbols2 := module2.SymbolsByPkg[loadable2.ImportPath]
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
// rst
|
||||||
|
switch f2 := symbols2["Symbol"].(type) {
|
||||||
|
|
||||||
|
// rstrst
|
||||||
|
case func(a, b string) (interface{}, error):
|
||||||
|
// var mystruct SymbolPlugS
|
||||||
|
|
||||||
|
var mystr STR
|
||||||
|
// var inter myInt
|
||||||
|
|
||||||
|
mystr.ress = f2
|
||||||
|
// inter = mystr.ress.(myInt)
|
||||||
|
// mystr.ress = Symbol
|
||||||
|
|
||||||
|
// result, err := Im("a", "b")
|
||||||
|
// if err != nil {
|
||||||
|
// panic(err)
|
||||||
|
// }
|
||||||
|
fmt.Println(mystr.ress("a", "b"))
|
||||||
|
fmt.Println(&mystr.ress)
|
||||||
|
// fmt.Println(*mystr.ress)
|
||||||
|
// fmt.Println(*tmpresult)
|
||||||
|
// fmt.Println()
|
||||||
|
default:
|
||||||
|
fmt.Println(symbols2)
|
||||||
|
|
||||||
|
panic("Function signature was not what was expected")
|
||||||
|
}
|
||||||
|
|
||||||
|
// deneme := symbols2["Symbol"]
|
||||||
|
// var inter myInt = deneme.(myInt)
|
||||||
|
// inter.Symbol("st", "st")
|
||||||
|
|
||||||
|
// Symbol := symbols2["Symbol"]
|
||||||
|
// if f, ok := Symbol.(func(a, b string) (interface{}, error)); ok {
|
||||||
|
// // fmt.Println(HandlerType(f)("rst", "rstrtd"))
|
||||||
|
// fmt.Println(f("ast", "srd"))
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for i, v := range symbols2 {
|
||||||
|
// fmt.Println(i, v)
|
||||||
|
// }
|
||||||
|
// fmt.Println(&symbols2)
|
||||||
|
// fmt.Println(symbols2)
|
||||||
|
// fmt.Println()
|
||||||
|
// fmt.Println(symbols)
|
||||||
|
// fmt.Println(&symbols)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
err = module2.Unload()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
var empt interface{} = symbols2
|
||||||
|
var inter myInt = empt.(myInt)
|
||||||
|
// inter.Symbol("a", "b")
|
||||||
|
fmt.Println(inter)
|
||||||
|
fmt.Println(symbols2["Symbol"])
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
type myInt interface {
|
||||||
|
// Symbol(a, b string) (interface{}, error)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"plugin"
|
|
||||||
|
|
||||||
"git.aliberksandikci.com.tr/asandikci/go-loader-test/pkg/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
func LoadPlugin(plugName string, chn chan interface{}) {
|
|
||||||
|
|
||||||
plug, err := plugin.Open(PluginDir + plugName + ".so")
|
|
||||||
utils.Check(err)
|
|
||||||
|
|
||||||
symPlug, err := plug.Lookup(utils.FirstUpperEN(plugName) + "Connect")
|
|
||||||
utils.Check(err)
|
|
||||||
|
|
||||||
var plugOut interface{}
|
|
||||||
plugOut, ok := symPlug.(interface{})
|
|
||||||
if !ok {
|
|
||||||
fmt.Println("unexpected type from module symbol")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
chn <- plugOut
|
|
||||||
}
|
|
BIN
go-loader-test
Executable file
BIN
go-loader-test
Executable file
Binary file not shown.
9
plugins/addsymbol/symbol.go
Normal file
9
plugins/addsymbol/symbol.go
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
package addsymbol
|
||||||
|
|
||||||
|
func Symbol(a, b string) (interface{}, error) {
|
||||||
|
return symbol(a, b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TMPFUNC() {
|
||||||
|
|
||||||
|
}
|
7
plugins/addsymbol/symbol_linux.go
Normal file
7
plugins/addsymbol/symbol_linux.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
// go:build linux && !windows
|
||||||
|
|
||||||
|
package addsymbol
|
||||||
|
|
||||||
|
func symbol(a, b string) string {
|
||||||
|
return a + "+" + b + "=linux"
|
||||||
|
}
|
7
plugins/addsymbol/symbol_windows.go
Normal file
7
plugins/addsymbol/symbol_windows.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
// go:build windows
|
||||||
|
|
||||||
|
package addsymbol
|
||||||
|
|
||||||
|
func symbol(a, b string) string {
|
||||||
|
return a + "-" + b + "=windows"
|
||||||
|
}
|
|
@ -1,11 +1,11 @@
|
||||||
package mypackage
|
package mypackage
|
||||||
|
|
||||||
func Info() map[string]interface{} {
|
// func Info() map[string]interface{} {
|
||||||
return map[string]interface{}{
|
// return map[string]interface{}{
|
||||||
"name": "tmptest",
|
// "name": "tmptest",
|
||||||
"version": "1.0.2",
|
// "version": "1.0.2",
|
||||||
"support": "any",
|
// "support": "any",
|
||||||
"description": "Temporary testing",
|
// "description": "Temporary testing",
|
||||||
"developer": "asandikci@aliberksandikci.com.tr",
|
// "developer": "asandikci@aliberksandikci.com.tr",
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
Loading…
Reference in a new issue