Skip to content

Instantly share code, notes, and snippets.

@tennc tennc/workers.js
Created Sep 25, 2019

Embed
What would you like to do?
这个更棒~ 速度更快~
// List of domains bind to your WorkersProxy.
const domain_list = ['https://你的cloudflareWorker应用地址/']
// Website you intended to retrieve for users.
const upstream = 'https://需要加速的网站的地址/'
// Website you intended to retrieve for users using mobile devices.
const upstream_mobile = 'https://需要加速的网站移动端的地址/'
// Countries and regions where you wish to suspend your service.
const blocked_region = []
// IP addresses which you wish to block from using your service.
const blocked_ip_address = ['0.0.0.0', '10.0.0.0']
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 = request.url;
if (await device_status(user_agent)){
upstream_domain = upstream
} else {
upstream_domain = upstream_mobile
}
for(let domain of domain_list) {
url = url.replace(domain, 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 headers = request.headers;
response = fetch(url, {
method: method,
headers: headers
})
}
return response;
}
async function device_status (userAgentInfo) {
var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.