Created
September 25, 2019 07:54
-
-
Save tennc/d05f3f26417b1a22dcb33fbb7a013af6 to your computer and use it in GitHub Desktop.
这个更棒~ 速度更快~
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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