22 lines
735 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 泛域名反代
// 在环境变量中添加 @域名前缀,例如@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
}));
}
};