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 c02f7ce
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 16 deletions.
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
18 changes: 3 additions & 15 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,44 +105,32 @@ 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

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)) && type === 'image' && headers.cookie) {
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

0 comments on commit c02f7ce

Please sign in to comment.