先创建一个worker填入以下内容
// 域名前缀映射配置
const domain_mappings = {
'youdomain.com': '最终访问头.',
//例如:
//'airv.us': 'www.',
//则你设置Worker路由为www.*都将会反代到airv.us
};
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
const current_host = url.host;
// 强制使用 HTTPS
if (url.protocol === 'http:') {
url.protocol = 'https:';
return Response.redirect(url.href, 301);
}
const host_prefix = getProxyPrefix(current_host);
if (!host_prefix) {
return new Response('Proxy prefix not matched', { status: 404 });
}
// 查找对应目标域名
let target_host = null;
for (const [origin_domain, prefix] of Object.entries(domain_mappings)) {
if (host_prefix === prefix) {
target_host = origin_domain;
break;
}
}
if (!target_host) {
return new Response('No matching target host for prefix', { status: 404 });
}
// 构造目标 URL
const new_url = new URL(request.url);
new_url.protocol = 'https:';
new_url.host = target_host;
// 创建新请求
const new_headers = new Headers(request.headers);
new_headers.set('Host', target_host);
new_headers.set('Referer', new_url.href);
try {
const response = await fetch(new_url.href, {
method: request.method,
headers: new_headers,
body: request.method !== 'GET' && request.method !== 'HEAD' ? request.body : undefined,
redirect: 'manual'
});
// 复制响应头并添加CORS
const response_headers = new Headers(response.headers);
response_headers.set('access-control-allow-origin', '*');
response_headers.set('access-control-allow-credentials', 'true');
response_headers.set('cache-control', 'public, max-age=600');
response_headers.delete('content-security-policy');
response_headers.delete('content-security-policy-report-only');
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: response_headers
});
} catch (err) {
return new Response(`Proxy Error: ${err.message}`, { status: 502 });
}
}
function getProxyPrefix(hostname) {
for (const prefix of Object.values(domain_mappings)) {
if (hostname.startsWith(prefix)) {
return prefix;
}
}
return null;
}然后在worker设置worker路由为
youdomain.com/*例如我此处为www.airv.us/*
最后用映射路由的域名(如www.airv.us)去cname一个优选域名
优选域名可以到https://vps789.com找一个即可
优选前 ping

优选后 ping
