diff --git a/agentblock.go b/agentblock.go deleted file mode 100644 index 5b2ec9d..0000000 --- a/agentblock.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - "net/http" - "strings" -) - -func agentBlock(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - userAgent := r.UserAgent() - if strings.Contains(userAgent, "Amazonbot") { - // Drop the request by returning a 403 Forbidden response - http.Error(w, "Forbidden", http.StatusForbidden) - return - } - - // Call the next handler in the chain - next.ServeHTTP(w, r) - }) -} diff --git a/ipblock.go b/block.go similarity index 71% rename from ipblock.go rename to block.go index cfd6025..ca12646 100644 --- a/ipblock.go +++ b/block.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" "net" "net/http" @@ -8,17 +9,36 @@ import ( "time" ) +func agentBlock(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + userAgent := r.UserAgent() + if strings.Contains(userAgent, "Amazonbot") { + // Drop the request by returning a 403 Forbidden response + http.Error(w, "Forbidden", http.StatusForbidden) + return + } + + // Call the next handler in the chain + next.ServeHTTP(w, r) + }) +} + func cloudflareBlock(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ip := net.ParseIP(r.Header.Get("CF-Connecting-IP")) + fmt.Println("should blopccccccccccc?", ip) if ip != nil { + fmt.Println(" !") for _, ipnet := range cloudflareRanges { + fmt.Println(" range", ipnet) if ipnet.Contains(ip) { + fmt.Println(" match") // cloudflare is not allowed log.Debug().Stringer("ip", ip).Msg("cloudflare (attacker) ip blocked") http.Redirect(w, r, "https://njump.me/", 302) return } + fmt.Println(" no match") } } diff --git a/http_logging.go b/http_logging.go new file mode 100644 index 0000000..0ff38f1 --- /dev/null +++ b/http_logging.go @@ -0,0 +1,20 @@ +package main + +import "net/http" + +func loggingMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + if r.URL.RawQuery != "" { + path += "?" + r.URL.RawQuery + } + log.Debug(). + Str("ip", r.Header.Get("X-Forwarded-For")). + Str("path", path). + Str("user-agent", r.Header.Get("User-Agent")). + Str("referer", r.Header.Get("Referer")). + Msg("request") + + next.ServeHTTP(w, r) + }) +} diff --git a/main.go b/main.go index 616a179..e0c617c 100644 --- a/main.go +++ b/main.go @@ -38,21 +38,6 @@ var ( tailwindDebugStuff template.HTML ) -func loggingMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fullURL := fmt.Sprintf("%s://%s%s", r.Proto, r.Host, r.URL.Path) - if r.URL.RawQuery != "" { - fullURL += "?" + r.URL.RawQuery - } - log.Debug().Str("Full URL:", fullURL).Msg("Request URL => ") - log.Debug().Str("ip", r.Header.Get("X-Forwarded-For")). - Str("user-agent", r.Header.Get("User-Agent")).Str("referer", r.Header.Get("Referer")).Msg("Request details => ") - - // Call the next handler in the chain - next.ServeHTTP(w, r) - }) -} - func main() { err := envconfig.Process("", &s) if err != nil {