zoraxy/src/mod/plugins/uirouter.go
Toby Chui 39d6d16c2a Updated plugin interface
- Updated plugin interface to support static path routing
- Added autosave for statistic data (workaround for #561)
2025-03-02 09:15:50 +08:00

59 lines
1.5 KiB
Go

package plugins
import (
"net/http"
"net/url"
"strconv"
"strings"
"imuslab.com/zoraxy/mod/dynamicproxy/dpcore"
"imuslab.com/zoraxy/mod/utils"
)
// HandlePluginUI handles the request to the plugin UI
// This function will route the request to the correct plugin UI handler
func (m *Manager) HandlePluginUI(pluginID string, w http.ResponseWriter, r *http.Request) {
plugin, err := m.GetPluginByID(pluginID)
if err != nil {
utils.SendErrorResponse(w, err.Error())
return
}
//Check if the plugin has UI
if plugin.Spec.UIPath == "" {
utils.SendErrorResponse(w, "Plugin does not have UI")
return
}
//Check if the plugin has UI handler
if plugin.uiProxy == nil {
utils.SendErrorResponse(w, "Plugin does not have UI handler")
return
}
upstreamOrigin := "127.0.0.1:" + strconv.Itoa(plugin.AssignedPort)
matchingPath := "/plugin.ui/" + plugin.Spec.ID
//Rewrite the request path to the plugin UI path
rewrittenURL := r.RequestURI
rewrittenURL = strings.TrimPrefix(rewrittenURL, matchingPath)
rewrittenURL = strings.ReplaceAll(rewrittenURL, "//", "/")
if rewrittenURL == "" {
rewrittenURL = "/"
}
r.URL, _ = url.Parse(rewrittenURL)
//Call the plugin UI handler
plugin.uiProxy.ServeHTTP(w, r, &dpcore.ResponseRewriteRuleSet{
UseTLS: false,
OriginalHost: r.Host,
ProxyDomain: upstreamOrigin,
NoCache: true,
PathPrefix: matchingPath,
Version: m.Options.SystemConst.ZoraxyVersion,
UpstreamHeaders: [][]string{
{"X-Zoraxy-Csrf", m.Options.CSRFTokenGen(r)},
},
})
}