udpate: add xunlei ck

This commit is contained in:
ctwj
2025-09-03 00:48:10 +08:00
parent 4aab45cda5
commit 37004107d0
7 changed files with 113 additions and 34 deletions

View File

@@ -8,6 +8,7 @@ import (
"sync"
"time"
"github.com/ctwj/urldb/db/entity"
"github.com/ctwj/urldb/utils"
)
@@ -347,6 +348,11 @@ func (a *AlipanService) getAlipan1(shareID string) (*AlipanShareInfo, error) {
return &result, nil
}
// GetUserInfoByEntity 根据 entity.Cks 获取用户信息(待实现)
func (a *AlipanService) GetUserInfoByEntity(cks entity.Cks) (*UserInfo, error) {
return nil, nil
}
// getAlipan2 通过分享id获取X-Share-Token
func (a *AlipanService) getAlipan2(shareID string) (*AlipanShareToken, error) {
data := map[string]interface{}{

View File

@@ -2,6 +2,8 @@ package pan
import (
"fmt"
"github.com/ctwj/urldb/db/entity"
)
// BaiduPanService 百度网盘服务
@@ -101,3 +103,8 @@ func (b *BaiduPanService) GetUserInfo(cookie string) (*UserInfo, error) {
ServiceType: "baidu",
}, nil
}
// GetUserInfoByEntity 根据 entity.Cks 获取用户信息(待实现)
func (b *BaiduPanService) GetUserInfoByEntity(cks entity.Cks) (*UserInfo, error) {
return nil, nil
}

View File

@@ -5,6 +5,8 @@ import (
"strconv"
"strings"
"sync"
"github.com/ctwj/urldb/db/entity"
)
// ServiceType 定义网盘服务类型
@@ -74,6 +76,7 @@ type UserInfo struct {
UsedSpace int64 `json:"usedSpace"` // 已使用空间
TotalSpace int64 `json:"totalSpace"` // 总空间
ServiceType string `json:"serviceType"` // 服务类型
ExtraData string `json:"extraData"` // 额外信息
}
// PanService 网盘服务接口
@@ -88,7 +91,9 @@ type PanService interface {
DeleteFiles(fileList []string) (*TransferResult, error)
// GetUserInfo 获取用户信息
GetUserInfo(cookie string) (*UserInfo, error)
GetUserInfo(ck string) (*UserInfo, error)
GetUserInfoByEntity(entity entity.Cks) (*UserInfo, error)
// GetServiceType 获取服务类型
GetServiceType() ServiceType

View File

@@ -1028,6 +1028,11 @@ func (q *QuarkPanService) GetUserInfo(cookie string) (*UserInfo, error) {
}, nil
}
// GetUserInfoByEntity 根据 entity.Cks 获取用户信息(待实现)
func (q *QuarkPanService) GetUserInfoByEntity(cks entity.Cks) (*UserInfo, error) {
return nil, nil
}
// formatBytes 格式化字节数为可读格式
func formatBytes(bytes int64) string {
const unit = 1024

View File

@@ -1,6 +1,10 @@
package pan
import "fmt"
import (
"fmt"
"github.com/ctwj/urldb/db/entity"
)
// UCService UC网盘服务
type UCService struct {
@@ -97,3 +101,8 @@ func (u *UCService) GetUserInfo(cookie string) (*UserInfo, error) {
ServiceType: "uc",
}, nil
}
// GetUserInfoByEntity 根据 entity.Cks 获取用户信息(待实现)
func (u *UCService) GetUserInfoByEntity(cks entity.Cks) (*UserInfo, error) {
return nil, nil
}

View File

@@ -9,6 +9,7 @@ import (
"sync"
"time"
"github.com/ctwj/urldb/db/entity"
"github.com/ctwj/urldb/db/repo"
)
@@ -79,7 +80,7 @@ func NewXunleiPanService(config *PanConfig, cksRepo ...repo.CksRepository) *Xunl
xunleiInstance.SetHeaders(map[string]string{
"Accept": "*/;",
"Accept-Encoding": "gzip, deflate",
"Accept-Encoding": "deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Content-Type": "application/json",
@@ -115,7 +116,7 @@ func GetXunleiInstance() *XunleiPanService {
return NewXunleiPanService(nil)
}
func (x *XunleiPanService) getAccessTokenByRefreshToken(refreshToken string) (map[string]interface{}, error) {
func (x *XunleiPanService) GetAccessTokenByRefreshToken(refreshToken string) (map[string]interface{}, error) {
// 构造请求体
body := map[string]interface{}{
"client_id": x.clientId,
@@ -137,12 +138,13 @@ func (x *XunleiPanService) getAccessTokenByRefreshToken(refreshToken string) (ma
return map[string]interface{}{}, fmt.Errorf("获取 access_token 请求失败: %v", err)
}
if resp["code"] != 0 || resp["data"] == nil {
return map[string]interface{}{}, fmt.Errorf("获取 access_token 失败: %v", resp)
// 正确做法:用 exists 判断
if _, exists := resp["access_token"]; exists {
// 会输出,即使值为 nil
} else {
return map[string]interface{}{}, fmt.Errorf("获取 access_token 请求失败: %v 不存在", "access_token")
}
data := resp["data"].(map[string]interface{})
return data, nil
return resp, nil
}
// getAccessToken 获取 Access Token内部包含缓存判断、刷新、保存- 匹配 PHP 版本
@@ -177,7 +179,7 @@ func (x *XunleiPanService) getAccessToken() (string, error) {
return "", fmt.Errorf("未配置迅雷 refresh_token请检查 cks 表的 ck 字段")
}
newData, err := x.getAccessTokenByRefreshToken(refreshTokenValue)
newData, err := x.GetAccessTokenByRefreshToken(refreshTokenValue)
if err != nil {
return "", fmt.Errorf("获取 access_token 失败: %v", err)
}
@@ -551,6 +553,11 @@ func (x *XunleiPanService) getTaskStatus(taskID string, retryIndex int, accessTo
return &data, nil
}
// GetUserInfoByEntity 根据 entity.Cks 获取用户信息(待实现)
func (x *XunleiPanService) GetUserInfoByEntity(cks entity.Cks) (*UserInfo, error) {
return nil, nil
}
// getShare 获取分享详情 - 匹配 PHP 版本
func (x *XunleiPanService) getShare(shareID, passCode, accessToken, captchaToken string) (map[string]interface{}, error) {
// 设置 headers
@@ -750,12 +757,21 @@ func (x *XunleiPanService) DeleteFiles(fileList []string) (*TransferResult, erro
return SuccessResult("删除成功", nil), nil
}
// GetUserInfo 获取用户信息 - 实现 PanService 接口,使用 HTTPGet
// GetUserInfo 获取用户信息 - 实现 PanService 接口,cookie 参数为 refresh_token先获取 access_token 再访问 API
func (x *XunleiPanService) GetUserInfo(cookie string) (*UserInfo, error) {
log.Printf("开始获取迅雷网盘用户信息")
log.Printf("开始获取迅雷网盘用户信息cookie 为 refresh_token")
// 临时设置cookie
x.SetHeader("Authorization", cookie)
// 使用 refresh_token 获取 access_token
accessTokenData, err := x.GetAccessTokenByRefreshToken(cookie)
if err != nil {
return nil, fmt.Errorf("获取 access_token 失败: %v", err)
}
accessToken := accessTokenData["access_token"].(string)
log.Printf("成功获取 access_token")
// 设置 Authorization header 为 Bearer token
x.SetHeader("Authorization", "Bearer "+accessToken)
// 获取用户信息
respData, err := x.HTTPGet(x.apiHost("user")+"/v1/user/me", nil)