Files
AntiCaptcha/internal/handler/task.go
T

125 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handler
import (
"encoding/base64"
"net/http"
"strings"
"anticaptcha/internal/captcha"
"anticaptcha/internal/model"
"github.com/gin-gonic/gin"
)
// 任务类型注册表:这是「新验证码类型」的扩展点。
// 加一种动图/多帧类验证码,只需要在 captcha 层加一个方法,再往这里注册一条,
// 前端和其他调用方都通过同一个 /api/task/run 入口调用,不用再加路由。
var taskTypes = map[string]string{
"animal_jump": "动图点选:目标在多帧之间跳动,返回没被经过的候选点",
}
type taskRequest struct {
Type string `json:"type"`
GIFBase64 string `json:"gif_base64"`
ImgBase64 string `json:"img_base64"`
Candidates []struct {
X int `json:"x"`
Y int `json:"y"`
} `json:"candidates"`
Radius float64 `json:"radius"`
ClassFilter string `json:"class_filter"`
Step int `json:"step"`
IncludeFrames bool `json:"include_frames"`
}
// TaskTypes GET /api/task/types —— 列出当前支持的多帧任务类型
func (h *CaptchaHandler) TaskTypes(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"types": taskTypes})
}
// RunTask POST /api/task/run —— 多帧(动图)任务的统一入口
func (h *CaptchaHandler) RunTask(c *gin.Context) {
var req taskRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "参数解析失败: " + err.Error()})
return
}
raw := req.GIFBase64
if raw == "" {
raw = req.ImgBase64
}
if raw == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "缺少 gif_base64(也接受 img_base64)"})
return
}
data, err := decodeAnyBase64(raw)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "base64 解析失败: " + err.Error()})
return
}
switch req.Type {
case "animal_jump":
cands := make([]captcha.Point, 0, len(req.Candidates))
for _, p := range req.Candidates {
cands = append(cands, captcha.Point{X: p.X, Y: p.Y})
}
res, err := h.handler.AnimalJump(data, cands, req.Radius, req.ClassFilter, req.Step)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if !req.IncludeFrames {
res.PerFrame = nil
}
c.JSON(http.StatusOK, model.CaptchaResult{Result: res})
default:
c.JSON(http.StatusBadRequest, gin.H{
"error": "未知任务类型: " + req.Type,
"types": taskTypes,
})
}
}
// DetectFrames POST /api/frames/detect —— 只做逐帧检测,供自定义流水线复用
func (h *CaptchaHandler) DetectFrames(c *gin.Context) {
var req taskRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "参数解析失败: " + err.Error()})
return
}
raw := req.GIFBase64
if raw == "" {
raw = req.ImgBase64
}
if raw == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "缺少 gif_base64(也接受 img_base64)"})
return
}
data, err := decodeAnyBase64(raw)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "base64 解析失败: " + err.Error()})
return
}
res, err := h.handler.DetectFramesRaw(data, req.Step)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, model.CaptchaResult{Result: gin.H{
"frames": res.Frames,
"used_frames": res.UsedFrames,
"per_frame": res.PerFrame,
}})
}
func decodeAnyBase64(s string) ([]byte, error) {
if i := strings.Index(s, ","); i >= 0 {
s = s[i+1:]
}
if d, err := base64.StdEncoding.DecodeString(s); err == nil {
return d, nil
}
return base64.RawStdEncoding.DecodeString(s)
}