|
| 1 | +package zoraxy_plugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + "fmt" |
| 6 | + "io/fs" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +type PluginUiRouter struct { |
| 14 | + PluginID string //The ID of the plugin |
| 15 | + TargetFs *embed.FS //The embed.FS where the UI files are stored |
| 16 | + TargetFsPrefix string //The prefix of the embed.FS where the UI files are stored, e.g. /web |
| 17 | + HandlerPrefix string //The prefix of the handler used to route this router, e.g. /ui |
| 18 | +} |
| 19 | + |
| 20 | +// NewPluginEmbedUIRouter creates a new PluginUiRouter with embed.FS |
| 21 | +// The targetFsPrefix is the prefix of the embed.FS where the UI files are stored |
| 22 | +// The targetFsPrefix should be relative to the root of the embed.FS |
| 23 | +// The targetFsPrefix should start with a slash (e.g. /web) that corresponds to the root folder of the embed.FS |
| 24 | +// The handlerPrefix is the prefix of the handler used to route this router |
| 25 | +// The handlerPrefix should start with a slash (e.g. /ui) that matches the http.Handle path |
| 26 | +// All prefix should not end with a slash |
| 27 | +func NewPluginEmbedUIRouter(pluginID string, targetFs *embed.FS, targetFsPrefix string, handlerPrefix string) *PluginUiRouter { |
| 28 | + //Make sure all prefix are in /prefix format |
| 29 | + if !strings.HasPrefix(targetFsPrefix, "/") { |
| 30 | + targetFsPrefix = "/" + targetFsPrefix |
| 31 | + } |
| 32 | + targetFsPrefix = strings.TrimSuffix(targetFsPrefix, "/") |
| 33 | + |
| 34 | + if !strings.HasPrefix(handlerPrefix, "/") { |
| 35 | + handlerPrefix = "/" + handlerPrefix |
| 36 | + } |
| 37 | + handlerPrefix = strings.TrimSuffix(handlerPrefix, "/") |
| 38 | + |
| 39 | + //Return the PluginUiRouter |
| 40 | + return &PluginUiRouter{ |
| 41 | + PluginID: pluginID, |
| 42 | + TargetFs: targetFs, |
| 43 | + TargetFsPrefix: targetFsPrefix, |
| 44 | + HandlerPrefix: handlerPrefix, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func (p *PluginUiRouter) populateCSRFToken(r *http.Request, fsHandler http.Handler) http.Handler { |
| 49 | + //Get the CSRF token from header |
| 50 | + csrfToken := r.Header.Get("X-Zoraxy-Csrf") |
| 51 | + if csrfToken == "" { |
| 52 | + csrfToken = "missing-csrf-token" |
| 53 | + } |
| 54 | + |
| 55 | + //Return the middleware |
| 56 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 57 | + // Check if the request is for an HTML file |
| 58 | + if strings.HasSuffix(r.URL.Path, "/") { |
| 59 | + // Redirect to the index.html |
| 60 | + http.Redirect(w, r, r.URL.Path+"index.html", http.StatusFound) |
| 61 | + return |
| 62 | + } |
| 63 | + if strings.HasSuffix(r.URL.Path, ".html") { |
| 64 | + //Read the target file from embed.FS |
| 65 | + targetFilePath := strings.TrimPrefix(r.URL.Path, "/") |
| 66 | + targetFilePath = p.TargetFsPrefix + "/" + targetFilePath |
| 67 | + targetFilePath = strings.TrimPrefix(targetFilePath, "/") |
| 68 | + targetFileContent, err := fs.ReadFile(*p.TargetFs, targetFilePath) |
| 69 | + if err != nil { |
| 70 | + http.Error(w, "File not found", http.StatusNotFound) |
| 71 | + return |
| 72 | + } |
| 73 | + body := string(targetFileContent) |
| 74 | + body = strings.ReplaceAll(body, "{{.csrfToken}}", csrfToken) |
| 75 | + http.ServeContent(w, r, r.URL.Path, time.Now(), strings.NewReader(body)) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + //Call the next handler |
| 80 | + fsHandler.ServeHTTP(w, r) |
| 81 | + }) |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +// GetHttpHandler returns the http.Handler for the PluginUiRouter |
| 86 | +func (p *PluginUiRouter) Handler() http.Handler { |
| 87 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 88 | + //Remove the plugin UI handler path prefix |
| 89 | + rewrittenURL := r.RequestURI |
| 90 | + rewrittenURL = strings.TrimPrefix(rewrittenURL, p.HandlerPrefix) |
| 91 | + rewrittenURL = strings.ReplaceAll(rewrittenURL, "//", "/") |
| 92 | + r.URL, _ = url.Parse(rewrittenURL) |
| 93 | + r.RequestURI = rewrittenURL |
| 94 | + |
| 95 | + //Serve the file from the embed.FS |
| 96 | + subFS, err := fs.Sub(*p.TargetFs, strings.TrimPrefix(p.TargetFsPrefix, "/")) |
| 97 | + if err != nil { |
| 98 | + fmt.Println(err.Error()) |
| 99 | + http.Error(w, "Internal Server Error", http.StatusInternalServerError) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + // Replace {{csrf_token}} with the actual CSRF token and serve the file |
| 104 | + p.populateCSRFToken(r, http.FileServer(http.FS(subFS))).ServeHTTP(w, r) |
| 105 | + }) |
| 106 | +} |
0 commit comments