organize middleware a little more.

This commit is contained in:
fiatjaf
2024-09-26 07:24:46 -03:00
parent 8b357b905e
commit 5a6cfd8975
4 changed files with 40 additions and 35 deletions

View File

@@ -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)
})
}

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"io" "io"
"net" "net"
"net/http" "net/http"
@@ -8,17 +9,36 @@ import (
"time" "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 { 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(r.Header.Get("CF-Connecting-IP"))
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")
} }
} }

20
http_logging.go Normal file
View File

@@ -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)
})
}

15
main.go
View File

@@ -38,21 +38,6 @@ var (
tailwindDebugStuff template.HTML 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() { func main() {
err := envconfig.Process("", &s) err := envconfig.Process("", &s)
if err != nil { if err != nil {