Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Plugin System #150

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions entities/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package entities

type Plugin interface {
Initialize() error
Execute() error
Cleanup() error
}
44 changes: 44 additions & 0 deletions services/plugin/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package services

import (
"plugin"
"path/filepath"
"CHAOS/entities"
)

type PluginLoader struct {}

func (pl *PluginLoader) LoadPlugins(pluginDir string) ([]entities.Plugin, error) {
var plugins []entities.Plugin

files, err := filepath.Glob(filepath.Join(pluginDir, "*.so"))
if err != nil {
return nil, err
}

for _, file := range files {
p, err := plugin.Open(file)
if err != nil {
return nil, err
}

symPlugin, err := p.Lookup("Plugin")
if err != nil {
return nil, err
}

var plug entities.Plugin
plug, ok := symPlugin.(entities.Plugin)
if !ok {
return nil, fmt.Errorf("unexpected type from module symbol")
}

if err := plug.Initialize(); err != nil {
return nil, err
}

plugins = append(plugins, plug)
}

return plugins, nil
}
43 changes: 43 additions & 0 deletions services/plugin/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package services

import (
"CHAOS/entities"
)

type PluginManager struct {
Plugins []entities.Plugin
Loader *PluginLoader
}

func NewPluginManager() *PluginManager {
return &PluginManager{
Loader: &PluginLoader{},
}
}

func (pm *PluginManager) LoadPlugins(pluginDir string) error {
plugins, err := pm.Loader.LoadPlugins(pluginDir)
if err != nil {
return err
}
pm.Plugins = plugins
return nil
}

func (pm *PluginManager) ExecutePlugins() error {
for _, p := range pm.Plugins {
if err := p.Execute(); err != nil {
return err
}
}
return nil
}

func (pm *PluginManager) CleanupPlugins() error {
for _, p := range pm.Plugins {
if err := p.Cleanup(); err != nil {
return err
}
}
return nil
}
1 change: 1 addition & 0 deletions services/plugin/network
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions web/plugin/network
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

23 changes: 23 additions & 0 deletions web/plugin/sample_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import "fmt"

type SamplePlugin struct{}

func (p *SamplePlugin) Initialize() error {
fmt.Println("Sample plugin initialized")
return nil
}

func (p *SamplePlugin) Execute() error {
fmt.Println("Sample plugin executed")
return nil
}

func (p *SamplePlugin) Cleanup() error {
fmt.Println("Sample plugin cleaned up")
return nil
}

// This is important! Go plugins must export a variable named "Plugin"
var Plugin SamplePlugin