Skip to content

Commit 3b457bf

Browse files
committed
Merge branch 'pr/jianjianai/440-2' into dev
2 parents 7e37dfb + 5289c35 commit 3b457bf

File tree

13 files changed

+789
-609
lines changed

13 files changed

+789
-609
lines changed

.github/workflows/cloudflare-worker-deploy.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ jobs:
1212
timeout-minutes: 60
1313
steps:
1414
- uses: actions/checkout@v2
15+
- name: Setup pnpm
16+
uses: pnpm/[email protected]
17+
with:
18+
run_install: true
19+
version: 9
20+
- name: build - 构建
21+
run: pnpm run build-worker
1522
- name: Build & Deploy Worker
1623
uses: cloudflare/wrangler-action@v3
1724
with:

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212
.DS_Store
1313

1414
release
15+
node_modules
16+
/_worker.js
17+
/worker.js

cloudflare/build.sh

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
#!/bin/bash
22

3-
cp -r web cloudflare/web
4-
5-
rm cloudflare/web/web.go
6-
rm -rf go-proxy-bingai
7-
3+
npm install
4+
npm run build-page
85
shopt -s extglob
9-
rm -rf !(cloudflare)
10-
mv cloudflare/* .
11-
rm -rf cloudflare build.sh
12-
13-
sed -i "s/if (currentUrl.pathname === '\/' || currentUrl.pathname.indexOf('\/web\/') === 0) {/if (currentUrl.pathname === '\/') {/g" worker.js
14-
sed -i "s/return home(currentUrl.pathname);/let res = new Response('', {\n status: 302,\n });\n res.headers.set('location', '\/web\/');\n return res;\n }\n if (currentUrl.pathname.indexOf('\/web\/') === 0) {\n return env.ASSETS.fetch(request);/g" worker.js
6+
rm -rf !(_worker.js|web)
157

16-
mv worker.js _worker.js

cloudflare/page.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { init_CUSTOM_OPTIONS } from "./src/OPTIONS"
2+
import { workerFetch } from './src/worker'
3+
4+
5+
export default {
6+
/**
7+
* fetch
8+
* @param {Request} request
9+
* @param {*} env
10+
* @param {*} ctx
11+
* @returns
12+
*/
13+
async fetch(request, env, ctx) {
14+
init_CUSTOM_OPTIONS(env);
15+
const currentUrl = new URL(request.url);
16+
if (currentUrl.pathname === '/') {
17+
let res = new Response('', {
18+
status: 302,
19+
});
20+
res.headers.set('location', '/web/');
21+
return res;
22+
}
23+
if (currentUrl.pathname.indexOf('/web/') === 0) {
24+
return env.ASSETS.fetch(request);
25+
}
26+
return workerFetch(request, env, ctx);
27+
}
28+
}

cloudflare/rollup.config.pages.mjs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import obfuscator from 'rollup-plugin-obfuscator';
2+
import compiler from '@ampproject/rollup-plugin-closure-compiler';
3+
4+
// rollup.config.mjs
5+
// ---cut-start---
6+
/** @type {import('rollup').RollupOptions} */
7+
// ---cut-end---
8+
export default {
9+
input: 'cloudflare/page.js',
10+
output: [
11+
{
12+
file: '_worker.js',
13+
format: 'es'
14+
}
15+
],
16+
plugins:[
17+
obfuscator({
18+
// global:false,
19+
// include:["cloudflare/src/OPTIONS.js","cloudflare/src/bingapi.js"],
20+
options: {
21+
// Your javascript-obfuscator options here
22+
// See what's allowed: https://github.com/javascript-obfuscator/javascript-obfuscator
23+
compact: false,
24+
controlFlowFlattening: false,
25+
deadCodeInjection: false,
26+
debugProtection: false,
27+
debugProtectionInterval: 0,
28+
disableConsoleOutput: false,
29+
identifierNamesGenerator: 'mangled',
30+
log: false,
31+
numbersToExpressions: false,
32+
renameGlobals: false,
33+
selfDefending: false,
34+
simplify: true,
35+
// splitStrings: true,
36+
// splitStringsChunkLength:10,
37+
stringArray: true,
38+
stringArrayEncoding: ['base64'],
39+
stringArrayIndexShift: true,
40+
stringArrayRotate: true,
41+
stringArrayShuffle: true,
42+
stringArrayWrappersCount: 1,
43+
stringArrayWrappersChainedCalls: true,
44+
stringArrayWrappersParametersMaxCount: 2,
45+
stringArrayWrappersType: 'variable',
46+
stringArrayThreshold: 1,
47+
unicodeEscapeSequence: false
48+
},
49+
}),
50+
compiler({
51+
compilation_level:"ADVANCED"
52+
})
53+
]
54+
};

