-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
web: add user level rate limit method (#208)
- Loading branch information
Showing
9 changed files
with
319 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, version 3. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// See the GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/> | ||
|
||
package web | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
|
||
"github.com/bangumi/server/internal/pkg/errgo" | ||
"github.com/bangumi/server/internal/web/accessor" | ||
"github.com/bangumi/server/internal/web/rate" | ||
"github.com/bangumi/server/internal/web/rate/action" | ||
"github.com/bangumi/server/internal/web/res" | ||
) | ||
|
||
type baseHandler interface { | ||
GetHTTPAccessor(c *fiber.Ctx) *accessor.Accessor | ||
} | ||
|
||
// rateMiddleware require Handler.NeedLogin before this middleware. | ||
func rateMiddleware(r rate.Manager, h baseHandler, action action.Action, limit rate.Limit) fiber.Handler { | ||
return func(c *fiber.Ctx) error { | ||
a := h.GetHTTPAccessor(c) | ||
if !a.Login { | ||
return res.Unauthorized("login required") | ||
} | ||
|
||
allowed, _, err := r.AllowAction(c.Context(), a.ID, action, limit) | ||
if err != nil { | ||
return errgo.Wrap(err, "rate.Manager.AllowAction") | ||
} | ||
if !allowed { | ||
return c.SendStatus(http.StatusTooManyRequests) | ||
} | ||
|
||
return c.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, version 3. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// See the GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/> | ||
|
||
package action | ||
|
||
type Action uint8 | ||
|
||
const ( | ||
Unknown Action = 0 | ||
Login Action = 1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.