2021-09-15 08:36:14 +00:00
|
|
|
package dashboard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
2024-11-08 11:12:35 +00:00
|
|
|
"strings"
|
2021-09-15 08:36:14 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/webui"
|
2021-09-15 08:36:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Handler expose dashboard routes.
|
|
|
|
type Handler struct {
|
|
|
|
assets fs.FS // optional assets, to override the webui.FS default
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append adds dashboard routes on the given router, optionally using the given
|
|
|
|
// assets (or webui.FS otherwise).
|
|
|
|
func Append(router *mux.Router, customAssets fs.FS) {
|
|
|
|
assets := customAssets
|
|
|
|
if assets == nil {
|
|
|
|
assets = webui.FS
|
|
|
|
}
|
|
|
|
// Expose dashboard
|
|
|
|
router.Methods(http.MethodGet).
|
|
|
|
Path("/").
|
|
|
|
HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
2024-11-08 11:12:35 +00:00
|
|
|
prefix := strings.TrimSuffix(req.Header.Get("X-Forwarded-Prefix"), "/")
|
|
|
|
http.Redirect(resp, req, prefix+"/dashboard/", http.StatusFound)
|
2021-09-15 08:36:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
router.Methods(http.MethodGet).
|
|
|
|
PathPrefix("/dashboard/").
|
|
|
|
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// allow iframes from our domains only
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
|
|
|
|
w.Header().Set("Content-Security-Policy", "frame-src 'self' https://traefik.io https://*.traefik.io;")
|
2022-12-09 07:24:05 +00:00
|
|
|
|
|
|
|
// The content type must be guessed by the file server.
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options
|
|
|
|
w.Header().Del("Content-Type")
|
|
|
|
|
2024-03-25 19:22:05 +00:00
|
|
|
http.StripPrefix("/dashboard/", http.FileServerFS(assets)).ServeHTTP(w, r)
|
2021-09-15 08:36:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assets := g.assets
|
|
|
|
if assets == nil {
|
|
|
|
assets = webui.FS
|
|
|
|
}
|
|
|
|
// allow iframes from our domains only
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
|
|
|
|
w.Header().Set("Content-Security-Policy", "frame-src 'self' https://traefik.io https://*.traefik.io;")
|
2022-12-29 08:46:04 +00:00
|
|
|
|
|
|
|
// The content type must be guessed by the file server.
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options
|
|
|
|
w.Header().Del("Content-Type")
|
|
|
|
|
2024-03-25 19:22:05 +00:00
|
|
|
http.FileServerFS(assets).ServeHTTP(w, r)
|
2021-09-15 08:36:14 +00:00
|
|
|
}
|