Skip to content

Commit

Permalink
fix: 不再兼容老旧逻辑,修复使用身份信息时出现不可用的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Aug 18, 2023
1 parent bf61aec commit 30ab8dc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ docker run --rm -it -e BING_HEADER=xxxx -p 7860:7860 weaigc/bingo
## 如何获取 BING_HEADER
> 配置了 BING_HEADER 意味着你将自己的账号共享给所有使用此服务的人,如果不需要免登录画图的功能,不建议设置此变量
打开 https://www.bing.com 并登录,然后访问 https://www.bing.com/turing/captcha/challenge,通过人机校验,然后
打开 https://www.bing.com 并登录,然后访问 https://www.bing.com/turing/captcha/challenge ,通过人机校验,然后

![BING HEADER](./docs/images/curl.png)

Expand Down
2 changes: 1 addition & 1 deletion src/lib/bots/bing/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function createImage(prompt: string, id: string, headers: HeadersIn
);

if (!/&id=([^&]+)$/.test(responseHeaders.get('location') || '')) {
throw new Error('请求异常,请检查 cookie 是否有效')
throw new Error('请求异常,请检查身份信息是否有效')
}

const resultId = RegExp.$1;
Expand Down
28 changes: 11 additions & 17 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { clsx, type ClassValue } from 'clsx'
import { customAlphabet } from 'nanoid'
import { twMerge } from 'tailwind-merge'
import { debug } from './isomorphic'

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
Expand All @@ -27,7 +28,7 @@ export function randomIP() {
return `11.${random(104, 107)}.${random(1, 255)}.${random(1, 255)}`
}

export const defaultUID = Math.random().toString(36).slice(2)
export const defaultUID = 'xxx-' + Math.random().toString(36).slice(2)

export function parseHeadersFromCurl(content: string) {
const re = /-H '([^:]+):\s*([^']+)/mg
Expand Down Expand Up @@ -79,7 +80,7 @@ export function parseCookie(cookie: string, cookieName: string) {
}

export function setCookie(key: string, value: string) {
const maxAge = 86400 * 30
const maxAge = value ? 86400 * 30 : 0
document.cookie = `${key}=${value || ''}; Path=/; Max-Age=${maxAge}; SameSite=None; Secure`
}

Expand All @@ -105,44 +106,37 @@ export function parseUA(ua?: string, default_ua = DEFAULT_UA) {

export function createHeaders(cookies: Partial<{ [key: string]: string }>, defaultHeaders?: Partial<{ [key: string]: string }>, type?: string) {
let {
BING_COOKIE = process.env.BING_COOKIE,
BING_UA = process.env.BING_UA,
BING_IP = process.env.BING_IP,
BING_HEADER = process.env.BING_HEADER,
IMAGE_ONLY = process.env.IMAGE_ONLY ?? '1',
_U = defaultUID,
} = cookies

debug('BING_HEADER', BING_HEADER)
if (BING_HEADER) {
const headers = extraHeadersFromCookie({
BING_HEADER,
...cookies,
}) || {}
if (/^(1|true|yes)$/.test(String(IMAGE_ONLY)) && type !== 'image') {
// 仅画图时设置 cookie
headers.cookie = `_U=${defaultUID}`
}
if (headers['user-agent']) {
if (/^(1|true|yes)$/.test(String(IMAGE_ONLY))) {
if (type === 'image') {
return headers
}
} else {
return headers
}
}

const ua = parseUA(BING_UA)

if (!BING_COOKIE) {
BING_COOKIE = defaultHeaders?.IMAGE_BING_COOKIE || defaultUID // hf 暂时不用 Cookie 也可以正常使用
}

const parsedCookie = parseCookie(BING_COOKIE, '_U')
if (!parsedCookie) {
throw new Error('Invalid Cookie')
}
return {
'x-forwarded-for': BING_IP || DEFAULT_IP,
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'User-Agent': ua!,
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',
cookie: `_U=${parsedCookie}` || '',
cookie: `_U=${_U}` || '',
}
}

Expand Down
1 change: 1 addition & 0 deletions src/pages/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
try {
const headers = createHeaders(req.cookies)

res.setHeader('set-cookie', headers.cookie)
res.writeHead(200, {
'Content-Type': 'application/json',
})
Expand Down
1 change: 0 additions & 1 deletion src/pages/api/sydney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BingWebBot } from '@/lib/bots/bing'
import { websocketUtils } from '@/lib/bots/bing/utils'
import { WatchDog, createHeaders } from '@/lib/utils'


export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const conversationContext = req.body
const headers = createHeaders(req.cookies)
Expand Down

0 comments on commit 30ab8dc

Please sign in to comment.