2021-03-15 20:02:37 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2021-09-09 19:51:13 +00:00
|
|
|
"fmt"
|
2021-03-15 20:02:37 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
2021-09-09 19:51:13 +00:00
|
|
|
"plugin"
|
2021-06-25 19:57:24 +00:00
|
|
|
"reflect"
|
2021-09-09 19:51:13 +00:00
|
|
|
"strings"
|
2023-06-29 04:44:54 +00:00
|
|
|
|
2021-09-16 20:32:25 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2021-10-10 20:08:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2022-12-08 19:40:22 +00:00
|
|
|
"github.com/openrelayxyz/plugeth-utils/core"
|
2022-07-27 17:38:42 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-09-09 19:51:13 +00:00
|
|
|
)
|
2021-03-15 20:02:37 +00:00
|
|
|
|
2022-12-08 19:40:22 +00:00
|
|
|
type Subcommand func(core.Context, []string) error
|
2021-03-15 20:02:37 +00:00
|
|
|
|
2022-03-14 15:52:22 +00:00
|
|
|
type pluginDetails struct {
|
2023-06-09 10:08:56 +00:00
|
|
|
p *plugin.Plugin
|
2022-03-14 15:52:22 +00:00
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2021-09-09 19:51:13 +00:00
|
|
|
type PluginLoader struct {
|
2022-03-14 15:52:22 +00:00
|
|
|
Plugins []pluginDetails
|
2021-03-15 20:02:37 +00:00
|
|
|
Subcommands map[string]Subcommand
|
2021-09-09 19:51:13 +00:00
|
|
|
Flags []*flag.FlagSet
|
2021-06-26 03:38:04 +00:00
|
|
|
LookupCache map[string][]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PluginLoader) Lookup(name string, validate func(interface{}) bool) []interface{} {
|
2021-09-09 19:51:13 +00:00
|
|
|
if v, ok := pl.LookupCache[name]; ok {
|
|
|
|
return v
|
|
|
|
}
|
2021-06-26 03:38:04 +00:00
|
|
|
results := []interface{}{}
|
|
|
|
for _, plugin := range pl.Plugins {
|
2022-03-14 15:52:22 +00:00
|
|
|
if v, err := plugin.p.Lookup(name); err == nil {
|
2021-06-26 03:38:04 +00:00
|
|
|
if validate(v) {
|
|
|
|
results = append(results, v)
|
2022-03-14 15:52:22 +00:00
|
|
|
} else {
|
|
|
|
log.Warn("Plugin matches hook but not signature", "plugin", plugin.name, "hook", name)
|
2021-06-26 03:38:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pl.LookupCache[name] = results
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func Lookup(name string, validate func(interface{}) bool) []interface{} {
|
|
|
|
if DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Lookup attempted, but PluginLoader is not initialized", "name", name)
|
|
|
|
return []interface{}{}
|
|
|
|
}
|
|
|
|
return DefaultPluginLoader.Lookup(name, validate)
|
2021-03-15 20:02:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 22:05:17 +00:00
|
|
|
var DefaultPluginLoader *PluginLoader
|
2021-06-25 15:57:56 +00:00
|
|
|
|
2021-03-15 20:02:37 +00:00
|
|
|
func NewPluginLoader(target string) (*PluginLoader, error) {
|
2023-06-29 08:12:47 +00:00
|
|
|
log.Info("Loading plugins from directory", "path", target)
|
2021-03-15 20:02:37 +00:00
|
|
|
pl := &PluginLoader{
|
2022-03-14 15:52:22 +00:00
|
|
|
Plugins: []pluginDetails{},
|
2021-03-15 20:02:37 +00:00
|
|
|
Subcommands: make(map[string]Subcommand),
|
2021-09-09 19:51:13 +00:00
|
|
|
Flags: []*flag.FlagSet{},
|
2021-06-26 03:38:04 +00:00
|
|
|
LookupCache: make(map[string][]interface{}),
|
2021-03-15 20:02:37 +00:00
|
|
|
}
|
|
|
|
files, err := ioutil.ReadDir(target)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Could not load plugins directory. Skipping.", "path", target)
|
|
|
|
return pl, nil
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
fpath := path.Join(target, file.Name())
|
|
|
|
if !strings.HasSuffix(file.Name(), ".so") {
|
2023-06-09 10:08:56 +00:00
|
|
|
log.Debug("File in plugin directory is not '.so' file. Skipping.", "file", fpath)
|
2021-03-15 20:02:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
plug, err := plugin.Open(fpath)
|
|
|
|
if err != nil {
|
2023-06-09 10:08:56 +00:00
|
|
|
log.Warn("File in plugin directory could not be loaded", "file", fpath, "error", err)
|
2021-03-15 20:02:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Any type of plugin can potentially specify flags
|
|
|
|
f, err := plug.Lookup("Flags")
|
|
|
|
if err == nil {
|
|
|
|
flagset, ok := f.(*flag.FlagSet)
|
|
|
|
if !ok {
|
|
|
|
log.Warn("Found plugin.Flags, but it its not a *FlagSet", "file", fpath)
|
|
|
|
} else {
|
|
|
|
pl.Flags = append(pl.Flags, flagset)
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 15:57:56 +00:00
|
|
|
sb, err := plug.Lookup("Subcommands")
|
|
|
|
if err == nil {
|
2022-12-08 19:40:22 +00:00
|
|
|
subcommands, ok := sb.(*map[string]func(core.Context, []string) error)
|
2021-03-15 20:02:37 +00:00
|
|
|
if !ok {
|
2022-12-08 19:40:22 +00:00
|
|
|
log.Warn("Could not cast plugin.Subcommands to `map[string]func(core.Context, []string) error`", "file", fpath, "type", reflect.TypeOf(sb))
|
2021-06-25 15:57:56 +00:00
|
|
|
} else {
|
2021-06-25 19:57:24 +00:00
|
|
|
for k, v := range *subcommands {
|
2021-06-25 15:57:56 +00:00
|
|
|
if _, ok := pl.Subcommands[k]; ok {
|
|
|
|
log.Warn("Subcommand redeclared", "file", fpath, "subcommand", k)
|
|
|
|
}
|
|
|
|
pl.Subcommands[k] = v
|
|
|
|
}
|
2021-03-15 20:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-14 15:52:22 +00:00
|
|
|
pl.Plugins = append(pl.Plugins, pluginDetails{plug, fpath})
|
2021-03-15 20:02:37 +00:00
|
|
|
}
|
|
|
|
return pl, nil
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:40:22 +00:00
|
|
|
func Initialize(target string, ctx core.Context) (err error) {
|
2021-06-25 22:05:17 +00:00
|
|
|
DefaultPluginLoader, err = NewPluginLoader(target)
|
2021-09-09 19:51:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-12 19:45:42 +00:00
|
|
|
DefaultPluginLoader.Initialize(ctx)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:40:22 +00:00
|
|
|
func (pl *PluginLoader) Initialize(ctx core.Context) {
|
2021-07-12 19:45:42 +00:00
|
|
|
fns := pl.Lookup("Initialize", func(i interface{}) bool {
|
2022-12-08 19:40:22 +00:00
|
|
|
_, ok := i.(func(core.Context, core.PluginLoader, core.Logger))
|
2021-07-12 19:45:42 +00:00
|
|
|
return ok
|
|
|
|
})
|
|
|
|
for _, fni := range fns {
|
2022-12-08 19:40:22 +00:00
|
|
|
if fn, ok := fni.(func(core.Context, core.PluginLoader, core.Logger)); ok {
|
2021-09-09 19:51:13 +00:00
|
|
|
fn(ctx, pl, log.Root())
|
2021-07-12 19:45:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-25 15:57:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 20:02:37 +00:00
|
|
|
func (pl *PluginLoader) RunSubcommand(ctx *cli.Context) (bool, error) {
|
2022-07-27 17:38:42 +00:00
|
|
|
args := ctx.Args().Slice()
|
2021-09-09 19:51:13 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
return false, fmt.Errorf("no subcommand arguments")
|
|
|
|
}
|
2021-03-15 20:02:37 +00:00
|
|
|
subcommand, ok := pl.Subcommands[args[0]]
|
2021-09-09 19:51:13 +00:00
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf("Subcommand %v does not exist", args[0])
|
|
|
|
}
|
2021-03-15 20:02:37 +00:00
|
|
|
return true, subcommand(ctx, args[1:])
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:57:56 +00:00
|
|
|
func RunSubcommand(ctx *cli.Context) (bool, error) {
|
2021-09-09 19:51:13 +00:00
|
|
|
if DefaultPluginLoader == nil {
|
|
|
|
return false, fmt.Errorf("Plugin loader not initialized")
|
|
|
|
}
|
2021-06-25 22:05:17 +00:00
|
|
|
return DefaultPluginLoader.RunSubcommand(ctx)
|
2021-06-25 15:57:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 20:02:37 +00:00
|
|
|
func (pl *PluginLoader) ParseFlags(args []string) bool {
|
|
|
|
for _, flagset := range pl.Flags {
|
|
|
|
flagset.Parse(args)
|
|
|
|
}
|
|
|
|
return len(pl.Flags) > 0
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:57:56 +00:00
|
|
|
func ParseFlags(args []string) bool {
|
2021-06-25 22:05:17 +00:00
|
|
|
if DefaultPluginLoader == nil {
|
2021-06-25 15:57:56 +00:00
|
|
|
log.Warn("Attempting to parse flags, but default PluginLoader has not been initialized")
|
|
|
|
return false
|
|
|
|
}
|
2021-06-25 22:05:17 +00:00
|
|
|
return DefaultPluginLoader.ParseFlags(args)
|
2021-03-15 20:02:37 +00:00
|
|
|
}
|
2021-09-16 20:32:25 +00:00
|
|
|
|
|
|
|
type feedWrapper struct {
|
|
|
|
feed *event.Feed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *feedWrapper) Send(item interface{}) int {
|
|
|
|
return f.feed.Send(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *feedWrapper) Subscribe(ch interface{}) core.Subscription {
|
|
|
|
return f.feed.Subscribe(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *PluginLoader) GetFeed() core.Feed {
|
|
|
|
return &feedWrapper{&event.Feed{}}
|
|
|
|
}
|