Skip to content

Commit

Permalink
feat: 兼容v2
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Oct 21, 2023
1 parent ef6cdb3 commit def3601
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ export function cookie2Headers(cookies: Partial<{ [key: string]: string }>) {
let {
BING_HEADER,
BING_HEADER0 = process.env.BING_HEADER,
BING_IP,
} = cookies || {}
const headers = extraHeadersFromCookie({
BING_HEADER,
Expand All @@ -184,7 +183,7 @@ export function cookie2Headers(cookies: Partial<{ [key: string]: string }>) {
return headers
}

export function createHeaders(cookies: Partial<{ [key: string]: string }>, useMock?: boolean) {
export function createHeaders(cookies: Partial<{ [key: string]: string }> = {}, useMock?: boolean) {
let {
BING_HEADER,
BING_HEADER0 = process.env.BING_HEADER,
Expand All @@ -193,6 +192,8 @@ export function createHeaders(cookies: Partial<{ [key: string]: string }>, useMo
} = cookies || {}
if (useMock == null) {
useMock = BING_HEADER ? false : (/^(1|true|yes)$/i.test(String(IMAGE_ONLY)) ? true : !BING_HEADER0)
} else if (useMock === false) {
cookies.BING_HEADER = ''
}
const headers = useMock ? mockUser(cookies) : cookie2Headers(cookies)
if (BING_IP) {
Expand Down
25 changes: 15 additions & 10 deletions src/pages/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
})
debug('status', response.status, response.url, headers)
if (response.status === 200) {
const json = await response.json().catch((e: any) => {})
console.log('json', json)
if (!json?.clientId) {
continue
}
json.encryptedconversationsignature = response.headers.get('X-Sydney-encryptedconversationsignature') || undefined
const json = await response.json().catch((e: any) => {
console.log('fetch error', e)
}) || {}
debug('json', json)
json.encryptedconversationsignature = json.encryptedconversationsignature || response.headers.get('X-Sydney-encryptedconversationsignature') || undefined

if (!json?.conversationSignature && !json.encryptedconversationsignature) {
if (!json?.clientId || (!json?.conversationSignature && !json.encryptedconversationsignature)) {
await sleep(2000)
continue
}

debug('headers', headers)
const cookies = [`BING_IP=${headers['x-forwarded-for']}`]
res.setHeader('set-cookie', cookies.map(cookie => `${cookie.trim()}; Max-Age=${86400 * 30}; Path=/;`))
const cookie = response.headers.getSetCookie().join('; ')
debug('headers', headers, cookie)

const bingCookie = btoa(`curl -H 'cookie: ${cookie}'`)
res.setHeader('set-cookie', [
...[`BING_HEADER=${bingCookie.trim()}`, `BING_IP=${response.headers.get('x-forwarded-for') || headers['x-forwarded-for']}`].map(c => `${c}; Max-Age=${86400 * 30}; Path=/;`),
// ...[`BING_HEADER0=`, `BING_HEADER1=`, `BING_HEADER2=`].map(c => `${c}; Max-Age=0; Path=/;`)
])

res.writeHead(200, {
'Content-Type': 'application/json',
Expand Down

0 comments on commit def3601

Please sign in to comment.