From b74feaefaade568ab8eb467f2a351c6494d4e4ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=AB=E9=81=97=E5=BF=98=E7=9A=84=E8=AE=B0=E5=BF=86?= <32097237+YIWANG-sketch@users.noreply.github.com> Date: Tue, 27 Aug 2024 19:22:08 +0800 Subject: [PATCH] adjust old cookie acquisition --- provider.mjs | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/provider.mjs b/provider.mjs index 15c3c5f..e73f2a8 100644 --- a/provider.mjs +++ b/provider.mjs @@ -18,7 +18,7 @@ class YouProvider { this.config = config; this.sessions = {}; // 可以是 'chrome', 'edge', 或 'auto' - this.preferredBrowser = 'auto'; + this.preferredBrowser = 'edge'; this.isCustomModeEnabled = process.env.USE_CUSTOM_MODE === "true"; this.isRotationEnabled = process.env.ENABLE_MODE_ROTATION === "true"; this.currentMode = "default"; @@ -252,8 +252,18 @@ class YouProvider { return null; } - // 如果计算出的结束日期已经过去,继续加月/年直到未来日期 - while (expirationDate <= today) { + // 计算从开始日期到今天间隔数 + const intervalsPassed = Math.floor((today - startDate) / (subscription.interval === 'month' ? 30 : 365) / (24 * 60 * 60 * 1000)); + + // 计算到期日期 + if (subscription.interval === 'month') { + expirationDate.setMonth(expirationDate.getMonth() + intervalsPassed); + } else { + expirationDate.setFullYear(expirationDate.getFullYear() + intervalsPassed); + } + + // 如果计算出的日期仍在过去,再加一个间隔 + if (expirationDate <= today) { if (subscription.interval === 'month') { expirationDate.setMonth(expirationDate.getMonth() + 1); } else { @@ -356,16 +366,31 @@ class YouProvider { sessionCookie = getSessionCookie(jwtSession, jwtToken, ds, dsr); if (ds) { - const jwt = JSON.parse(Buffer.from(ds.split(".")[1], "base64").toString()); - sessionCookie.email = jwt.email; - sessionCookie.isNewVersion = true; - } else { - const jwt = JSON.parse(Buffer.from(jwtToken.split(".")[1], "base64").toString()); - sessionCookie.email = jwt.user.name; - sessionCookie.isNewVersion = false; + try { + const jwt = JSON.parse(Buffer.from(ds.split(".")[1], "base64").toString()); + sessionCookie.email = jwt.email; + sessionCookie.isNewVersion = true; + } catch (error) { + console.error('解析DS令牌时出错:', error); + return null; + } + } else if (jwtToken) { + try { + const jwt = JSON.parse(Buffer.from(jwtToken.split(".")[1], "base64").toString()); + sessionCookie.email = jwt.user?.email || jwt.email || jwt.user?.name; + sessionCookie.isNewVersion = false; + } catch (error) { + console.error('JWT令牌解析错误:', error); + return null; + } } } + if (!sessionCookie || !sessionCookie.some(c => c.name === 'stytch_session' || c.name === 'DS')) { + console.error('无法提取有效的会话 cookie'); + return null; + } + return sessionCookie; }