Skip to content

Commit

Permalink
feat: ⚡ 可选聊天服务器、cloudflare 聊天服务器部署
Browse files Browse the repository at this point in the history
  • Loading branch information
adams549659584 committed May 25, 2023
1 parent 842e02b commit bfd58c5
Show file tree
Hide file tree
Showing 35 changed files with 2,055 additions and 1,306 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// "Go_Proxy_BingAI_SOCKS_PWD": "xxx",
// "Go_Proxy_BingAI_USER_TOKEN_1": "xxx",
// "Go_Proxy_BingAI_USER_TOKEN_2": "xxx"
// "Go_Proxy_BingAI_AUTH_KEY": "xxx",
}
}
]
Expand Down
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

基于微软 New Bing 简单定制,拥有一致的 UI 体验,支持 ChatGPT 提示词,国内可用,基本兼容微软 Bing AI 所有功能,无需登录即可畅聊。

⭐ Bing 官方聊天服务器(相对较快和稳定,推荐)不可用时, 可用 ModHeader 添加 X-Forwarded-For 请求头,对应 URL 是 wss://sydney.bing.com/sydney/ChatHub,具体可参考 https://zhuanlan.zhihu.com/p/606655303

⭐ 聊天服务器 (暂时默认 Cloudflare ) 可在右上角 设置 => 服务选择 中切换

