From f3ea96187b9b5cc0042c405125ecc4c78e2fca9f Mon Sep 17 00:00:00 2001 From: gigirassy Date: Sat, 28 Feb 2026 03:43:48 +0100 Subject: [PATCH] crapcode --- main.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 1ce3a74..520465d 100644 --- a/main.go +++ b/main.go @@ -22,18 +22,17 @@ import ( ) const ( - defaultListen = "0.0.0.0:3333" + defaultListen = ":3333" defaultDir = "./images" - defaultWidth = 30 + defaultWidth = 80 cacheSuffixFmt = ".ansi.%d.txt" allowedUA = "curl" maxImageDim = 2000 - redirectURL = "https://example.com" - ) var ( imgDir string + redirectURL string cacheLock sync.RWMutex cachedPaths []string ) @@ -42,6 +41,7 @@ func main() { rand.Seed(time.Now().UnixNano()) imgDir = getenv("IMAGES_DIR", defaultDir) + redirectURL = getenv("REDIRECT_URL", "https://example.com") listen := getenv("LISTEN", defaultListen) if err := os.MkdirAll(imgDir, 0755); err != nil { @@ -55,7 +55,7 @@ func main() { http.HandleFunc("/", rootHandler) - log.Printf("listening on %s — only curl clients accepted", listen) + log.Printf("listening on %s — only curl clients accepted; non-curl will be redirected to %s", listen, redirectURL) if err := http.ListenAndServe(listen, nil); err != nil { log.Fatalf("server failed: %v", err) } @@ -71,10 +71,8 @@ func getenv(k, fallback string) string { func rootHandler(w http.ResponseWriter, r *http.Request) { ua := strings.ToLower(r.Header.Get("User-Agent")) - if !strings.Contains(ua, allowedUA) { - http.Redirect(w, r, redirectURL, http.StatusFound) - + http.Redirect(w, r, redirectURL, http.StatusFound) return } @@ -127,7 +125,9 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.WriteHeader(http.StatusOK) - io.Copy(w, f) + if _, err := io.Copy(w, f); err != nil { + log.Printf("write error: %v", err) + } } func listImageFiles() []string { @@ -141,7 +141,6 @@ func listImageFiles() []string { case ".jpg", ".jpeg", ".png", ".gif", ".bmp": files = append(files, path) default: - } return nil })