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
+25 -9
View File
@@ -24,10 +24,11 @@ type Config struct {
DatabasePath string
// public host like "https://bin.zip", NOT the address minbin listens on
Host string
Addr string
IPAdressSource string
TrustedProxies []netip.Prefix
Host string
Addr string
IPAdressSource string
TrustedProxies []netip.Prefix
BannedClientASNs map[uint32]struct{}
}
func LoadConfig() (Config, error) {
@@ -89,6 +90,20 @@ func LoadConfig() (Config, error) {
trustedProxies = append(trustedProxies, prefix)
}
}
bannedAsns := make(map[uint32]struct{})
if rawAsn := envOrDefault("MINIBIN_BANNED_ASNS", ""); rawAsn != "" {
for entry := range strings.SplitSeq(rawAsn, ",") {
entry = strings.TrimSpace(entry)
if entry == "" {
continue
}
asn, err := strconv.ParseUint(entry, 10, 32)
if err != nil {
return Config{}, fmt.Errorf("MINIBIN_BANNED_ASNS: invalid number %q: %w", entry, err)
}
bannedAsns[uint32(asn)] = struct{}{}
}
}
return Config{
UploadDir: envOrDefault("MINIBIN_UPLOAD_DIR", "uploads"),
@@ -100,11 +115,12 @@ func LoadConfig() (Config, error) {
PoWDifficulty: powDifficulty,
ReportOptions: reportOptions,
DatabasePath: envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite"),
Host: envOrDefault("MINIBIN_HOST", "http://localhost:8080"),
Addr: envOrDefault("MINIBIN_ADDR", "localhost:8080"),
IPAdressSource: ipAddressSource,
TrustedProxies: trustedProxies,
DatabasePath: envOrDefault("MINIBIN_DATABASE_PATH", "minibin.sqlite"),
Host: envOrDefault("MINIBIN_HOST", "http://localhost:8080"),
Addr: envOrDefault("MINIBIN_ADDR", "localhost:8080"),
IPAdressSource: ipAddressSource,
TrustedProxies: trustedProxies,
BannedClientASNs: bannedAsns,
}, nil
}
+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)
}
})
}
+1
View File
@@ -149,6 +149,7 @@ func (pages *Pages) HTTPError(w http.ResponseWriter, r *http.Request, message st
_ = pages.Templates.ExecuteTemplate(w, "result.html", map[string]any{
"Title": t,
"Message": message,
"Qt": GetRandomQt(pages.Qts),
})
return
}
+21 -9
View File
@@ -10,6 +10,7 @@ import (
"git.whoswhip.dev/whoswhip/minibin/internal/pages"
"git.whoswhip.dev/whoswhip/minibin/internal/reports"
"git.whoswhip.dev/whoswhip/minibin/internal/uploads"
"github.com/oschwald/maxminddb-golang/v2"
"gorm.io/gorm"
)
@@ -18,7 +19,7 @@ func health(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "OK")
}
func NewRouter(db *gorm.DB, cfg config.Config, p *pages.Pages, tracker *uploads.AccessTracker, cStore *reports.ChallengeStore) http.Handler {
func NewRouter(db *gorm.DB, cfg config.Config, p *pages.Pages, tracker *uploads.AccessTracker, cStore *reports.ChallengeStore, asnMMDB ...*maxminddb.Reader) http.Handler {
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("web/static"))
@@ -35,23 +36,34 @@ func NewRouter(db *gorm.DB, cfg config.Config, p *pages.Pages, tracker *uploads.
mux.HandleFunc("GET /privacy", pages.StaticPage(p, "privacy.html"))
mux.HandleFunc("GET /faq", pages.FaqHandler(p, cfg))
clientIpResolver := newClientIPResolve(cfg.IPAdressSource, cfg.TrustedProxies)
reportLimiter := middleware.NewRateLimiter(12*time.Second, 2, p, clientIpResolver)
cipr := newClientIPResolve(cfg.IPAdressSource, cfg.TrustedProxies)
reportLimiter := middleware.NewRateLimiter(12*time.Second, 2, p, cipr)
mux.HandleFunc("GET /report", reports.ReportPageHandler(p, cStore, cfg))
mux.HandleFunc("POST /report", reportLimiter.LimitRequest(reports.ReportUploadHandler(db, cfg, p, cStore)))
mux.Handle("POST /report", reportLimiter.LimitRequest(reports.ReportUploadHandler(db, cfg, p, cStore)))
u := http.NewServeMux()
u.HandleFunc("GET /{id}", uploads.ContentHandler(db, cfg, tracker, p))
u.HandleFunc("GET /{id}/info", uploads.ContentInfoHandler(db, tracker, p))
mux.Handle("/", u)
uploadLimiter := middleware.NewRateLimiter(6*time.Second, 3, p, clientIpResolver)
uploadLimiter := middleware.NewRateLimiter(6*time.Second, 3, p, cipr)
ulr := uploadLimiter.LimitRequest
mux.HandleFunc("POST /upload", ulr(uploads.UploadHandler(db, cfg, p)))
mux.HandleFunc("PUT /upload", ulr(uploads.UploadHandler(db, cfg, p)))
mux.HandleFunc("DELETE /{id}", ulr(uploads.DeleteHandler(db, cfg, p)))
mux.HandleFunc("POST /{id}/delete", ulr(uploads.DeleteHandler(db, cfg, p)))
mux.Handle("POST /upload", ulr(uploads.UploadHandler(db, cfg, p)))
mux.Handle("PUT /upload", ulr(uploads.UploadHandler(db, cfg, p)))
mux.Handle("DELETE /{id}", ulr(uploads.DeleteHandler(db, cfg, p)))
mux.Handle("POST /{id}/delete", ulr(uploads.DeleteHandler(db, cfg, p)))
mux.HandleFunc("GET /health", health)
if len(cfg.BannedClientASNs) > 0 && len(asnMMDB) > 0 {
ammdb := asnMMDB[0]
blocker := &middleware.ASNBlocker{
DB: ammdb,
Blocked: cfg.BannedClientASNs,
ResolveIP: cipr,
Pages: p,
}
return blocker.BlockASN(mux)
}
return mux
}