patch: fix(upgrade) bing client (#505)

This commit is contained in:
josc146
2023-09-06 23:52:28 +08:00
parent 97c04c2464
commit e71ee74c6d
9 changed files with 288 additions and 123 deletions
+53 -47
View File
@@ -2,6 +2,7 @@
import { v4 as uuidv4 } from 'uuid'
import BingImageCreator from './BingImageCreator'
import { fetchBg } from '../../../utils/fetch-bg.mjs'
/**
* https://stackoverflow.com/a/58326357
@@ -71,51 +72,53 @@ export default class BingAIClient {
}
async createNewConversation() {
this.headers = {
accept: 'application/json',
'accept-language': 'en-US,en;q=0.9',
'content-type': 'application/json',
'sec-ch-ua': '"Microsoft Edge";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
'sec-ch-ua-arch': '"x86"',
'sec-ch-ua-bitness': '"64"',
'sec-ch-ua-full-version': '"113.0.1774.50"',
'sec-ch-ua-full-version-list':
'"Microsoft Edge";v="113.0.1774.50", "Chromium";v="113.0.5672.127", "Not-A.Brand";v="24.0.0.0"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-model': '""',
'sec-ch-ua-platform': '"Windows"',
'sec-ch-ua-platform-version': '"15.0.0"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'sec-ms-gec': genRanHex(64).toUpperCase(),
'sec-ms-gec-version': '1-115.0.1866.1',
'x-ms-client-request-id': uuidv4(),
'x-ms-useragent':
'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50',
cookie:
this.options.cookies ||
(this.options.userToken ? `_U=${this.options.userToken}` : undefined),
Referer: 'https://www.bing.com/search?q=Bing+AI&showconv=1',
'Referrer-Policy': 'origin-when-cross-origin',
// Workaround for request being blocked due to geolocation
// 'x-forwarded-for': '1.1.1.1', // 1.1.1.1 seems to no longer work.
...(this.options.xForwardedFor ? { 'x-forwarded-for': this.options.xForwardedFor } : {}),
}
// filter undefined values
this.headers = Object.fromEntries(
Object.entries(this.headers).filter(([, value]) => value !== undefined),
)
const fetchOptions = {
headers: {
accept: 'application/json',
'accept-language': 'en-US,en;q=0.9',
'content-type': 'application/json',
'sec-ch-ua': '"Microsoft Edge";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
'sec-ch-ua-arch': '"x86"',
'sec-ch-ua-bitness': '"64"',
'sec-ch-ua-full-version': '"113.0.1774.50"',
'sec-ch-ua-full-version-list':
'"Microsoft Edge";v="113.0.1774.50", "Chromium";v="113.0.5672.127", "Not-A.Brand";v="24.0.0.0"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-model': '""',
'sec-ch-ua-platform': '"Windows"',
'sec-ch-ua-platform-version': '"15.0.0"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'sec-ms-gec': genRanHex(64).toUpperCase(),
'sec-ms-gec-version': '1-115.0.1866.1',
'x-ms-client-request-id': uuidv4(),
'x-ms-useragent':
'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50',
cookie:
this.options.cookies ||
(this.options.userToken ? `_U=${this.options.userToken}` : undefined),
Referer: 'https://www.bing.com/search?q=Bing+AI&showconv=1',
'Referrer-Policy': 'origin-when-cross-origin',
// Workaround for request being blocked due to geolocation
// 'x-forwarded-for': '1.1.1.1', // 1.1.1.1 seems to no longer work.
...(this.options.xForwardedFor ? { 'x-forwarded-for': this.options.xForwardedFor } : {}),
},
headers: this.headers,
}
if (this.options.proxy) {
// fetchOptions.dispatcher = new ProxyAgent(this.options.proxy);
}
const response = await fetch(`${this.options.host}/turing/conversation/create`, fetchOptions)
const { status, headers } = response
if (status === 200 && +headers.get('content-length') < 5) {
throw new Error('/turing/conversation/create: Your IP is blocked by BingAI.')
}
// if (this.options.proxy) {
// fetchOptions.dispatcher = new ProxyAgent(this.options.proxy)
// } else {
// fetchOptions.dispatcher = new Agent({ connect: { timeout: 20_000 } })
// }
const response = await fetchBg(`${this.options.host}/turing/conversation/create`, fetchOptions)
const body = await response.text()
try {
return JSON.parse(body)
@@ -126,10 +129,10 @@ export default class BingAIClient {
async createWebSocketConnection() {
return new Promise((resolve, reject) => {
// let agent;
if (this.options.proxy) {
// agent = new HttpsProxyAgent(this.options.proxy);
}
// let agent
// if (this.options.proxy) {
// agent = new HttpsProxyAgent(this.options.proxy)
// }
const ws = new WebSocket('wss://sydney.bing.com/sydney/ChatHub')
@@ -449,6 +452,9 @@ export default class BingAIClient {
if (!messages?.length || messages[0].author !== 'bot') {
return
}
if (messages[0].contentOrigin === 'Apology') {
return
}
if (messages[0]?.contentType === 'IMAGE') {
// You will never get a message of this type without 'gencontentv3' being on.
bicIframe = this.bic