diff --git a/cmd/dkl-local-server/boot-img.go b/cmd/dkl-local-server/boot-img.go index 63d5c7d..5d95783 100644 --- a/cmd/dkl-local-server/boot-img.go +++ b/cmd/dkl-local-server/boot-img.go @@ -109,7 +109,7 @@ func qemuImgBootImg(format string) func(out io.Writer, ctx *renderContext) (err var grubSupportVersion = flag.String("grub-support", "1.1.0", "GRUB support version") func setupBootImage(bootImg *os.File, ctx *renderContext) (err error) { - path, err := ctx.distFetch("grub-support", *grubSupportVersion) + path, err := distFetch("grub-support", *grubSupportVersion) if err != nil { return } diff --git a/cmd/dkl-local-server/boot-tar.go b/cmd/dkl-local-server/boot-tar.go index 3a8640c..74b1df2 100644 --- a/cmd/dkl-local-server/boot-tar.go +++ b/cmd/dkl-local-server/boot-tar.go @@ -31,7 +31,7 @@ func buildBootTar(out io.Writer, ctx *renderContext) (err error) { } // kernel - kernelPath, err := ctx.distFetch("kernels", ctx.Host.Kernel) + kernelPath, err := distFetch("kernels", ctx.Host.Kernel) if err != nil { return } @@ -92,7 +92,7 @@ func buildBootEFITar(out io.Writer, ctx *renderContext) (err error) { } // kernel - kernelPath, err := ctx.distFetch("kernels", ctx.Host.Kernel) + kernelPath, err := distFetch("kernels", ctx.Host.Kernel) if err != nil { return } diff --git a/cmd/dkl-local-server/bootv2.go b/cmd/dkl-local-server/bootv2.go index 9f33c8f..33f92ac 100644 --- a/cmd/dkl-local-server/bootv2.go +++ b/cmd/dkl-local-server/bootv2.go @@ -42,7 +42,7 @@ func buildInitrd(out io.Writer, ctx *renderContext) (err error) { cat := cpiocat.New(out) // initrd - initrdPath, err := ctx.distFetch("initrd", ctx.Host.Initrd) + initrdPath, err := distFetch("initrd", ctx.Host.Initrd) if err != nil { return } @@ -54,7 +54,7 @@ func buildInitrd(out io.Writer, ctx *renderContext) (err error) { case "modules": layerVersion := ctx.Host.Versions[layer] - modulesPath, err := ctx.distFetch("layers", layer, layerVersion) + modulesPath, err := distFetch("layers", layer, layerVersion) if err != nil { return err } @@ -165,7 +165,7 @@ func buildBootstrap(out io.Writer, ctx *renderContext) (err error) { return fmt.Errorf("layer %q not mapped to a version", layer) } - outPath, err := ctx.distFetch("layers", layer, layerVersion) + outPath, err := distFetch("layers", layer, layerVersion) if err != nil { return err } diff --git a/cmd/dkl-local-server/kernel.go b/cmd/dkl-local-server/kernel.go index 430aa63..035e8bd 100644 --- a/cmd/dkl-local-server/kernel.go +++ b/cmd/dkl-local-server/kernel.go @@ -8,7 +8,7 @@ import ( ) func renderKernel(w http.ResponseWriter, r *http.Request, ctx *renderContext) error { - path, err := ctx.distFetch("kernels", ctx.Host.Kernel) + path, err := distFetch("kernels", ctx.Host.Kernel) if err != nil { return err } @@ -19,7 +19,7 @@ func renderKernel(w http.ResponseWriter, r *http.Request, ctx *renderContext) er } func fetchKernel(out io.Writer, ctx *renderContext) (err error) { - path, err := ctx.distFetch("kernels", ctx.Host.Kernel) + path, err := distFetch("kernels", ctx.Host.Kernel) if err != nil { return err } diff --git a/cmd/dkl-local-server/main.go b/cmd/dkl-local-server/main.go index 55a996e..b72663f 100644 --- a/cmd/dkl-local-server/main.go +++ b/cmd/dkl-local-server/main.go @@ -72,7 +72,7 @@ func main() { staticHandler := http.FileServer(http.FS(dlshtml.FS)) http.Handle("/favicon.ico", staticHandler) http.Handle("/ui/", staticHandler) - http.Handle("/dist/", http.StripPrefix("/dist/", http.FileServer(http.Dir(*dataDir+"/dist")))) + http.Handle("/dist/", http.StripPrefix("/dist/", upstreamServer{})) http.Handle("/public-state", streamsse.StreamHandler(wPublicState)) http.Handle("/state", requireAdmin(streamsse.StreamHandler(wState))) diff --git a/cmd/dkl-local-server/render-context.go b/cmd/dkl-local-server/render-context.go index 88b1b70..b70a7c0 100644 --- a/cmd/dkl-local-server/render-context.go +++ b/cmd/dkl-local-server/render-context.go @@ -138,7 +138,7 @@ func (ctx *renderContext) render(templateText string) (ba []byte, err error) { return } -func (ctx *renderContext) distFilePath(path ...string) string { +func distFilePath(path ...string) string { return filepath.Join(append([]string{*dataDir, "dist"}, path...)...) } diff --git a/cmd/dkl-local-server/upstream.go b/cmd/dkl-local-server/upstream.go index 7a5aa0e..0abf486 100644 --- a/cmd/dkl-local-server/upstream.go +++ b/cmd/dkl-local-server/upstream.go @@ -9,9 +9,11 @@ import ( "log" "net/http" "os" + "path" gopath "path" "path/filepath" "strconv" + "strings" "time" "github.com/dustin/go-humanize" @@ -22,8 +24,22 @@ var ( upstreamURL = flag.String("upstream", "https://dkl.novit.io/dist", "Upstream server for dist elements") ) -func (ctx *renderContext) distFetch(path ...string) (outPath string, err error) { - outPath = ctx.distFilePath(path...) +type upstreamServer struct{} + +func (_ upstreamServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { + path := path.Clean(req.URL.Path) + outPath, err := distFetch(strings.Split(path, "/")...) + if err != nil { + w.WriteHeader(http.StatusBadGateway) + w.Write([]byte(err.Error() + "\n")) + return + } + + http.ServeFile(w, req, outPath) +} + +func distFetch(path ...string) (outPath string, err error) { + outPath = distFilePath(path...) if _, err = os.Stat(outPath); err == nil { return