mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-07 21:48:18 -08:00
24 lines
636 B
Go
24 lines
636 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"slices"
|
|
|
|
"github.com/gabehf/koito/internal/cfg"
|
|
"github.com/gabehf/koito/internal/logger"
|
|
)
|
|
|
|
func AllowedHosts(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
l := logger.Get()
|
|
if cfg.AllowAllHosts() {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
} else if slices.Contains(cfg.AllowedHosts(), r.Host) {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
l.Warn().Msgf("Request denied from host %s. If you want to allow requests like this, add the host to your %s variable", r.Host, cfg.ALLOWED_HOSTS_ENV)
|
|
w.WriteHeader(http.StatusForbidden)
|
|
})
|
|
}
|