mirror of https://github.com/gabehf/Koito.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
636 B
25 lines
636 B
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)
|
|
})
|
|
}
|