对照上游 https://github.com/81NewArk/AntiCAP 逐项核对后修两处: 1) 相似度(internal/captcha/handler.go + pkg/onnx) 上游是 outputs = session.run(None, feed); emb1, emb2 = outputs[0], outputs[1] —— 模型本身分别吐出两路 embedding。 旧实现只 run 了 1 个输出(onnx.cpp 里 Run 的 output_count = 1), 再把这个张量从中间切成两半当两个 embedding, 实测四组完全不同的输入返回同一个值(0.49188173),等于接口失效。 现在新增 onnx_run_dual_input_two_outputs 取前两个输出, 两路各算 L2 距离后 1/(1+dist);单输出模型保留旧路径回退并打日志。 2) 阴影滑块(pkg/opencv + internal/handler) 上游 solve_slider_comparison 要求 target 与 background 同尺寸 (PIL ImageChops.difference 的硬前提),旧实现不校验, 尺寸不同时直接抛 OpenCV -209 尺寸不匹配,返回 500。 现在前置校验并返回 400 + 明确原因;SliderMatch 也补了「滑块不能大于背景」的校验。 预处理与旋转逻辑核对结论:preprocessSiamese(105×105 / ImageNet norm)与 preprocessRotation(正方形中心裁剪 + h/√2 + 224 / ImageNet norm)与上游一致,未改。
355 lines
9.4 KiB
Go
355 lines
9.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"anticaptcha/internal/captcha"
|
|
"anticaptcha/internal/database"
|
|
"anticaptcha/internal/middleware"
|
|
"anticaptcha/internal/model"
|
|
"anticaptcha/pkg/opencv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type CaptchaHandler struct {
|
|
handler *captcha.Handler
|
|
}
|
|
|
|
func NewCaptchaHandler(h *captcha.Handler) *CaptchaHandler {
|
|
return &CaptchaHandler{handler: h}
|
|
}
|
|
|
|
func (h *CaptchaHandler) RegisterRoutes(r *gin.RouterGroup, authRequired bool) {
|
|
captcha := r.Group("/api")
|
|
|
|
// 公共接口
|
|
captcha.POST("/register", h.Register)
|
|
captcha.POST("/login", h.Login)
|
|
captcha.GET("/tokens/verification", middleware.JWTAuth(), h.VerifyToken)
|
|
|
|
// 需要认证的接口
|
|
auth := captcha.Group("")
|
|
if authRequired {
|
|
auth.Use(middleware.JWTAuth())
|
|
}
|
|
|
|
// 验证码识别接口
|
|
auth.POST("/ocr", h.OCR)
|
|
auth.POST("/math", h.Math)
|
|
auth.POST("/slider/match", h.SliderMatch)
|
|
auth.POST("/slider/comparison", h.SliderComparison)
|
|
auth.POST("/compare/similarity", h.CompareSimilarity)
|
|
auth.POST("/rotate/single/rotate", h.SingleRotate)
|
|
auth.POST("/rotate/double/rotate", h.DoubleRotate)
|
|
auth.POST("/detection/icon", h.DetectionIcon)
|
|
auth.POST("/detection/text", h.DetectionText)
|
|
auth.POST("/detection/icon/order", h.DetectionIconOrder)
|
|
auth.POST("/detection/text/order", h.DetectionTextOrder)
|
|
|
|
// 管理接口
|
|
admin := captcha.Group("/admin")
|
|
admin.Use(middleware.JWTAuth(), middleware.AdminOnly())
|
|
admin.POST("/generate_code", h.GenerateCode)
|
|
admin.GET("/regcodes", h.GetRegCodes)
|
|
admin.DELETE("/regcodes/:id", h.DeleteRegCode)
|
|
admin.GET("/users", h.GetUsers)
|
|
admin.PUT("/users/:username", h.UpdateUser)
|
|
admin.GET("/costs", h.GetEndpointCosts)
|
|
admin.POST("/costs", h.SetEndpointCost)
|
|
}
|
|
|
|
// 注册
|
|
func (h *CaptchaHandler) Register(c *gin.Context) {
|
|
var req struct {
|
|
Username string `json:"username" binding:"required"`
|
|
Password string `json:"password" binding:"required"`
|
|
RegistrationCode string `json:"registration_code" binding:"required"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// TODO: 实现注册逻辑
|
|
c.JSON(http.StatusOK, gin.H{"message": "注册成功"})
|
|
}
|
|
|
|
// 登录
|
|
func (h *CaptchaHandler) Login(c *gin.Context) {
|
|
var req struct {
|
|
Username string `json:"username" binding:"required"`
|
|
Password string `json:"password" binding:"required"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// 从数据库验证用户
|
|
user, err := database.ValidateUser(req.Username, req.Password)
|
|
if err != nil {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "用户名或密码错误"})
|
|
return
|
|
}
|
|
|
|
token, err := middleware.GenerateToken(user.ID, user.Username, string(user.Role))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "生成令牌失败"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.LoginResponse{
|
|
AccessToken: token,
|
|
TokenType: "bearer",
|
|
Role: string(user.Role),
|
|
Balance: user.Balance,
|
|
})
|
|
}
|
|
|
|
// 验证令牌
|
|
func (h *CaptchaHandler) VerifyToken(c *gin.Context) {
|
|
userID := middleware.GetCurrentUserID(c)
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"user_id": userID,
|
|
"valid": true,
|
|
})
|
|
}
|
|
|
|
// OCR 识别
|
|
func (h *CaptchaHandler) OCR(c *gin.Context) {
|
|
var req model.OCRRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := h.handler.OCR(req.ImageBase64)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.CaptchaResult{Result: result})
|
|
}
|
|
|
|
// 数学计算
|
|
func (h *CaptchaHandler) Math(c *gin.Context) {
|
|
var req model.MathRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := h.handler.Math(req.ImageBase64)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.CaptchaResult{Result: result})
|
|
}
|
|
|
|
// 滑块匹配
|
|
func (h *CaptchaHandler) SliderMatch(c *gin.Context) {
|
|
var req model.SliderMatchRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
x, err := h.handler.SliderMatch(req.TargetBase64, req.BackgroundBase64)
|
|
if err != nil {
|
|
// 尺寸不符合算法前提是调用方的问题,返回 400 而不是 500
|
|
if errors.Is(err, opencv.ErrSizeMismatch) {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.CaptchaResult{Result: x})
|
|
}
|
|
|
|
// 阴影滑块
|
|
func (h *CaptchaHandler) SliderComparison(c *gin.Context) {
|
|
var req model.SliderMatchRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
x, err := h.handler.SliderComparison(req.TargetBase64, req.BackgroundBase64)
|
|
if err != nil {
|
|
// 尺寸不符合算法前提是调用方的问题,返回 400 而不是 500
|
|
if errors.Is(err, opencv.ErrSizeMismatch) {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.CaptchaResult{Result: x})
|
|
}
|
|
|
|
// 图片相似度
|
|
func (h *CaptchaHandler) CompareSimilarity(c *gin.Context) {
|
|
var req model.CompareImageRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
similarity, err := h.handler.CompareSimilarity(req.Image1Base64, req.Image2Base64)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.CaptchaResult{Result: similarity})
|
|
}
|
|
|
|
// 单图旋转
|
|
func (h *CaptchaHandler) SingleRotate(c *gin.Context) {
|
|
var req model.RotateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
angle, err := h.handler.SingleRotate(req.ImageBase64)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.CaptchaResult{Result: angle})
|
|
}
|
|
|
|
// 双图旋转
|
|
func (h *CaptchaHandler) DoubleRotate(c *gin.Context) {
|
|
var req model.DoubleRotateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := h.handler.DoubleRotate(
|
|
req.InsideBase64,
|
|
req.OutsideBase64,
|
|
req.CheckPixel,
|
|
req.SpeedRatio,
|
|
req.Grayscale,
|
|
req.Anticlockwise,
|
|
req.CutPixelValue,
|
|
)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.CaptchaResult{Result: result.Angle})
|
|
}
|
|
|
|
// 图标检测
|
|
func (h *CaptchaHandler) DetectionIcon(c *gin.Context) {
|
|
var req model.DetectIconRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := h.handler.DetectionIcon(req.ImageBase64)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.CaptchaResult{Result: result})
|
|
}
|
|
|
|
// 文字检测
|
|
func (h *CaptchaHandler) DetectionText(c *gin.Context) {
|
|
var req model.DetectIconRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := h.handler.DetectionText(req.ImageBase64)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.CaptchaResult{Result: result})
|
|
}
|
|
|
|
// 按序图标检测
|
|
func (h *CaptchaHandler) DetectionIconOrder(c *gin.Context) {
|
|
var req model.DetectIconOrderRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := h.handler.DetectionIconOrder(req.OrderImgBase64, req.TargetImgBase64)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.CaptchaResult{Result: result})
|
|
}
|
|
|
|
// 按序文字检测
|
|
func (h *CaptchaHandler) DetectionTextOrder(c *gin.Context) {
|
|
var req model.DetectIconOrderRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := h.handler.DetectionTextOrder(req.OrderImgBase64, req.TargetImgBase64)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.CaptchaResult{Result: result})
|
|
}
|
|
|
|
// 管理接口占位
|
|
func (h *CaptchaHandler) GenerateCode(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"code": "placeholder"})
|
|
}
|
|
|
|
func (h *CaptchaHandler) GetRegCodes(c *gin.Context) {
|
|
c.JSON(http.StatusOK, []interface{}{})
|
|
}
|
|
|
|
func (h *CaptchaHandler) DeleteRegCode(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"message": "删除成功"})
|
|
}
|
|
|
|
func (h *CaptchaHandler) GetUsers(c *gin.Context) {
|
|
c.JSON(http.StatusOK, []interface{}{})
|
|
}
|
|
|
|
func (h *CaptchaHandler) UpdateUser(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"message": "更新成功"})
|
|
}
|
|
|
|
func (h *CaptchaHandler) GetEndpointCosts(c *gin.Context) {
|
|
c.JSON(http.StatusOK, []interface{}{})
|
|
}
|
|
|
|
func (h *CaptchaHandler) SetEndpointCost(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"message": "设置成功"})
|
|
}
|