ratelimiting!!!!

This commit is contained in:
2026-11-22 19:07:55 -05:00
parent 80a5ae1f69
commit 26fe94ca45
9 changed files with 199 additions and 14 deletions
+70
View File
@@ -0,0 +1,70 @@
package server
import (
"fmt"
"net"
"net/http"
"net/netip"
"slices"
"strings"
)
func newClientIPResolve(source string, trustedProxies []netip.Prefix) func(*http.Request) (string, error) {
isTrusted := func(ip netip.Addr) bool {
for _, prefix := range trustedProxies {
if prefix.Contains(ip) {
return true
}
}
return false
}
return func(r *http.Request) (string, error) {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return "", err
}
peer, err := netip.ParseAddr(host)
if err != nil {
return "", err
}
if source == "remote" || !isTrusted(peer) {
return peer.String(), nil
}
switch source {
case "cloudflare":
value := r.Header.Values("CF-Connecting-IP")
if len(value) != 1 {
return "", fmt.Errorf("expected a single CF-Connecting-IP header")
}
ip, err := netip.ParseAddr(value[0])
if err != nil {
return "", fmt.Errorf("invalid CF-Connecting-IP: %w", err)
}
return ip.Unmap().String(), nil
case "xff":
values := r.Header.Values("X-Forwarded-For")
hops := strings.Split(strings.Join(values, ","), ",")
// walk backwards (right to left), while skipping trusted proxies, the first untrusted ip will be assumed to be the client ip
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Forwarded-For#syntax
for _, hop := range slices.Backward(hops) {
if hop == "" {
continue
}
ip, err := netip.ParseAddr(strings.TrimSpace(hop))
if err != nil {
return "", fmt.Errorf("invalid X-Forwarded-For hop: %w", err)
}
ip = ip.Unmap()
if !isTrusted(ip) {
return ip.String(), nil
}
}
return "", fmt.Errorf("no client ip in X-Forwarded-For")
}
return "", fmt.Errorf("unknown client ip source %q", source)
}
}
+17 -5
View File
@@ -3,8 +3,10 @@ package server
import (
"fmt"
"net/http"
"time"
"git.whoswhip.dev/whoswhip/minibin/internal/config"
"git.whoswhip.dev/whoswhip/minibin/internal/middleware"
"git.whoswhip.dev/whoswhip/minibin/internal/pages"
"git.whoswhip.dev/whoswhip/minibin/internal/reports"
"git.whoswhip.dev/whoswhip/minibin/internal/uploads"
@@ -18,6 +20,7 @@ func health(w http.ResponseWriter, r *http.Request) {
func NewRouter(db *gorm.DB, cfg config.Config, p *pages.Pages, tracker *uploads.AccessTracker, cStore *reports.ChallengeStore) http.Handler {
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("web/static"))
mux.Handle("GET /css/", fs)
mux.Handle("GET /js/", fs)
@@ -26,20 +29,29 @@ func NewRouter(db *gorm.DB, cfg config.Config, p *pages.Pages, tracker *uploads.
"/qts/",
http.FileServer((http.Dir("qts"))),
))
mux.HandleFunc("GET /{$}", pages.HomeHandler(p, cfg))
mux.HandleFunc("GET /tos", pages.StaticPage(p, "tos.html"))
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)
mux.HandleFunc("GET /report", reports.ReportPageHandler(p, cStore, cfg))
mux.HandleFunc("POST /report", reports.ReportUploadHandler(db, cfg, p, cStore))
mux.HandleFunc("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)
mux.HandleFunc("POST /upload", uploads.UploadHandler(db, cfg, p))
mux.HandleFunc("PUT /upload", uploads.UploadHandler(db, cfg, p))
mux.HandleFunc("DELETE /{id}", uploads.DeleteHandler(db, cfg, p))
mux.HandleFunc("POST /{id}/delete", uploads.DeleteHandler(db, cfg, p))
uploadLimiter := middleware.NewRateLimiter(6*time.Second, 3, p, clientIpResolver)
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.HandleFunc("GET /health", health)
return mux
}