master-slave-mode
This commit is contained in:
109
main.go
109
main.go
@@ -1,67 +1,120 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
redisClient *redis.Client
|
redisClient *redis.Client
|
||||||
hashName = "bl:sendnrw"
|
hashName = "bl:cat"
|
||||||
|
blocklistMode string
|
||||||
|
masterURL string
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Redis Client initialisieren
|
// Modus und Master-URL aus ENV
|
||||||
redisClient = redis.NewClient(&redis.Options{
|
blocklistMode = os.Getenv("BLOCKLIST_MODE")
|
||||||
Addr: "10.10.5.249:6379", // ggf. anpassen
|
if blocklistMode != "master" && blocklistMode != "slave" {
|
||||||
Password: "", // falls Passwort benötigt wird
|
log.Fatal("BLOCKLIST_MODE muss 'master' oder 'slave' sein")
|
||||||
DB: 0, // Standard DB
|
|
||||||
})
|
|
||||||
|
|
||||||
// Testverbindung
|
|
||||||
if err := redisClient.Ping(ctx).Err(); err != nil {
|
|
||||||
log.Fatalf("Keine Verbindung zu Redis: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", logIPHandler)
|
if blocklistMode == "slave" {
|
||||||
|
masterURL = os.Getenv("MASTER_URL")
|
||||||
|
if masterURL == "" {
|
||||||
|
log.Fatal("MASTER_URL muss gesetzt sein im slave Modus")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Println("Server startet auf :8082")
|
if blocklistMode == "master" {
|
||||||
if err := http.ListenAndServe(":8082", nil); err != nil {
|
// Redis initialisieren
|
||||||
|
redisAddr := os.Getenv("REDIS_ADDR")
|
||||||
|
if redisAddr == "" {
|
||||||
|
redisAddr = "localhost:6379"
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Endpunkte registrieren
|
||||||
|
http.HandleFunc("/", handleRoot)
|
||||||
|
http.HandleFunc("/add", handleAdd)
|
||||||
|
|
||||||
|
log.Println("Server läuft im Modus:", blocklistMode)
|
||||||
|
log.Println("Server startet auf :8080")
|
||||||
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||||
log.Fatal("ListenAndServe: ", err)
|
log.Fatal("ListenAndServe: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func logIPHandler(w http.ResponseWriter, r *http.Request) {
|
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
ip := getClientIP(r)
|
ip := getClientIP(r)
|
||||||
if ip == "" {
|
if ip == "" {
|
||||||
http.Error(w, "Ungültige IP", http.StatusBadRequest)
|
http.Error(w, "Ungültige IP", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ipKey := ip + "/32"
|
processIP(ip, w)
|
||||||
|
}
|
||||||
|
|
||||||
// IP in Redis Hash speichern
|
func handleAdd(w http.ResponseWriter, r *http.Request) {
|
||||||
err := redisClient.HSet(ctx, hashName, ipKey, 1).Err()
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil || len(body) == 0 {
|
||||||
log.Printf("Fehler beim Schreiben in Redis: %v", err)
|
http.Error(w, "Ungültiger Body", http.StatusBadRequest)
|
||||||
http.Error(w, "Interner Fehler", http.StatusInternalServerError)
|
return
|
||||||
|
}
|
||||||
|
ip := strings.TrimSpace(string(body))
|
||||||
|
if !isValidIP(ip) {
|
||||||
|
http.Error(w, "Ungültige IP", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("IP %s in Redis gespeichert", ipKey)
|
processIP(ip, w)
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
func processIP(ip string, w http.ResponseWriter) {
|
||||||
w.Write([]byte("IP erfasst: " + ipKey + "\n"))
|
ipKey := ip + "/32"
|
||||||
|
|
||||||
|
if blocklistMode == "master" {
|
||||||
|
err := redisClient.HSet(ctx, hashName, ipKey, 1).Err()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Fehler beim Schreiben in 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"))
|
||||||
|
} 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)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
w.WriteHeader(resp.StatusCode)
|
||||||
|
io.Copy(w, resp.Body)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getClientIP(r *http.Request) string {
|
func getClientIP(r *http.Request) string {
|
||||||
// X-Forwarded-For prüfen
|
|
||||||
xff := r.Header.Get("X-Forwarded-For")
|
xff := r.Header.Get("X-Forwarded-For")
|
||||||
if xff != "" {
|
if xff != "" {
|
||||||
parts := strings.Split(xff, ",")
|
parts := strings.Split(xff, ",")
|
||||||
@@ -71,13 +124,11 @@ func getClientIP(r *http.Request) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// X-Real-IP prüfen
|
|
||||||
xrip := r.Header.Get("X-Real-Ip")
|
xrip := r.Header.Get("X-Real-Ip")
|
||||||
if xrip != "" && isValidIP(xrip) {
|
if xrip != "" && isValidIP(xrip) {
|
||||||
return xrip
|
return xrip
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoteAddr als Fallback
|
|
||||||
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
|
Reference in New Issue
Block a user