一无所有的小白免费搭建codeforces镜像站(什么都不需要,无门槛)free
🎖️

一无所有的小白免费搭建codeforces镜像站(什么都不需要,无门槛)free

提示

本文是针对小白的,会非常详细,有很多图!

准备

一个电子邮箱
一台可以访问互联网的电脑

步骤

注册cloudflare账号

进入cloudflare官网
点击右上角的注册
notion image
输入邮箱和密码
notion image
点击邮箱中收到的验证邮箱链接
notion image
点击继续转到仪表盘
notion image
点击workers
notion image
填写自己想要子域名称。注意,这一步很重要,这决定着你搭建成功后的网址,务必选择一个短一点有、好记的。我这里填了m27
notion image
选择继续使用free计划
notion image

部署

点击创建worker
notion image
点击左上角的名称并改成cf
notion image
将下方脚本全部删除,并将这篇代码粘贴到其中
// Website you intended to retrieve for users. //const upstream = 'www.google.com' const upstream = 'codeforces.com' // Custom pathname for the upstream website. const upstream_path = '/' // Website you intended to retrieve for users using mobile devices. //const upstream_mobile = 'www.google.com' const upstream_mobile = 'codeforces.com' // 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', '127.0.0.1'] // Whether to use HTTPS protocol for upstream address. const https = true // Whether to disable cache. const disable_cache = false // Replace texts. // const replace_dict = { // '$upstream': '$custom_domain', // '//google.com': '' // } const replace_dict = { '$upstream': '$custom_domain', 'st.cf.<YOURNAME>.workers.dev':'stcf.<YOURNAME>.workers.dev', 'assets.cf.<YOURNAME>.workers.dev':'cfassets.<YOURNAME>.workers.dev', 'userpic.cf.<YOURNAME>.workers.dev':'userpic.<YOURNAME>.workers.dev', 'espresso.cf.<YOURNAME>.workers.dev':'espresso.<YOURNAME>.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; }
并将32、33、34行中的<YOURNAME> 改为前面你输入的子域,比如我的是m27,就改成这样
notion image
点击脚本框右下角的保存并部署
notion image
返回worker页面,再创建一个worker
notion image
左上角改名为userpic
并将脚本内容替换为
// Website you intended to retrieve for users. //const upstream = 'www.google.com' const upstream = 'userpic.codeforces.com' // Custom pathname for the upstream website. const upstream_path = '/' // Website you intended to retrieve for users using mobile devices. //const upstream_mobile = 'www.google.com' const upstream_mobile = 'userpic.codeforces.com' // 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', '127.0.0.1'] // Whether to use HTTPS protocol for upstream address. const https = true // Whether to disable cache. const disable_cache = false // Replace texts. // const replace_dict = { // '$upstream': '$custom_domain', // '//google.com': '' // } const replace_dict = { '$upstream': '$custom_domain', } 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 + '//' + url_hostname); let original_response = await fetch(url.href, { method: method, headers: new_request_headers }) 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_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; }
然后保存并部署
notion image
同样地,再创建三个worker
第一个名为stcf ,内容为
// Website you intended to retrieve for users. //const upstream = 'www.google.com' const upstream = 'st.codeforces.com' // Custom pathname for the upstream website. const upstream_path = '/' // Website you intended to retrieve for users using mobile devices. //const upstream_mobile = 'www.google.com' const upstream_mobile = 'st.codeforces.com' // Countries and regions where you wish to suspend your service. const blocked_region = ['0.0.0.0', '127.0.0.1'] // IP addresses which you wish to block from using your service. const blocked_ip_address = [] // Whether to use HTTPS protocol for upstream address. const https = true // Whether to disable cache. const disable_cache = false // Replace texts. // const replace_dict = { // '$upstream': '$custom_domain', // '//google.com': '' // } const replace_dict = { '$upstream': '$custom_domain', } 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 + '//' + url_hostname); let original_response = await fetch(url.href, { method: method, headers: new_request_headers }) 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_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; }
第二个名为cfassets ,内容为
// Website you intended to retrieve for users. //const upstream = 'www.google.com' const upstream = 'assets.codeforces.com' // Custom pathname for the upstream website. const upstream_path = '/' // Website you intended to retrieve for users using mobile devices. //const upstream_mobile = 'www.google.com' const upstream_mobile = 'assets.codeforces.com' // 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', '127.0.0.1'] // Whether to use HTTPS protocol for upstream address. const https = true // Whether to disable cache. const disable_cache = false // Replace texts. // const replace_dict = { // '$upstream': '$custom_domain', // '//google.com': '' // } const replace_dict = { '$upstream': '$custom_domain', } 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 + '//' + url_hostname); let original_response = await fetch(url.href, { method: method, headers: new_request_headers }) 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_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; }
第三个名为espresso 内容为
// Website you intended to retrieve for users. //const upstream = 'www.google.com' const upstream = 'espresso.codeforces.com' // Custom pathname for the upstream website. const upstream_path = '/' // Website you intended to retrieve for users using mobile devices. //const upstream_mobile = 'www.google.com' const upstream_mobile = 'espresso.codeforces.com' // 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', '127.0.0.1'] // Whether to use HTTPS protocol for upstream address. const https = true // Whether to disable cache. const disable_cache = false // Replace texts. // const replace_dict = { // '$upstream': '$custom_domain', // '//google.com': '' // } const replace_dict = { '$upstream': '$custom_domain', } 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 + '//' + url_hostname); let original_response = await fetch(url.href, { method: method, headers: new_request_headers }) 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_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; }

完成

访问 https://cf.<YOURNAME>.workers.dev/ 即可看到你的cf镜像站了(<YOURMANE>为前面你填的子域),但是这时速度不一定最优,我们还可以做一系列优化(虽然不优化速度应该也不错)

优化速度

自选ip

下载文件并解压到桌面(C盘位置)
发现有这些文件
notion image
按Windows+R键,并在弹出窗口中输入cmd
notion image
按回车打开命令行
notion image
复制解压后的路径(要在C盘)
notion image
然后在cmd中输入
cd 路径
路径即为刚才复制的路径
按回车,继续
notion image
输入并按下回车键
CloudflareST.exe
等两行进度条跑完后,可以看到许多ip,选择最上面的那个记下来,马上就会用到(我这里是104.17.31.0
notion image

修改host

关于如何修改host,请百度,这里推荐用火绒安全软件中的host修改工具
在host文件中添加以下内容
104.17.31.0 cf.<YOURNAME>.workers.dev 104.17.31.0 cfassets.<YOURNAME>.workers.dev 104.17.31.0 userpic.<YOURNAME>.workers.dev 104.17.31.0 stcf.<YOURNAME>.workers.dev 104.17.31.0 espresso.<YOURNAME>.workers.dev 104.17.31.0 codeforces.org 104.17.31.0 mathjax.codeforces.org
<YOURNAME>为前面自己选的子域
104.17.31.0替换成刚才你选的ip
然后就完成了

鸣谢

没有这些项目,就不会有本文