Skip to content

Commit

Permalink
feat: 增加 create 接口中转
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Aug 22, 2023
1 parent 63c7d6d commit 11367be
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/lib/bots/bing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from './types'

import { convertMessageToMarkdown, websocketUtils, streamAsyncIterable } from './utils'
import { WatchDog, createChunkDecoder } from '@/lib/utils'
import { createChunkDecoder } from '@/lib/utils'

type Params = SendMessageParams<{ bingConversationStyle: BingConversationStyle }>

Expand Down Expand Up @@ -351,11 +351,14 @@ export class BingWebBot {
params.onEvent({ type: 'DONE' })
conversation.invocationId = parseInt(event.invocationId, 10) + 1
} else if (event.type === 1) {
const messages = event.arguments[0].messages
const { messages, throttling } = event.arguments[0] || {}
if (messages) {
const text = convertMessageToMarkdown(messages[0])
this.lastText = text
params.onEvent({ type: 'UPDATE_ANSWER', data: { text, spokenText: messages[0].text, throttling: event.arguments[0].throttling } })
params.onEvent({ type: 'UPDATE_ANSWER', data: { text, spokenText: messages[0].text } })
}
if (throttling) {
params.onEvent({ type: 'UPDATE_ANSWER', data: { text: '', throttling } })
}
} else if (event.type === 2) {
const messages = event.item.messages as ChatResponseMessage[] | undefined
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function createHeaders(cookies: Partial<{ [key: string]: string }>, type?
BING_HEADER,
...cookies,
}) || {}
headers['x-forward-for'] = BING_IP || randomIP()
headers['x-forwarded-for'] = BING_IP || randomIP()
return headers
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/pages/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import { fetch, debug } from '@/lib/isomorphic'
import { createHeaders, randomIP } from '@/lib/utils'
import { sleep } from '@/lib/bots/bing/utils'

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

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
let count = 0
const headers = createHeaders(req.cookies)
do {
headers['x-forwarded-for'] = headers['x-forwarded-for'] || randomIP()
debug(`try ${count+1}`, headers['x-forwarded-for'])
const response = await fetch(API_ENDPOINT, { method: 'GET', headers })
const endpoints = (process.env.ENDPOINT || 'www.bing.com').split(',')
const endpoint = endpoints[count % endpoints.length]
debug(`try ${count+1}`, endpoint, headers['x-forwarded-for'])
const response = await fetch(`https://${endpoint || 'www.bing.com'}/turing/conversation/create`, { method: 'GET', headers })
if (response.status === 200) {
res.setHeader('set-cookie', [headers.cookie, `BING_IP=${headers['x-forwarded-for']}`]
.map(cookie => `${cookie}; Max-Age=${86400 * 30}; Path=/; SameSite=None; Secure`))
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/kblob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
method: 'POST',
body: formData.getBuffer(),
headers: {
'x-forward-for': headers['x-forwarded-for'],
'x-forwarded-for': headers['x-forwarded-for'],
'user-agent': headers['User-Agent'],
cookie: headers['cookie'],
'Referer': 'https://www.bing.com/search',
Expand Down
22 changes: 22 additions & 0 deletions src/pages/api/turing/conversation/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const runtime = 'experimental-edge'

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

export async function GET(req: Request) {
const headers = {
"x-forwarded-for": req.headers.get('x-forwarded-for')! || undefined,
"Accept-Encoding": req.headers.get('accept-encoding')! || undefined,
"Accept-Language": req.headers.get('accept-language')! || undefined,
"x-ms-useragent": req.headers.get('x-ms-useragent')! || undefined,
"User-Agent": req.headers.get('user-agent')!,
"cookie": req.headers.get('cookie')!,
}
console.log('req headers', headers)

return fetch(API_ENDPOINT, {
method: 'GET',
// @ts-ignore
headers,
})
}
export default GET;

0 comments on commit 11367be

Please sign in to comment.