* fix(app,ui,overlay,ocr,cli): harden autoinstall auth, upgrade defaults, and lifecycle after #134 * fix(app,cli,ui): address PR #210 review findings and keep default auto-install enabled
137 lines
3.8 KiB
Go
137 lines
3.8 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/control"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
func (a *App) startPluginControlServer() {
|
|
a.mu.Lock()
|
|
if a.ctx == nil || a.pluginMgr == nil || a.controlServer != nil {
|
|
a.mu.Unlock()
|
|
return
|
|
}
|
|
a.mu.Unlock()
|
|
|
|
currentManager := func() *plugin.Manager {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
return a.pluginMgr
|
|
}
|
|
server, err := control.ListenWithOptions(control.PipeName(), func(ctx context.Context, pluginID, channel string, message json.RawMessage) (json.RawMessage, error) {
|
|
if pluginID == control.HostPluginID && channel == control.ChannelAutoInstall {
|
|
return a.handleControlAutoInstall(ctx, message)
|
|
}
|
|
mgr := currentManager()
|
|
if mgr == nil {
|
|
return nil, errors.New("plugin manager is not ready")
|
|
}
|
|
return mgr.ChannelMessage(ctx, pluginID, channel, control.SessionFrom(ctx), message)
|
|
}, control.Options{OnSessionEnd: func(session string, targets []control.Target) {
|
|
mgr := currentManager()
|
|
if mgr == nil {
|
|
return
|
|
}
|
|
for _, target := range targets {
|
|
if target.PluginID == control.HostPluginID {
|
|
continue
|
|
}
|
|
mgr.ChannelSessionClosed(target.PluginID, target.Channel, session)
|
|
}
|
|
}})
|
|
if err != nil {
|
|
a.emitLog("plugin", "Could not open local plugin control channel: "+err.Error())
|
|
return
|
|
}
|
|
|
|
a.mu.Lock()
|
|
if a.ctx == nil || a.pluginMgr == nil || a.controlServer != nil {
|
|
a.mu.Unlock()
|
|
_ = server.Close()
|
|
return
|
|
}
|
|
a.controlServer = server
|
|
a.mu.Unlock()
|
|
}
|
|
|
|
func sameDataRoot(aPath, bPath string) bool {
|
|
aClean := filepath.Clean(strings.TrimSpace(aPath))
|
|
bClean := filepath.Clean(strings.TrimSpace(bPath))
|
|
if runtime.GOOS == "windows" {
|
|
return strings.EqualFold(aClean, bClean)
|
|
}
|
|
return aClean == bClean
|
|
}
|
|
|
|
func (a *App) handleControlAutoInstall(ctx context.Context, message json.RawMessage) (json.RawMessage, error) {
|
|
var req control.AutoInstallRequest
|
|
if err := json.Unmarshal(message, &req); err != nil {
|
|
return nil, fmt.Errorf("invalid auto-install control request: %w", err)
|
|
}
|
|
action := strings.ToLower(strings.TrimSpace(req.Action))
|
|
a.mu.Lock()
|
|
appRoot := ""
|
|
if a.cfg != nil {
|
|
appRoot = a.cfg.DataRoot
|
|
}
|
|
a.mu.Unlock()
|
|
reqRoot := strings.TrimSpace(req.DataRoot)
|
|
mutating := action == "resume" || action == "stop"
|
|
if appRoot != "" && ((mutating && reqRoot == "") || (reqRoot != "" && !sameDataRoot(reqRoot, appRoot))) {
|
|
return nil, errors.New("data_root_mismatch: running desktop app uses a different data directory")
|
|
}
|
|
var (
|
|
res any
|
|
err error
|
|
)
|
|
switch action {
|
|
case "status":
|
|
if strings.TrimSpace(req.VMName) == "" {
|
|
res, err = a.listAutoInstallStatuses(req.PluginID)
|
|
} else {
|
|
res, err = a.GetAutoInstallStatus(req.PluginID, req.DriverID, req.VMName)
|
|
}
|
|
case "resume":
|
|
if strings.TrimSpace(req.VMName) == "" {
|
|
return nil, errors.New("vm name is required for auto-install resume")
|
|
}
|
|
res, err = a.resumeAutoInstallFromControl(ctx, req.PluginID, req.DriverID, req.VMName)
|
|
case "stop":
|
|
if strings.TrimSpace(req.VMName) == "" {
|
|
return nil, errors.New("vm name is required for auto-install stop")
|
|
}
|
|
if err = a.StopAutoInstall(req.PluginID, req.DriverID, req.VMName); err == nil {
|
|
res = map[string]any{"ok": true}
|
|
}
|
|
case "diagnostic":
|
|
if strings.TrimSpace(req.VMName) == "" {
|
|
return nil, errors.New("vm name is required for auto-install diagnostic")
|
|
}
|
|
res, err = a.GetAutoInstallDiagnosticSummary(req.PluginID, req.DriverID, req.VMName)
|
|
default:
|
|
return nil, fmt.Errorf("unknown auto-install control action %q", req.Action)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return json.Marshal(res)
|
|
}
|
|
|
|
func (a *App) stopPluginControlServer() {
|
|
a.mu.Lock()
|
|
server := a.controlServer
|
|
a.controlServer = nil
|
|
a.mu.Unlock()
|
|
if server != nil {
|
|
_ = server.Close()
|
|
}
|
|
}
|