Skip to content

Commit

Permalink
feat: 同步new bing接口改动
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Oct 7, 2023
1 parent 59b2c44 commit fe1d8ac
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 31 deletions.
Binary file modified docs/images/wechat2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bingo",
"version": "0.9.0",
"version": "1.0.0",
"private": true,
"main": "./cloudflare/cli.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@/components/ui/dialog'
import { Button } from './ui/button'
import { Input } from './ui/input'
import { ChunkKeys, parseCookies, extraCurlFromCookie, parseHeadersFromCurl, encodeHeadersToCookie, setCookie } from '@/lib/utils'
import { ChunkKeys, parseCookies, extraCurlFromCookie, parseHeadersFromCurl, encodeHeadersToCookie, setCookie, resetCookies } from '@/lib/utils'
import { ExternalLink } from './external-link'
import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard'

Expand Down Expand Up @@ -131,7 +131,7 @@ export function Settings() {
}
encodeHeadersToCookie(headerValue).forEach(cookie => setCookie(cookie))
} else {
[...ChunkKeys, 'BING_COOKIE', 'BING_UA', 'BING_IP', '_U'].forEach(key => setCookie(key, ''))
resetCookies()
}
setCookie('IMAGE_ONLY', RegExp.$1 === 'cn' || imageOnly || !headerValue ? '1' : '0')

Expand Down
1 change: 1 addition & 0 deletions src/lib/bots/bing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export class BingWebBot {
conversationId: conversation.conversationId,
userIpAddress: conversation.userIpAddress,
conversationSignature: conversation.conversationSignature,
encryptedconversationsignature: conversation.encryptedconversationsignature,
clientId: conversation.clientId,
invocationId: conversation.invocationId ?? 0,
conversationStyle,
Expand Down
12 changes: 2 additions & 10 deletions src/lib/bots/bing/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export interface ConversationInfoBase {
conversationId: string
userIpAddress: string
clientId: string
conversationSignature: string
conversationSignature?: string
encryptedconversationsignature?: string
invocationId: number
}

Expand All @@ -147,15 +148,6 @@ export interface ConversationInfo extends ConversationInfoBase {
imageUrl?: string
}

export interface BingChatResponse {
conversationSignature: string
conversationId: string
clientId: string
conversationExpiryTime: Date
response: string
details: ChatResponseMessage
}

export interface Throttling {
maxNumLongDocSummaryUserMessagesInConversation: number
maxNumUserMessagesInConversation: number
Expand Down
8 changes: 7 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export function parseCookies(cookie: string, cookieNames: string[]) {
return cookies
}

export function resetCookies() {
[...ChunkKeys, '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'
export const DEFAULT_UA_MOBILE = `Mozilla/5.0 (iPhone; CPU iPhone OS 15_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.7 Mobile/15E148 Safari/605.1.15 BingSapphire/1.0.410427012`

Expand All @@ -158,6 +162,7 @@ export function mockUser(cookies: Partial<{ [key: string]: string }>) {
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'User-Agent': ua!,
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.3 OS/Win32',
'referer': 'https://www.bing.com/search?showconv=1&sendquery=1&q=Bing%20AI&form=MY02CJ&OCID=MY02CJ&OCID=MY02CJ&pl=launch',
cookie: `_U=${_U || defaultUID}; MUID=${MUID || muid()}`,
}
}
Expand All @@ -178,8 +183,9 @@ export function createHeaders(cookies: Partial<{ [key: string]: string }>, type?
BING_HEADER,
...cookies,
})
headers['x-forwarded-for'] = BING_IP || randomIP()
// headers['x-forwarded-for'] = BING_IP || randomIP()
headers['user-agent'] = parseUA(headers['user-agent'])
headers['referer'] = 'https://www.bing.com/search?showconv=1&sendquery=1&q=Bing%20AI&form=MY02CJ&OCID=MY02CJ&OCID=MY02CJ&pl=launch'
headers['x-ms-useragent'] = headers['x-ms-useragent'] || 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.3 OS/Win32'
return headers
}
Expand Down
17 changes: 13 additions & 4 deletions src/pages/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const endpoints = [req.headers['x-endpoint'], ...(process.env.ENDPOINT || '').split(','), 'www.bing.com'].filter(Boolean)
const endpoint = endpoints[count % endpoints.length]
const { conversationId } = req.query
const query = conversationId ? new URLSearchParams({
conversationId: String(conversationId),
}) : ''
const query = new URLSearchParams({
bundleVersion: '1.1055.8',
})
if (conversationId) {
query.set('conversationId', String(conversationId))
}

debug(`try ${count+1}`, endpoint, headers['x-forwarded-for'])
const response = await fetch(`https://${endpoint || 'www.bing.com'}/turing/conversation/create?${query}`, { method: 'GET', headers })
.catch(e => {
Expand All @@ -29,7 +33,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (response.status === 200) {
const json = await response.json().catch((e: any) => {})
console.log('json', json)
if (!json?.conversationSignature) {
if (!json?.clientId) {
continue
}
json.encryptedconversationsignature = response.headers.get('X-Sydney-encryptedconversationsignature') || undefined

if (!json?.conversationSignature && !json.encryptedconversationsignature) {
continue
}

Expand Down
20 changes: 9 additions & 11 deletions src/pages/api/sydney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ 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)
const headers = createHeaders(req.cookies, req.cookies['BING_HEADER1'] ? undefined : 'image')
const id = headers['x-forwarded-for']
headers['x-forwarded-for'] = conversationContext?.userIpAddress || headers['x-forwarded-for']
// headers['x-forwarded-for'] = conversationContext?.userIpAddress || headers['x-forwarded-for']

debug(id, conversationContext, headers)
res.setHeader('Content-Type', 'text/stream; charset=UTF-8')

const ws = new WebSocket(`wss://${req.headers['x-ws-endpoint'] || WS_ENDPOINT}/sydney/ChatHub`, {
headers: {
...headers,
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',
pragma: 'no-cache',
}
const uri = new URL(`wss://${req.headers['x-ws-endpoint'] || WS_ENDPOINT}/sydney/ChatHub`)
if ('encryptedconversationsignature' in conversationContext) {
uri.searchParams.set('sec_access_token', conversationContext['encryptedconversationsignature'])
}
debug(id, 'wss url', uri.toString())
const ws = new WebSocket(uri.toString(), {
headers,
})

const closeDog = new WatchDog()
Expand Down

0 comments on commit fe1d8ac

Please sign in to comment.