Skip to content
quarry Artifact [6a23c71f4c]
Login

Artifact [6a23c71f4c]

Artifact 6a23c71f4c9ee39f6a58c8842b70182d7f4ca7ae6fb862a521c8f43235cad097:


package runnerdist

import (
	_ "embed"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"regexp"
	"strings"
)

//go:embed install.sh
var installScript string

// cliName is a client build's served name; each is the same bytes as the
// quarry-runner build for that platform.
var cliName = regexp.MustCompile(`^quarry-(linux|darwin|windows)-(amd64|arm64)(\.exe)?$`)

// CLIFile is the name the client for this platform is served as.
func (p Platform) CLIFile() string {
	name := "quarry-" + p.OS + "-" + p.Arch
	if p.OS == "windows" {
		name += ".exe"
	}
	return name
}

// CLIHandler serves the client download from dir (the same directory as
// the runner builds): GET /cli/{file} for quarry-OS-ARCH, the runner's
// own name, and SHA256SUMS; and GET /install.sh, the installer made for
// origin, the public address of this forge. A forge with no https origin
// has no installer to give.
func CLIHandler(dir, origin string) http.Handler {
	origin = strings.TrimSuffix(origin, "/")
	mux := http.NewServeMux()
	mux.HandleFunc("GET /cli/{file}", func(w http.ResponseWriter, req *http.Request) {
		name := req.PathValue("file")
		switch {
		case name == SumsFile:
			serveSums(w, req, dir)
		case cliName.MatchString(name):
			serveBuild(w, req, filepath.Join(dir, "quarry-runner-"+strings.TrimPrefix(name, "quarry-")), name)
		case fileName.MatchString(name):
			serveBuild(w, req, filepath.Join(dir, name), name)
		default:
			http.NotFound(w, req)
		}
	})
	mux.HandleFunc("GET /install.sh", func(w http.ResponseWriter, req *http.Request) {
		u, err := url.Parse(origin)
		if err != nil || u.Scheme != "https" || u.Host == "" || u.Path != "" || !safeHost.MatchString(u.Host) {
			http.Error(w, "this forge has no public https address set, so it has no installer to give", http.StatusNotFound)
			return
		}
		w.Header().Set("Content-Type", "text/x-shellscript; charset=utf-8")
		w.Header().Set("X-Content-Type-Options", "nosniff")
		w.Header().Set("Cache-Control", "no-cache")
		w.Write([]byte(strings.ReplaceAll(installScript, "@ORIGIN@", origin)))
	})
	return mux
}

// safeHost is a host and port and nothing a shell would read as more.
var safeHost = regexp.MustCompile(`^[A-Za-z0-9.-]+(:[0-9]+)?$`)

func serveBuild(w http.ResponseWriter, req *http.Request, path, name string) {
	f, err := os.Open(path)
	if err != nil {
		http.Error(w, "this forge carries no "+name+" build", http.StatusNotFound)
		return
	}
	defer f.Close()
	st, err := f.Stat()
	if err != nil || st.IsDir() {
		http.NotFound(w, req)
		return
	}
	w.Header().Set("Content-Type", "application/octet-stream")
	w.Header().Set("Content-Disposition", `attachment; filename="`+name+`"`)
	w.Header().Set("X-Content-Type-Options", "nosniff")
	w.Header().Set("Cache-Control", "no-cache")
	http.ServeContent(w, req, name, st.ModTime(), f)
}

// serveSums answers with the runner's checksum list, each quarry-runner
// line given again under the quarry name of the same bytes.
func serveSums(w http.ResponseWriter, req *http.Request, dir string) {
	raw, err := os.ReadFile(filepath.Join(dir, SumsFile))
	if err != nil {
		http.Error(w, "this forge carries no "+SumsFile, http.StatusNotFound)
		return
	}
	var out strings.Builder
	for _, line := range strings.Split(strings.TrimSpace(string(raw)), "\n") {
		hash, name, ok := strings.Cut(line, "  ")
		if !ok || !fileName.MatchString(strings.TrimPrefix(name, "*")) {
			continue
		}
		out.WriteString(line + "\n")
		out.WriteString(hash + "  quarry-" + strings.TrimPrefix(strings.TrimPrefix(name, "*"), "quarry-runner-") + "\n")
	}
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	w.Header().Set("X-Content-Type-Options", "nosniff")
	w.Header().Set("Cache-Control", "no-cache")
	w.Write([]byte(out.String()))
}