feat: add account probes and mute scanning
This commit is contained in:
1 parent
e50b4e1831
commit
8c0eed2214
41 files changed
+1730
-172
No files matched your search
@@ -0,0 +1,216 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
dsprotocol "ds2api/internal/deepseek/protocol"
|
||||
)
|
||||
|
||||
type TokenValidationResult struct {
|
||||
Valid bool
|
||||
HTTPStatus int
|
||||
Code int
|
||||
BizCode int
|
||||
Message string
|
||||
}
|
||||
|
||||
type AccountCapabilities struct {
|
||||
Vision *bool
|
||||
Models []string
|
||||
CheckedAt int64
|
||||
Source string
|
||||
}
|
||||
|
||||
type AccountMuteStatus struct {
|
||||
Muted bool
|
||||
MuteUntil float64
|
||||
CheckedAt int64
|
||||
HTTPStatus int
|
||||
Code int
|
||||
BizCode int
|
||||
Message string
|
||||
Source string
|
||||
}
|
||||
|
||||
func (c *Client) ValidateToken(ctx context.Context, token string) (*TokenValidationResult, error) {
|
||||
token = strings.TrimSpace(token)
|
||||
if token == "" {
|
||||
return &TokenValidationResult{Valid: false, Message: "empty token"}, nil
|
||||
}
|
||||
clients := c.requestClientsFromContext(ctx)
|
||||
body, status, err := c.getJSONWithStatus(ctx, clients.regular, dsprotocol.DeepSeekCurrentUserURL, c.authHeaders(token))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
code, bizCode, msg, bizMsg := extractResponseStatus(body)
|
||||
valid := status == http.StatusOK && code == 0 && bizCode == 0 && !isTokenInvalid(status, code, bizCode, msg, bizMsg)
|
||||
result := &TokenValidationResult{Valid: valid, HTTPStatus: status, Code: code, BizCode: bizCode}
|
||||
if !valid {
|
||||
result.Message = failureMessage(msg, bizMsg, fmt.Sprintf("HTTP %d", status))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetAccountMuteStatus(ctx context.Context, token string) (*AccountMuteStatus, error) {
|
||||
token = strings.TrimSpace(token)
|
||||
if token == "" {
|
||||
return nil, errors.New("empty token")
|
||||
}
|
||||
clients := c.requestClientsFromContext(ctx)
|
||||
body, status, err := c.getJSONWithStatus(ctx, clients.regular, dsprotocol.DeepSeekCurrentUserURL, c.authHeaders(token))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
code, bizCode, msg, bizMsg := extractResponseStatus(body)
|
||||
info := extractMuteInfo(body)
|
||||
result := &AccountMuteStatus{
|
||||
Muted: info.Muted,
|
||||
MuteUntil: info.Until,
|
||||
CheckedAt: time.Now().Unix(),
|
||||
HTTPStatus: status,
|
||||
Code: code,
|
||||
BizCode: bizCode,
|
||||
Message: failureMessage(msg, bizMsg, fmt.Sprintf("HTTP %d", status)),
|
||||
Source: "users_current",
|
||||
}
|
||||
if info.Muted {
|
||||
return result, nil
|
||||
}
|
||||
if status != http.StatusOK || code != 0 || bizCode != 0 || isTokenInvalid(status, code, bizCode, msg, bizMsg) {
|
||||
return nil, fmt.Errorf("current user failed: %s", result.Message)
|
||||
}
|
||||
result.Message = ""
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetAccountCapabilities(ctx context.Context, token string, accountID string) (*AccountCapabilities, error) {
|
||||
token = strings.TrimSpace(token)
|
||||
if token == "" {
|
||||
return nil, errors.New("empty token")
|
||||
}
|
||||
endpoint, err := url.Parse(dsprotocol.DeepSeekClientSettingsURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
q := endpoint.Query()
|
||||
q.Set("did", DeviceID(accountID))
|
||||
q.Set("scope", "model")
|
||||
endpoint.RawQuery = q.Encode()
|
||||
|
||||
clients := c.requestClientsFromContext(ctx)
|
||||
body, status, err := c.getJSONWithStatus(ctx, clients.regular, endpoint.String(), c.authHeaders(token))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
code, bizCode, msg, bizMsg := extractResponseStatus(body)
|
||||
if status != http.StatusOK || code != 0 || bizCode != 0 || isTokenInvalid(status, code, bizCode, msg, bizMsg) {
|
||||
return nil, fmt.Errorf("client settings failed: %s", failureMessage(msg, bizMsg, fmt.Sprintf("HTTP %d", status)))
|
||||
}
|
||||
|
||||
configs := extractModelConfigs(body)
|
||||
modelSet := map[string]struct{}{}
|
||||
var vision *bool
|
||||
seenVision := false
|
||||
for _, item := range configs {
|
||||
modelType := stringFromProbeAny(item["model_type"])
|
||||
if modelType == "" {
|
||||
modelType = stringFromProbeAny(item["modelType"])
|
||||
}
|
||||
if modelType != "" {
|
||||
modelSet[modelType] = struct{}{}
|
||||
}
|
||||
if modelType == "vision" {
|
||||
seenVision = true
|
||||
if value, ok := boolFromProbeAny(item["switchable"]); ok {
|
||||
vision = boolPtr(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(configs) > 0 && !seenVision {
|
||||
vision = boolPtr(false)
|
||||
}
|
||||
models := make([]string, 0, len(modelSet))
|
||||
for model := range modelSet {
|
||||
models = append(models, model)
|
||||
}
|
||||
sort.Strings(models)
|
||||
return &AccountCapabilities{
|
||||
Vision: vision,
|
||||
Models: models,
|
||||
CheckedAt: time.Now().Unix(),
|
||||
Source: "client_settings",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func extractModelConfigs(body map[string]any) []map[string]any {
|
||||
data, _ := body["data"].(map[string]any)
|
||||
bizData, _ := data["biz_data"].(map[string]any)
|
||||
settings, _ := bizData["settings"].(map[string]any)
|
||||
modelConfigs, _ := settings["model_configs"].(map[string]any)
|
||||
switch raw := modelConfigs["value"].(type) {
|
||||
case []any:
|
||||
out := make([]map[string]any, 0, len(raw))
|
||||
for _, item := range raw {
|
||||
if m, ok := item.(map[string]any); ok {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out
|
||||
case string:
|
||||
var decoded []map[string]any
|
||||
if err := json.Unmarshal([]byte(raw), &decoded); err == nil {
|
||||
return decoded
|
||||
}
|
||||
var rawItems []any
|
||||
if err := json.Unmarshal([]byte(raw), &rawItems); err != nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]map[string]any, 0, len(rawItems))
|
||||
for _, item := range rawItems {
|
||||
if m, ok := item.(map[string]any); ok {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func stringFromProbeAny(v any) string {
|
||||
if s, ok := v.(string); ok {
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func boolFromProbeAny(v any) (bool, bool) {
|
||||
switch x := v.(type) {
|
||||
case bool:
|
||||
return x, true
|
||||
case string:
|
||||
switch strings.ToLower(strings.TrimSpace(x)) {
|
||||
case "true", "1":
|
||||
return true, true
|
||||
case "false", "0":
|
||||
return false, true
|
||||
}
|
||||
case float64:
|
||||
return x != 0, true
|
||||
case int:
|
||||
return x != 0, true
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
|
||||
func boolPtr(v bool) *bool {
|
||||
return &v
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
dsprotocol "ds2api/internal/deepseek/protocol"
|
||||
)
|
||||
|
||||
func TestValidateTokenUsesCurrentUserEndpoint(t *testing.T) {
|
||||
var gotURL string
|
||||
var gotAuth string
|
||||
c := &Client{regular: doerFunc(func(req *http.Request) (*http.Response, error) {
|
||||
gotURL = req.URL.String()
|
||||
gotAuth = req.Header.Get("Authorization")
|
||||
return probeResponse(http.StatusOK, `{"code":0,"data":{"biz_code":0}}`, req), nil
|
||||
})}
|
||||
|
||||
result, err := c.ValidateToken(context.Background(), "tok")
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateToken error: %v", err)
|
||||
}
|
||||
if !result.Valid {
|
||||
t.Fatalf("expected token valid, got %#v", result)
|
||||
}
|
||||
if gotURL != dsprotocol.DeepSeekCurrentUserURL {
|
||||
t.Fatalf("unexpected endpoint: %s", gotURL)
|
||||
}
|
||||
if gotAuth != "Bearer tok" {
|
||||
t.Fatalf("unexpected auth header: %q", gotAuth)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateTokenMarksInvalidStatus(t *testing.T) {
|
||||
c := &Client{regular: doerFunc(func(req *http.Request) (*http.Response, error) {
|
||||
return probeResponse(http.StatusUnauthorized, `{"code":401,"msg":"invalid token","data":{"biz_code":401}}`, req), nil
|
||||
})}
|
||||
|
||||
result, err := c.ValidateToken(context.Background(), "bad")
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateToken error: %v", err)
|
||||
}
|
||||
if result.Valid {
|
||||
t.Fatalf("expected invalid token, got %#v", result)
|
||||
}
|
||||
if result.HTTPStatus != http.StatusUnauthorized {
|
||||
t.Fatalf("expected status 401, got %d", result.HTTPStatus)
|
||||
}
|
||||
if !strings.Contains(result.Message, "invalid token") {
|
||||
t.Fatalf("expected invalid token message, got %q", result.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAccountCapabilitiesParsesModelConfigsArray(t *testing.T) {
|
||||
var gotURL string
|
||||
c := &Client{regular: doerFunc(func(req *http.Request) (*http.Response, error) {
|
||||
gotURL = req.URL.String()
|
||||
return probeResponse(http.StatusOK, `{
|
||||
"code":0,
|
||||
"data":{"biz_code":0,"biz_data":{"settings":{"model_configs":{"value":[
|
||||
{"model_type":"chat","switchable":true},
|
||||
{"model_type":"vision","switchable":true},
|
||||
{"model_type":"expert","switchable":false}
|
||||
]}}}}
|
||||
}`, req), nil
|
||||
})}
|
||||
|
||||
result, err := c.GetAccountCapabilities(context.Background(), "tok", "u@example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("GetAccountCapabilities error: %v", err)
|
||||
}
|
||||
if result.Vision == nil || !*result.Vision {
|
||||
t.Fatalf("expected vision switchable, got %#v", result.Vision)
|
||||
}
|
||||
if !reflect.DeepEqual(result.Models, []string{"chat", "expert", "vision"}) {
|
||||
t.Fatalf("unexpected models: %#v", result.Models)
|
||||
}
|
||||
if result.Source != "client_settings" {
|
||||
t.Fatalf("unexpected source: %q", result.Source)
|
||||
}
|
||||
if !strings.HasPrefix(gotURL, dsprotocol.DeepSeekClientSettingsURL+"?") {
|
||||
t.Fatalf("unexpected endpoint: %s", gotURL)
|
||||
}
|
||||
if !strings.Contains(gotURL, "scope=model") || !strings.Contains(gotURL, "did=") {
|
||||
t.Fatalf("expected scope and did query, got %s", gotURL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAccountCapabilitiesParsesModelConfigsString(t *testing.T) {
|
||||
c := &Client{regular: doerFunc(func(req *http.Request) (*http.Response, error) {
|
||||
return probeResponse(http.StatusOK, `{
|
||||
"code":0,
|
||||
"data":{"biz_code":0,"biz_data":{"settings":{"model_configs":{"value":"[{\"model_type\":\"vision\",\"switchable\":\"false\"}]"}}}}
|
||||
}`, req), nil
|
||||
})}
|
||||
|
||||
result, err := c.GetAccountCapabilities(context.Background(), "tok", "u@example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("GetAccountCapabilities error: %v", err)
|
||||
}
|
||||
if result.Vision == nil || *result.Vision {
|
||||
t.Fatalf("expected vision switchable=false, got %#v", result.Vision)
|
||||
}
|
||||
if !reflect.DeepEqual(result.Models, []string{"vision"}) {
|
||||
t.Fatalf("unexpected models: %#v", result.Models)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAccountCapabilitiesMarksVisionUnavailableWhenAbsent(t *testing.T) {
|
||||
c := &Client{regular: doerFunc(func(req *http.Request) (*http.Response, error) {
|
||||
return probeResponse(http.StatusOK, `{
|
||||
"code":0,
|
||||
"data":{"biz_code":0,"biz_data":{"settings":{"model_configs":{"value":[
|
||||
{"model_type":"chat","switchable":true},
|
||||
{"model_type":"expert","switchable":true}
|
||||
]}}}}
|
||||
}`, req), nil
|
||||
})}
|
||||
|
||||
result, err := c.GetAccountCapabilities(context.Background(), "tok", "u@example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("GetAccountCapabilities error: %v", err)
|
||||
}
|
||||
if result.Vision == nil || *result.Vision {
|
||||
t.Fatalf("expected vision unavailable, got %#v", result.Vision)
|
||||
}
|
||||
if !reflect.DeepEqual(result.Models, []string{"chat", "expert"}) {
|
||||
t.Fatalf("unexpected models: %#v", result.Models)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAccountMuteStatusParsesNestedChatMute(t *testing.T) {
|
||||
c := &Client{regular: doerFunc(func(req *http.Request) (*http.Response, error) {
|
||||
return probeResponse(http.StatusOK, `{
|
||||
"code":0,
|
||||
"data":{"biz_code":0,"biz_data":{"chat":{"is_muted":1,"mute_until":1234.5}}}
|
||||
}`, req), nil
|
||||
})}
|
||||
|
||||
result, err := c.GetAccountMuteStatus(context.Background(), "tok")
|
||||
if err != nil {
|
||||
t.Fatalf("GetAccountMuteStatus error: %v", err)
|
||||
}
|
||||
if !result.Muted || result.MuteUntil != 1234.5 {
|
||||
t.Fatalf("unexpected mute status: %#v", result)
|
||||
}
|
||||
if result.Source != "users_current" {
|
||||
t.Fatalf("unexpected source: %q", result.Source)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAccountMuteStatusReturnsMutedBizFailure(t *testing.T) {
|
||||
c := &Client{regular: doerFunc(func(req *http.Request) (*http.Response, error) {
|
||||
return probeResponse(http.StatusOK, `{
|
||||
"code":0,
|
||||
"data":{"biz_code":5,"biz_msg":"muted","biz_data":{"chat":{"is_muted":true,"mute_until":"4567"}}}
|
||||
}`, req), nil
|
||||
})}
|
||||
|
||||
result, err := c.GetAccountMuteStatus(context.Background(), "tok")
|
||||
if err != nil {
|
||||
t.Fatalf("GetAccountMuteStatus error: %v", err)
|
||||
}
|
||||
if !result.Muted || result.MuteUntil != 4567 {
|
||||
t.Fatalf("unexpected mute status: %#v", result)
|
||||
}
|
||||
if result.BizCode != 5 {
|
||||
t.Fatalf("expected biz_code 5, got %d", result.BizCode)
|
||||
}
|
||||
}
|
||||
|
||||
func probeResponse(status int, body string, req *http.Request) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: status,
|
||||
Header: make(http.Header),
|
||||
Body: io.NopCloser(strings.NewReader(body)),
|
||||
Request: req,
|
||||
}
|
||||
}
|
||||
@@ -26,14 +26,32 @@ func extractMuteInfo(resp map[string]any) muteInfo {
|
||||
_, bizCode, msg, bizMsg := extractResponseStatus(resp)
|
||||
data, _ := resp["data"].(map[string]any)
|
||||
bizData, _ := data["biz_data"].(map[string]any)
|
||||
isMuted := intFrom(bizData["is_muted"]) == 1
|
||||
chat, _ := bizData["chat"].(map[string]any)
|
||||
isMuted := muteFlagFrom(bizData["is_muted"]) || muteFlagFrom(chat["is_muted"])
|
||||
muteUntil := floatFrom(bizData["mute_until"])
|
||||
if muteUntil == 0 {
|
||||
muteUntil = floatFrom(chat["mute_until"])
|
||||
}
|
||||
combined := strings.ToLower(strings.TrimSpace(msg) + " " + strings.TrimSpace(bizMsg))
|
||||
if bizCode == 5 || isMuted || strings.Contains(combined, "muted") {
|
||||
return muteInfo{Muted: true, Until: floatFrom(bizData["mute_until"])}
|
||||
if bizCode == 5 || isMuted || strings.Contains(combined, "muted") || strings.Contains(combined, "禁言") {
|
||||
return muteInfo{Muted: true, Until: muteUntil}
|
||||
}
|
||||
return muteInfo{}
|
||||
}
|
||||
|
||||
func muteFlagFrom(v any) bool {
|
||||
switch x := v.(type) {
|
||||
case bool:
|
||||
return x
|
||||
case string:
|
||||
switch strings.ToLower(strings.TrimSpace(x)) {
|
||||
case "true", "1", "yes":
|
||||
return true
|
||||
}
|
||||
}
|
||||
return intFrom(v) == 1
|
||||
}
|
||||
|
||||
func floatFrom(v any) float64 {
|
||||
switch x := v.(type) {
|
||||
case float64:
|
||||
|
||||
Reference in new issue
Block a user