adapt to the new version of cookie

This commit is contained in:
被遗忘的记忆
2024-08-24 20:10:26 +08:00
committed by GitHub
parent 02011e9954
commit be7d39c358
+59 -7
View File
@@ -18,14 +18,14 @@ class YouProvider {
this.config = config; this.config = config;
this.sessions = {}; this.sessions = {};
// 可以是 'chrome', 'edge', 或 'auto' // 可以是 'chrome', 'edge', 或 'auto'
this.preferredBrowser = 'auto'; this.preferredBrowser = 'edge';
this.isCustomModeEnabled = process.env.USE_CUSTOM_MODE === "true"; this.isCustomModeEnabled = process.env.USE_CUSTOM_MODE === "true";
this.isRotationEnabled = process.env.ENABLE_MODE_ROTATION === "true"; this.isRotationEnabled = process.env.ENABLE_MODE_ROTATION === "true";
this.currentMode = "default"; this.currentMode = "default";
this.switchCounter = 0; this.switchCounter = 0;
this.requestsInCurrentMode = 0; this.requestsInCurrentMode = 0;
this.switchThreshold = this.getRandomSwitchThreshold(); this.switchThreshold = this.getRandomSwitchThreshold();
this.lastDefaultThreshold = 0; // 记录上一次default模式的阈值 this.lastDefaultThreshold = 0; // 记录上一次default模式的阈值
} }
getRandomSwitchThreshold() { getRandomSwitchThreshold() {
@@ -57,26 +57,68 @@ class YouProvider {
// extract essential jwt session and token from cookie // extract essential jwt session and token from cookie
for (let index = 0; index < config.sessions.length; index++) { for (let index = 0; index < config.sessions.length; index++) {
let session = config.sessions[index]; let session = config.sessions[index];
var { jwtSession, jwtToken } = extractCookie(session.cookie); var { jwtSession, jwtToken, ds, dsr, youproSubscription, youSubscription, aiModel } = extractCookie(session.cookie);
if (jwtSession && jwtToken) { if (jwtSession && jwtToken) {
// 旧版cookie处理
try { try {
let jwt = JSON.parse(Buffer.from(jwtToken.split(".")[1], "base64").toString()); let jwt = JSON.parse(Buffer.from(jwtToken.split(".")[1], "base64").toString());
this.sessions[jwt.user.name] = { this.sessions[jwt.user.name] = {
configIndex: index, configIndex: index,
jwtSession, jwtSession,
jwtToken, jwtToken,
youproSubscription,
youSubscription,
aiModel,
valid: false, valid: false,
}; };
console.log(`已添加 #${index} ${jwt.user.name}`); console.log(`已添加 #${index} ${jwt.user.name} (旧版cookie)`);
} catch (e) { } catch (e) {
console.error(`解析第${index}个cookie失败`); console.error(`解析第${index}旧版cookie失败: ${e.message}`);
}
} else if (ds) {
// 新版cookie处理,只要有DS就处理
try {
let jwt = JSON.parse(Buffer.from(ds.split(".")[1], "base64").toString());
this.sessions[jwt.email] = {
configIndex: index,
ds,
dsr, // 即使dsr为null也会被添加
youproSubscription,
youSubscription,
aiModel,
valid: false,
};
console.log(`已添加 #${index} ${jwt.email} (新版cookie)`);
if (!dsr) {
console.warn(`警告: 第${index}个cookie缺少DSR字段。程序继续执行。`);
}
} catch (e) {
console.error(`解析第${index}个新版cookie失败: ${e.message}`);
} }
} else { } else {
console.error(`${index}个cookie中缺少jwtSession或jwtToken,请重新获取`); // 无效cookie处理
console.error(`${index}个cookie无效,请重新获取。`);
if (jwtSession || jwtToken) {
console.error(`检测到部分旧版cookie字段,但不完整。`);
if (!jwtSession) console.error(`缺少: stytch_session`);
if (!jwtToken) console.error(`缺少: stytch_session_jwt`);
} else if (ds) {
console.error(`检测到新版cookie的DS字段,但缺少其他必要字段。`);
} else {
console.error(`未检测到任何有效的cookie字段。`);
}
console.error(`提示: 请确保登录You.com并成功获取完整的cookie。`);
continue; // 跳过无效的cookie,继续下一个
}
if (youproSubscription === "false" || youSubscription === "freemium") {
console.warn(`警告: 第${index}个cookie可能没有有效的订阅。请检查You是否有有效的Pro订阅。`);
} }
} }
console.log(`已添加 ${Object.keys(this.sessions).length} 个 cookie,开始验证有效性(是否有订阅)`); console.log(`已添加 ${Object.keys(this.sessions).length} 个 cookie,开始验证有效性(是否有订阅)`);
for (var username of Object.keys(this.sessions)) { for (var username of Object.keys(this.sessions)) {
var session = this.sessions[username]; var session = this.sessions[username];
createDirectoryIfNotExists(path.join(__dirname, "browser_profiles", username)); createDirectoryIfNotExists(path.join(__dirname, "browser_profiles", username));
@@ -90,7 +132,15 @@ class YouProvider {
}) })
.then(async (response) => { .then(async (response) => {
const { page, browser, setTarget } = response; const { page, browser, setTarget } = response;
await page.setCookie(...getSessionCookie(session.jwtSession, session.jwtToken)); await page.setCookie(...getSessionCookie(
session.jwtSession,
session.jwtToken,
session.ds,
session.dsr,
session.youproSubscription,
session.youSubscription,
session.aiModel
));
page.goto("https://you.com", { timeout: 60000 }); page.goto("https://you.com", { timeout: 60000 });
await sleep(5000); // 等待加载完毕 await sleep(5000); // 等待加载完毕
@@ -268,6 +318,8 @@ class YouProvider {
}); });
} }
await page.goto("https://you.com/?chatMode=default", { waitUntil: "domcontentloaded" });
// 计算用户消息长度 // 计算用户消息长度
let userMessage = [{ question: "", answer: "" }]; let userMessage = [{ question: "", answer: "" }];
let userQuery = ""; let userQuery = "";