cloudflare/rollup.config.workers.mjs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import obfuscator from 'rollup-plugin-obfuscator';
2+
import compiler from '@ampproject/rollup-plugin-closure-compiler';
3+
4+
// rollup.config.mjs
5+
// ---cut-start---
6+
/** @type {import('rollup').RollupOptions} */
7+
// ---cut-end---
8+
export default {
9+
input: 'cloudflare/worker.js',
10+
output: [
11+
{
12+
file: 'worker.js',
13+
format: 'es',
14+
}
15+
],
16+
plugins:[
17+
obfuscator({
18+
// global:false,
19+
// include:["cloudflare/src/OPTIONS.js","cloudflare/src/bingapi.js"],
20+
options: {
21+
// Your javascript-obfuscator options here
22+
// See what's allowed: https://github.com/javascript-obfuscator/javascript-obfuscator
23+
compact: false,
24+
controlFlowFlattening: false,
25+
deadCodeInjection: false,
26+
debugProtection: false,
27+
debugProtectionInterval: 0,
28+
disableConsoleOutput: false,
29+
identifierNamesGenerator: 'mangled',
30+
log: false,
31+
numbersToExpressions: false,
32+
renameGlobals: false,
33+
selfDefending: false,
34+
simplify: true,
35+
// splitStrings: true,
36+
// splitStringsChunkLength:10,
37+
stringArray: true,
38+
stringArrayEncoding: ['base64'],
39+
stringArrayIndexShift: true,
40+
stringArrayRotate: true,
41+
stringArrayShuffle: true,
42+
stringArrayWrappersCount: 1,
43+
stringArrayWrappersChainedCalls: true,
44+
stringArrayWrappersParametersMaxCount: 2,
45+
stringArrayWrappersType: 'variable',
46+
stringArrayThreshold: 1,
47+
unicodeEscapeSequence: false
48+
},
49+
}),
50+
compiler({
51+
compilation_level:"ADVANCED"
52+
})
53+
]
54+
};

cloudflare/src/OPTIONS.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// 同查找 _U 一样, 查找 KievRPSSecAuth 的值并替换下方的xxx
2+
export const CUSTOM_OPTIONS = {
3+
KievRPSSecAuth: '',
4+
_RwBf: '',
5+
MUID: '',
6+
_U: '',
7+
8+
BYPASS_SERVER: '',
9+
APIKEY: '',
10+
Go_Proxy_BingAI_BLANK_API_KEY: false,
11+
12+
Go_Proxy_BingAI_AUTH_KEY: '',
13+
14+
INFO: '',
15+
NIGHTLY: false,
16+
}
17+
18+
export const WEB_CONFIG = {
19+
WORKER_URL: '', // 如无特殊需求请,保持为''
20+
};
21+
22+
23+
export const RAND_IP_COOKIE_NAME = 'BingAI_Rand_IP';
24+
export const AUTH_KEY_COOKIE_NAME = 'BingAI_Auth_Key';
25+
26+
export const SYDNEY_ORIGIN = 'https://sydney.bing.com';
27+
export const BING_ORIGIN = 'https://www.bing.com';
28+
export const BING_SOURCE_ORIGIN = 'https://th.bing.com';
29+
export const BING_SR_ORIGIN = 'https://sr.bing.com';
30+
export const EDGE_ORIGIN = 'https://edgeservices.bing.com';
31+
export const DESIGNER_ORIGIN = 'https://designer.microsoft.com';
32+
export const DESIGNER_CDN_ORIGIN = 'https://cdn.designerapp.osi.office.net';
33+
export const DESIGNER_APP_ORIGIN = 'https://designerapp.officeapps.live.com';
34+
export const DESIGNER_APP_EDOG_ORIGIN = 'https://designerapp.edog.officeapps.live.com';
35+
export const DESIGNER_DOCUMENT_ORIGIN = 'https://document.designerapp.officeapps.live.com';
36+
export const DESIGNER_USERASSETS_ORIGIN = 'https://userassets.designerapp.officeapps.live.com';
37+
export const DESIGNER_MEDIASUGGESTION_ORIGIN = 'https://mediasuggestion.designerapp.officeapps.live.com';
38+
export const DESIGNER_RTC_ORIGIN = 'https://rtc.designerapp.officeapps.live.com';
39+
export const KEEP_REQ_HEADERS = [
40+
'accept',
41+
'accept-encoding',
42+
'accept-language',
43+
'authorization',
44+
'connection',
45+
'cookie',
46+
'upgrade',
47+
'user-agent',
48+
'sec-websocket-extensions',
49+
'sec-websocket-key',
50+
'sec-websocket-version',
51+
'x-request-id',
52+
'content-length',
53+
'content-type',
54+
'access-control-request-headers',
55+
'access-control-request-method',
56+
'sec-ms-gec',
57+
'sec-ms-gec-version',
58+
'x-client-data',
59+
'x-ms-client-request-id',
60+
'x-ms-useragent',
61+
];
62+
63+
64+
65+
66+
export function init_CUSTOM_OPTIONS(env) {
67+
CUSTOM_OPTIONS.KievRPSSecAuth = env.USER_KievRPSSecAuth || '';
68+
CUSTOM_OPTIONS._RwBf = env.USER_RwBf || '';
69+
CUSTOM_OPTIONS.MUID = env.USER_MUID || '';
70+
CUSTOM_OPTIONS._U = env.Go_Proxy_BingAI_USER_TOKEN || '';
71+
CUSTOM_OPTIONS.BYPASS_SERVER = env.BYPASS_SERVER || '';
72+
CUSTOM_OPTIONS.APIKEY = env.APIKEY || '';
73+
CUSTOM_OPTIONS.Go_Proxy_BingAI_BLANK_API_KEY = (env.Go_Proxy_BingAI_BLANK_API_KEY != '' && env.Go_Proxy_BingAI_BLANK_API_KEY != undefined && env.Go_Proxy_BingAI_BLANK_API_KEY != null);
74+
CUSTOM_OPTIONS.INFO = env.INFO || '';
75+
CUSTOM_OPTIONS.NIGHTLY = (env.NIGHTLY != '' && env.NIGHTLY != undefined && env.NIGHTLY != null);
76+
CUSTOM_OPTIONS.Go_Proxy_BingAI_AUTH_KEY = env.Go_Proxy_BingAI_AUTH_KEY || '';
77+
}
File renamed without changes.

