代码拉取完成,页面将自动刷新
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
_ "net/http/pprof"
neturl "net/url"
"strings"
"github.com/gorilla/mux"
"github.com/rancher/ui-plugin-operator/pkg/controllers"
"github.com/rancher/ui-plugin-operator/pkg/controllers/plugin"
"github.com/rancher/ui-plugin-operator/pkg/crd"
"github.com/rancher/ui-plugin-operator/pkg/version"
command "github.com/rancher/wrangler-cli"
_ "github.com/rancher/wrangler/v2/pkg/generated/controllers/apiextensions.k8s.io"
_ "github.com/rancher/wrangler/v2/pkg/generated/controllers/networking.k8s.io"
"github.com/rancher/wrangler/v2/pkg/kubeconfig"
"github.com/rancher/wrangler/v2/pkg/ratelimit"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
debugConfig command.DebugConfig
)
type PluginOperator struct {
Kubeconfig string `usage:"Kubeconfig file" env:"KUBECONFIG"`
Namespace string `usage:"Namespace to watch for UIPlugins" default:"cattle-ui-plugin-system" env:"NAMESPACE"`
ControllerName string `usage:"Unique name to identify this controller that is added to all UIPlugins tracked by this controller" default:"ui-plugin-operator" env:"CONTROLLER_NAME"`
NodeName string `usage:"Name of the node this controller is running on" env:"NODE_NAME"`
}
func (a *PluginOperator) Run(cmd *cobra.Command, _ []string) error {
if len(a.Namespace) == 0 {
return fmt.Errorf("helm-locker can only be started in a single namespace")
}
go func() {
logrus.Println(http.ListenAndServe(":6060", nil))
}()
debugConfig.MustSetupDebug()
cfg := kubeconfig.GetNonInteractiveClientConfig(a.Kubeconfig)
clientConfig, err := cfg.ClientConfig()
if err != nil {
return err
}
clientConfig.RateLimiter = ratelimit.None
ctx := cmd.Context()
if err := crd.Create(ctx, clientConfig); err != nil {
return err
}
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/index.json", http.StatusPermanentRedirect)
})
r.HandleFunc("/index.json", indexHandler)
r.HandleFunc("/{name}/{version}/{rest:.*}", pluginHandler)
http.Handle("/", r)
go func() {
log.Println(http.ListenAndServe(":8080", nil))
}()
if err := controllers.Register(ctx, a.Namespace, a.ControllerName, a.NodeName, cfg); err != nil {
return err
}
<-cmd.Context().Done()
return nil
}
func main() {
cmd := command.Command(&PluginOperator{}, cobra.Command{
Version: version.FriendlyVersion(),
})
cmd = command.AddDebug(cmd, &debugConfig)
command.Main(cmd)
}
func indexHandler(w http.ResponseWriter, _ *http.Request) {
index, err := json.Marshal(&plugin.Index)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logrus.Error(err)
}
w.Write(index)
}
func pluginHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
logrus.Debugf("http request vars %s", vars)
entry, ok := plugin.Index.Entries[vars["name"]]
if !ok || entry.Version != vars["version"] {
msg := fmt.Sprintf("plugin [name: %s version: %s] does not exist in index", vars["name"], vars["version"])
http.Error(w, msg, http.StatusNotFound)
logrus.Debug(msg)
return
}
if entry.NoCache {
logrus.Debugf("[noCache: %v] proxying request to [endpoint: %v]\n", entry.NoCache, entry.Endpoint)
proxyRequest(entry.Endpoint, vars["rest"], w, r)
} else {
logrus.Debugf("[noCache: %v] serving plugin files from filesystem cache\n", entry.NoCache)
http.FileServer(http.Dir(plugin.FSCacheRootDir)).ServeHTTP(w, r)
}
}
func proxyRequest(target, path string, w http.ResponseWriter, r *http.Request) {
url, err := neturl.Parse(target)
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse url [%s]", target), http.StatusInternalServerError)
return
}
if denylist(url.Host) {
http.Error(w, fmt.Sprintf("url [%s] is forbidden", target), http.StatusForbidden)
return
}
proxy := httputil.NewSingleHostReverseProxy(url)
r.URL.Host = url.Host
r.URL.Scheme = url.Scheme
r.URL.Path = path
r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
r.Host = url.Host
proxy.ServeHTTP(w, r)
}
func denylist(host string) bool {
// temp: is there a way to check if an IP equivalent to localhost is being used?
denied := map[string]struct{}{
"localhost": {},
"127.0.0.1": {},
"0.0.0.0": {},
"": {},
}
hostWithoutPort := strings.Split(host, ":")[0]
_, isDenied := denied[hostWithoutPort]
return isDenied
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。