Skip to content

Commit

Permalink
Retry bing api with x-forwarded-for header
Browse files Browse the repository at this point in the history
  • Loading branch information
wong2 committed Mar 24, 2023
1 parent c748a01 commit 6426134
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/app/bots/bing/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { ofetch, FetchError } from 'ofetch'
import { random } from 'lodash-es'
import { ofetch } from 'ofetch'
import { uuid } from '~utils'
import { ChatError, ErrorCode } from '~utils/errors'
import { ConversationResponse } from './types'

// https://github.com/acheong08/EdgeGPT/blob/master/src/EdgeGPT.py#L32
function randomIP() {
return `13.${random(104, 107)}.${random(0, 255)}.${random(0, 255)}`
}

const API_ENDPOINT = 'https://www.bing.com/turing/conversation/create'

export async function createConversation(): Promise<ConversationResponse> {
const headers = {
'x-ms-client-request-id': uuid(),
Expand All @@ -11,17 +19,16 @@ export async function createConversation(): Promise<ConversationResponse> {

let resp: ConversationResponse
try {
resp = await ofetch('https://www.bing.com/turing/conversation/create', { headers })
resp = await ofetch(API_ENDPOINT, { headers, redirect: 'error' })
if (!resp.result) {
console.debug('bing/conversation/create', resp)
resp = await ofetch('https://edgeservices.bing.com/edgesvc/turing/conversation/create', { headers })
throw new Error('Invalid response')
}
} catch (err) {
if (err instanceof FetchError && err.status === 404) {
resp = await ofetch('https://edgeservices.bing.com/edgesvc/turing/conversation/create', { headers })
} else {
throw err
}
console.error('retry bing create', err)
resp = await ofetch(API_ENDPOINT, {
headers: { ...headers, 'x-forwarded-for': randomIP() },
redirect: 'error',
})
}

if (resp.result.value !== 'Success') {
Expand Down

0 comments on commit 6426134

Please sign in to comment.