cloudflare/src/rewriteBody.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { WEB_CONFIG, BING_ORIGIN, BING_SOURCE_ORIGIN, EDGE_ORIGIN, DESIGNER_ORIGIN, DESIGNER_CDN_ORIGIN, DESIGNER_APP_ORIGIN , DESIGNER_APP_EDOG_ORIGIN , DESIGNER_DOCUMENT_ORIGIN , DESIGNER_USERASSETS_ORIGIN , DESIGNER_MEDIASUGGESTION_ORIGIN, DESIGNER_RTC_ORIGIN } from "./OPTIONS.js"
2+
3+
const replaceURL = (body) => {
4+
body = body.replaceAll(BING_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", ""));
5+
body = body.replaceAll(EDGE_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", ""));
6+
body = body.replaceAll(BING_SOURCE_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", "") + '/th');
7+
body = body.replaceAll(DESIGNER_CDN_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", "") + '/designer-cdn');
8+
body = body.replaceAll(DESIGNER_APP_EDOG_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", "") + '/designer-app-edog');
9+
body = body.replaceAll(DESIGNER_DOCUMENT_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", "") + '/designer-document');
10+
body = body.replaceAll(DESIGNER_USERASSETS_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", "") + '/designer-userassets');
11+
body = body.replaceAll(DESIGNER_MEDIASUGGESTION_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", "") + '/designer-mediasuggestion');
12+
body = body.replaceAll(DESIGNER_RTC_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", "") + '/designer-rtc');
13+
body = body.replaceAll(DESIGNER_APP_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", "") + '/designer-app');
14+
body = body.replaceAll(DESIGNER_ORIGIN.replace("http://", "").replace("https://", ""), WEB_CONFIG.WORKER_URL.replace("http://", "").replace("https://", "") + '/designer');
15+
return body
16+
}
17+
18+
export const rewriteBody = async (res) => {
19+
const content_type = res.headers.get("Content-Type") || "";
20+
let encoding = null;
21+
let body = res.body;
22+
if (content_type.startsWith("text/html") || res.url.endsWith("js")) {
23+
let decodedContent = new TextDecoder("utf-8").decode(new Int8Array(await res.clone().arrayBuffer()));
24+
if (decodedContent) {
25+
// @ts-ignore
26+
body = replaceURL(decodedContent);
27+
}
28+
}
29+
return { body, encoding };
30+
}

0 commit comments

Comments
 (0)