70 lines
2.4 KiB
Go
70 lines
2.4 KiB
Go
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"
|
|
"github.com/oschwald/maxminddb-golang/v2"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func health(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
fmt.Fprintln(w, "OK")
|
|
}
|
|
|
|
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"))
|
|
mux.Handle("GET /css/", fs)
|
|
mux.Handle("GET /js/", fs)
|
|
mux.Handle("GET /static/", http.StripPrefix("/static/", fs))
|
|
mux.Handle("GET /qts/", http.StripPrefix(
|
|
"/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))
|
|
|
|
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.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, cipr)
|
|
ulr := uploadLimiter.LimitRequest
|
|
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
|
|
}
|