mirror of
https://codeberg.org/gigirassy/asciiwebserv
synced 2026-08-30 15:37:40 +00:00
crapcode
This commit is contained in:
@@ -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
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user