forked from weaigc/bingo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
687 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
}, | ||
}; | ||
} |
Oops, something went wrong.