Skip to content

Commit

Permalink
feat: 兼容2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Oct 10, 2023
1 parent 2d096d8 commit 7ead1a4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/lib/bots/bing/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export interface ConversationInfo extends ConversationInfoBase {
conversationStyle: BingConversationStyle
prompt: string
imageUrl?: string
source?: 'cib' | 'WindowsCopilot'
}

export interface Throttling {
Expand Down
25 changes: 13 additions & 12 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,16 @@ 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)
const contentChunks = base64Content.match(/.{1,4000}/g) || []
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) {
Expand Down Expand Up @@ -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'
Expand All @@ -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'])

Expand All @@ -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()
Expand Down
9 changes: 7 additions & 2 deletions src/pages/api/sydney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7ead1a4

Please sign in to comment.