Cloudflare has a very useful feature called Workers, which allows you to run programs in a serverless state, including common reverse proxies.
So I use this feature to proxy gravatar and achieve the goal of accelerating domestic access.
Without further ado, let's start the deployment process.
1. After logging in, click on Workers
on the homepage, and then click on Create Service
on the left side.
2. Fill in the service name as you like, and then click on create in the lower right corner.
3. Click on Quick Edit
in the upper right corner
4. Enter the following code in the left editor
Click to expand the code
// Replace with the site you want to mirror
const upstream = 'gravatar.com'
// If the site has a dedicated mobile adaptation site, otherwise keep it the same as above
const upstream_mobile = 'gravatar.com'
// Countries you want to block
const blocked_region = []
// Block self-access
const blocked_ip_address = []
// Replace with the site you want to mirror
const replace_dict = {
'$upstream': '$custom_domain',
'//gravatar.com': ''
}
// The following content does not need to be modified
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request));
})
async function fetchAndApply(request) {
const region = request.headers.get('cf-ipcountry').toUpperCase();
const ip_address = request.headers.get('cf-connecting-ip');
const user_agent = request.headers.get('user-agent');
let response = null;
let url = new URL(request.url);
let url_host = url.host;
if (url.protocol == 'http:') {
url.protocol = 'https:'
response = Response.redirect(url.href);
return response;
}
if (await device_status(user_agent)) {
upstream_domain = upstream
} else {
upstream_domain = upstream_mobile
}
url.host = upstream_domain;
if (blocked_region.includes(region)) {
response = new Response('Access denied: WorkersProxy is not available in your region yet.', {
status: 403
});
} else if(blocked_ip_address.includes(ip_address)){
response = new Response('Access denied: Your IP address is blocked by WorkersProxy.', {
status: 403
});
} else{
let method = request.method;
let request_headers = request.headers;
let new_request_headers = new Headers(request_headers);
new_request_headers.set('Host', upstream_domain);
new_request_headers.set('Referer', url.href);
let original_response = await fetch(url.href, {
method: method,
headers: new_request_headers
})
let original_response_clone = original_response.clone();
let original_text = null;
let response_headers = original_response.headers;
let new_response_headers = new Headers(response_headers);
let status = original_response.status;
new_response_headers.set('access-control-allow-origin', '*');
new_response_headers.set('access-control-allow-credentials', true);
new_response_headers.delete('content-security-policy');
new_response_headers.delete('content-security-policy-report-only');
new_response_headers.delete('clear-site-data');
const content_type = new_response_headers.get('content-type');
if (content_type.includes('text/html') && content_type.includes('UTF-8')) {
original_text = await replace_response_text(original_response_clone, upstream_domain, url_host);
} else {
original_text = original_response_clone.body
}
response = new Response(original_text, {
status,
headers: new_response_headers
})
}
return response;
}
async function replace_response_text(response, upstream_domain, host_name) {
let text = await response.text()
var i, j;
for (i in replace_dict) {
j = replace_dict[i]
if (i == '$upstream') {
i = upstream_domain
} else if (i == '$custom_domain') {
i = host_name
}
if (j == '$upstream') {
j = upstream_domain
} else if (j == '$custom_domain') {
j = host_name
}
let re = new RegExp(i, 'g')
text = text.replace(re, j);
}
return text;
}
async function device_status (user_agent_info) {
var agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
var flag = true;
for (var v = 0; v < agents.length; v++) { if (user_agent_info.indexOf(agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
Then click on Save and Deploy
, and you can successfully proxy the gravatar mirror.
Then you can follow the same steps to proxy any website. I used https://zsh.im to proxy my domestic blog https://zburu.com.
It is relatively easy to access with a delay of about 100+ms.
5. Custom domain
In the trigger, you can find the custom domain option, enter the domain name that has been bound to Cloudflare. Once again, praise Cloudflare. If the domain name is bound in advance, you can directly enter the subdomain here, and Cloudflare will automatically resolve it.
Bind the domain name in the Websites
section on the left side of the homepage.
# Share my free mirror.
gravatar.zsh.im/avatar