From 7ead1a48030ea9cdbc3bcb31672c2490ef76e853 Mon Sep 17 00:00:00 2001 From: weaigc <879821485@qq.com> Date: Tue, 10 Oct 2023 19:40:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=BC=E5=AE=B92.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/bots/bing/types.ts | 1 + src/lib/utils.ts | 25 +++++++++++++------------ src/pages/api/sydney.ts | 9 +++++++-- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/lib/bots/bing/types.ts b/src/lib/bots/bing/types.ts index 3ace89e7..27b27e4c 100644 --- a/src/lib/bots/bing/types.ts +++ b/src/lib/bots/bing/types.ts @@ -146,6 +146,7 @@ export interface ConversationInfo extends ConversationInfoBase { conversationStyle: BingConversationStyle prompt: string imageUrl?: string + source?: 'cib' | 'WindowsCopilot' } export interface Throttling { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 62e5965c..4417bdce 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -82,7 +82,7 @@ export function parseHeadersFromCurl(content: string) { return headers } -export const ChunkKeys = ['BING_HEADER', 'BING_HEADER1', 'BING_HEADER2'] +export const ChunkKeys = ['BING_HEADER0', 'BING_HEADER1', 'BING_HEADER2'] export function encodeHeadersToCookie(content: string) { const base64Content = btoa(content) @@ -90,11 +90,8 @@ export function encodeHeadersToCookie(content: string) { return ChunkKeys.map((key, index) => `${key}=${contentChunks[index] ?? ''}`) } -export function extraCurlFromCookie(cookies: Partial<{ [key: string]: string }>) { - let base64Content = '' - ChunkKeys.forEach((key) => { - base64Content += (cookies[key] || '') - }) +export function extraCurlFromCookie(cookies: Partial<{ [key: string]: string }> = {}) { + const base64Content = cookies.BING_HEADER || ChunkKeys.map((key) => cookies[key] || '').join('') try { return atob(base64Content) } catch (e) { @@ -133,7 +130,7 @@ export function parseCookies(cookie: string, cookieNames: string[]) { } export function resetCookies() { - [...ChunkKeys, 'BING_COOKIE', 'BING_UA', '_U', 'BING_IP', 'MUID'].forEach(key => setCookie(key, '')) + [...ChunkKeys, 'BING_HEADER', '', 'BING_COOKIE', 'BING_UA', '_U', 'BING_IP', 'MUID'].forEach(key => setCookie(key, '')) } export const DEFAULT_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.0.0' @@ -145,14 +142,16 @@ export function parseUA(ua?: string, default_ua = DEFAULT_UA) { export function mockUser(cookies: Partial<{ [key: string]: string }>) { const { - BING_HEADER = process.env.BING_HEADER || '', + BING_HEADER, + BING_HEADER0 = process.env.BING_HEADER, BING_UA = process.env.BING_UA, - BING_IP = process.env.BING_IP || '', + BING_IP = process.env.BING_IP, } = cookies const ua = parseUA(BING_UA) const { _U, MUID } = parseCookies(extraHeadersFromCookie({ BING_HEADER, + BING_HEADER0, ...cookies, }).cookie, ['MUID']) @@ -169,16 +168,18 @@ export function mockUser(cookies: Partial<{ [key: string]: string }>) { export function createHeaders(cookies: Partial<{ [key: string]: string }>, useMock?: boolean) { let { - BING_HEADER = process.env.BING_HEADER, - BING_IP = process.env.BING_IP || '', + BING_HEADER, + BING_HEADER0 = process.env.BING_HEADER, + BING_IP = process.env.BING_IP, IMAGE_ONLY = process.env.IMAGE_ONLY ?? '1', } = cookies || {} useMock = useMock ?? /^(1|true|yes)$/i.test(String(IMAGE_ONLY)) - if (!BING_HEADER || useMock) { + if ((!BING_HEADER && !BING_HEADER0) || useMock) { return mockUser(cookies) } const headers = extraHeadersFromCookie({ BING_HEADER, + BING_HEADER0, ...cookies, }) headers['x-forwarded-for'] = BING_IP || randomIP() diff --git a/src/pages/api/sydney.ts b/src/pages/api/sydney.ts index 0ebd6f45..8a363dba 100644 --- a/src/pages/api/sydney.ts +++ b/src/pages/api/sydney.ts @@ -14,7 +14,7 @@ const { WS_ENDPOINT = 'sydney.bing.com' } = process.env export default async function handler(req: NextApiRequest, res: NextApiResponse) { const conversationContext = req.body - const headers = createHeaders(req.cookies, Boolean(req.cookies['BING_HEADER1'])) + const headers = createHeaders(req.cookies) const id = headers['x-forwarded-for'] // headers['x-forwarded-for'] = conversationContext?.userIpAddress || headers['x-forwarded-for'] @@ -62,7 +62,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) await new Promise((resolve) => ws.onopen = resolve) ws.send(websocketUtils.packMessage({ protocol: 'json', version: 1 })) ws.send(websocketUtils.packMessage({ type: 6 })) - ws.send(websocketUtils.packMessage(BingWebBot.buildChatRequest(conversationContext!))) + ws.send(websocketUtils.packMessage( + BingWebBot.buildChatRequest({ + ...conversationContext, + source: req.cookies?.BING_SOURCE, + }) + )) req.socket.once('close', () => { debug(id, 'connection close') ws.close()