From d9ebab59291c4208734e2f28433c9f3215a7d501 Mon Sep 17 00:00:00 2001 From: weaigc <879821485@qq.com> Date: Wed, 16 Aug 2023 22:54:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0cookie=E4=BB=85?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E7=94=BB=E5=9B=BE=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ src/components/settings.tsx | 22 +++++++++++++++++++--- src/lib/bots/bing/index.ts | 2 +- src/lib/utils.ts | 28 ++++++++++++++++++++++++---- src/pages/api/create.ts | 4 ++-- src/pages/api/image.ts | 2 +- 6 files changed, 49 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 1ee944a4..c63ab798 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,8 @@ https://bing.github1s.tk ## 安装和使用 +> 由于目前微软封杀比较严重,推荐优先使用 [部署 Huggingface](#部署到-huggingface) 。 + * 使用 Node 启动 ```bash diff --git a/src/components/settings.tsx b/src/components/settings.tsx index e18aa5b4..45ba6044 100644 --- a/src/components/settings.tsx +++ b/src/components/settings.tsx @@ -13,14 +13,16 @@ import { } from '@/components/ui/dialog' import { Button } from './ui/button' import { Input } from './ui/input' -import { ChunkKeys, parseCookies, extraCurlFromCookie, randomIP, encodeHeadersToCookie } from '@/lib/utils' +import { ChunkKeys, parseCookies, extraCurlFromCookie, encodeHeadersToCookie, getCookie, setCookie } from '@/lib/utils' import { ExternalLink } from './external-link' import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard' + export function Settings() { const { isCopied, copyToClipboard } = useCopyToClipboard({ timeout: 2000 }) const [loc, setLoc] = useAtom(hashAtom) const [curlValue, setCurlValue] = useState(extraCurlFromCookie(parseCookies(document.cookie, ChunkKeys))) + const [imageOnly, setImageOnly] = useState(getCookie('IMAGE_ONLY') !== '0') const [enableTTS, setEnableTTS] = useAtom(voiceAtom) useEffect(() => { @@ -58,6 +60,19 @@ export function Settings() { placeholder="在此填写用户信息,格式: curl 'https://www.bing.com/turing/captcha/challenge' ..." onChange={e => setCurlValue(e.target.value)} /> +
+ 身份信息仅用于画图(推荐) + setImageOnly(checked)} + > + + +
+ @@ -71,7 +86,7 @@ export function Settings() { if (headerValue) { try { headerValue = atob(headerValue) - } catch (e) {} + } catch (e) { } if (!/^\s*curl ['"]https:\/\/www\.bing\.com\/turing\/captcha\/challenge['"]/.test(headerValue)) { toast.error('格式不正确') return @@ -79,8 +94,9 @@ export function Settings() { const maxAge = 86400 * 30 encodeHeadersToCookie(headerValue).forEach(cookie => document.cookie = `${cookie}; Max-Age=${maxAge}; Path=/; SameSite=None; Secure`) } else { - [...ChunkKeys, 'BING_COOKIE', 'BING_UA', 'BING_IP'].forEach(key => document.cookie = `${key}=; Path=/; SameSite=None; Secure`) + [...ChunkKeys, 'BING_COOKIE', 'BING_UA', 'BING_IP'].forEach(key => setCookie(key, '')) } + setCookie('IMAGE_ONLY', imageOnly ? '1' : '0') toast.success('保存成功') setLoc('') diff --git a/src/lib/bots/bing/index.ts b/src/lib/bots/bing/index.ts index 2c4afae0..ec039c1b 100644 --- a/src/lib/bots/bing/index.ts +++ b/src/lib/bots/bing/index.ts @@ -139,7 +139,7 @@ export class BingWebBot { } if (!resp?.result) { - throw new ChatError('Invalid response', ErrorCode.UNKOWN_ERROR) + throw new ChatError('你的 VPS 或代理可能被封禁,如有疑问,请前往 https://github.com/weaigc/bingo 咨询', ErrorCode.UNKOWN_ERROR) } const { value, message } = resp.result || {} diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 07feedb3..0a09ddc4 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -27,6 +27,8 @@ export function randomIP() { return `11.${random(104, 107)}.${random(1, 255)}.${random(1, 255)}` } +export const defaultUID = Math.random().toString(36).slice(2) + export function parseHeadersFromCurl(content: string) { const re = /-H '([^:]+):\s*([^']+)/mg const headers: HeadersInit = {} @@ -76,6 +78,16 @@ export function parseCookie(cookie: string, cookieName: string) { return targetCookie ? decodeURIComponent(targetCookie).trim() : cookie.indexOf('=') === -1 ? cookie.trim() : '' } +export function setCookie(key: string, value: string) { + const maxAge = 86400 * 30 + document.cookie = `${key}=${value || ''}; Path=/; Max-Age=${maxAge}; SameSite=None; Secure` +} + +export function getCookie(cookieName: string) { + const re = new RegExp(`(?:[; ]|^)${cookieName}=([^;]*)`) + return re.test(document.cookie) ? RegExp.$1 : '' +} + export function parseCookies(cookie: string, cookieNames: string[]) { const cookies: { [key: string]: string } = {} cookieNames.forEach(cookieName => { @@ -91,25 +103,33 @@ export function parseUA(ua?: string, default_ua = DEFAULT_UA) { return / EDGE?/i.test(decodeURIComponent(ua || '')) ? decodeURIComponent(ua!.trim()) : default_ua } -export function createHeaders(cookies: Partial<{ [key: string]: string }>, defaultHeaders?: Partial<{ [key: string]: string }>) { +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', } = cookies if (BING_HEADER) { - return extraHeadersFromCookie({ + 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']) { + return headers + } } const ua = parseUA(BING_UA) if (!BING_COOKIE) { - BING_COOKIE = defaultHeaders?.IMAGE_BING_COOKIE || 'xxx' // hf 暂时不用 Cookie 也可以正常使用 + BING_COOKIE = defaultHeaders?.IMAGE_BING_COOKIE || defaultUID // hf 暂时不用 Cookie 也可以正常使用 } const parsedCookie = parseCookie(BING_COOKIE, '_U') diff --git a/src/pages/api/create.ts b/src/pages/api/create.ts index 508fa97e..430bb2d5 100644 --- a/src/pages/api/create.ts +++ b/src/pages/api/create.ts @@ -4,8 +4,8 @@ import { NextApiRequest, NextApiResponse } from 'next' import { fetch, debug } from '@/lib/isomorphic' import { createHeaders } from '@/lib/utils' -// const API_ENDPOINT = 'https://www.bing.com/turing/conversation/create' -const API_ENDPOINT = 'https://edgeservices.bing.com/edgesvc/turing/conversation/create'; +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 { diff --git a/src/pages/api/image.ts b/src/pages/api/image.ts index 4b894bea..fbc0c8de 100644 --- a/src/pages/api/image.ts +++ b/src/pages/api/image.ts @@ -18,7 +18,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) try { const headers = createHeaders(req.cookies, { IMAGE_BING_COOKIE: process.env.IMAGE_BING_COOKIE - }) + }, 'image') debug('headers', headers) const response = await createImage(String(prompt), String(id), {