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