Skip to content

Commit

Permalink
feat: 兼容 v2
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Oct 15, 2023
1 parent 82dde68 commit de08e51
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ Bingo,一个让你呼吸顺畅 New Bing。

- 触发器 中自定义访问域名。


### 部署到 Render

感谢 [@SokWith](https://github.com/SokWith) 测试,Render 目前已复活。

[![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/weaigc/bingo)
</details>



### 部署其它平台
<details>
<summary>
Expand All @@ -93,12 +103,6 @@ V2 版本已解决此问题,详见: https://github.com/weaigc/bingo/tree/v2

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?demo-title=bingo&demo-description=bingo&demo-url=https%3A%2F%2Fbing.github1s.tk%2F&project-name=bingo&repository-name=bingo&repository-url=https%3A%2F%2Fgithub.com%2Fweaigc%2Fbingo&from=templates&skippable-integrations=1&env=BING_HEADER&envDescription=%E5%A6%82%E6%9E%9C%E4%B8%8D%E7%9F%A5%E9%81%93%E6%80%8E%E4%B9%88%E9%85%8D%E7%BD%AE%E8%AF%B7%E7%82%B9%E5%8F%B3%E4%BE%A7Learn+More&envLink=https%3A%2F%2Fgithub.com%2Fweaigc%2Fbingo%2Fblob%2Fmain%2F.env.example)

#### 部署到 Render

[![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/weaigc/bingo)
</details>



## 环境和依赖

Expand Down
Binary file modified docs/images/wechat2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 20 additions & 8 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,29 +166,41 @@ export function mockUser(cookies: Partial<{ [key: string]: string }>) {
}
}

export function createHeaders(cookies: Partial<{ [key: string]: string }>, useMock?: boolean) {
export function cookie2Headers(cookies: Partial<{ [key: string]: string }>) {
let {
BING_HEADER,
BING_HEADER0 = process.env.BING_HEADER,
BING_IP = process.env.BING_IP,
IMAGE_ONLY = process.env.IMAGE_ONLY ?? '1',
BING_IP,
} = cookies || {}
useMock = useMock ?? /^(1|true|yes)$/i.test(String(IMAGE_ONLY))
if ((!BING_HEADER && !BING_HEADER0) || useMock) {
return mockUser(cookies)
}
const headers = extraHeadersFromCookie({
BING_HEADER,
BING_HEADER0,
...cookies,
})
headers['x-forwarded-for'] = BING_IP || randomIP()

headers['user-agent'] = parseUA(headers['user-agent'])
headers['referer'] = 'https://www.bing.com/search?showconv=1&sendquery=1&q=Bing%20AI&form=MY02CJ&OCID=MY02CJ&OCID=MY02CJ&pl=launch'
headers['x-ms-useragent'] = headers['x-ms-useragent'] || 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.3 OS/Win32'
return headers
}

export function createHeaders(cookies: Partial<{ [key: string]: string }>, useMock?: boolean) {
let {
BING_HEADER,
BING_HEADER0 = process.env.BING_HEADER,
BING_IP,
IMAGE_ONLY = process.env.IMAGE_ONLY ?? '1',
} = cookies || {}
if (useMock == null) {
useMock = BING_HEADER ? false : (/^(1|true|yes)$/i.test(String(IMAGE_ONLY)) ? true : !BING_HEADER0)
}
const headers = useMock ? mockUser(cookies) : cookie2Headers(cookies)
if (BING_IP) {
headers['x-forwarded-for'] = BING_IP
}
return headers
}

export class WatchDog {
private tid = 0
watch(fn: Function, timeout = 2000) {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import { sleep } from '@/lib/bots/bing/utils'
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
let count = 0
const headers = createHeaders(req.cookies)
const headers = createHeaders({
...req.cookies,
BING_IP: randomIP()
})
do {
headers['x-forwarded-for'] = headers['x-forwarded-for'] || randomIP()
const endpoints = [req.headers['x-endpoint'], ...(process.env.ENDPOINT || '').split(','), 'www.bing.com'].filter(Boolean)
const endpoint = endpoints[count % endpoints.length]
const { conversationId } = req.query
Expand Down

0 comments on commit de08e51

Please sign in to comment.