CF:自定义Hotlink Protection

由于图床和博客可能使用不同的顶级域名,因此CF自带的Hotlink Protection不能用了,需要在Scrape Shield里关闭它,然后利用Workers来实现可以自定义的Hotlink Protection

来源

https://developers.cloudflare.com/workers/examples/hot-link-protection/

模板修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const HOMEPAGE_URL = 'https://occdn.limour.top/';
const PROTECTED_TYPE = 'image/';

async function handleRequest(request) {
// Fetch the original request
const response = await fetch(request);

// If it's an image, engage hotlink protection based on the
// Referer header.
const referer = request.headers.get('Referer');
const contentType = response.headers.get('Content-Type') '';

if (referer && contentType.startsWith(PROTECTED_TYPE)) {
// If the hostnames don't match, it's a hotlink
let refHostname = new URL(referer).hostname
if (!refHostname.endsWith('limour.top') && !refHostname.endsWith('j11.fun')) {
// Redirect the user to your website
return Response.redirect(HOMEPAGE_URL, 302);
}
}

// Everything is fine, return the response normally.
return response;
}

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});

CF:自定义Hotlink Protection
https://occdn.limour.top/1903.html
Author
Limour
Posted on
June 29, 2022
Licensed under