From 0f0f5f600c6141fe5f8067e59ec2880903855227 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Tue, 7 Oct 2025 15:07:37 -0700 Subject: Make redirects configurable --- roseh.moe.go | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'roseh.moe.go') diff --git a/roseh.moe.go b/roseh.moe.go index 9f63d65..81285e7 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "bytes" "crypto/hmac" "crypto/rand" @@ -17,6 +18,7 @@ import ( "log" "net/http" "os" + "path/filepath" "strings" "sync" "time" @@ -26,9 +28,10 @@ import ( ) var ( - port = flag.Int("port", 42069, "port to listen on") - selfURL = flag.String("self-url", "http://localhost:42069", "base URL of the server") - secretsFile = flag.String("secrets", "secrets", "secrets file") + port = flag.Int("port", 42069, "port to listen on") + selfURL = flag.String("self-url", "http://localhost:42069", "base URL of the server") + secretsFile = flag.String("secrets", "secrets", "secrets file") + redirectsDir = flag.String("redirect", "redirect", "directory containing Go package redirects") serverStartTime = time.Now() ) @@ -452,14 +455,26 @@ func autosave(w http.ResponseWriter, r *http.Request) { } } +func findRedirect(kind, name string) (string, bool) { + f, err := os.Open(filepath.Join(*redirectsDir, kind)) + if err != nil { + return "", false + } + defer f.Close() + scanner := bufio.NewScanner(f) + pfx := []byte(name + "\t") + for scanner.Scan() { + line := scanner.Bytes() + if bytes.HasPrefix(line, pfx) { + return string(line[len(pfx):]), true + } + } + return "", false +} + func cmd(w http.ResponseWriter, r *http.Request) { - var source string - switch r.PathValue("cmd") { - case "hole": - source = "git https://gitlab.com/rhogenson/hole.git" - case "trash": - source = "git https://github.com/rhogenson/trash.git" - default: + source, ok := findRedirect("cmd", r.PathValue("cmd")) + if !ok { notFound(w, r) return } @@ -471,11 +486,8 @@ func cmd(w http.ResponseWriter, r *http.Request) { } func pkg(w http.ResponseWriter, r *http.Request) { - var source string - switch r.PathValue("pkg") { - case "wordlist": - source = "git https://gitlab.com/rhogenson/wordlist.git" - default: + source, ok := findRedirect("pkg", r.PathValue("pkg")) + if !ok { notFound(w, r) return } -- cgit v1.3.1