创建 Worker
首先你需要一个 Cloudflare 账号,在 Workers 中创建一个新的 Worker。
粘贴以下代码:
const upstream = 'github.com' const upstream_path = '/' const upstream_mobile = 'github.com' const blocked_region = [] const blocked_ip_address = ['0.0.0.0', '127.0.0.1'] const https = true const disable_cache = false const replace_dict = { '$upstream': '$custom_domain', 'sta.cf.<YOUR-SUBDOMAIN>.workers.dev':'stacf.<YOUR-SUBDOMAIN>.workers.dev', 'assets.cf.<YOUR-SUBDOMAIN>.workers.dev':'cfassets.<YOUR-SUBDOMAIN>.workers.dev' } let data={} 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_hostname = url.hostname; if (https == true) { url.protocol = 'https:'; } else { url.protocol = 'http:'; } if (await device_status(user_agent)) { var upstream_domain = upstream; } else { var upstream_domain = upstream_mobile; } url.host = upstream_domain; if (url.pathname == '/') { url.pathname = upstream_path; } else { url.pathname = upstream_path + url.pathname; } 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.protocol + '//' + upstream_domain); if(method == 'POST'){ let origin_formData = await request.text() data = { method: method, headers: new_request_headers, body : origin_formData } console.log(data) }else if(method == 'GET'){ data = { method: method, headers: new_request_headers } } let fuckKeys = Object.fromEntries(new_request_headers) console.log(method) console.log(fuckKeys) let original_response = await fetch(url.href, data) connection_upgrade = new_request_headers.get("Upgrade"); if (connection_upgrade && connection_upgrade.toLowerCase() == "websocket") { return original_response; } 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; if (disable_cache) { new_response_headers.set('Cache-Control', 'no-store'); } 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'); if (new_response_headers.get("x-pjax-url")) { new_response_headers.set("x-pjax-url", response_headers.get("x-pjax-url").replace("//" + upstream_domain, "//" + url_hostname)); } const content_type = new_response_headers.get('content-type'); if (content_type != null && content_type.includes('text/html') && content_type.includes('UTF-8')) { original_text = await replace_response_text(original_response_clone, upstream_domain, url_hostname); } else { original_text = original_response_clone.body } response = new Response(original_text, { status, headers: new_response_headers }) } return response; } async function replace_form_data(formData, 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 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; }
配置域名(可选)
创建完 Worker 之后, 在 DNS 中创建一条 CNAME 记录:
CNAME github cdn-github.willin.workers.dev Proxied Auto
域名自定,值为 Worker 域名,开启 CDN Proxied。
等一会配置生效即可访问。
使用限制
注意, Cloudflare 免费账号每天有 100k 的请求次数限制。
之前搭了一个自用,不知道为什么流量一天比一天高,已经扛不住了。所以在这里把脚本分享出来,需要的请自行搭建(免费)。
原文链接:https://blog.csdn.net/jslygwx/article/details/124313916?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165934461816781790724367%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165934461816781790724367&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-3-124313916-null-null.nonecase&utm_term=%E6%90%AD%E5%BB%BAcdn
原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/5963