22 lines
735 B
JavaScript
22 lines
735 B
JavaScript
// 泛域名反代
|
||
// 在环境变量中添加 @域名前缀,例如@github=https://github.com/,则访问https://github.xxxx.xxx/时,实际访问https://github.com/
|
||
|
||
export default {
|
||
async fetch(request, env) {
|
||
const url = new URL(request.url);
|
||
new URL(request.url)
|
||
let name = url.hostname.split('.')[0];
|
||
let target = env['@' + name];
|
||
if (!target) {
|
||
return new Response('Not Found', {
|
||
status: 404
|
||
});
|
||
}
|
||
return fetch(new Request(target + url.pathname + url.search, {
|
||
body: request.body,
|
||
headers: request.headers,
|
||
method: request.method,
|
||
redirect: request.redirect
|
||
}));
|
||
}
|
||
}; |