Skip to content

Commit

Permalink
feat: 支持 replit 及 cf 部署
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Sep 13, 2023
1 parent c3d6380 commit 4b6ef0a
Show file tree
Hide file tree
Showing 5 changed files with 687 additions and 35 deletions.
39 changes: 17 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ Bingo,一个让你呼吸顺畅 New Bing。

</div>

## 演示站点
## 演示站点(由于 New Bing 封锁,目前需要绑定用户信息才能使用,请参考说明自行配置)

https://bing.github1s.tk
站点1:https://bing.github1s.tk

站点2:https://bingo.weaigc.repl.co


[![img](./docs/images/demo.png)](https://bing.github1s.tk)
Expand Down Expand Up @@ -46,41 +47,36 @@ https://bing.github1s.tk
- [ ] 国际化翻译
- [ ] 支持 OpenAI API

## 一键部署
> HuggingFace 已经被封禁,新方法正在内测中,如有需要,请直接访问演示站点 https://bing.github1s.tk
## 在线部署(Replit及CF均受到不同程度的封锁,故需要配置用户信息才能使用)
### 1. 部署到 Replit

你也可以一键部署自己的 New Bing AI 到 🤗 HuggingFace 。
[点击部署Replit](https://replit.com/@weaigc/bingo?v=1)

### 部署到 Huggingface
1. 点击此图标
[![Deploy to HuggingFace](https://img.shields.io/badge/%E7%82%B9%E5%87%BB%E9%83%A8%E7%BD%B2-%F0%9F%A4%97-fff)](https://huggingface.co/login?next=%2Fspaces%2Fhf4all%2Fbingo%3Fduplicate%3Dtrue%26visibility%3Dpublic),配置可以不改。

2. 部署署完成后,点击“设置” 》“站点域名”,点一下,复制一下 HF 域名信息,然后分享给别人即可。

> Huggingface 不支持绑定自己的域名,不过我们可以使用曲线救国的方式来达到这个目的
> 1. 方式二,借助 Cloudflare Workers [部署Cloudflare Workers](#使用Cloudflare-Workers自定义域名)
> 2. 方式一,借助 Github Pages 及 iframe [如何绑定域名](https://github.com/weaigc/bingo/issues/4)
### 使用Cloudflare Workers自定义域名

> 核心代码 [worker.js](./cloudflare/worker.js)
### 2. 部署到 CloudFlare (需要有自己的域名才可以)

- [注册 Cloudflare 账号](https://dash.cloudflare.com/sign-up)

- 添加一个新的网站,需要你有自己的域名并且将域名`Name Server`托管给 Cloudflare 才行(更多信息可自行 Google)

- 通过左侧菜单进入「Workers」,并点击「Create a Worker」。

- 创建 Worker 服务,复制 [worker.js](./cloudflare/worker.js) 全部代码,粘贴至创建的服务中,根据注释进行改动,保存并部署。
- 创建 Worker 服务,复制 [worker.js](./cloudflare/worker.js) 全部代码,修改第一行 `SITE_HOST` 为自己的域名,粘贴至创建的服务中,根据注释进行改动,保存并部署。

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

### 部署其它平台
<details>
<summary>
由于其他平台目前遭到 New Bing 封杀,会遇到很多问题,不再做推荐,有需要的可以自行查看
由于其他平台目前遭到 New Bing 比较彻底的封杀,会遇到很多问题,不再做推荐,有需要的可以自行查看
</summary>

#### 部署到 HuggingFace
1. 点击此图标
[![Deploy to HuggingFace](https://img.shields.io/badge/%E7%82%B9%E5%87%BB%E9%83%A8%E7%BD%B2-%F0%9F%A4%97-fff)](https://huggingface.co/login?next=%2Fspaces%2Fhf4all%2Fbingo%3Fduplicate%3Dtrue%26visibility%3Dpublic),配置可以不改。

2. 部署署完成后,点击“设置” 》“站点域名”,点一下,复制一下 HF 域名信息,然后分享给别人即可。


#### 部署到 Netlify
[![Deploy to Netlify Button](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/weaigc/bingo)

Expand All @@ -101,7 +97,6 @@ https://bing.github1s.tk

## 安装和使用

> 由于目前微软封杀比较严重,推荐优先使用 [部署 Huggingface](#部署到-huggingface)
> 本地部署需要你本机或服务器 IP 是国外 IP ,否则会无法连接到 New Bing。
* 使用 Node 启动
Expand All @@ -125,7 +120,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
13 changes: 13 additions & 0 deletions cloudflare/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { spawn } = require('child_process');
const { join } = require('path');

const miniflare = join(require.resolve('miniflare'), '../cli.js')
const script = join(__dirname, './worker.js');

const proc = spawn(`node`, ['--experimental-vm-modules', miniflare, script, '-w', '-d', '-m', '-p', process.env.PORT || 8080])
proc.stdout.on('data', (chunk) => {
process.stdout.write(chunk)
})
proc.stderr.on('data', (chunk) => {
process.stderr.write(chunk)
})
87 changes: 76 additions & 11 deletions cloudflare/worker.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,83 @@
const TRAGET_HOST='hf4all-bingo.hf.space' // 请将此域名改成你自己的,域名信息在设置》站点域名查看。
const SITE_HOST = '这城改为你的域名' // 为空则自动推断
const TARGET_HOST='hf4all-bingo.hf.space' // 后台服务,默认不需要修改

export default {
async fetch(request) {
const uri = new URL(request.url);
if (uri.protocol === 'http:') {
uri.protocol = 'https:';
return new Response('', {
status: 301,
async handleOptions(request) {
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS',
'Access-Control-Max-Age': '86400',
}

if (
request.headers.get('Origin') !== null &&
request.headers.get('Access-Control-Request-Method') !== null &&
request.headers.get('Access-Control-Request-Headers') !== null
) {
return new Response(null, {
headers: {
location: uri.toString(),
...corsHeaders,
'Access-Control-Allow-Headers': request.headers.get(
'Access-Control-Request-Headers'
),
},
})
} else {
return new Response(null, {
headers: {
Allow: 'GET, HEAD, POST, OPTIONS',
},
})
}
},

async handleWebSocket(headers) {
headers.set('Host', 'sydney.bing.com')
return fetch('https://sydney.bing.com/sydney/ChatHub', {
headers
})
},

async fetch(request) {
const uri = new URL(request.url)
console.log('uri', uri.toString())
if (request.method === 'OPTIONS') {
return this.handleOptions(request)
}
const headersObj = {}
for (const [key, value] of request.headers.entries()) {
if (key.startsWith('cf-') || key.startsWith('x-') || ['connection', 'origin', 'referer', 'host', 'authority'].includes(key)) continue
headersObj[key] = value
}
headersObj['x-forwarded-for'] = request.headers.get('x-forwarded-for')?.split(',')?.[0]
if (!headersObj['x-forwarded-for']) {
delete headersObj['x-forwarded-for']
}
headersObj['x-ms-useragent'] = request.headers.get('x-ms-useragent') || 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.3 OS/Win32'
headersObj['referer'] = 'https://www.bing.com/search?q=Bing+AI'
const headers = new Headers(headersObj)
console.log('headers', headersObj)

const upgradeHeader = headers.get('Upgrade')
if (upgradeHeader === 'websocket') {
return this.handleWebSocket(headers)
}
if (uri.pathname.startsWith('/turing/')) {
uri.host = 'www.bing.com'
} else {
if (uri.protocol === 'http:' && !/^[0-9.:]+$/.test(TARGET_HOST)) {
uri.protocol = 'https:';
}
headers.set('x-endpoint', SITE_HOST || uri.host)
headers.set('x-ws-endpoint', SITE_HOST || uri.host)
uri.host = TARGET_HOST
}
uri.host = TRAGET_HOST
return fetch(new Request(uri.toString(), request));
headers.set('Host', uri.host)
return fetch(uri.toString(), {
headers,
method: request.method,
redirect: request.redirect,
body: request.body,
})
},
};
}
Loading

0 comments on commit 4b6ef0a

Please sign in to comment.