2023-01-16 21:35:37 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-02-22 04:40:15 +00:00
|
|
|
|
|
|
|
"heckel.io/ntfy/util"
|
2023-01-16 21:35:37 +00:00
|
|
|
)
|
|
|
|
|
2023-02-23 03:26:43 +00:00
|
|
|
type contextKey int
|
|
|
|
|
|
|
|
const (
|
|
|
|
contextRateVisitor contextKey = iota + 2586
|
|
|
|
contextTopic
|
2023-03-04 03:22:07 +00:00
|
|
|
contextMatrixPushKey
|
2023-03-14 14:19:15 +00:00
|
|
|
contextUnifiedPush
|
2023-02-23 03:26:43 +00:00
|
|
|
)
|
|
|
|
|
2023-02-08 20:20:44 +00: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-02-22 04:40:15 +00:00
|
|
|
// limitRequestsWithTopic limits requests with a topic and stores the rate-limiting-subscriber and topic into request.Context
|
|
|
|
func (s *Server) limitRequestsWithTopic(next handleFunc) handleFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
t, err := s.topicFromPath(r.URL.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-23 02:00:56 +00:00
|
|
|
vrate := v
|
2023-02-23 02:33:18 +00:00
|
|
|
if rateVisitor := t.RateVisitor(); rateVisitor != nil {
|
|
|
|
vrate = rateVisitor
|
2023-02-22 04:40:15 +00:00
|
|
|
}
|
2023-02-23 03:26:43 +00:00
|
|
|
r = withContext(r, map[contextKey]any{
|
|
|
|
contextRateVisitor: vrate,
|
|
|
|
contextTopic: t,
|
|
|
|
})
|
2023-02-22 04:40:15 +00:00
|
|
|
if util.ContainsIP(s.config.VisitorRequestExemptIPAddrs, v.ip) {
|
|
|
|
return next(w, r, v)
|
2023-02-23 02:00:56 +00:00
|
|
|
} else if !vrate.RequestAllowed() {
|
2023-02-22 04:40:15 +00:00
|
|
|
return errHTTPTooManyRequestsLimitRequests
|
|
|
|
}
|
|
|
|
return next(w, r, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 21:35:37 +00: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 01:43:06 +00:00
|
|
|
if v.User() == nil {
|
2023-01-16 21:35:37 +00: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 04:01:26 +00:00
|
|
|
if s.config.StripeSecretKey == "" || s.stripe == nil {
|
2023-01-16 21:35:37 +00: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 01:43:06 +00:00
|
|
|
if v.User().Billing.StripeCustomerID == "" {
|
2023-01-16 21:35:37 +00: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
|
|
|
|
}
|
|
|
|
}
|