-
-
Notifications
You must be signed in to change notification settings - Fork 216
ascii2d 403 解决方案
神代綺凛 edited this page Dec 1, 2024
·
2 revisions
通过 Cloudflare Worker 反代可解决问题,之后将 Worker 域名填写到配置项 ascii2dHost
即可
const ASCII2D_HOST = 'ascii2d.net';
/**
* @param {Request} request
*/
async function handleRequest(request) {
const url = new URL(request.url);
const origHost = url.host;
url.host = ASCII2D_HOST;
const response = await fetch(url, getRequestInit(request));
if ([301, 302, 303, 307, 308].includes(response.status)) {
return getRedirectResponse(origHost, response);
}
return response;
}
/**
* @param {Request} request
* @returns {RequestInit}
*/
function getRequestInit({ headers, method, body }) {
return {
headers: new Headers([...headers].filter(([name]) => !name.startsWith('cf-'))),
method,
body,
redirect: 'manual',
};
}
/**
* @param {string} host
* @param {Response} response
* @returns {Response}
*/
function getRedirectResponse(host, { headers, body, status, statusText }) {
const newLocation = new URL(headers.get('location'));
newLocation.host = host;
return new Response(body, {
status,
statusText,
headers: {
...Object.fromEntries(headers),
location: newLocation.href,
},
});
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
没有的请自行注册,使用免费计划,不需要付费/绑卡等
首次进入 Worker 可能会要进行一系列初始设置操作,例如 Worker 子域取名等,自行完成即可
- 点创建
- 继续点创建
- 取个名,默认名称也没关系,点部署
- 点编辑代码
- 将上方给出的代码复制粘贴覆盖左侧编辑区原先所有代码,点部署,等待部署完成后点一下刷新,如果能成功加载出 ascii2d 主页,那么部署成功(可能需要等一段时间,新域名的 SSL 证书签发需要一定时间)
- 将该 Worker 的域名形如 xxx.yyy.workers.dev 填到机器人配置项
ascii2dHost
,重载机器人配置,大功告成