|
Please translate the text into English and only return one translation result without any other characters. Do not use the words "pseudo-original" or "rewrite": "As the title suggests, the original worker code was copied from the internet.
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
// Preparation for reverse proxy purpose domain, change it to what you want
let target_url = "https://XXXX.com";
// The regular expression of content replacement
let target_url_reg = /XXXX\.com/g;
// You want to run which countries access
const allowed_region = ['CN', 'HK'];
// Block self-access
const blocked_ip_address = ['0.0.0.0', '127.0.0.1'];
async function handleRequest(request) {
const region = request.headers.get('cf-ipcountry').toUpperCase();
const ip_address = request.headers.get('cf-connecting-ip');
let url = new URL(request.url);
url.hostname = new URL(target_url).hostname;
// Copy the request object and update its attributes
let headers = new Headers(request.headers);
headers.set('Referer', target_url);
headers.set('User-Agent', request.headers.get('user-agent'));
const param = {
method: request.method,
headers: headers,
body: request.body,
redirect: 'manual'
};
// If the IP address is blocked by WorkersProxy, return an access denied response with status code 403
if(blocked_ip_address.includes(ip_address)) {
response = new Response('Access denied: Your IP address is blocked by WorkersProxy.', {
status: 403
});
} else if(allowed_region.includes(region)){
let response = await fetch(url, param);
// Check the Content-Type header of the response
const contentType = response.headers.get('content-type');
if(contentType && contentType.includes('text')) {
// If the type is text, replace the URL in the response body with the hostname of the requested URL
let responseBody = await response.text();
responseBody = await handleResBody(request, responseBody);
// Copy the response object and update its attributes
let headers = await handleResHeader(response);
return new Response(responseBody, {
status: response.status,
statusText: response.statusText,
headers: headers
});
} else {
// If the type is not text, directly return the response object
return response;
}
} else {
response = new Response('Access denied: WorkersProxy is not available in your region yet.', {
status: 403
});
}
} |
|