Skip to content

Commit fe1d8ac

Browse files
committed
feat: 同步new bing接口改动
1 parent 59b2c44 commit fe1d8ac

File tree

9 files changed

+37
-31
lines changed

9 files changed

+37
-31
lines changed

docs/images/wechat2.jpg

2.38 KB
Loading

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bingo",
3-
"version": "0.9.0",
3+
"version": "1.0.0",
44
"private": true,
55
"main": "./cloudflare/cli.js",
66
"scripts": {

src/components/settings.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '@/components/ui/dialog'
1414
import { Button } from './ui/button'
1515
import { Input } from './ui/input'
16-
import { ChunkKeys, parseCookies, extraCurlFromCookie, parseHeadersFromCurl, encodeHeadersToCookie, setCookie } from '@/lib/utils'
16+
import { ChunkKeys, parseCookies, extraCurlFromCookie, parseHeadersFromCurl, encodeHeadersToCookie, setCookie, resetCookies } from '@/lib/utils'
1717
import { ExternalLink } from './external-link'
1818
import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard'
1919

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

src/lib/bots/bing/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ export class BingWebBot {
281281
conversationId: conversation.conversationId,
282282
userIpAddress: conversation.userIpAddress,
283283
conversationSignature: conversation.conversationSignature,
284+
encryptedconversationsignature: conversation.encryptedconversationsignature,
284285
clientId: conversation.clientId,
285286
invocationId: conversation.invocationId ?? 0,
286287
conversationStyle,

src/lib/bots/bing/types.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ export interface ConversationInfoBase {
137137
conversationId: string
138138
userIpAddress: string
139139
clientId: string
140-
conversationSignature: string
140+
conversationSignature?: string
141+
encryptedconversationsignature?: string
141142
invocationId: number
142143
}
143144

@@ -147,15 +148,6 @@ export interface ConversationInfo extends ConversationInfoBase {
147148
imageUrl?: string
148149
}
149150

150-
export interface BingChatResponse {
151-
conversationSignature: string
152-
conversationId: string
153-
clientId: string
154-
conversationExpiryTime: Date
155-
response: string
156-
details: ChatResponseMessage
157-
}
158-
159151
export interface Throttling {
160152
maxNumLongDocSummaryUserMessagesInConversation: number
161153
maxNumUserMessagesInConversation: number

src/lib/utils.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ export function parseCookies(cookie: string, cookieNames: string[]) {
132132
return cookies
133133
}
134134

135+
export function resetCookies() {
136+
[...ChunkKeys, 'BING_COOKIE', 'BING_UA', '_U', 'BING_IP', 'MUID'].forEach(key => setCookie(key, ''))
137+
}
138+
135139
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'
136140
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`
137141

@@ -158,6 +162,7 @@ export function mockUser(cookies: Partial<{ [key: string]: string }>) {
158162
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
159163
'User-Agent': ua!,
160164
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.3 OS/Win32',
165+
'referer': 'https://www.bing.com/search?showconv=1&sendquery=1&q=Bing%20AI&form=MY02CJ&OCID=MY02CJ&OCID=MY02CJ&pl=launch',
161166
cookie: `_U=${_U || defaultUID}; MUID=${MUID || muid()}`,
162167
}
163168
}
@@ -178,8 +183,9 @@ export function createHeaders(cookies: Partial<{ [key: string]: string }>, type?
178183
BING_HEADER,
179184
...cookies,
180185
})
181-
headers['x-forwarded-for'] = BING_IP || randomIP()
186+
// headers['x-forwarded-for'] = BING_IP || randomIP()
182187
headers['user-agent'] = parseUA(headers['user-agent'])
188+
headers['referer'] = 'https://www.bing.com/search?showconv=1&sendquery=1&q=Bing%20AI&form=MY02CJ&OCID=MY02CJ&OCID=MY02CJ&pl=launch'
183189
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'
184190
return headers
185191
}

src/pages/api/create.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
1414
const endpoints = [req.headers['x-endpoint'], ...(process.env.ENDPOINT || '').split(','), 'www.bing.com'].filter(Boolean)
1515
const endpoint = endpoints[count % endpoints.length]
1616
const { conversationId } = req.query
17-
const query = conversationId ? new URLSearchParams({
18-
conversationId: String(conversationId),
19-
}) : ''
17+
const query = new URLSearchParams({
18+
bundleVersion: '1.1055.8',
19+
})
20+
if (conversationId) {
21+
query.set('conversationId', String(conversationId))
22+
}
23+
2024
debug(`try ${count+1}`, endpoint, headers['x-forwarded-for'])
2125
const response = await fetch(`https://${endpoint || 'www.bing.com'}/turing/conversation/create?${query}`, { method: 'GET', headers })
2226
.catch(e => {
@@ -29,7 +33,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
2933
if (response.status === 200) {
3034
const json = await response.json().catch((e: any) => {})
3135
console.log('json', json)
32-
if (!json?.conversationSignature) {
36+
if (!json?.clientId) {
37+
continue
38+
}
39+
json.encryptedconversationsignature = response.headers.get('X-Sydney-encryptedconversationsignature') || undefined
40+
41+
if (!json?.conversationSignature && !json.encryptedconversationsignature) {
3342
continue
3443
}
3544

src/pages/api/sydney.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ const { WS_ENDPOINT = 'sydney.bing.com' } = process.env
1414

1515
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
1616
const conversationContext = req.body
17-
const headers = createHeaders(req.cookies)
17+
const headers = createHeaders(req.cookies, req.cookies['BING_HEADER1'] ? undefined : 'image')
1818
const id = headers['x-forwarded-for']
19-
headers['x-forwarded-for'] = conversationContext?.userIpAddress || headers['x-forwarded-for']
19+
// headers['x-forwarded-for'] = conversationContext?.userIpAddress || headers['x-forwarded-for']
2020

2121
debug(id, conversationContext, headers)
2222
res.setHeader('Content-Type', 'text/stream; charset=UTF-8')
23-
24-
const ws = new WebSocket(`wss://${req.headers['x-ws-endpoint'] || WS_ENDPOINT}/sydney/ChatHub`, {
25-
headers: {
26-
...headers,
27-
'accept-language': 'zh-CN,zh;q=0.9',
28-
'cache-control': 'no-cache',
29-
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',
30-
pragma: 'no-cache',
31-
}
23+
const uri = new URL(`wss://${req.headers['x-ws-endpoint'] || WS_ENDPOINT}/sydney/ChatHub`)
24+
if ('encryptedconversationsignature' in conversationContext) {
25+
uri.searchParams.set('sec_access_token', conversationContext['encryptedconversationsignature'])
26+
}
27+
debug(id, 'wss url', uri.toString())
28+
const ws = new WebSocket(uri.toString(), {
29+
headers,
3230
})
3331

3432
const closeDog = new WatchDog()

0 commit comments

Comments
 (0)