asn blocking!!!!!

This commit is contained in:
2026-11-22 22:08:12 -05:00
parent 41ba8dfb90
commit 1834abc719
10 changed files with 130 additions and 25 deletions
+55
View File
@@ -0,0 +1,55 @@
package middleware
import (
"net/http"
"net/netip"
p "git.whoswhip.dev/whoswhip/minibin/internal/pages"
"github.com/oschwald/maxminddb-golang/v2"
)
type ASNRecord struct {
ASN uint32 `maxminddb:"autonomous_system_number"`
}
type ASNBlocker struct {
DB *maxminddb.Reader
Blocked map[uint32]struct{}
Pages *p.Pages
ResolveIP func(*http.Request) (string, error)
}
func (b *ASNBlocker) BlockASN(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
{
ip, err := b.ResolveIP(r)
if err != nil {
b.Pages.HTTPError(w, r, "couldn't idenitify client address", http.StatusInternalServerError)
return
}
parsed, err := netip.ParseAddr(ip)
if err != nil {
b.Pages.HTTPError(w, r, "couldn't idenitify client address", http.StatusInternalServerError)
}
result := b.DB.Lookup(parsed)
if !result.Found() {
next.ServeHTTP(w, r)
}
var record ASNRecord
if err := result.Decode(&record); err != nil {
b.Pages.HTTPError(w, r, "couldn't idenitify client address", http.StatusInternalServerError)
return
}
if _, blocked := b.Blocked[record.ASN]; blocked {
b.Pages.HTTPError(w, r, "traffic from your ASN is not allowed", http.StatusForbidden, "403, forbidden")
return
}
next.ServeHTTP(w, r)
}
})
}
+3 -3
View File
@@ -34,8 +34,8 @@ func NewRateLimiter(refillEvery time.Duration, burst int, pages *p.Pages, resolv
}
}
func (rl *RateLimiter) LimitRequest(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
func (rl *RateLimiter) LimitRequest(next http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip, err := rl.resolveIP(r)
if err != nil {
rl.pages.HTTPError(w, r, "couldn't idenitify client address", http.StatusInternalServerError)
@@ -61,5 +61,5 @@ func (rl *RateLimiter) LimitRequest(next http.HandlerFunc) http.HandlerFunc {
return
}
next(w, r)
}
})
}