2023-01-17 00:35:37 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-02-08 23:20:44 +03:00
|
|
|
"heckel.io/ntfy/util"
|
2023-01-17 00:35:37 +03:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-02-08 23:20:44 +03:00
|
|
|
func (s *Server) limitRequests(next handleFunc) handleFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
if util.ContainsIP(s.config.VisitorRequestExemptIPAddrs, v.ip) {
|
|
|
|
return next(w, r, v)
|
|
|
|
} else if !v.RequestAllowed() {
|
|
|
|
return errHTTPTooManyRequestsLimitRequests
|
|
|
|
}
|
|
|
|
return next(w, r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 00:35:37 +03:00
|
|
|
func (s *Server) ensureWebEnabled(next handleFunc) handleFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
if !s.config.EnableWeb {
|
|
|
|
return errHTTPNotFound
|
|
|
|
}
|
|
|
|
return next(w, r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ensureUserManager(next handleFunc) handleFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
if s.userManager == nil {
|
|
|
|
return errHTTPNotFound
|
|
|
|
}
|
|
|
|
return next(w, r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ensureUser(next handleFunc) handleFunc {
|
|
|
|
return s.ensureUserManager(func(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2023-01-29 04:43:06 +03:00
|
|
|
if v.User() == nil {
|
2023-01-17 00:35:37 +03:00
|
|
|
return errHTTPUnauthorized
|
|
|
|
}
|
|
|
|
return next(w, r, v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ensurePaymentsEnabled(next handleFunc) handleFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2023-01-19 07:01:26 +03:00
|
|
|
if s.config.StripeSecretKey == "" || s.stripe == nil {
|
2023-01-17 00:35:37 +03:00
|
|
|
return errHTTPNotFound
|
|
|
|
}
|
|
|
|
return next(w, r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ensureStripeCustomer(next handleFunc) handleFunc {
|
|
|
|
return s.ensureUser(func(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
2023-01-29 04:43:06 +03:00
|
|
|
if v.User().Billing.StripeCustomerID == "" {
|
2023-01-17 00:35:37 +03:00
|
|
|
return errHTTPBadRequestNotAPaidUser
|
|
|
|
}
|
|
|
|
return next(w, r, v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) withAccountSync(next handleFunc) handleFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
err := next(w, r, v)
|
|
|
|
if err == nil {
|
|
|
|
s.publishSyncEventAsync(v)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|