addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Fetch and log a request
* @param {Request} request
*/
const ErrorPageHotLink = `
<!DOCTYPE html>
<html>
<body>
<h1>Error Page</h1>
<p>此网站的管理员禁止盗链此资源</p>
</body>
</html>
`;
// 防盗链错误页面
async function rawHtmlResponse(html) {
const init = {
status: 403,
headers: {
'content-type': 'text/html;charset=UTF-8',
},
};
return new Response(html, init);
}
async function handleRequest(request) {
const refer = request.headers.get('referer')
// 自定义判断refer来进行错误页面返回
if (refer === null || refer === '') {
return rawHtmlResponse(ErrorPageHotLink);
}
const response = await fetch(request);
return response;
}
自定义防盗链
暂无讨论,说说你的看法吧