Modernized dpcore code

- Rewritten dpcore transport object with deprecated API removed
- Optimized concurrent connection counts from 32 to 256
- Updated random port range for plugins
- Added debug output to plugin library
This commit is contained in:
Toby Chui
2025-03-10 22:00:33 +08:00
parent 3e57a90bb6
commit dfd5ef5578
23 changed files with 241 additions and 266 deletions

View File

@@ -2,7 +2,7 @@ package plugins
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
@@ -19,13 +19,11 @@ import (
)
func (m *Manager) StartPlugin(pluginID string) error {
plugin, ok := m.LoadedPlugins.Load(pluginID)
if !ok {
return errors.New("plugin not found")
thisPlugin, err := m.GetPluginByID(pluginID)
if err != nil {
return err
}
thisPlugin := plugin.(*Plugin)
//Get the plugin Entry point
pluginEntryPoint, err := m.GetPluginEntryPoint(thisPlugin.RootDir)
if err != nil {
@@ -91,15 +89,15 @@ func (m *Manager) StartPlugin(pluginID string) error {
}
// Store the cmd object so it can be accessed later for stopping the plugin
plugin.(*Plugin).process = cmd
plugin.(*Plugin).Enabled = true
thisPlugin.process = cmd
thisPlugin.Enabled = true
//Create a new static forwarder router for each of the static capture paths
plugin.(*Plugin).StartAllStaticPathRouters()
thisPlugin.StartAllStaticPathRouters()
//If the plugin contains dynamic capture, create a dynamic capture handler
if thisPlugin.AcceptDynamicRoute() {
plugin.(*Plugin).StartDynamicForwardRouter()
thisPlugin.StartDynamicForwardRouter()
}
return nil
@@ -121,18 +119,17 @@ func (m *Manager) StartUIHandlerForPlugin(targetPlugin *Plugin, pluginListeningP
return err
}
fmt.Println("DEBUG: Requesting Plugin UI URL: ", pluginUIURL)
// Generate the plugin subpath to be trimmed
pluginMatchingPath := filepath.ToSlash(filepath.Join("/plugin.ui/"+targetPlugin.Spec.ID+"/")) + "/"
if targetPlugin.Spec.UIPath != "" {
targetPlugin.uiProxy = dpcore.NewDynamicProxyCore(
pluginUIURL,
pluginMatchingPath,
&dpcore.DpcoreOptions{
IgnoreTLSVerification: true,
},
&dpcore.DpcoreOptions{},
)
targetPlugin.AssignedPort = pluginListeningPort
m.LoadedPlugins.Store(targetPlugin.Spec.ID, targetPlugin)
}
return nil
}
@@ -153,20 +150,25 @@ func (m *Manager) handlePluginSTDOUT(pluginID string, line string) {
// StopPlugin stops a plugin, it is garanteed that the plugin is stopped after this function
func (m *Manager) StopPlugin(pluginID string) error {
plugin, ok := m.LoadedPlugins.Load(pluginID)
if !ok {
return errors.New("plugin not found")
thisPlugin, err := m.GetPluginByID(pluginID)
if err != nil {
return err
}
thisPlugin := plugin.(*Plugin)
var err error
//Make a GET request to plugin ui path /term to gracefully stop the plugin
if thisPlugin.uiProxy != nil {
requestURI := "http://127.0.0.1:" + strconv.Itoa(thisPlugin.AssignedPort) + "/" + thisPlugin.Spec.UIPath + "/term"
resp, err := http.Get(requestURI)
uiRelativePath := thisPlugin.Spec.UIPath
if !strings.HasPrefix(uiRelativePath, "/") {
uiRelativePath = "/" + uiRelativePath
}
requestURI := "http://127.0.0.1:" + strconv.Itoa(thisPlugin.AssignedPort) + uiRelativePath + "/term"
client := http.Client{
Timeout: 3 * time.Second,
}
resp, err := client.Get(requestURI)
if err != nil {
//Plugin do not support termination request, do it the hard way
// Plugin does not support termination request, do it the hard way
m.Log("Plugin "+thisPlugin.Spec.ID+" termination request failed. Force shutting down", nil)
} else {
defer resp.Body.Close()
@@ -176,7 +178,6 @@ func (m *Manager) StopPlugin(pluginID string) error {
} else {
m.Log("Plugin "+thisPlugin.Spec.ID+" termination request returned status: "+resp.Status, nil)
}
}
}
}
@@ -208,20 +209,20 @@ func (m *Manager) StopPlugin(pluginID string) error {
//Remove the UI proxy
thisPlugin.uiProxy = nil
plugin.(*Plugin).Enabled = false
plugin.(*Plugin).StopAllStaticPathRouters()
plugin.(*Plugin).StopDynamicForwardRouter()
thisPlugin.Enabled = false
thisPlugin.StopAllStaticPathRouters()
thisPlugin.StopDynamicForwardRouter()
return nil
}
// Check if the plugin is still running
func (m *Manager) PluginStillRunning(pluginID string) bool {
plugin, ok := m.LoadedPlugins.Load(pluginID)
if !ok {
plugin, err := m.GetPluginByID(pluginID)
if err != nil {
return false
}
if plugin.(*Plugin).process == nil {
if plugin.process == nil {
return false
}
return plugin.(*Plugin).process.ProcessState == nil
return plugin.process.ProcessState == nil
}