Captcha Gateway

This commit is contained in:
2025-03-22 19:48:19 +08:00
parent ba1d96b118
commit 62dcbbf424
14 changed files with 474 additions and 5 deletions

View File

@ -0,0 +1,25 @@
package api
import (
"git.solsynth.dev/hypernet/nexus/pkg/internal/captcha"
"git.solsynth.dev/hypernet/nexus/pkg/internal/web/exts"
"github.com/gofiber/fiber/v2"
)
func renderCaptcha(c *fiber.Ctx) error {
return c.Render("captcha", captcha.GetTemplateData())
}
func validateCaptcha(c *fiber.Ctx) error {
var body struct {
CaptchaToken string `json:"captcha_tk"`
}
if err := exts.BindAndValidate(c, &body); err != nil {
return err
}
if !captcha.Validate(body.CaptchaToken, c.IP()) {
return c.SendStatus(fiber.StatusNotAcceptable)
}
return c.SendStatus(fiber.StatusOK)
}

View File

@ -12,6 +12,8 @@ import (
)
func MapControllers(app *fiber.App) {
app.Get("/captcha", renderCaptcha)
app.Post("/captcha", validateCaptcha)
app.Get("/check-ip", getClientIP)
app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{

View File

@ -11,6 +11,7 @@ import (
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/idempotency"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/template/html/v2"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"github.com/spf13/viper"
@ -21,6 +22,8 @@ type WebApp struct {
}
func NewServer() *WebApp {
engine := html.New(viper.GetString("templates_dir"), ".tmpl")
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
EnableIPValidation: true,
@ -32,6 +35,7 @@ func NewServer() *WebApp {
BodyLimit: 512 * 1024 * 1024 * 1024, // 512 TiB
ReadBufferSize: 5 * 1024 * 1024, // 5MB for large JWT
EnablePrintRoutes: viper.GetBool("debug.print_routes"),
Views: engine,
})
app.Use(fiberzerolog.New(fiberzerolog.Config{