diff --git a/main.go b/main.go index f62a7a0..9be7abb 100644 --- a/main.go +++ b/main.go @@ -23,10 +23,6 @@ import ( // Redis + Context var ctx = context.Background() -var rdb = redis.NewClient(&redis.Options{ - Addr: "flodredis:6379", - Password: os.Getenv("REDIS_PASS"), -}) // ────────────────────────────────────────────────────────────────────────────── // Helpers @@ -130,6 +126,11 @@ func main() { } func updateBlocklistMetrics() { + var rdb = redis.NewClient(&redis.Options{ + Addr: "flodredis:6379", + DB: 0, + Password: os.Getenv("REDIS_PASS"), + }) for cat := range blocklistURLs { key := "bl:" + cat count, err := rdb.HLen(ctx, key).Result() @@ -219,6 +220,11 @@ func importBlocklists() error { } func importCategory(cat, url string) error { + var rdb = redis.NewClient(&redis.Options{ + Addr: "flodredis:6379", + DB: 0, + Password: os.Getenv("REDIS_PASS"), + }) fmt.Printf("⬇️ Lade %s (%s)\n", cat, url) resp, err := http.Get(url) if err != nil { @@ -288,6 +294,11 @@ func normalizePrefix(s string) (string, bool) { } func handleWhitelist(w http.ResponseWriter, r *http.Request) { + var rdb = redis.NewClient(&redis.Options{ + Addr: "flodredis:6379", + DB: 0, + Password: os.Getenv("REDIS_PASS"), + }) if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return @@ -326,6 +337,11 @@ func handleWhitelist(w http.ResponseWriter, r *http.Request) { // Check-Handler func handleCheck(w http.ResponseWriter, r *http.Request) { + var rdb = redis.NewClient(&redis.Options{ + Addr: "flodredis:6379", + DB: 0, + Password: os.Getenv("REDIS_PASS"), + }) checkRequests.Inc() ipStr := strings.TrimPrefix(r.URL.Path, "/check/") ip, err := netip.ParseAddr(ipStr) @@ -362,6 +378,11 @@ func handleCheck(w http.ResponseWriter, r *http.Request) { // Check-Handler func handleTraefik(w http.ResponseWriter, r *http.Request) { + var rdb = redis.NewClient(&redis.Options{ + Addr: "flodredis:6379", + DB: 0, + Password: os.Getenv("REDIS_PASS"), + }) checkRequests.Inc() ipStr := r.Header.Get("X-Forwarded-For") if ipStr == "" { @@ -401,6 +422,11 @@ func handleTraefik(w http.ResponseWriter, r *http.Request) { // Check-Logik func checkIP(ip netip.Addr, cats []string) ([]string, error) { + var rdb = redis.NewClient(&redis.Options{ + Addr: "flodredis:6379", + DB: 0, + Password: os.Getenv("REDIS_PASS"), + }) wl, err := rdb.Exists(ctx, "wl:"+ip.String()).Result() if err != nil { return nil, err @@ -426,6 +452,11 @@ func checkIP(ip netip.Addr, cats []string) ([]string, error) { } func loadCategoryPrefixes(cat string) ([]netip.Prefix, error) { + var rdb = redis.NewClient(&redis.Options{ + Addr: "flodredis:6379", + DB: 0, + Password: os.Getenv("REDIS_PASS"), + }) prefixCacheMu.Lock() defer prefixCacheMu.Unlock() entry, ok := prefixCache[cat] @@ -461,6 +492,11 @@ func writeJSON(w http.ResponseWriter, v any) { } func handleDownload(w http.ResponseWriter, r *http.Request) { + var rdb = redis.NewClient(&redis.Options{ + Addr: "flodredis:6379", + DB: 0, + Password: os.Getenv("REDIS_PASS"), + }) cat := strings.TrimPrefix(r.URL.Path, "/download/") if cat == "" { http.Error(w, "category missing", http.StatusBadRequest)