forked from mirrors/ntfy
v1/account API response, rate limiting bla
This commit is contained in:
parent
ac56fa36ba
commit
56ab34a57f
2 changed files with 34 additions and 4 deletions
|
@ -40,9 +40,7 @@ func (s *Server) handleAccountGet(w http.ResponseWriter, r *http.Request, v *vis
|
|||
return err
|
||||
}
|
||||
response := &apiAccountSettingsResponse{
|
||||
Usage: &apiAccountUsageLimits{
|
||||
Basis: "ip",
|
||||
},
|
||||
Usage: &apiAccountUsageLimits{},
|
||||
}
|
||||
if v.user != nil {
|
||||
response.Username = v.user.Name
|
||||
|
@ -66,11 +64,37 @@ func (s *Server) handleAccountGet(w http.ResponseWriter, r *http.Request, v *vis
|
|||
EmailsLimit: v.user.Plan.EmailsLimit,
|
||||
AttachmentsBytesLimit: v.user.Plan.AttachmentBytesLimit,
|
||||
}
|
||||
} else {
|
||||
if v.user.Role == auth.RoleAdmin {
|
||||
response.Usage.Basis = "account"
|
||||
response.Plan = &apiAccountSettingsPlan{
|
||||
Name: "Unlimited",
|
||||
MessagesLimit: 0,
|
||||
EmailsLimit: 0,
|
||||
AttachmentsBytesLimit: 0,
|
||||
}
|
||||
} else {
|
||||
response.Usage.Basis = "ip"
|
||||
response.Plan = &apiAccountSettingsPlan{
|
||||
Name: "Free",
|
||||
MessagesLimit: s.config.VisitorRequestLimitBurst,
|
||||
EmailsLimit: s.config.VisitorEmailLimitBurst,
|
||||
AttachmentsBytesLimit: s.config.VisitorAttachmentTotalSizeLimit,
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
response.Username = auth.Everyone
|
||||
response.Role = string(auth.RoleAnonymous)
|
||||
response.Usage.Basis = "account"
|
||||
response.Plan = &apiAccountSettingsPlan{
|
||||
Name: "Anonymous",
|
||||
MessagesLimit: s.config.VisitorRequestLimitBurst,
|
||||
EmailsLimit: s.config.VisitorEmailLimitBurst,
|
||||
AttachmentsBytesLimit: s.config.VisitorAttachmentTotalSizeLimit,
|
||||
}
|
||||
}
|
||||
response.Usage.Messages = int(v.requests.Tokens())
|
||||
response.Usage.AttachmentsBytes = stats.VisitorAttachmentBytesUsed
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
return err
|
||||
|
|
|
@ -45,12 +45,18 @@ type visitorStats struct {
|
|||
}
|
||||
|
||||
func newVisitor(conf *Config, messageCache *messageCache, ip netip.Addr, user *auth.User) *visitor {
|
||||
var requests *rate.Limiter
|
||||
if user != nil && user.Plan != nil {
|
||||
requests = rate.NewLimiter(rate.Limit(user.Plan.MessagesLimit)*rate.Every(24*time.Hour), user.Plan.MessagesLimit)
|
||||
} else {
|
||||
requests = rate.NewLimiter(rate.Every(conf.VisitorRequestLimitReplenish), conf.VisitorRequestLimitBurst)
|
||||
}
|
||||
return &visitor{
|
||||
config: conf,
|
||||
messageCache: messageCache,
|
||||
ip: ip,
|
||||
user: user,
|
||||
requests: rate.NewLimiter(rate.Every(conf.VisitorRequestLimitReplenish), conf.VisitorRequestLimitBurst),
|
||||
requests: requests,
|
||||
emails: rate.NewLimiter(rate.Every(conf.VisitorEmailLimitReplenish), conf.VisitorEmailLimitBurst),
|
||||
subscriptions: util.NewFixedLimiter(int64(conf.VisitorSubscriptionLimit)),
|
||||
bandwidth: util.NewBytesLimiter(conf.VisitorAttachmentDailyBandwidthLimit, 24*time.Hour),
|
||||
|
|
Loading…
Reference in a new issue