From e6b59e4ff115459c623addd5c268297ade472264 Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Sat, 12 Jul 2025 19:46:52 +0300 Subject: [PATCH] lol --- main.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 7b5dd7c..b58485b 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,14 @@ package main import ( + "compress/gzip" "encoding/base64" "fmt" + "io" "log" "net/http" "os" + "strings" "sync" "time" @@ -22,6 +25,30 @@ type KeyPress struct { Key string `json:"key"` } +type gzipResponseWriter struct { + io.Writer + http.ResponseWriter +} + +func (w gzipResponseWriter) Write(b []byte) (int, error) { + return w.Writer.Write(b) +} + +func gzipHandler(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { + next(w, r) + return + } + + gz := gzip.NewWriter(w) + defer gz.Close() + + w.Header().Set("Content-Encoding", "gzip") + next(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r) + } +} + func wsHandler(w http.ResponseWriter, r *http.Request) { conn, err := upgrader.Upgrade(w, r, nil) if err != nil { @@ -257,10 +284,10 @@ func main() { go func(p string) { defer wg.Done() mux := http.NewServeMux() - mux.HandleFunc("/", handler) + mux.HandleFunc("/", gzipHandler(handler)) mux.HandleFunc("/ws", wsHandler) - mux.HandleFunc("/status", statusHandler) - mux.HandleFunc("/trolling", trollingHandler) + mux.HandleFunc("/status", gzipHandler(statusHandler)) + mux.HandleFunc("/trolling", gzipHandler(trollingHandler)) server := &http.Server{ Addr: ":" + p,