增加伪装请求,企图降低封号几率

This commit is contained in:
Vinlic 2024-03-19 15:56:27 +08:00
parent eef674eac8
commit c9b3574b0b

View File

@ -24,7 +24,7 @@ const FAKE_HEADERS = {
'Accept-Encoding': 'gzip, deflate, br, zstd', 'Accept-Encoding': 'gzip, deflate, br, zstd',
'Accept-Language': 'zh-CN,zh;q=0.9', 'Accept-Language': 'zh-CN,zh;q=0.9',
'Origin': 'https://kimi.moonshot.cn', 'Origin': 'https://kimi.moonshot.cn',
'Cookie': util.generateCookie(), // 'Cookie': util.generateCookie(),
'R-Timezone': 'Asia/Shanghai', 'R-Timezone': 'Asia/Shanghai',
'Sec-Ch-Ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"', 'Sec-Ch-Ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Mobile': '?0',
@ -57,7 +57,7 @@ async function requestToken(refreshToken: string) {
const result = await axios.get('https://kimi.moonshot.cn/api/auth/token/refresh', { const result = await axios.get('https://kimi.moonshot.cn/api/auth/token/refresh', {
headers: { headers: {
Authorization: `Bearer ${refreshToken}`, Authorization: `Bearer ${refreshToken}`,
Referer: 'https://kimi.moonshot.cn', Referer: 'https://kimi.moonshot.cn/',
...FAKE_HEADERS ...FAKE_HEADERS
}, },
timeout: 15000, timeout: 15000,
@ -74,7 +74,7 @@ async function requestToken(refreshToken: string) {
} }
})() })()
.then(result => { .then(result => {
if(accessTokenRequestQueueMap[refreshToken]) { if (accessTokenRequestQueueMap[refreshToken]) {
accessTokenRequestQueueMap[refreshToken].forEach(resolve => resolve(result)); accessTokenRequestQueueMap[refreshToken].forEach(resolve => resolve(result));
delete accessTokenRequestQueueMap[refreshToken]; delete accessTokenRequestQueueMap[refreshToken];
} }
@ -82,13 +82,13 @@ async function requestToken(refreshToken: string) {
return result; return result;
}) })
.catch(err => { .catch(err => {
if(accessTokenRequestQueueMap[refreshToken]) { if (accessTokenRequestQueueMap[refreshToken]) {
accessTokenRequestQueueMap[refreshToken].forEach(resolve => resolve(err)); accessTokenRequestQueueMap[refreshToken].forEach(resolve => resolve(err));
delete accessTokenRequestQueueMap[refreshToken]; delete accessTokenRequestQueueMap[refreshToken];
} }
return err; return err;
}); });
if(_.isError(result)) if (_.isError(result))
throw result; throw result;
return result; return result;
} }
@ -128,7 +128,7 @@ async function createConversation(name: string, refreshToken: string) {
}, { }, {
headers: { headers: {
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
Referer: 'https://kimi.moonshot.cn', Referer: 'https://kimi.moonshot.cn/',
...FAKE_HEADERS ...FAKE_HEADERS
}, },
timeout: 15000, timeout: 15000,
@ -177,6 +177,10 @@ async function createCompletion(messages: any[], refreshToken: string, useSearch
const refFileUrls = extractRefFileUrls(messages); const refFileUrls = extractRefFileUrls(messages);
const refs = refFileUrls.length ? await Promise.all(refFileUrls.map(fileUrl => uploadFile(fileUrl, refreshToken))) : []; const refs = refFileUrls.length ? await Promise.all(refFileUrls.map(fileUrl => uploadFile(fileUrl, refreshToken))) : [];
// 伪装调用获取用户信息
fakeRequest(refreshToken)
.catch(err => logger.error(err));
// 创建会话 // 创建会话
const convId = await createConversation(`cmpl-${util.uuid(false)}`, refreshToken); const convId = await createConversation(`cmpl-${util.uuid(false)}`, refreshToken);
@ -210,7 +214,7 @@ async function createCompletion(messages: any[], refreshToken: string, useSearch
return answer; return answer;
})() })()
.catch(err => { .catch(err => {
if(retryCount < MAX_RETRY_COUNT) { if (retryCount < MAX_RETRY_COUNT) {
logger.error(`Stream response error: ${err.message}`); logger.error(`Stream response error: ${err.message}`);
logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`); logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
return (async () => { return (async () => {
@ -238,6 +242,10 @@ async function createCompletionStream(messages: any[], refreshToken: string, use
const refFileUrls = extractRefFileUrls(messages); const refFileUrls = extractRefFileUrls(messages);
const refs = refFileUrls.length ? await Promise.all(refFileUrls.map(fileUrl => uploadFile(fileUrl, refreshToken))) : []; const refs = refFileUrls.length ? await Promise.all(refFileUrls.map(fileUrl => uploadFile(fileUrl, refreshToken))) : [];
// 伪装调用获取用户信息
fakeRequest(refreshToken)
.catch(err => logger.error(err));
// 创建会话 // 创建会话
const convId = await createConversation(`cmpl-${util.uuid(false)}`, refreshToken); const convId = await createConversation(`cmpl-${util.uuid(false)}`, refreshToken);
@ -268,7 +276,7 @@ async function createCompletionStream(messages: any[], refreshToken: string, use
}); });
})() })()
.catch(err => { .catch(err => {
if(retryCount < MAX_RETRY_COUNT) { if (retryCount < MAX_RETRY_COUNT) {
logger.error(`Stream response error: ${err.message}`); logger.error(`Stream response error: ${err.message}`);
logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`); logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
return (async () => { return (async () => {
@ -280,6 +288,38 @@ async function createCompletionStream(messages: any[], refreshToken: string, use
}); });
} }
/**
* 访
*
*
*
* @param refreshToken access_token的refresh_token
*/
async function fakeRequest(refreshToken: string) {
const token = await acquireToken(refreshToken);
const options = {
headers: {
Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn/`,
...FAKE_HEADERS
}
};
await [
() => axios.get('https://kimi.moonshot.cn/api/user', options),
() => axios.get('https://kimi.moonshot.cn/api/chat_1m/user/status', options),
() => axios.post('https://kimi.moonshot.cn/api/chat/list', {
offset: 0,
size: 50
}, options),
() => axios.post('https://kimi.moonshot.cn/api/show_case/list', {
offset: 0,
size: 4,
enable_cache: true,
order: "asc"
}, options)
][Math.floor(Math.random() * 4)]();
}
/** /**
* URL * URL
* *
@ -356,7 +396,7 @@ async function preSignUrl(filename: string, refreshToken: string) {
timeout: 15000, timeout: 15000,
headers: { headers: {
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn`, Referer: `https://kimi.moonshot.cn/`,
...FAKE_HEADERS ...FAKE_HEADERS
}, },
validateStatus: () => true validateStatus: () => true
@ -437,7 +477,7 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
headers: { headers: {
'Content-Type': mimeType, 'Content-Type': mimeType,
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn`, Referer: `https://kimi.moonshot.cn/`,
...FAKE_HEADERS ...FAKE_HEADERS
}, },
validateStatus: () => true validateStatus: () => true
@ -453,7 +493,7 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
}, { }, {
headers: { headers: {
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn`, Referer: `https://kimi.moonshot.cn/`,
...FAKE_HEADERS ...FAKE_HEADERS
} }
}); });
@ -466,7 +506,7 @@ async function uploadFile(fileUrl: string, refreshToken: string) {
}, { }, {
headers: { headers: {
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn`, Referer: `https://kimi.moonshot.cn/`,
...FAKE_HEADERS ...FAKE_HEADERS
} }
}); });