Based on QuantumNous/new-api v1.0.0-rc.36 (shallow history squashed). - CNY quota display mode: RMB-denominated top-up and crediting across Epay/Waffo channels - New CheckinButton in both authenticated and public headers (Turnstile supported) - New /subscribe page with public plan snapshot, Alipay/balance payment, auto group upgrade/reset - New /docs 12-section beginner guide with anchor navigation - New home PromoBanner linking to /docs; i18n synced (en/zh/zh-TW/fr/ja/ru/vi) - README: document custom features, live demo https://ai.88531.cn
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type turnstileCheckResponse struct {
|
|
Success bool `json:"success"`
|
|
}
|
|
|
|
func TurnstileCheck() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if common.TurnstileCheckEnabled {
|
|
response := c.Query("turnstile")
|
|
if response == "" {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": "Turnstile token 为空",
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
rawRes, err := http.PostForm("https://challenges.cloudflare.com/turnstile/v0/siteverify", url.Values{
|
|
"secret": {common.TurnstileSecretKey},
|
|
"response": {response},
|
|
"remoteip": {c.ClientIP()},
|
|
})
|
|
if err != nil {
|
|
common.SysLog(err.Error())
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
defer rawRes.Body.Close()
|
|
var res turnstileCheckResponse
|
|
err = common.DecodeJson(rawRes.Body, &res)
|
|
if err != nil {
|
|
common.SysLog(err.Error())
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": err.Error(),
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
if !res.Success {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": "Turnstile 校验失败,请刷新重试!",
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|