embed and unpack gloomi in a decent way

This commit is contained in:
Zoe
2025-05-19 15:50:53 +00:00
parent a8ec911f74
commit e65a30f0e4
7 changed files with 34 additions and 42 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,3 @@
plugs
dist
**/*.so
gloomd

View File

@@ -44,7 +44,7 @@ func (p *GLoomI) RegisterRoutes(router fiber.Router) {
apiRouter.Get("/plugins", func(c fiber.Ctx) error {
plugins, err := GetPlugins(p.client)
if err != nil {
fmt.Printf("plugs: %+v\n", plugins)
fmt.Printf("Loaded plugins: %+v\n", plugins)
return c.Status(fiber.StatusInternalServerError).SendString("Failed to list plugins: " + err.Error())
}

1
go.mod
View File

@@ -3,7 +3,6 @@ module github.com/juls0730/gloom
go 1.24.2
require (
github.com/joho/godotenv v1.5.1
github.com/juls0730/sentinel v0.0.0-20250515154110-2e7e6586cacd
github.com/mattn/go-sqlite3 v1.14.24
)

2
go.sum
View File

@@ -1,7 +1,5 @@
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/juls0730/sentinel v0.0.0-20250515154110-2e7e6586cacd h1:JNazPdlAs307Gtaqmb+wfCjcOzO3MRXxg9nf0bAKAnc=
github.com/juls0730/sentinel v0.0.0-20250515154110-2e7e6586cacd/go.mod h1:CnRvcleiS2kvK1N2PeQmeoRP5EXpBDpHPkg72vAUaSg=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=

68
main.go
View File

@@ -20,7 +20,6 @@ import (
"time"
"github.com/BurntSushi/toml"
"github.com/joho/godotenv"
"github.com/juls0730/gloom/libs"
"github.com/juls0730/sentinel"
_ "github.com/mattn/go-sqlite3"
@@ -41,6 +40,8 @@ type PreloadPlugin struct {
Domains []string `json:"domains"`
}
const DEFAULT_PLUGIN_DIR = "plugs"
type GLoom struct {
// path to the /tmp directory where the pluginHost binary is unpacked, and where the pluginHost sockets are created
tmpDir string
@@ -99,26 +100,6 @@ func NewGloom(proxyManager *sentinel.ProxyManager) (*GLoom, error) {
return nil, err
}
pluginDir := filepath.Join(gloomDir, "plugs")
if err := os.MkdirAll(pluginDir, 0755); err != nil {
return nil, err
}
// if gloomi is built into the binary
if _, err := embeddedAssets.Open("dist/gloomi.so"); err == nil {
// and if the plugin doesn't exist, copy it over
if _, err := os.Stat(filepath.Join(pluginDir, "gloomi.so")); os.IsNotExist(err) {
gloomiData, err := embeddedAssets.ReadFile("dist/gloomi.so")
if err != nil {
return nil, err
}
if err := os.WriteFile(filepath.Join(pluginDir, "gloomi.so"), gloomiData, 0755); err != nil {
return nil, err
}
}
}
tmpDir, err := os.MkdirTemp(os.TempDir(), "gloom")
if err != nil {
return nil, err
@@ -135,7 +116,6 @@ func NewGloom(proxyManager *sentinel.ProxyManager) (*GLoom, error) {
gloom := &GLoom{
tmpDir: tmpDir,
gloomDir: gloomDir,
pluginDir: pluginDir,
plugins: libs.SyncMap[string, *PluginHost]{},
DB: db,
ProxyManager: proxyManager,
@@ -145,6 +125,26 @@ func NewGloom(proxyManager *sentinel.ProxyManager) (*GLoom, error) {
return nil, err
}
if err := os.MkdirAll(gloom.pluginDir, 0755); err != nil {
return nil, err
}
// if gloomi is built into the binary
if _, err := embeddedAssets.Open("dist/gloomi.so"); err == nil {
// and if the plugin doesn't exist, copy it over
// TODO: instead, check if the plugin doesnt exist OR the binary has a newer timestamp than the current version
if _, err := os.Stat(filepath.Join(gloom.pluginDir, "gloomi.so")); os.IsNotExist(err) {
gloomiData, err := embeddedAssets.ReadFile("dist/gloomi.so")
if err != nil {
return nil, err
}
if err := os.WriteFile(filepath.Join(gloom.pluginDir, "gloomi.so"), gloomiData, 0755); err != nil {
return nil, err
}
}
}
return gloom, nil
}
@@ -168,6 +168,14 @@ func (gloom *GLoom) loadConfig() error {
return err
}
var ok bool
gloom.pluginDir, ok = config.(map[string]any)["pluginDir"].(string)
if !ok || gloom.pluginDir == "" {
gloom.pluginDir = DEFAULT_PLUGIN_DIR
}
gloom.pluginDir = filepath.Join(gloom.gloomDir, gloom.pluginDir)
proloadPlugins, ok := config.(map[string]any)["plugins"].([]map[string]any)
if ok {
for _, plugin := range proloadPlugins {
@@ -596,14 +604,8 @@ func (rpc *GloomRPC) UploadPlugin(plugin PluginUpload, reply *string) error {
}
}
plugsDir := "plugs"
if os.Getenv("PLUGINS_DIR") != "" {
plugsDir = os.Getenv("PLUGINS_DIR")
}
if _, err := os.Stat(plugsDir); os.IsNotExist(err) {
if err := os.Mkdir(plugsDir, 0755); err != nil {
if _, err := os.Stat(rpc.gloom.pluginDir); os.IsNotExist(err) {
if err := os.Mkdir(rpc.gloom.pluginDir, 0755); err != nil {
*reply = "Plugin upload failed"
return err
}
@@ -668,12 +670,6 @@ func (rpc *GloomRPC) DeletePlugin(pluginName string, reply *string) error {
return nil
}
func init() {
if err := godotenv.Load(); err != nil {
fmt.Println("No .env file found")
}
}
func main() {
debug, err := strconv.ParseBool(os.Getenv("DEBUG"))
if err != nil {

View File

@@ -10,7 +10,7 @@
"build:gloomi": "sh -c \"cd gloomi; zqdgr build\"",
"build:gloom": "zqdgr build:pluginHost && go build -tags=gloomi -o dist/gloom",
"build:nogloomi": "zqdgr build:pluginHost && go build -tags=!gloomi -o dist/gloom",
"clean": "rm -rf plugs && rm -rf dist && rm -rf plugin/plugin.so",
"clean": "rm -rf dist && rm -rf plugin/plugin.so",
"dev": "zqdgr build && ./dist/gloom"
},
"pattern": "**/*.go",