Compare commits
2 Commits
23ff28fe5a
...
ed920a09f9
| Author | SHA1 | Date | |
|---|---|---|---|
| ed920a09f9 | |||
| 9f0b0d702e |
10
go.mod
Normal file
10
go.mod
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
module git.send.nrw/sendnrw/flod-pod
|
||||||
|
|
||||||
|
go 1.24.3
|
||||||
|
|
||||||
|
require github.com/redis/go-redis/v9 v9.10.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
|
)
|
||||||
6
go.sum
Normal file
6
go.sum
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
|
github.com/redis/go-redis/v9 v9.10.0 h1:FxwK3eV8p/CQa0Ch276C7u2d0eNC9kCmAYQ7mCXCzVs=
|
||||||
|
github.com/redis/go-redis/v9 v9.10.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
|
||||||
144
main.go
Normal file
144
main.go
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ctx = context.Background()
|
||||||
|
redisClient *redis.Client
|
||||||
|
hashName = "bl:cat"
|
||||||
|
blocklistMode string
|
||||||
|
masterURL string
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Modus und Master-URL aus ENV
|
||||||
|
blocklistMode = os.Getenv("BLOCKLIST_MODE")
|
||||||
|
if blocklistMode != "master" && blocklistMode != "slave" {
|
||||||
|
log.Fatal("BLOCKLIST_MODE muss 'master' oder 'slave' sein")
|
||||||
|
}
|
||||||
|
|
||||||
|
if blocklistMode == "slave" {
|
||||||
|
masterURL = os.Getenv("MASTER_URL")
|
||||||
|
if masterURL == "" {
|
||||||
|
log.Fatal("MASTER_URL muss gesetzt sein im slave Modus")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if blocklistMode == "master" {
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ip := getClientIP(r)
|
||||||
|
if ip == "" {
|
||||||
|
http.Error(w, "Ungültige IP", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
processIP(ip, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ip := strings.TrimSpace(string(body))
|
||||||
|
if !isValidIP(ip) {
|
||||||
|
http.Error(w, "Ungültige IP", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
processIP(ip, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func processIP(ip string, w http.ResponseWriter) {
|
||||||
|
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 {
|
||||||
|
xff := r.Header.Get("X-Forwarded-For")
|
||||||
|
if xff != "" {
|
||||||
|
parts := strings.Split(xff, ",")
|
||||||
|
ip := strings.TrimSpace(parts[0])
|
||||||
|
if isValidIP(ip) {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
xrip := r.Header.Get("X-Real-Ip")
|
||||||
|
if xrip != "" && isValidIP(xrip) {
|
||||||
|
return xrip
|
||||||
|
}
|
||||||
|
|
||||||
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if isValidIP(host) {
|
||||||
|
return host
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func isValidIP(ip string) bool {
|
||||||
|
return net.ParseIP(ip) != nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user