forked from jianjianai/NewBingGoGo-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudflareWorker.js
293 lines (273 loc) · 10.1 KB
/
cloudflareWorker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
let joinStats = true; //可选加入统计。 加入统计不会收集任何隐私信息,仅统计访问量。
let webPath = 'https://raw.githubusercontent.com/jianjianai/NewBingGoGo-Web/master/src/main/resources'; //web页面地址,可以修改成自己的仓库来自定义前端页面
let serverConfig = {
"h1": "NewBingGoGo",
"h2": "简单开始和NewBing聊天",
"p":"",
"firstMessages":[
"好的,我已清理好板子,可以重新开始了。我可以帮助你探索什么?",
"明白了,我已经抹去了过去,专注于现在。我们现在应该探索什么?",
"重新开始总是很棒。问我任何问题!",
"好了,我已经为新的对话重置了我的大脑。你现在想聊些什么?",
"很好,让我们来更改主题。你在想什么?",
"谢谢你帮我理清头绪! 我现在能帮你做什么?",
"没问题,很高兴你喜欢上一次对话。让我们转到一个新主题。你想要了解有关哪些内容的详细信息?",
"谢谢你! 知道你什么时候准备好继续前进总是很有帮助的。我现在能为你回答什么问题?",
"当然,我已准备好进行新的挑战。我现在可以为你做什么?"
],
"firstProposes":[
"教我一个新单词",
"我需要有关家庭作业的帮助",
"我想学习一项新技能",
"最深的海洋是哪个?",
"一年有多少小时?",
"宇宙是如何开始的?",
"寻找非虚构作品",
"火烈鸟为何为粉色?",
"有什么新闻?",
"让我大笑",
"给我看鼓舞人心的名言",
"世界上最小的哺乳动物是什么?",
"向我显示食谱",
"最深的海洋是哪个?",
"为什么人类需要睡眠?",
"教我有关登月的信息",
"我想学习一项新技能",
"如何创建预算?",
"给我说个笑话",
"全息影像的工作原理是什么?",
"如何设定可实现的目标?",
"金字塔是如何建成的?",
"激励我!",
"宇宙是如何开始的?",
"如何制作蛋糕?"
]
}
let cookies = [
""
]
export default {
async fetch(request, _env) {
return await handleRequest(request);
}
}
let serverConfigString = JSON.stringify(serverConfig);
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
let url = new URL(request.url);
let path = url.pathname;
if(path === '/challenge'){//过验证的接口
let r = url.searchParams.get('redirect');
if (r){
return getRedirect(r);
}
return new Response(`验证成功`,{
status: 200,
statusText: 'ok',
headers: {
"content-type": "text/html; charset=utf-8"
}
})
}
if (path === '/sydney/ChatHub') { //魔法聊天
return goChatHub(request);
}
if (path === "/turing/conversation/create") { //创建聊天
return goUrl(request, "https://www.bing.com/turing/conversation/create",{
"referer":"https://www.bing.com/search?q=Bing+AI"
});
}
if(path==="/edgesvc/turing/captcha/create"){//请求验证码图片
return goUrl(request,"https://edgeservices.bing.com/edgesvc/turing/captcha/create",{
"referer":"https://edgeservices.bing.com/edgesvc/chat?udsframed=1&form=SHORUN&clientscopes=chat,noheader,channelstable,&shellsig=709707142d65bbf48ac1671757ee0fd1996e2943&setlang=zh-CN&lightschemeovr=1"
});
}
if(path==="/edgesvc/turing/captcha/verify"){//提交验证码
return goUrl(request,"https://edgeservices.bing.com/edgesvc/turing/captcha/verify?"+ url.search,{
"referer":"https://edgeservices.bing.com/edgesvc/chat?udsframed=1&form=SHORUN&clientscopes=chat,noheader,channelstable,&shellsig=709707142d65bbf48ac1671757ee0fd1996e2943&setlang=zh-CN&lightschemeovr=1"
});
}
if (path.startsWith('/msrewards/api/v1/enroll')) { //加入候补
return goUrl(request, "https://www.bing.com/msrewards/api/v1/enroll" + url.search);
}
if (path === '/images/create') { //AI画图
return goUrl(request, "https://www.bing.com/images/create" + url.search, {
"referer": "https://www.bing.com/search?q=bingAI"
});
}
if (path.startsWith('/images/create/async/results')) { //请求AI画图图片
url.hostname = "www.bing.com";
return goUrl(request, url.toString(), {
"referer": "https://www.bing.com/images/create?partner=sydney&showselective=1&sude=1&kseed=7000"
});
}
if (path.startsWith('/rp')) { //显示AI画图错误提示图片
url.hostname = "www.bing.com";
return goUrl(request, url.toString(), {
"referer": "https://www.bing.com/search?q=bingAI"
});
}
//用于测试
if (path.startsWith("/test/")) {
let a = path.replace("/test/",'');
return goUrl(request, a);
}
//请求服务器配置
if(path==='/web/resource/config.json'){
return new Response(serverConfigString,{
status: 200,
statusText: 'ok',
headers: {
"content-type": "application/x-javascript; charset=utf-8",
"cache-control":"max-age=14400"
}
})
}
if (path.startsWith("/web/")||path === "/favicon.ico") { //web请求
if(!joinStats){
if(path==="/web/js/other/stats.js"){
return new Response("console.log(\"未加入统计\");",{
status: 200,
statusText: 'ok',
headers: {
"content-type": "application/x-javascript; charset=utf-8",
"cache-control":"max-age=14400"
}
})
}
}
let a = `${webPath}${path}`;
return await goWeb(a);
}
return getRedirect('/web/NewBingGoGo.html');
}
async function goWeb(path) {
let res = await fetch(path);
let mimeType;
if (path.endsWith(".html")) {
mimeType = "text/html; charset=utf-8";
} else if (path.endsWith(".js")) {
mimeType = "application/x-javascript; charset=utf-8";
} else if (path.endsWith(".css")) {
mimeType = "text/css; charset=utf-8";
} else if (path.endsWith(".png")) {
mimeType = "image/png";
} else if (path.endsWith(".ico")) {
mimeType = "image/png";
}
return new Response(res.body, {
status: 200,
statusText: 'ok',
headers: {
"content-type": mimeType,
"cache-control":"max-age=14400"
}
});
}
async function goChatHub(request){
let url = new URL(request.url);
//构建 fetch 参数
let fp = {
method: request.method,
headers: {
"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.57",
"Host":"sydney.bing.com",
"Origin":"https://www.bing.com"
}
}
//保留头部信息
let reqHeaders = request.headers;
let dropHeaders = ["Accept-Language","Accept-Encoding","Connection","Upgrade"];
for (let h of dropHeaders) {
if (reqHeaders.has(h)) {
fp.headers[h] = reqHeaders.get(h);
}
}
let randomAddress = url.searchParams.get("randomAddress");
if(randomAddress){
fp.headers["X-forwarded-for"] = randomAddress;
}
let res = await fetch("https://sydney.bing.com/sydney/ChatHub", fp);
return new Response(res.body, res);
}
//请求某地址
async function goUrl(request, url, addHeaders) {
//构建 fetch 参数
let fp = {
method: request.method,
headers: {}
}
//保留头部信息
let reqHeaders = request.headers;
let dropHeaders = ["accept", "accept-language","accept-encoding"];
for (let h of dropHeaders) {
if (reqHeaders.has(h)) {
fp.headers[h] = reqHeaders.get(h);
}
}
fp.headers["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.57"
//客户端指定的随机地址
let randomAddress = reqHeaders.get("randomAddress");
if(!randomAddress){
randomAddress = "12.24.144.227";
}
//添加X-forwarded-for
fp.headers['x-forwarded-for'] = randomAddress;
if (addHeaders) {
//添加头部信息
for (let h in addHeaders) {
fp.headers[h] = addHeaders[h];
}
}
let cookieID = 0;
if(reqHeaders.get('NewBingGoGoWeb')){//如果是web版
//添加配置的随机cookie
if (cookies.length === 0) {
return getReturnError("没有任何可用cookie,请前在第一行代码cookies变量中添加cookie");
}
cookieID = Math.floor(Math.random() * cookies.length);
let userCookieID = reqHeaders.get("cookieID");
if (userCookieID) {
if (userCookieID >= 0 && userCookieID <= cookies.length-1) {
cookieID = userCookieID;
} else {
return getReturnError("cookieID不存在,请刷新页面测试!");
}
}
fp.headers["cookie"] = cookies[cookieID];
}else {//如果是插件版
fp.headers["cookie"] = reqHeaders.get('cookie');
}
let res = await fetch(url, fp);
let newRes = new Response(res.body,res);
newRes.headers.set("cookieID",`${cookieID}`);
return newRes;
}
//获取用于返回的错误信息
function getReturnError(error) {
return new Response(JSON.stringify({
value: 'error',
message: error
}), {
status: 200,
statusText: 'ok',
headers: {
"content-type": "application/json",
"NewBingGoGoError":'true'
}
})
}
//返回重定向
function getRedirect(url) {
return new Response("正在重定向到" + url, {
status: 302,
statusText: 'redirect',
headers: {
"content-type": "text/html",
"location": url
}
})
}