Release 0.1.0
All checks were successful
release-tag / release-image (push) Successful in 1m31s

This commit is contained in:
2025-06-15 17:02:55 +02:00
parent ed920a09f9
commit b3a2bafb80
4 changed files with 137 additions and 16 deletions

49
main.go
View File

@@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"fmt"
"io"
"log"
"net"
@@ -16,7 +17,7 @@ import (
var (
ctx = context.Background()
redisClient *redis.Client
hashName = "bl:cat"
hashName = "bl:manual"
blocklistMode string
masterURL string
)
@@ -24,14 +25,27 @@ var (
func main() {
// Modus und Master-URL aus ENV
blocklistMode = os.Getenv("BLOCKLIST_MODE")
hashName = os.Getenv("HASH_NAME")
if hashName == "" {
hashName = "bl:manual"
}
//DEBUG
//blocklistMode = "master"
//DEBUG
if blocklistMode != "master" && blocklistMode != "slave" {
log.Fatal("BLOCKLIST_MODE muss 'master' oder 'slave' sein")
log.Fatal("BLOCKLIST_MODE has to be 'master' or 'slave'")
} else {
log.Println("✅ BLOCKLIST_MODE set to:", blocklistMode)
}
if blocklistMode == "slave" {
masterURL = os.Getenv("MASTER_URL")
log.Println("✅ MASTER_URL set to:", masterURL)
//DEBUG
//masterURL = "http://127.0.0.1:8081"
//DEBUG
if masterURL == "" {
log.Fatal("MASTER_URL muss gesetzt sein im slave Modus")
log.Fatal("MASTER_URL has to be set in slave mode")
}
}
@@ -41,13 +55,14 @@ func main() {
if redisAddr == "" {
redisAddr = "localhost:6379"
}
log.Println("✅ Redis Connection set to:", redisAddr)
redisClient = redis.NewClient(&redis.Options{
Addr: redisAddr,
Password: "",
DB: 0,
})
if err := redisClient.Ping(ctx).Err(); err != nil {
log.Fatalf("Keine Verbindung zu Redis: %v", err)
log.Fatalf("❌ No connection to redis-server: %v", err)
}
}
@@ -55,17 +70,18 @@ func main() {
http.HandleFunc("/", handleRoot)
http.HandleFunc("/add", handleAdd)
log.Println("Server läuft im Modus:", blocklistMode)
log.Println("Server startet auf :8080")
log.Println("Server running in mode:", blocklistMode)
log.Println("🚀 Server listening at :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe: ", err)
log.Fatal("⚠️ ListenAndServe: ", err)
}
}
func handleRoot(w http.ResponseWriter, r *http.Request) {
ip := getClientIP(r)
if ip == "" {
http.Error(w, "Ungültige IP", http.StatusBadRequest)
http.Error(w, "Wrong IP", http.StatusBadRequest)
return
}
@@ -75,12 +91,12 @@ func handleRoot(w http.ResponseWriter, r *http.Request) {
func handleAdd(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil || len(body) == 0 {
http.Error(w, "Ungültiger Body", http.StatusBadRequest)
http.Error(w, "Wrong Body", http.StatusBadRequest)
return
}
ip := strings.TrimSpace(string(body))
if !isValidIP(ip) {
http.Error(w, "Ungültige IP", http.StatusBadRequest)
http.Error(w, "Wrong IP", http.StatusBadRequest)
return
}
@@ -93,23 +109,24 @@ func processIP(ip string, w http.ResponseWriter) {
if blocklistMode == "master" {
err := redisClient.HSet(ctx, hashName, ipKey, 1).Err()
if err != nil {
log.Printf("Fehler beim Schreiben in Redis: %v", err)
log.Printf("❌ Error writing to redis: %v", err)
http.Error(w, "Interner Fehler", http.StatusInternalServerError)
return
}
log.Printf("IP %s in Redis gespeichert", ipKey)
w.WriteHeader(http.StatusOK)
w.Write([]byte("IP gespeichert: " + ipKey + "\n"))
log.Printf("IP %s in saved to redis", ipKey)
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("Temporary unavailable"))
} else {
// Slave: an Master weiterleiten
resp, err := http.Post(masterURL+"/add", "text/plain", bytes.NewBuffer([]byte(ip)))
if err != nil {
log.Printf("Fehler beim Weiterleiten an Master: %v", err)
http.Error(w, "Fehler beim Weiterleiten", http.StatusBadGateway)
log.Printf("❌ Error relaying to Master: %v", err)
http.Error(w, "Error relaying", http.StatusBadGateway)
return
}
defer resp.Body.Close()
w.WriteHeader(resp.StatusCode)
fmt.Println(resp.Body)
io.Copy(w, resp.Body)
}
}