From def3601db4078196d1cd2140135c88623e4bf0fc Mon Sep 17 00:00:00 2001 From: weaigc <879821485@qq.com> Date: Sat, 21 Oct 2023 23:55:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=BC=E5=AE=B9v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/utils.ts | 5 +++-- src/pages/api/create.ts | 25 +++++++++++++++---------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 96470b74..47b98f09 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -170,7 +170,6 @@ export function cookie2Headers(cookies: Partial<{ [key: string]: string }>) { let { BING_HEADER, BING_HEADER0 = process.env.BING_HEADER, - BING_IP, } = cookies || {} const headers = extraHeadersFromCookie({ BING_HEADER, @@ -184,7 +183,7 @@ export function cookie2Headers(cookies: Partial<{ [key: string]: string }>) { return headers } -export function createHeaders(cookies: Partial<{ [key: string]: string }>, useMock?: boolean) { +export function createHeaders(cookies: Partial<{ [key: string]: string }> = {}, useMock?: boolean) { let { BING_HEADER, BING_HEADER0 = process.env.BING_HEADER, @@ -193,6 +192,8 @@ export function createHeaders(cookies: Partial<{ [key: string]: string }>, useMo } = cookies || {} if (useMock == null) { useMock = BING_HEADER ? false : (/^(1|true|yes)$/i.test(String(IMAGE_ONLY)) ? true : !BING_HEADER0) + } else if (useMock === false) { + cookies.BING_HEADER = '' } const headers = useMock ? mockUser(cookies) : cookie2Headers(cookies) if (BING_IP) { diff --git a/src/pages/api/create.ts b/src/pages/api/create.ts index a5a4c0c4..805c9e5b 100644 --- a/src/pages/api/create.ts +++ b/src/pages/api/create.ts @@ -33,20 +33,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) }) debug('status', response.status, response.url, headers) if (response.status === 200) { - const json = await response.json().catch((e: any) => {}) - console.log('json', json) - if (!json?.clientId) { - continue - } - json.encryptedconversationsignature = response.headers.get('X-Sydney-encryptedconversationsignature') || undefined + const json = await response.json().catch((e: any) => { + console.log('fetch error', e) + }) || {} + debug('json', json) + json.encryptedconversationsignature = json.encryptedconversationsignature || response.headers.get('X-Sydney-encryptedconversationsignature') || undefined - if (!json?.conversationSignature && !json.encryptedconversationsignature) { + if (!json?.clientId || (!json?.conversationSignature && !json.encryptedconversationsignature)) { + await sleep(2000) continue } - debug('headers', headers) - const cookies = [`BING_IP=${headers['x-forwarded-for']}`] - res.setHeader('set-cookie', cookies.map(cookie => `${cookie.trim()}; Max-Age=${86400 * 30}; Path=/;`)) + const cookie = response.headers.getSetCookie().join('; ') + debug('headers', headers, cookie) + + const bingCookie = btoa(`curl -H 'cookie: ${cookie}'`) + res.setHeader('set-cookie', [ + ...[`BING_HEADER=${bingCookie.trim()}`, `BING_IP=${response.headers.get('x-forwarded-for') || headers['x-forwarded-for']}`].map(c => `${c}; Max-Age=${86400 * 30}; Path=/;`), + // ...[`BING_HEADER0=`, `BING_HEADER1=`, `BING_HEADER2=`].map(c => `${c}; Max-Age=0; Path=/;`) + ]) res.writeHead(200, { 'Content-Type': 'application/json',