mirror of
https://github.com/fish2018/pansou.git
synced 2025-11-25 03:14:59 +08:00
update
This commit is contained in:
@@ -23,19 +23,20 @@ import (
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"pansou/model"
|
||||
"pansou/plugin"
|
||||
"pansou/util/json"
|
||||
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
cloudscraper "github.com/Advik-B/cloudscraper/lib"
|
||||
)
|
||||
|
||||
// 插件配置参数
|
||||
const (
|
||||
MaxConcurrentUsers = 10 // 最多使用的用户数
|
||||
MaxConcurrentDetails = 50 // 最大并发详情请求数
|
||||
DebugLog = false // 调试日志开关(排查问题时改为true)
|
||||
MaxConcurrentUsers = 10 // 最多使用的用户数
|
||||
MaxConcurrentDetails = 50 // 最大并发详情请求数
|
||||
DebugLog = true // 调试日志开关(排查问题时改为true)
|
||||
)
|
||||
|
||||
// 默认账户配置(可通过Web界面添加更多账户)
|
||||
@@ -57,9 +58,9 @@ func init() {
|
||||
if cachePath == "" {
|
||||
cachePath = "./cache"
|
||||
}
|
||||
|
||||
|
||||
StorageDir = filepath.Join(cachePath, "gying_users")
|
||||
|
||||
|
||||
if err := os.MkdirAll(StorageDir, 0755); err != nil {
|
||||
fmt.Printf("⚠️ 警告: 无法创建Gying存储目录 %s: %v\n", StorageDir, err)
|
||||
} else {
|
||||
@@ -419,8 +420,8 @@ type SearchData struct {
|
||||
|
||||
// DetailData 详情接口JSON数据结构
|
||||
type DetailData struct {
|
||||
Code int `json:"code"`
|
||||
WP bool `json:"wp"`
|
||||
Code int `json:"code"`
|
||||
WP bool `json:"wp"`
|
||||
Panlist struct {
|
||||
ID []string `json:"id"`
|
||||
Name []string `json:"name"`
|
||||
@@ -467,7 +468,7 @@ func (p *GyingPlugin) RegisterWebRoutes(router *gin.RouterGroup) {
|
||||
gying := router.Group("/gying")
|
||||
gying.GET("/:param", p.handleManagePage)
|
||||
gying.POST("/:param", p.handleManagePagePOST)
|
||||
|
||||
|
||||
fmt.Printf("[Gying] Web路由已注册: /gying/:param\n")
|
||||
}
|
||||
|
||||
@@ -491,7 +492,7 @@ func (p *GyingPlugin) SearchWithResult(keyword string, ext map[string]interface{
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 找到 %d 个有效用户\n", len(users))
|
||||
}
|
||||
|
||||
|
||||
if len(users) == 0 {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 没有有效用户,返回空结果\n")
|
||||
@@ -550,7 +551,7 @@ func (p *GyingPlugin) loadAllUsers() {
|
||||
// scraper实例将在initDefaultAccounts中通过重新登录获取
|
||||
p.users.Store(user.Hash, &user)
|
||||
count++
|
||||
|
||||
|
||||
if DebugLog {
|
||||
hasPassword := "无"
|
||||
if user.EncryptedPassword != "" {
|
||||
@@ -565,7 +566,9 @@ func (p *GyingPlugin) loadAllUsers() {
|
||||
|
||||
// initDefaultAccounts 初始化所有账户(异步执行,不阻塞启动)
|
||||
// 包括:1. DefaultAccounts(代码配置) 2. 从文件加载的用户(使用加密密码重新登录)
|
||||
func (p *GyingPlugin) initDefaultAccounts() {
|
||||
func (p *GyingPlugin) initDefaultAccounts() {
|
||||
fmt.Printf("[Gying] ========== 异步初始化所有账户 ==========\n")
|
||||
|
||||
// 步骤1:处理DefaultAccounts(代码中配置的默认账户)
|
||||
for i, account := range DefaultAccounts {
|
||||
if DebugLog {
|
||||
@@ -574,7 +577,7 @@ func (p *GyingPlugin) initDefaultAccounts() {
|
||||
|
||||
p.initOrRestoreUser(account.Username, account.Password, "default")
|
||||
}
|
||||
|
||||
|
||||
// 步骤2:遍历所有已加载的用户,恢复没有scraper的用户
|
||||
var usersToRestore []*User
|
||||
p.users.Range(func(key, value interface{}) bool {
|
||||
@@ -586,30 +589,32 @@ func (p *GyingPlugin) initDefaultAccounts() {
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
|
||||
if len(usersToRestore) > 0 {
|
||||
fmt.Printf("[Gying] 发现 %d 个需要恢复的用户(使用加密密码重新登录)\n", len(usersToRestore))
|
||||
for i, user := range usersToRestore {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] [恢复用户 %d/%d] 处理: %s\n", i+1, len(usersToRestore), user.UsernameMasked)
|
||||
}
|
||||
|
||||
|
||||
// 解密密码
|
||||
password, err := p.decryptPassword(user.EncryptedPassword)
|
||||
if err != nil {
|
||||
fmt.Printf("[Gying] ❌ 用户 %s 解密密码失败: %v\n", user.UsernameMasked, err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
p.initOrRestoreUser(user.Username, password, "restore")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("[Gying] ========== 所有账户初始化完成 ==========\n")
|
||||
}
|
||||
|
||||
// initOrRestoreUser 初始化或恢复单个用户(登录并保存)
|
||||
func (p *GyingPlugin) initOrRestoreUser(username, password, source string) {
|
||||
hash := p.generateHash(username)
|
||||
|
||||
|
||||
// 检查scraper是否已存在
|
||||
_, scraperExists := p.scrapers.Load(hash)
|
||||
if scraperExists {
|
||||
@@ -618,7 +623,7 @@ func (p *GyingPlugin) initOrRestoreUser(username, password, source string) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 登录
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 开始登录账户: %s\n", username)
|
||||
@@ -639,7 +644,7 @@ func (p *GyingPlugin) initOrRestoreUser(username, password, source string) {
|
||||
fmt.Printf("[Gying] ❌ 加密密码失败: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 保存用户
|
||||
user := &User{
|
||||
Hash: hash,
|
||||
@@ -653,10 +658,10 @@ func (p *GyingPlugin) initOrRestoreUser(username, password, source string) {
|
||||
ExpireAt: time.Now().AddDate(0, 4, 0), // 121天有效期
|
||||
LastAccessAt: time.Now(),
|
||||
}
|
||||
|
||||
|
||||
// 保存scraper实例到内存
|
||||
p.scrapers.Store(hash, scraper)
|
||||
|
||||
|
||||
if err := p.saveUser(user); err != nil {
|
||||
fmt.Printf("[Gying] ❌ 保存账户失败: %v\n", err)
|
||||
return
|
||||
@@ -700,7 +705,7 @@ func (p *GyingPlugin) deleteUser(hash string) error {
|
||||
// getActiveUsers 获取有效用户
|
||||
func (p *GyingPlugin) getActiveUsers() []*User {
|
||||
var users []*User
|
||||
|
||||
|
||||
p.users.Range(func(key, value interface{}) bool {
|
||||
user := value.(*User)
|
||||
if user.Status == "active" && user.Cookie != "" {
|
||||
@@ -708,7 +713,7 @@ func (p *GyingPlugin) getActiveUsers() []*User {
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
|
||||
return users
|
||||
}
|
||||
|
||||
@@ -789,13 +794,13 @@ func (p *GyingPlugin) handleGetStatus(c *gin.Context, hash string) {
|
||||
}
|
||||
|
||||
respondSuccess(c, "获取成功", gin.H{
|
||||
"hash": hash,
|
||||
"logged_in": loggedIn,
|
||||
"status": user.Status,
|
||||
"username_masked": user.UsernameMasked,
|
||||
"login_time": user.LoginAt.Format("2006-01-02 15:04:05"),
|
||||
"expire_time": user.ExpireAt.Format("2006-01-02 15:04:05"),
|
||||
"expires_in_days": expiresInDays,
|
||||
"hash": hash,
|
||||
"logged_in": loggedIn,
|
||||
"status": user.Status,
|
||||
"username_masked": user.UsernameMasked,
|
||||
"login_time": user.LoginAt.Format("2006-01-02 15:04:05"),
|
||||
"expire_time": user.ExpireAt.Format("2006-01-02 15:04:05"),
|
||||
"expires_in_days": expiresInDays,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -825,7 +830,7 @@ func (p *GyingPlugin) handleLogin(c *gin.Context, hash string, reqData map[strin
|
||||
respondError(c, "加密密码失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 保存用户
|
||||
user := &User{
|
||||
Hash: hash,
|
||||
@@ -838,7 +843,7 @@ func (p *GyingPlugin) handleLogin(c *gin.Context, hash string, reqData map[strin
|
||||
ExpireAt: time.Now().AddDate(0, 4, 0), // 121天
|
||||
LastAccessAt: time.Now(),
|
||||
}
|
||||
|
||||
|
||||
if _, exists := p.getUserByHash(hash); !exists {
|
||||
user.CreatedAt = time.Now()
|
||||
}
|
||||
@@ -895,13 +900,13 @@ func (p *GyingPlugin) handleTestSearch(c *gin.Context, hash string, reqData map[
|
||||
respondError(c, "用户scraper实例不存在,请重新登录")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
scraper, ok := scraperVal.(*cloudscraper.Scraper)
|
||||
if !ok || scraper == nil {
|
||||
respondError(c, "scraper实例无效,请重新登录")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 执行搜索
|
||||
results, err := p.searchWithScraper(keyword, scraper)
|
||||
if err != nil {
|
||||
@@ -946,27 +951,27 @@ func (p *GyingPlugin) handleTestSearch(c *gin.Context, hash string, reqData map[
|
||||
func (p *GyingPlugin) encryptPassword(password string) (string, error) {
|
||||
// 使用固定密钥(实际应用中可以使用配置或环境变量)
|
||||
key := []byte("gying-secret-key-32bytes-long!!!") // 32字节密钥用于AES-256
|
||||
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
// 创建GCM模式
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
// 生成随机nonce
|
||||
nonce := make([]byte, gcm.NonceSize())
|
||||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
// 加密
|
||||
ciphertext := gcm.Seal(nonce, nonce, []byte(password), nil)
|
||||
|
||||
|
||||
// 返回base64编码的密文
|
||||
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
||||
}
|
||||
@@ -975,34 +980,34 @@ func (p *GyingPlugin) encryptPassword(password string) (string, error) {
|
||||
func (p *GyingPlugin) decryptPassword(encrypted string) (string, error) {
|
||||
// 使用与加密相同的密钥
|
||||
key := []byte("gying-secret-key-32bytes-long!!!")
|
||||
|
||||
|
||||
// base64解码
|
||||
ciphertext, err := base64.StdEncoding.DecodeString(encrypted)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
nonceSize := gcm.NonceSize()
|
||||
if len(ciphertext) < nonceSize {
|
||||
return "", fmt.Errorf("ciphertext too short")
|
||||
}
|
||||
|
||||
|
||||
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
|
||||
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
return string(plaintext), nil
|
||||
}
|
||||
|
||||
@@ -1015,64 +1020,64 @@ func (p *GyingPlugin) createScraperWithCookies(cookieStr string) (*cloudscraper.
|
||||
// 创建cloudscraper实例,配置以保护cookies不被刷新
|
||||
scraper, err := cloudscraper.New(
|
||||
cloudscraper.WithSessionConfig(
|
||||
false, // refreshOn403 = false,禁用403时自动刷新
|
||||
365*24*time.Hour, // interval = 1年,基本不刷新
|
||||
0, // maxRetries = 0
|
||||
false, // refreshOn403 = false,禁用403时自动刷新
|
||||
365*24*time.Hour, // interval = 1年,基本不刷新
|
||||
0, // maxRetries = 0
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建cloudscraper失败: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// 如果有保存的cookies,使用反射设置到scraper的内部http.Client
|
||||
if cookieStr != "" {
|
||||
cookies := parseCookieString(cookieStr)
|
||||
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 正在恢复 %d 个cookie到scraper实例\n", len(cookies))
|
||||
}
|
||||
|
||||
|
||||
// 使用反射访问scraper的unexported client字段
|
||||
scraperValue := reflect.ValueOf(scraper).Elem()
|
||||
clientField := scraperValue.FieldByName("client")
|
||||
|
||||
|
||||
if clientField.IsValid() && !clientField.IsNil() {
|
||||
// 使用反射访问client (需要使用Elem()因为是指针)
|
||||
clientValue := reflect.NewAt(clientField.Type(), unsafe.Pointer(clientField.UnsafeAddr())).Elem()
|
||||
client, ok := clientValue.Interface().(*http.Client)
|
||||
|
||||
|
||||
if ok && client != nil && client.Jar != nil {
|
||||
// 将cookies设置到cookiejar
|
||||
// 注意:必须使用正确的URL和cookie属性
|
||||
gyingURL, _ := url.Parse("https://www.gying.net")
|
||||
var httpCookies []*http.Cookie
|
||||
|
||||
|
||||
for name, value := range cookies {
|
||||
cookie := &http.Cookie{
|
||||
Name: name,
|
||||
Value: value,
|
||||
Name: name,
|
||||
Value: value,
|
||||
// 不设置Domain和Path,让cookiejar根据URL自动推导
|
||||
// cookiejar.SetCookies会根据提供的URL自动设置正确的Domain和Path
|
||||
}
|
||||
httpCookies = append(httpCookies, cookie)
|
||||
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 准备恢复Cookie: %s=%s\n",
|
||||
fmt.Printf("[Gying] 准备恢复Cookie: %s=%s\n",
|
||||
cookie.Name, cookie.Value[:min(10, len(cookie.Value))])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
client.Jar.SetCookies(gyingURL, httpCookies)
|
||||
|
||||
|
||||
// 验证cookies是否被正确设置
|
||||
if DebugLog {
|
||||
storedCookies := client.Jar.Cookies(gyingURL)
|
||||
fmt.Printf("[Gying] ✅ 成功恢复 %d 个cookie到scraper的cookiejar\n", len(cookies))
|
||||
fmt.Printf("[Gying] 验证: cookiejar中现有 %d 个cookie\n", len(storedCookies))
|
||||
|
||||
// 详细打印每个cookie以便调试
|
||||
|
||||
// 详细打印每个cookie以便调试
|
||||
for i, c := range storedCookies {
|
||||
fmt.Printf("[Gying] 设置后Cookie[%d]: %s=%s (Domain:%s, Path:%s)\n",
|
||||
fmt.Printf("[Gying] 设置后Cookie[%d]: %s=%s (Domain:%s, Path:%s)\n",
|
||||
i, c.Name, c.Value[:min(10, len(c.Value))], c.Domain, c.Path)
|
||||
}
|
||||
}
|
||||
@@ -1087,7 +1092,7 @@ func (p *GyingPlugin) createScraperWithCookies(cookieStr string) (*cloudscraper.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return scraper, nil
|
||||
}
|
||||
|
||||
@@ -1095,7 +1100,7 @@ func (p *GyingPlugin) createScraperWithCookies(cookieStr string) (*cloudscraper.
|
||||
func parseCookieString(cookieStr string) map[string]string {
|
||||
cookies := make(map[string]string)
|
||||
parts := strings.Split(cookieStr, ";")
|
||||
|
||||
|
||||
for _, part := range parts {
|
||||
part = strings.TrimSpace(part)
|
||||
if idx := strings.Index(part, "="); idx > 0 {
|
||||
@@ -1104,18 +1109,18 @@ func parseCookieString(cookieStr string) map[string]string {
|
||||
cookies[name] = value
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return cookies
|
||||
}
|
||||
|
||||
// ============ 登录逻辑 ============
|
||||
|
||||
// doLogin 执行登录,返回scraper实例和cookie字符串
|
||||
//
|
||||
//
|
||||
// 登录流程(3步):
|
||||
// 1. GET登录页 (https://www.gying.net/user/login/) → 获取PHPSESSID
|
||||
// 2. POST登录 (https://www.gying.net/user/login) → 获取BT_auth、BT_cookietime等认证cookies
|
||||
// 3. GET详情页 (https://www.gying.net/mv/wkMn) → 触发防爬cookies (vrg_sc、vrg_go等)
|
||||
// 1. GET登录页 (https://www.gying.net/user/login/) → 获取PHPSESSID
|
||||
// 2. POST登录 (https://www.gying.net/user/login) → 获取BT_auth、BT_cookietime等认证cookies
|
||||
// 3. GET详情页 (https://www.gying.net/mv/wkMn) → 触发防爬cookies (vrg_sc、vrg_go等)
|
||||
//
|
||||
// 返回: (*cloudscraper.Scraper, cookie字符串, error)
|
||||
func (p *GyingPlugin) doLogin(username, password string) (*cloudscraper.Scraper, string, error) {
|
||||
@@ -1126,7 +1131,14 @@ func (p *GyingPlugin) doLogin(username, password string) (*cloudscraper.Scraper,
|
||||
}
|
||||
|
||||
// 创建cloudscraper实例(每个用户独立的实例)
|
||||
scraper, err := cloudscraper.New()
|
||||
// 关键配置:禁用403自动刷新,防止cookie被清空
|
||||
scraper, err := cloudscraper.New(
|
||||
cloudscraper.WithSessionConfig(
|
||||
false, // refreshOn403 = false,禁用403时自动刷新(重要!)
|
||||
365*24*time.Hour, // interval = 1年,基本不刷新
|
||||
0, // maxRetries = 0
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 创建cloudscraper失败: %v\n", err)
|
||||
@@ -1135,12 +1147,12 @@ func (p *GyingPlugin) doLogin(username, password string) (*cloudscraper.Scraper,
|
||||
}
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] cloudscraper创建成功\n")
|
||||
fmt.Printf("[Gying] cloudscraper创建成功(已禁用403自动刷新)\n")
|
||||
}
|
||||
|
||||
// 创建cookieMap用于收集所有cookies
|
||||
cookieMap := make(map[string]string)
|
||||
|
||||
|
||||
// ========== 步骤1: GET登录页 (获取初始PHPSESSID) ==========
|
||||
loginPageURL := "https://www.gying.net/user/login/"
|
||||
if DebugLog {
|
||||
@@ -1160,7 +1172,7 @@ func (p *GyingPlugin) doLogin(username, password string) (*cloudscraper.Scraper,
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 登录页面状态码: %d\n", getResp.StatusCode)
|
||||
}
|
||||
|
||||
|
||||
// 从登录页响应中收集cookies
|
||||
for _, setCookie := range getResp.Header["Set-Cookie"] {
|
||||
parts := strings.Split(setCookie, ";")
|
||||
@@ -1205,7 +1217,7 @@ func (p *GyingPlugin) doLogin(username, password string) (*cloudscraper.Scraper,
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 响应状态码: %d\n", resp.StatusCode)
|
||||
}
|
||||
|
||||
|
||||
// 从POST登录响应中收集cookies
|
||||
for _, setCookie := range resp.Header["Set-Cookie"] {
|
||||
parts := strings.Split(setCookie, ";")
|
||||
@@ -1255,7 +1267,7 @@ func (p *GyingPlugin) doLogin(username, password string) (*cloudscraper.Scraper,
|
||||
// 检查登录结果(兼容多种类型:int、float64、json.Number、string)
|
||||
var codeValue int
|
||||
codeInterface := loginResp["code"]
|
||||
|
||||
|
||||
switch v := codeInterface.(type) {
|
||||
case int:
|
||||
codeValue = v
|
||||
@@ -1291,16 +1303,16 @@ func (p *GyingPlugin) doLogin(username, password string) (*cloudscraper.Scraper,
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 步骤3: GET详情页收集完整Cookie\n")
|
||||
}
|
||||
|
||||
|
||||
detailResp, err := scraper.Get("https://www.gying.net/mv/wkMn")
|
||||
if err == nil {
|
||||
defer detailResp.Body.Close()
|
||||
ioutil.ReadAll(detailResp.Body)
|
||||
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 详情页状态码: %d\n", detailResp.StatusCode)
|
||||
}
|
||||
|
||||
|
||||
// 从详情页响应中收集cookies
|
||||
for _, setCookie := range detailResp.Header["Set-Cookie"] {
|
||||
parts := strings.Split(setCookie, ";")
|
||||
@@ -1321,14 +1333,14 @@ func (p *GyingPlugin) doLogin(username, password string) (*cloudscraper.Scraper,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 构建cookie字符串
|
||||
var cookieParts []string
|
||||
for name, value := range cookieMap {
|
||||
cookieParts = append(cookieParts, fmt.Sprintf("%s=%s", name, value))
|
||||
}
|
||||
cookieStr := strings.Join(cookieParts, "; ")
|
||||
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] ✅ 登录成功!提取到 %d 个Cookie\n", len(cookieMap))
|
||||
fmt.Printf("[Gying] Cookie字符串长度: %d\n", len(cookieStr))
|
||||
@@ -1354,6 +1366,54 @@ func min(a, b int) int {
|
||||
return b
|
||||
}
|
||||
|
||||
// ============ 重新登录逻辑 ============
|
||||
|
||||
// reloginUser 重新登录指定用户
|
||||
func (p *GyingPlugin) reloginUser(user *User) error {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 🔄 开始重新登录用户: %s\n", user.UsernameMasked)
|
||||
}
|
||||
|
||||
// 解密密码
|
||||
password, err := p.decryptPassword(user.EncryptedPassword)
|
||||
if err != nil {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] ❌ 解密密码失败: %v\n", err)
|
||||
}
|
||||
return fmt.Errorf("解密密码失败: %w", err)
|
||||
}
|
||||
|
||||
// 执行登录
|
||||
scraper, cookie, err := p.doLogin(user.Username, password)
|
||||
if err != nil {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] ❌ 重新登录失败: %v\n", err)
|
||||
}
|
||||
return fmt.Errorf("重新登录失败: %w", err)
|
||||
}
|
||||
|
||||
// 更新scraper实例
|
||||
p.scrapers.Store(user.Hash, scraper)
|
||||
|
||||
// 更新用户信息
|
||||
user.Cookie = cookie
|
||||
user.LoginAt = time.Now()
|
||||
user.ExpireAt = time.Now().AddDate(0, 4, 0)
|
||||
user.Status = "active"
|
||||
|
||||
if err := p.saveUser(user); err != nil {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] ⚠️ 保存用户失败: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] ✅ 用户 %s 重新登录成功\n", user.UsernameMasked)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ============ 搜索逻辑 ============
|
||||
|
||||
// executeSearchTasks 并发执行搜索任务
|
||||
@@ -1370,27 +1430,33 @@ func (p *GyingPlugin) executeSearchTasks(users []*User, keyword string) []model.
|
||||
// 获取用户的scraper实例
|
||||
scraperVal, exists := p.scrapers.Load(u.Hash)
|
||||
var scraper *cloudscraper.Scraper
|
||||
|
||||
|
||||
if !exists {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 用户 %s 没有scraper实例,尝试使用已保存的cookie创建\n", u.UsernameMasked)
|
||||
}
|
||||
|
||||
// 为用户创建新的cloudscraper实例
|
||||
newScraper, err := cloudscraper.New()
|
||||
|
||||
// 为用户创建新的cloudscraper实例(禁用403自动刷新)
|
||||
newScraper, err := cloudscraper.New(
|
||||
cloudscraper.WithSessionConfig(
|
||||
false, // refreshOn403 = false
|
||||
365*24*time.Hour, // interval = 1年
|
||||
0, // maxRetries = 0
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 为用户 %s 创建scraper失败: %v\n", u.UsernameMasked, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// 存储新创建的scraper实例
|
||||
p.scrapers.Store(u.Hash, newScraper)
|
||||
scraper = newScraper
|
||||
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 已为用户 %s 创建新的scraper实例\n", u.UsernameMasked)
|
||||
fmt.Printf("[Gying] 已为用户 %s 创建新的scraper实例(已禁用403刷新)\n", u.UsernameMasked)
|
||||
}
|
||||
} else {
|
||||
var ok bool
|
||||
@@ -1403,10 +1469,10 @@ func (p *GyingPlugin) executeSearchTasks(users []*User, keyword string) []model.
|
||||
}
|
||||
}
|
||||
|
||||
results, err := p.searchWithScraper(keyword, scraper)
|
||||
results, err := p.searchWithScraperWithRetry(keyword, scraper, u)
|
||||
if err != nil {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 用户 %s 搜索失败: %v\n", u.UsernameMasked, err)
|
||||
fmt.Printf("[Gying] 用户 %s 搜索失败(已重试): %v\n", u.UsernameMasked, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1423,7 +1489,49 @@ func (p *GyingPlugin) executeSearchTasks(users []*User, keyword string) []model.
|
||||
return p.deduplicateResults(allResults)
|
||||
}
|
||||
|
||||
// searchWithCookie 使用scraper搜索
|
||||
// searchWithScraperWithRetry 使用scraper搜索(带403自动重新登录重试)
|
||||
func (p *GyingPlugin) searchWithScraperWithRetry(keyword string, scraper *cloudscraper.Scraper, user *User) ([]model.SearchResult, error) {
|
||||
results, err := p.searchWithScraper(keyword, scraper)
|
||||
|
||||
// 检测是否为403错误
|
||||
if err != nil && strings.Contains(err.Error(), "403") {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] ⚠️ 检测到403错误,尝试重新登录用户 %s\n", user.UsernameMasked)
|
||||
}
|
||||
|
||||
// 尝试重新登录
|
||||
if reloginErr := p.reloginUser(user); reloginErr != nil {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] ❌ 重新登录失败: %v\n", reloginErr)
|
||||
}
|
||||
return nil, fmt.Errorf("403错误且重新登录失败: %w", reloginErr)
|
||||
}
|
||||
|
||||
// 获取新的scraper实例
|
||||
scraperVal, exists := p.scrapers.Load(user.Hash)
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("重新登录后未找到scraper实例")
|
||||
}
|
||||
|
||||
newScraper, ok := scraperVal.(*cloudscraper.Scraper)
|
||||
if !ok || newScraper == nil {
|
||||
return nil, fmt.Errorf("重新登录后scraper实例无效")
|
||||
}
|
||||
|
||||
// 使用新scraper重试搜索
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 🔄 使用新登录状态重试搜索\n")
|
||||
}
|
||||
results, err = p.searchWithScraper(keyword, newScraper)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("重新登录后搜索仍然失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return results, err
|
||||
}
|
||||
|
||||
// searchWithScraper 使用scraper搜索
|
||||
func (p *GyingPlugin) searchWithScraper(keyword string, scraper *cloudscraper.Scraper) ([]model.SearchResult, error) {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] ---------- searchWithScraper 开始 ----------\n")
|
||||
@@ -1432,7 +1540,7 @@ func (p *GyingPlugin) searchWithScraper(keyword string, scraper *cloudscraper.Sc
|
||||
|
||||
// 1. 使用cloudscraper请求搜索页面
|
||||
searchURL := fmt.Sprintf("https://www.gying.net/s/1---1/%s", url.QueryEscape(keyword))
|
||||
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 搜索URL: %s\n", searchURL)
|
||||
fmt.Printf("[Gying] 使用cloudscraper发送请求\n")
|
||||
@@ -1451,6 +1559,7 @@ func (p *GyingPlugin) searchWithScraper(keyword string, scraper *cloudscraper.Sc
|
||||
fmt.Printf("[Gying] 搜索响应状态码: %d\n", resp.StatusCode)
|
||||
}
|
||||
|
||||
// 读取响应body
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
if DebugLog {
|
||||
@@ -1471,10 +1580,25 @@ func (p *GyingPlugin) searchWithScraper(keyword string, scraper *cloudscraper.Sc
|
||||
}
|
||||
}
|
||||
|
||||
// 检查403错误
|
||||
if resp.StatusCode == 403 {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] ❌ 收到403 Forbidden - Cookie可能已过期或被网站拒绝\n")
|
||||
if len(body) > 0 {
|
||||
preview := string(body)
|
||||
if len(preview) > 300 {
|
||||
preview = preview[:300] + "..."
|
||||
}
|
||||
fmt.Printf("[Gying] 403响应内容: %s\n", preview)
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("HTTP 403 Forbidden - 可能需要重新登录")
|
||||
}
|
||||
|
||||
// 2. 提取 _obj.search JSON
|
||||
re := regexp.MustCompile(`_obj\.search=(\{.*?\});`)
|
||||
matches := re.FindSubmatch(body)
|
||||
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] 正则匹配结果: 找到 %d 个匹配\n", len(matches))
|
||||
}
|
||||
@@ -1523,7 +1647,7 @@ func (p *GyingPlugin) searchWithScraper(keyword string, scraper *cloudscraper.Sc
|
||||
|
||||
// 3. 并发请求详情接口
|
||||
results := p.fetchAllDetails(&searchData, scraper)
|
||||
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] fetchAllDetails 返回 %d 条结果\n", len(results))
|
||||
fmt.Printf("[Gying] ---------- searchWithCookie 结束 ----------\n")
|
||||
@@ -1557,7 +1681,7 @@ func (p *GyingPlugin) fetchAllDetails(searchData *SearchData, scraper *cloudscra
|
||||
defer func() { <-semaphore }()
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] [%d/%d] 获取详情: ID=%s, Type=%s\n",
|
||||
fmt.Printf("[Gying] [%d/%d] 获取详情: ID=%s, Type=%s\n",
|
||||
index+1, len(searchData.L.I), searchData.L.I[index], searchData.L.D[index])
|
||||
}
|
||||
|
||||
@@ -1575,7 +1699,7 @@ func (p *GyingPlugin) fetchAllDetails(searchData *SearchData, scraper *cloudscra
|
||||
result := p.buildResult(detail, searchData, index)
|
||||
if result.Title != "" && len(result.Links) > 0 {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] [%d/%d] ✅ 成功: %s (%d个链接)\n",
|
||||
fmt.Printf("[Gying] [%d/%d] ✅ 成功: %s (%d个链接)\n",
|
||||
index+1, len(searchData.L.I), result.Title, len(result.Links))
|
||||
}
|
||||
mu.Lock()
|
||||
@@ -1584,7 +1708,7 @@ func (p *GyingPlugin) fetchAllDetails(searchData *SearchData, scraper *cloudscra
|
||||
mu.Unlock()
|
||||
} else {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] [%d/%d] ⚠️ 跳过: 标题或链接为空 (标题:%s, 链接数:%d)\n",
|
||||
fmt.Printf("[Gying] [%d/%d] ⚠️ 跳过: 标题或链接为空 (标题:%s, 链接数:%d)\n",
|
||||
index+1, len(searchData.L.I), result.Title, len(result.Links))
|
||||
}
|
||||
}
|
||||
@@ -1594,7 +1718,7 @@ func (p *GyingPlugin) fetchAllDetails(searchData *SearchData, scraper *cloudscra
|
||||
wg.Wait()
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] <<< fetchAllDetails 完成: 成功=%d, 失败=%d, 总计=%d\n",
|
||||
fmt.Printf("[Gying] <<< fetchAllDetails 完成: 成功=%d, 失败=%d, 总计=%d\n",
|
||||
successCount, failCount, len(searchData.L.I))
|
||||
}
|
||||
|
||||
@@ -1604,7 +1728,7 @@ func (p *GyingPlugin) fetchAllDetails(searchData *SearchData, scraper *cloudscra
|
||||
// fetchDetail 获取详情
|
||||
func (p *GyingPlugin) fetchDetail(resourceID, resourceType string, scraper *cloudscraper.Scraper) (*DetailData, error) {
|
||||
detailURL := fmt.Sprintf("https://www.gying.net/res/downurl/%s/%s", resourceType, resourceID)
|
||||
|
||||
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] fetchDetail: %s\n", detailURL)
|
||||
}
|
||||
@@ -1624,7 +1748,18 @@ func (p *GyingPlugin) fetchDetail(resourceID, resourceType string, scraper *clou
|
||||
fmt.Printf("[Gying] 响应状态码: %d\n", resp.StatusCode)
|
||||
}
|
||||
|
||||
// 检查403错误
|
||||
if resp.StatusCode == 403 {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] ❌ 详情接口返回403 - Cookie可能已过期\n")
|
||||
}
|
||||
return nil, fmt.Errorf("HTTP 403 Forbidden")
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
if DebugLog {
|
||||
fmt.Printf("[Gying] ❌ HTTP错误: %d\n", resp.StatusCode)
|
||||
}
|
||||
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
@@ -1710,7 +1845,7 @@ func (p *GyingPlugin) extractPanLinks(detail *DetailData) []model.Link {
|
||||
|
||||
for i := 0; i < len(detail.Panlist.URL); i++ {
|
||||
linkURL := strings.TrimSpace(detail.Panlist.URL[i])
|
||||
|
||||
|
||||
// 去除URL中的访问码标记
|
||||
linkURL = regexp.MustCompile(`(访问码:.*?)`).ReplaceAllString(linkURL, "")
|
||||
linkURL = regexp.MustCompile(`\(访问码:.*?\)`).ReplaceAllString(linkURL, "")
|
||||
@@ -1732,7 +1867,7 @@ func (p *GyingPlugin) extractPanLinks(detail *DetailData) []model.Link {
|
||||
if i < len(detail.Panlist.P) && detail.Panlist.P[i] != "" {
|
||||
password = detail.Panlist.P[i]
|
||||
}
|
||||
|
||||
|
||||
// 从URL提取提取码(优先)
|
||||
if urlPwd := p.extractPasswordFromURL(linkURL); urlPwd != "" {
|
||||
password = urlPwd
|
||||
@@ -1781,7 +1916,7 @@ func (p *GyingPlugin) extractPasswordFromURL(linkURL string) string {
|
||||
return matches[1]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 115网盘: ?password=xxxx
|
||||
if strings.Contains(linkURL, "?password=") {
|
||||
re := regexp.MustCompile(`\?password=([a-zA-Z0-9]+)`)
|
||||
@@ -1974,4 +2109,3 @@ func (p *GyingPlugin) markInactiveUsers() int {
|
||||
|
||||
return markedCount
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user