增加token检测

This commit is contained in:
2026-05-05 23:54:15 +08:00
parent 2a60d34711
commit 283a2b7a80
8 changed files with 943 additions and 18 deletions
+156 -16
View File
@@ -4,12 +4,14 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
"server/models"
"server/pkg/jwtutil"
"server/pkg/tokenprobe"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
@@ -154,6 +156,15 @@ func listPoolRows(c *beego.Controller, module string) {
status := strings.TrimSpace(c.GetString("status"))
where, whereArgs := accountPoolListWhere(dataType, status, keyword)
if module == "cursor" {
u := strings.TrimSpace(c.GetString("usable"))
if u == "1" || u == "0" {
if v, err := strconv.ParseInt(u, 10, 8); err == nil {
where = "(" + where + ") AND is_used = ?"
whereArgs = append(whereArgs, int8(v))
}
}
}
offset := (page - 1) * pageSize
var list interface{}
@@ -696,32 +707,161 @@ func updatePoolRemark(c *beego.Controller, module string) {
_ = c.ServeJSON()
}
func (c *PlatformAccountPoolCursorController) List() { listPoolRows(&c.Controller, "cursor") }
func (c *PlatformAccountPoolCursorController) Add() { addPoolRow(&c.Controller, "cursor") }
func (c *PlatformAccountPoolCursorController) BatchAdd() { batchAddPoolRows(&c.Controller, "cursor") }
func (c *PlatformAccountPoolCursorController) Detail() { getPoolDetail(&c.Controller, "cursor") }
func (c *PlatformAccountPoolCursorController) Extract() { extractPoolRow(&c.Controller, "cursor") }
func probePoolToken(c *beego.Controller, module string) {
if _, err := requirePlatformAuth(c); err != nil {
poolJSONErr(c, 401, 401, err.Error())
return
}
raw, err := io.ReadAll(c.Ctx.Request.Body)
if err != nil {
poolJSONErr(c, 400, 400, "参数错误")
return
}
var payload struct {
ID uint64 `json:"id"`
AccessToken string `json:"accessToken"`
Token string `json:"token"`
}
if err := json.Unmarshal(raw, &payload); err != nil {
poolJSONErr(c, 400, 400, "参数错误")
return
}
var token string
switch module {
case "cursor":
token = strings.TrimSpace(payload.AccessToken)
if token == "" {
token = strings.TrimSpace(payload.Token)
}
if token == "" {
if payload.ID == 0 {
poolJSONErr(c, 400, 400, "请传入 Cursor 的 accessToken(会话 JWT),或传 id 从库中读取")
return
}
var row models.PlatformAccountPoolCursor
if err := models.Orm.QueryTable(new(models.PlatformAccountPoolCursor)).Filter("id", payload.ID).One(&row); err != nil {
poolJSONErr(c, 404, 404, "记录不存在")
return
}
token = strings.TrimSpace(row.Token)
}
case "windsurf":
if payload.ID == 0 {
poolJSONErr(c, 400, 400, "缺少有效 id")
return
}
var row models.PlatformAccountPoolWindsurf
if err := models.Orm.QueryTable(new(models.PlatformAccountPoolWindsurf)).Filter("id", payload.ID).One(&row); err != nil {
poolJSONErr(c, 404, 404, "记录不存在")
return
}
token = strings.TrimSpace(row.Token)
case "krio":
if payload.ID == 0 {
poolJSONErr(c, 400, 400, "缺少有效 id")
return
}
var row models.PlatformAccountPoolKiro
if err := models.Orm.QueryTable(new(models.PlatformAccountPoolKiro)).Filter("id", payload.ID).One(&row); err != nil {
poolJSONErr(c, 404, 404, "记录不存在")
return
}
token = strings.TrimSpace(row.Token)
default:
poolJSONErr(c, 400, 400, "无效模块")
return
}
if token == "" {
poolJSONErr(c, 400, 400, "该记录无 Token,无法探测")
return
}
r := tokenprobe.ProbeOfficial(module, token)
data := map[string]interface{}{
"ok": r.OK,
"detail": r.Detail,
"httpStatus": r.HTTPStatus,
}
if r.ProbeMessage != "" {
data["probeMessage"] = r.ProbeMessage
}
if r.Endpoint != "" {
data["endpoint"] = r.Endpoint
}
if r.BytesRead > 0 {
data["bytesRead"] = r.BytesRead
}
if r.RawPreview != "" {
data["rawPreview"] = r.RawPreview
}
if r.RequestBodyPrefixHex != "" {
data["requestBodyPrefixHex"] = r.RequestBodyPrefixHex
}
if r.StreamProtocol != "" {
data["streamProtocol"] = r.StreamProtocol
}
if r.StreamNote != "" {
data["streamNote"] = r.StreamNote
}
if module == "cursor" && payload.ID > 0 && r.HTTPStatus == http.StatusOK {
var isUsed int8
if r.OK {
isUsed = 1
} else {
isUsed = 0
}
if _, uerr := models.Orm.QueryTable(new(models.PlatformAccountPoolCursor)).Filter("id", payload.ID).Update(orm.Params{
"is_used": isUsed,
"update_time": time.Now(),
}); uerr == nil {
data["is_used"] = int(isUsed)
}
}
c.Data["json"] = map[string]interface{}{
"code": 200,
"msg": "success",
"data": data,
}
_ = c.ServeJSON()
}
func (c *PlatformAccountPoolCursorController) List() { listPoolRows(&c.Controller, "cursor") }
func (c *PlatformAccountPoolCursorController) Add() { addPoolRow(&c.Controller, "cursor") }
func (c *PlatformAccountPoolCursorController) BatchAdd() { batchAddPoolRows(&c.Controller, "cursor") }
func (c *PlatformAccountPoolCursorController) Detail() { getPoolDetail(&c.Controller, "cursor") }
func (c *PlatformAccountPoolCursorController) Extract() { extractPoolRow(&c.Controller, "cursor") }
func (c *PlatformAccountPoolCursorController) Replenish() { replenishPoolRow(&c.Controller, "cursor") }
func (c *PlatformAccountPoolCursorController) UpdateRemark() {
updatePoolRemark(&c.Controller, "cursor")
}
func (c *PlatformAccountPoolCursorController) ProbeToken() { probePoolToken(&c.Controller, "cursor") }
func (c *PlatformAccountPoolWindsurfController) List() { listPoolRows(&c.Controller, "windsurf") }
func (c *PlatformAccountPoolWindsurfController) Add() { addPoolRow(&c.Controller, "windsurf") }
func (c *PlatformAccountPoolWindsurfController) BatchAdd() { batchAddPoolRows(&c.Controller, "windsurf") }
func (c *PlatformAccountPoolWindsurfController) Detail() { getPoolDetail(&c.Controller, "windsurf") }
func (c *PlatformAccountPoolWindsurfController) Extract() { extractPoolRow(&c.Controller, "windsurf") }
func (c *PlatformAccountPoolWindsurfController) Replenish() { replenishPoolRow(&c.Controller, "windsurf") }
func (c *PlatformAccountPoolWindsurfController) List() { listPoolRows(&c.Controller, "windsurf") }
func (c *PlatformAccountPoolWindsurfController) Add() { addPoolRow(&c.Controller, "windsurf") }
func (c *PlatformAccountPoolWindsurfController) BatchAdd() {
batchAddPoolRows(&c.Controller, "windsurf")
}
func (c *PlatformAccountPoolWindsurfController) Detail() { getPoolDetail(&c.Controller, "windsurf") }
func (c *PlatformAccountPoolWindsurfController) Extract() { extractPoolRow(&c.Controller, "windsurf") }
func (c *PlatformAccountPoolWindsurfController) Replenish() {
replenishPoolRow(&c.Controller, "windsurf")
}
func (c *PlatformAccountPoolWindsurfController) UpdateRemark() {
updatePoolRemark(&c.Controller, "windsurf")
}
func (c *PlatformAccountPoolWindsurfController) ProbeToken() {
probePoolToken(&c.Controller, "windsurf")
}
func (c *PlatformAccountPoolKrioController) List() { listPoolRows(&c.Controller, "krio") }
func (c *PlatformAccountPoolKrioController) Add() { addPoolRow(&c.Controller, "krio") }
func (c *PlatformAccountPoolKrioController) BatchAdd() { batchAddPoolRows(&c.Controller, "krio") }
func (c *PlatformAccountPoolKrioController) Detail() { getPoolDetail(&c.Controller, "krio") }
func (c *PlatformAccountPoolKrioController) Extract() { extractPoolRow(&c.Controller, "krio") }
func (c *PlatformAccountPoolKrioController) List() { listPoolRows(&c.Controller, "krio") }
func (c *PlatformAccountPoolKrioController) Add() { addPoolRow(&c.Controller, "krio") }
func (c *PlatformAccountPoolKrioController) BatchAdd() { batchAddPoolRows(&c.Controller, "krio") }
func (c *PlatformAccountPoolKrioController) Detail() { getPoolDetail(&c.Controller, "krio") }
func (c *PlatformAccountPoolKrioController) Extract() { extractPoolRow(&c.Controller, "krio") }
func (c *PlatformAccountPoolKrioController) Replenish() { replenishPoolRow(&c.Controller, "krio") }
func (c *PlatformAccountPoolKrioController) UpdateRemark() {
updatePoolRemark(&c.Controller, "krio")
}
func (c *PlatformAccountPoolKrioController) ProbeToken() { probePoolToken(&c.Controller, "krio") }