官網
https://workers.cloudflare.com/
登陸的時候有時會自動定向到CDN的控制台,這個時候可以返回官網然后點Log in,就會定向到worker的控制台,我有一次把注冊當成了登陸輸入了半天
簡要概括
用一句不嚴謹的話來說就是
cloudflare workers 一個可以讓你免費在雲上運行一個代理腳本
1.腳本可以使用 javascript 編寫, 並且使用的API跟瀏覽器Service Worker API類似但有一些細節上的擴充,瀏覽器Service Worker API是可以運行在本地瀏覽器中的前置代理,而cloudflare workers不在你的瀏覽器中運行,但是應該可以跟瀏覽器Service Worker API配合使用
2.簡單使用不需要自己注冊域名,會分配一個cloudflare的子域名給你,而每一個worker項又在你的子域名上再派生一個子域名,所以只要添加了worker項,立即就可以根據分配的域名訪問,完整自動分配的域名類似於 worker項的名字.你起的名字.cloudflare的域
3.只要是cloudflare任播網絡的IP節點,都可以通過URL來訪問你的worker項,也就是說ip很多,隨便哪個都可以用來訪問你的worker項
4.(我也一知半解)cloudflare workers在邊緣網絡運行,或者說你手動指向位於其他國家的任播網絡ip節點來訪問你的worker,那么worker就會就近運行進行DNS解析之類的,那么源站看到的請求ip就是該國家的ip(話說我也不太確定這句話對不對),默認國內解析出的ip反正都不位於國內
5.只要源站不屏蔽任播網絡的節點,理論上你可以用它訪問任何網站
6.貌似應該支持視頻流,API中有Stream這個東西
一些問題
1.不怕你無法訪問worker,就怕源站屏蔽任播網絡的節點,導致worker去訪問源站出現404或者被判為爬蟲,一直進行人機驗證
2.單純不依賴其他代理軟件,以純worker的形式代理一個網站,需要將響應的html修改,把網站的腳本和圖片之類的鏈接修改為你的worker的域名,除非目標網站全部采用相對鏈接,所以網上找到的worker模板有的只是單純代理一個域名,網站圖片就可能無法加載
參考
可以在瀏覽器搜索欄中輸入
cloudflare workers
看看它自動提示什么
也可以參考博客
https://blog.superpotato.dev/2019/10/02/cloudflare-break-the-wall/
模板
我個人只知道兩個模板,一個簡單些(需要自己設置),一個復雜些(但是可以粘貼就用,支持好多網站)
模板一
項目鏈接
https://github.com/xiaoyang-liu-cs/booster.js
只要將下面的模板粘貼到worker中保存部署,就可以立刻訪問URL查看效果,但是網站圖片無法加載
我也有錄制的簡單講解視頻
https://www.bilibili.com/video/BV19K4y1m7oA
注釋是我加的
//主要配置這個對象 const config = { basic: { //源站的url upstream: 'https://en.wikipedia.org/', //對於移動端的源站url mobileRedirect: 'https://en.m.wikipedia.org/', }, //屏蔽的國家,默認有CN被我刪了,否則會返回錯誤 firewall: { blockedRegion: ['KP', 'SY', 'PK', 'CU'], blockedIPAddress: [], scrapeShield: true, }, //不同地區源站url的特化 routes: { TW: 'https://zh.wikipedia.org/', HK: 'https://zh.wikipedia.org/', FR: 'https://fr.wikipedia.org/', }, //這些我也不知道是啥 optimization: { cacheEverything: false, cacheTtl: 5, mirage: true, polish: 'off', minify: { javascript: true, css: true, html: true, }, }, }; //下面的都是共用的模板 async function isMobile(userAgent) { const agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod']; return agents.any((agent) => userAgent.indexOf(agent) > 0); } async function fetchAndApply(request) { const region = request.headers.get('cf-ipcountry') || ''; const ipAddress = request.headers.get('cf-connecting-ip') || ''; const userAgent = request.headers.get('user-agent') || ''; //根據屏蔽的國家返回錯誤 if (region !== '' && config.firewall.blockedRegion.includes(region.toUpperCase())) { return new Response( 'Access denied: booster.js is not available in your region.', { status: 403, }, ); } if (ipAddress !== '' && config.firewall.blockedIPAddress.includes(ipAddress)) { return new Response( 'Access denied: Your IP address is blocked by booster.js.', { status: 403, }, ); } const requestURL = new URL(request.url); let upstreamURL = null; //構造請求的url if (userAgent && isMobile(userAgent) === true) { upstreamURL = new URL(config.basic.mobileRedirect); } else if (region && region.toUpperCase() in config.routes) { upstreamURL = new URL(config.routes[region.toUpperCase()]); } else { upstreamURL = new URL(config.basic.upstream); } //假如源站url有路徑就都加一起 requestURL.protocol = upstreamURL.protocol; requestURL.host = upstreamURL.host; requestURL.pathname = upstreamURL.pathname + requestURL.pathname; //有請求主體的跟沒有請求主體的 let newRequest; if (request.method === 'GET' || request.method === 'HEAD') { newRequest = new Request(requestURL, { cf: { cacheEverything: config.optimization.cacheEverything, cacheTtl: config.optimization.cacheTtl, mirage: config.optimization.mirage, polish: config.optimization.polish, minify: config.optimization.minify, scrapeShield: config.firewall.scrapeShield, }, method: request.method, headers: request.headers, }); } else { const requestBody = await request.text(); newRequest = new Request(requestURL, { cf: { cacheEverything: config.optimization.cacheEverything, cacheTtl: config.optimization.cacheTtl, mirage: config.optimization.mirage, polish: config.optimization.polish, minify: config.optimization.minify, scrapeShield: config.firewall.scrapeShield, }, method: request.method, headers: request.headers, body: requestBody, }); } const fetchedResponse = await fetch(newRequest); //這個貌似是處理重定向 const modifiedResponseHeaders = new Headers(fetchedResponse.headers); if (modifiedResponseHeaders.has('x-pjax-url')) { const pjaxURL = new URL(modifiedResponseHeaders.get('x-pjax-url')); pjaxURL.protocol = requestURL.protocol; pjaxURL.host = requestURL.host; pjaxURL.pathname = pjaxURL.path.replace(requestURL.pathname, '/'); modifiedResponseHeaders.set( 'x-pjax-url', pjaxURL.href, ); } return new Response( fetchedResponse.body, { headers: modifiedResponseHeaders, status: fetchedResponse.status, statusText: fetchedResponse.statusText, }, ); } // eslint-disable-next-line no-restricted-globals addEventListener('fetch', (event) => { event.respondWith(fetchAndApply(event.request)); });
模板二
項目鏈接
https://github.com/EtherDream/jsproxy/tree/master/cf-worker
同樣的把下面的代碼無腦粘貼就能看效果,這個強大很多,默認從'https://etherdream.github.io/jsproxy'加載配置文件
這個注釋是原本就有的
'use strict' /** * static files (404.html, sw.js, conf.js) */ const ASSET_URL = 'https://etherdream.github.io/jsproxy' const JS_VER = 10 const MAX_RETRY = 1 /** @type {RequestInit} */ const PREFLIGHT_INIT = { status: 204, headers: new Headers({ 'access-control-allow-origin': '*', 'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS', 'access-control-max-age': '1728000', }), } /** * @param {any} body * @param {number} status * @param {Object<string, string>} headers */ function makeRes(body, status = 200, headers = {}) { headers['--ver'] = JS_VER headers['access-control-allow-origin'] = '*' return new Response(body, {status, headers}) } /** * @param {string} urlStr */ function newUrl(urlStr) { try { return new URL(urlStr) } catch (err) { return null } } addEventListener('fetch', e => { const ret = fetchHandler(e) .catch(err => makeRes('cfworker error:\n' + err.stack, 502)) e.respondWith(ret) }) /** * @param {FetchEvent} e */ async function fetchHandler(e) { const req = e.request const urlStr = req.url const urlObj = new URL(urlStr) const path = urlObj.href.substr(urlObj.origin.length) if (urlObj.protocol === 'http:') { urlObj.protocol = 'https:' return makeRes('', 301, { 'strict-transport-security': 'max-age=99999999; includeSubDomains; preload', 'location': urlObj.href, }) } if (path.startsWith('/http/')) { return httpHandler(req, path.substr(6)) } switch (path) { case '/http': return makeRes('請更新 cfworker 到最新版本!') case '/ws': return makeRes('not support', 400) case '/works': return makeRes('it works') default: // static files return fetch(ASSET_URL + path) } } /** * @param {Request} req * @param {string} pathname */ function httpHandler(req, pathname) { const reqHdrRaw = req.headers if (reqHdrRaw.has('x-jsproxy')) { return Response.error() } // preflight if (req.method === 'OPTIONS' && reqHdrRaw.has('access-control-request-headers') ) { return new Response(null, PREFLIGHT_INIT) } let acehOld = false let rawSvr = '' let rawLen = '' let rawEtag = '' const reqHdrNew = new Headers(reqHdrRaw) reqHdrNew.set('x-jsproxy', '1') // 此處邏輯和 http-dec-req-hdr.lua 大致相同 // https://github.com/EtherDream/jsproxy/blob/master/lua/http-dec-req-hdr.lua const refer = reqHdrNew.get('referer') const query = refer.substr(refer.indexOf('?') + 1) if (!query) { return makeRes('missing params', 403) } const param = new URLSearchParams(query) for (const [k, v] of Object.entries(param)) { if (k.substr(0, 2) === '--') { // 系統信息 switch (k.substr(2)) { case 'aceh': acehOld = true break case 'raw-info': [rawSvr, rawLen, rawEtag] = v.split('|') break } } else { // 還原 HTTP 請求頭 if (v) { reqHdrNew.set(k, v) } else { reqHdrNew.delete(k) } } } if (!param.has('referer')) { reqHdrNew.delete('referer') } // cfworker 會把路徑中的 `//` 合並成 `/` const urlStr = pathname.replace(/^(https?):\/+/, '$1://') const urlObj = newUrl(urlStr) if (!urlObj) { return makeRes('invalid proxy url: ' + urlStr, 403) } /** @type {RequestInit} */ const reqInit = { method: req.method, headers: reqHdrNew, redirect: 'manual', } if (req.method === 'POST') { reqInit.body = req.body } return proxy(urlObj, reqInit, acehOld, rawLen, 0) } /** * * @param {URL} urlObj * @param {RequestInit} reqInit * @param {number} retryTimes */ async function proxy(urlObj, reqInit, acehOld, rawLen, retryTimes) { const res = await fetch(urlObj.href, reqInit) const resHdrOld = res.headers const resHdrNew = new Headers(resHdrOld) let expose = '*' for (const [k, v] of resHdrOld.entries()) { if (k === 'access-control-allow-origin' || k === 'access-control-expose-headers' || k === 'location' || k === 'set-cookie' ) { const x = '--' + k resHdrNew.set(x, v) if (acehOld) { expose = expose + ',' + x } resHdrNew.delete(k) } else if (acehOld && k !== 'cache-control' && k !== 'content-language' && k !== 'content-type' && k !== 'expires' && k !== 'last-modified' && k !== 'pragma' ) { expose = expose + ',' + k } } if (acehOld) { expose = expose + ',--s' resHdrNew.set('--t', '1') } // verify if (rawLen) { const newLen = resHdrOld.get('content-length') || '' const badLen = (rawLen !== newLen) if (badLen) { if (retryTimes < MAX_RETRY) { urlObj = await parseYtVideoRedir(urlObj, newLen, res) if (urlObj) { return proxy(urlObj, reqInit, acehOld, rawLen, retryTimes + 1) } } return makeRes(res.body, 400, { '--error': `bad len: ${newLen}, except: ${rawLen}`, 'access-control-expose-headers': '--error', }) } if (retryTimes > 1) { resHdrNew.set('--retry', retryTimes) } } let status = res.status resHdrNew.set('access-control-expose-headers', expose) resHdrNew.set('access-control-allow-origin', '*') resHdrNew.set('--s', status) resHdrNew.set('--ver', JS_VER) resHdrNew.delete('content-security-policy') resHdrNew.delete('content-security-policy-report-only') resHdrNew.delete('clear-site-data') if (status === 301 || status === 302 || status === 303 || status === 307 || status === 308 ) { status = status + 10 } return new Response(res.body, { status, headers: resHdrNew, }) } /** * @param {URL} urlObj */ function isYtUrl(urlObj) { return ( urlObj.host.endsWith('.googlevideo.com') && urlObj.pathname.startsWith('/videoplayback') ) } /** * @param {URL} urlObj * @param {number} newLen * @param {Response} res */ async function parseYtVideoRedir(urlObj, newLen, res) { if (newLen > 2000) { return null } if (!isYtUrl(urlObj)) { return null } try { const data = await res.text() urlObj = new URL(data) } catch (err) { return null } if (!isYtUrl(urlObj)) { return null } return urlObj }