⭐ 自定义聊天服务器参考下面 [部署聊天服务器](#部署聊天服务器) 章节

⭐ 国内可用 (部署服务器需要直连 www.bing.com 不重定向 CN ,可配置 socks 连接)

⭐ 支持现有开源提示词库
Expand All @@ -22,6 +28,7 @@
- [Railway](#Railway)
- [Vercel](#Vercel)
- [Render](#Render)
- [部署聊天服务器](#部署聊天服务器)
- [TODO](#TODO)

## 网页展示
Expand Down Expand Up @@ -104,6 +111,8 @@ Go_Proxy_BingAI_SOCKS_PWD=xxx
Go_Proxy_BingAI_USER_TOKEN_1=xxx
Go_Proxy_BingAI_USER_TOKEN_2=xxx
Go_Proxy_BingAI_USER_TOKEN_3=xxx ...
# 简单授权认证密码,可选
Go_Proxy_BingAI_AUTH_KEY=xxx
```

## 部署
Expand Down Expand Up @@ -153,7 +162,7 @@ services:

### Release

[Github Releases](https://github.com/adams549659584/go-proxy-bingai/releases) 下载适用于对应平台的压缩包,解压后可得到可执行文件 go-proxy-bingai,直接运行即可。
[GitHub Releases](https://github.com/adams549659584/go-proxy-bingai/releases) 下载适用于对应平台的压缩包,解压后可得到可执行文件 go-proxy-bingai,直接运行即可。

### Railway

Expand All @@ -176,6 +185,8 @@ RAILWAY_DOCKERFILE_PATH=docker/Dockerfile

### Vercel

> ⭐ Vercel 部署不支持 Websocket ,需选择 官方聊天服务器 或 Cloudflare
一键部署,点这里 => [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/adams549659584/go-proxy-bingai&project-name=go-proxy-bingai&repository-name=go-proxy-bingai-vercel)

![Vercel 一键部署](./docs/img/vercel-1.png)
Expand All @@ -190,11 +201,23 @@ RAILWAY_DOCKERFILE_PATH=docker/Dockerfile

![Render 域名](./docs/img/render-2.png)

## 部署聊天服务器

> 核心代码 [worker.js](./cloudflare/worker.js)
> 具体部署 Cloudflare Workers 教程自行查询,大概如下
- [注册 Cloudflare 账号](https://dash.cloudflare.com/sign-up)

- 创建 Worker 服务,复制 [worker.js](./cloudflare/worker.js) 全部代码,粘贴至创建的服务中,保存并部署。

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

## TODO

- [x] 撰写
- [x] Vue3 重构
- [x] 提示词
- [x] 历史聊天
- [ ] 导出消息到本地(Markdown、图片、PDF)
- [ ] 简单访问权限控制
- [x] 导出消息到本地(Markdown、图片、PDF)
- [x] 简单访问权限控制
10 changes: 0 additions & 10 deletions api/chathub.go

This file was deleted.

48 changes: 48 additions & 0 deletions api/helper/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package helper

import (
"adams549659584/go-proxy-bingai/common"
"encoding/json"
"net/http"
)

type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}

func CommonResult(w http.ResponseWriter, code int, msg string, data interface{}) error {
res := Response{
Code: code,
Message: msg,
Data: data,
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(res)
if err != nil {
return err
}
return nil
}

func SuccessResult(w http.ResponseWriter, data interface{}) error {
return CommonResult(w, http.StatusOK, "success", data)
}

func ErrorResult(w http.ResponseWriter, code int, msg string) error {
return CommonResult(w, code, msg, nil)
}

func UnauthorizedResult(w http.ResponseWriter) error {
return ErrorResult(w, http.StatusUnauthorized, "unauthorized")
}

func CheckAuth(r *http.Request) bool {
isAuth := true
if len(common.AUTH_KEY) > 0 {
ckAuthKey, _ := r.Cookie(common.AUTH_KEY_COOKIE_NAME)
isAuth = ckAuthKey != nil && len(ckAuthKey.Value) > 0 && common.AUTH_KEY == ckAuthKey.Value
}
return isAuth
}
5 changes: 5 additions & 0 deletions api/index.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"adams549659584/go-proxy-bingai/api/helper"
"adams549659584/go-proxy-bingai/common"
"net/http"
)
Expand All @@ -9,6 +10,10 @@ func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.Redirect(w, r, common.PROXY_WEB_PAGE_PATH, http.StatusFound)
} else {
if !helper.CheckAuth(r) {
helper.UnauthorizedResult(w)
return
}
common.NewSingleHostReverseProxy(common.BING_URL).ServeHTTP(w, r)
}
}
15 changes: 15 additions & 0 deletions api/sydney.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package api

import (
"adams549659584/go-proxy-bingai/api/helper"
"adams549659584/go-proxy-bingai/common"
"net/http"
)

func Sydney(w http.ResponseWriter, r *http.Request) {
if !helper.CheckAuth(r) {
helper.UnauthorizedResult(w)
return
}
common.NewSingleHostReverseProxy(common.BING_SYDNEY_URL).ServeHTTP(w, r)
}
24 changes: 24 additions & 0 deletions api/sysConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package api

import (
"adams549659584/go-proxy-bingai/api/helper"
"adams549659584/go-proxy-bingai/common"
"net/http"
)

type SysConfig struct {
// 是否系统配置 cookie
IsSysCK bool `json:"isSysCK"`
// 是否已授权
IsAuth bool `json:"isAuth"`
SydneyBaseUrl string `json:"sydneyBaseUrl"`
}

func SysConf(w http.ResponseWriter, r *http.Request) {
isAuth := helper.CheckAuth(r)
conf := SysConfig{
IsSysCK: len(common.USER_TOKEN_LIST) > 0,
IsAuth: isAuth,
}
helper.SuccessResult(w, conf)
}
5 changes: 5 additions & 0 deletions api/web.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"adams549659584/go-proxy-bingai/api/helper"
"adams549659584/go-proxy-bingai/common"
"adams549659584/go-proxy-bingai/web"
"net/http"
Expand All @@ -10,6 +11,10 @@ func WebStatic(w http.ResponseWriter, r *http.Request) {
if _, ok := web.WEB_PATH_MAP[r.URL.Path]; ok || r.URL.Path == common.PROXY_WEB_PREFIX_PATH {
http.StripPrefix(common.PROXY_WEB_PREFIX_PATH, http.FileServer(web.GetWebFS())).ServeHTTP(w, r)
} else {
if !helper.CheckAuth(r) {
helper.UnauthorizedResult(w)
return
}
common.NewSingleHostReverseProxy(common.BING_URL).ServeHTTP(w, r)
}
}
124 changes: 124 additions & 0 deletions cloudflare/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
const SYDNEY_ORIGIN = 'https://sydney.bing.com';
const KEEP_REQ_HEADERS = [
'accept',
'accept-encoding',
'accept-language',
'connection',
'cookie',
'upgrade',
'user-agent',
'sec-websocket-extensions',
'sec-websocket-key',
'sec-websocket-version',
'x-request-id',
'content-length',
'content-type',
'access-control-request-headers',
'access-control-request-method',
];
const IP_RANGE = [
['3.2.50.0', '3.5.31.255'], //192,000
['3.12.0.0', '3.23.255.255'], //786,432
['3.30.0.0', '3.33.34.255'], //205,568
['3.40.0.0', '3.63.255.255'], //1,572,864
['3.80.0.0', '3.95.255.255'], //1,048,576
['3.100.0.0', '3.103.255.255'], //262,144
['3.116.0.0', '3.119.255.255'], //262,144
['3.128.0.0', '3.247.255.255'], //7,864,320
];

/**
* 随机整数 [min,max)
* @param {number} min
* @param {number} max
* @returns
*/
const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min)) + min;

/**
* ip 转 int
* @param {string} ip
* @returns
*/
const ipToInt = (ip) => {
const ipArr = ip.split('.');
let result = 0;
result += +ipArr[0] << 24;
result += +ipArr[1] << 16;
result += +ipArr[2] << 8;
result += +ipArr[3];
return result;
};

/**
* int 转 ip
* @param {number} intIP
* @returns
*/
const intToIp = (intIP) => {
return `${(intIP >> 24) & 255}.${(intIP >> 16) & 255}.${(intIP >> 8) & 255}.${intIP & 255}`;
};

const getRandomIP = () => {
const randIndex = getRandomInt(0, IP_RANGE.length);
const startIp = IP_RANGE[randIndex][0];
const endIp = IP_RANGE[randIndex][1];
const startIPInt = ipToInt(startIp);
const endIPInt = ipToInt(endIp);
const randomInt = getRandomInt(startIPInt, endIPInt);
const randomIP = intToIp(randomInt);
return randomIP;
};

export default {
/**
* fetch
* @param {Request} request
* @param {*} env
* @param {*} ctx
* @returns
*/
async fetch(request, env, ctx) {
const currentUrl = new URL(request.url);
const targetUrl = new URL(SYDNEY_ORIGIN + currentUrl.pathname + currentUrl.search);

const newHeaders = new Headers();
request.headers.forEach((value, key) => {
// console.log(`old : ${key} : ${value}`);
if (KEEP_REQ_HEADERS.includes(key)) {
newHeaders.set(key, value);
}
});
newHeaders.set('host', targetUrl.host);
newHeaders.set('origin', targetUrl.origin);
newHeaders.set('referer', 'https://www.bing.com/search?q=Bing+AI');
const randIP = getRandomIP();
// console.log('randIP : ', randIP);
newHeaders.set('X-Forwarded-For', randIP);
const oldUA = request.headers.get('user-agent');
const isMobile = oldUA.includes('Mobile') || oldUA.includes('Android');
if (isMobile) {
newHeaders.set(
'user-agent',
'Mozilla/5.0 (iPhone; CPU iPhone OS 15_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.7 Mobile/15E148 Safari/605.1.15 BingSapphire/1.0.410427012'
);
} else {
newHeaders.set('user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35');
}

// newHeaders.forEach((value, key) => console.log(`${key} : ${value}`));
const newReq = new Request(targetUrl, {
method: request.method,
headers: newHeaders,
body: request.body,
});
// console.log('request url : ', newReq.url);
const res = await fetch(newReq);
return res;
// const newRes = new Response(res.body, res);
// newRes.headers.set('access-control-allow-origin', '*');
// newRes.headers.set('access-control-allow-methods', '*');
// newRes.headers.set('access-control-allow-headers', '*');
// return newRes;
},
};
4 changes: 0 additions & 4 deletions common/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2322,9 +2322,6 @@ var IP_RANGE = [][]string{
{"100.43.0.0", "100.43.95.255"}, //24,576
{"100.43.128.0", "100.63.255.255"}, //1,343,488
{"100.128.0.0", "100.255.255.255"}, //8,388,608
{"101.45.0.0", "101.45.255.255"}, //65,536
{"101.49.0.0", "101.49.127.255"}, //32,768
{"101.49.192.0", "101.49.255.255"}, //16,384
{"104.0.0.0", "104.28.7.255"}, //1,837,056
{"104.28.157.0", "104.28.255.255"}, //25,344
{"104.29.105.0", "104.30.1.255"}, //39,168
Expand Down Expand Up @@ -2926,7 +2923,6 @@ var IP_RANGE = [][]string{
{"137.167.0.0", "137.169.40.255"}, //141,568
{"137.169.43.0", "137.170.255.255"}, //120,064
{"137.173.0.0", "137.173.255.255"}, //65,536
{"137.175.0.0", "137.175.127.255"}, //32,768
{"137.176.0.0", "137.177.34.255"}, //74,496
{"137.177.36.0", "137.184.159.255"}, //490,496
{"137.184.176.0", "137.184.247.255"}, //18,432
Expand Down
Loading

3 comments on commit bfd58c5

@vercel
Copy link

@vercel vercel bot commented on bfd58c5 May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hexu1992
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assee

@hexu1992
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you help me

Please sign in to comment.