mirror of
https://codeberg.org/gigirassy/asciiwebserv
synced 2026-08-30 23:47:39 +00:00
crapcode
This commit is contained in:
@@ -22,18 +22,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultListen = "0.0.0.0:3333"
|
defaultListen = ":3333"
|
||||||
defaultDir = "./images"
|
defaultDir = "./images"
|
||||||
defaultWidth = 30
|
defaultWidth = 80
|
||||||
cacheSuffixFmt = ".ansi.%d.txt"
|
cacheSuffixFmt = ".ansi.%d.txt"
|
||||||
allowedUA = "curl"
|
allowedUA = "curl"
|
||||||
maxImageDim = 2000
|
maxImageDim = 2000
|
||||||
redirectURL = "https://example.com"
|
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
imgDir string
|
imgDir string
|
||||||
|
redirectURL string
|
||||||
cacheLock sync.RWMutex
|
cacheLock sync.RWMutex
|
||||||
cachedPaths []string
|
cachedPaths []string
|
||||||
)
|
)
|
||||||
@@ -42,6 +41,7 @@ func main() {
|
|||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
imgDir = getenv("IMAGES_DIR", defaultDir)
|
imgDir = getenv("IMAGES_DIR", defaultDir)
|
||||||
|
redirectURL = getenv("REDIRECT_URL", "https://example.com")
|
||||||
listen := getenv("LISTEN", defaultListen)
|
listen := getenv("LISTEN", defaultListen)
|
||||||
|
|
||||||
if err := os.MkdirAll(imgDir, 0755); err != nil {
|
if err := os.MkdirAll(imgDir, 0755); err != nil {
|
||||||
@@ -55,7 +55,7 @@ func main() {
|
|||||||
|
|
||||||
http.HandleFunc("/", rootHandler)
|
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 {
|
if err := http.ListenAndServe(listen, nil); err != nil {
|
||||||
log.Fatalf("server failed: %v", err)
|
log.Fatalf("server failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -71,10 +71,8 @@ func getenv(k, fallback string) string {
|
|||||||
|
|
||||||
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
ua := strings.ToLower(r.Header.Get("User-Agent"))
|
ua := strings.ToLower(r.Header.Get("User-Agent"))
|
||||||
|
|
||||||
if !strings.Contains(ua, allowedUA) {
|
if !strings.Contains(ua, allowedUA) {
|
||||||
http.Redirect(w, r, redirectURL, http.StatusFound)
|
http.Redirect(w, r, redirectURL, http.StatusFound)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +125,9 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
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 {
|
func listImageFiles() []string {
|
||||||
@@ -141,7 +141,6 @@ func listImageFiles() []string {
|
|||||||
case ".jpg", ".jpeg", ".png", ".gif", ".bmp":
|
case ".jpg", ".jpeg", ".png", ".gif", ".bmp":
|
||||||
files = append(files, path)
|
files = append(files, path)
|
||||||
default:
|
default:
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user