we were using conflicting IP addresses from different sources. fix this.

This commit is contained in:
fiatjaf
2024-09-26 07:30:24 -03:00
parent 5a6cfd8975
commit 82c32c21f4
3 changed files with 18 additions and 8 deletions

View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"io" "io"
"net" "net"
"net/http" "net/http"
@@ -25,20 +24,15 @@ func agentBlock(next http.Handler) http.Handler {
func cloudflareBlock(next http.Handler) http.Handler { func cloudflareBlock(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip := net.ParseIP(r.Header.Get("CF-Connecting-IP")) ip := net.ParseIP(actualIP(r))
fmt.Println("should blopccccccccccc?", ip)
if ip != nil { if ip != nil {
fmt.Println(" !")
for _, ipnet := range cloudflareRanges { for _, ipnet := range cloudflareRanges {
fmt.Println(" range", ipnet)
if ipnet.Contains(ip) { if ipnet.Contains(ip) {
fmt.Println(" match")
// cloudflare is not allowed // cloudflare is not allowed
log.Debug().Stringer("ip", ip).Msg("cloudflare (attacker) ip blocked") log.Debug().Stringer("ip", ip).Msg("cloudflare (attacker) ip blocked")
http.Redirect(w, r, "https://njump.me/", 302) http.Redirect(w, r, "https://njump.me/", 302)
return return
} }
fmt.Println(" no match")
} }
} }

View File

@@ -9,7 +9,7 @@ func loggingMiddleware(next http.Handler) http.Handler {
path += "?" + r.URL.RawQuery path += "?" + r.URL.RawQuery
} }
log.Debug(). log.Debug().
Str("ip", r.Header.Get("X-Forwarded-For")). Str("ip", actualIP(r)).
Str("path", path). Str("path", path).
Str("user-agent", r.Header.Get("User-Agent")). Str("user-agent", r.Header.Get("User-Agent")).
Str("referer", r.Header.Get("Referer")). Str("referer", r.Header.Get("Referer")).

16
ip.go Normal file
View File

@@ -0,0 +1,16 @@
package main
import (
"net/http"
"strings"
)
func actualIP(r *http.Request) string {
if cf := r.Header.Get("CF-Connecting-IP"); cf != "" {
return cf
} else if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
return strings.Split(xff, ",")[0]
} else {
return r.RemoteAddr
}
}