记录我的一些生活写照、无聊的牢骚、内心世界的活动 注册 | 登陆

白嫖cloudflare搭建属于自己的git加速站

白嫖cloudflare搭建属于自己的git加速站

不想自己动手的话,文末也有搭建好的,可以直接去白嫖!


基于开源项目gh-proxy地址:-->点击访问<--

cloudflare地址:-->点击访问<--

首先访问cloudflare注册一个你自己的账户

然后点击workers

之后弹出来这个界面,你需要在输入框中输入你自己的workers的名字

请注意!一定要方便记忆,不要乱输,这个会出现在你的网址中!!!

输入好后点set up

然后选择订阅模式,白嫖怪直接free!

免费版本每天的访问次数是10w次,应该是远远够用的!

然后要验证邮箱!

去你邮箱完成验证

验证完成后刷新,点击创建

然后按下图填写,之后点创建

然后点击快速编辑

将下面的代码复制 --->点击查看源码<--

  1. 'use strict'
  2.  
  3. /**
  4. * static files (404.html, sw.js, conf.js)
  5. */
  6. const ASSET_URL = 'https://hunshcn.github.io/gh-proxy/'
  7. // 前缀,如果自定义路由为example.com/gh/*,将PREFIX改为 '/gh/',注意,少一个杠都会错!
  8. const PREFIX = '/'
  9. // git使用cnpmjs镜像、分支文件使用jsDelivr镜像的开关,0为关闭,默认开启
  10. const Config = {
  11. jsdelivr: 1,
  12. cnpmjs: 1
  13. }
  14.  
  15. /** @type {RequestInit} */
  16. const PREFLIGHT_INIT = {
  17. status: 204,
  18. headers: new Headers({
  19. 'access-control-allow-origin': '*',
  20. 'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS',
  21. 'access-control-max-age': '1728000',
  22. }),
  23. }
  24.  
  25.  
  26. const exp1 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/i
  27. const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob|raw)\/.*$/i
  28. const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-).*$/i
  29. const exp4 = /^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+?\/.+$/i
  30. const exp5 = /^(?:https?:\/\/)?gist\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+$/i
  31. const exp6 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/tags.*$/i
  32.  
  33. /**
  34. * @param {any} body
  35. * @param {number} status
  36. * @param {Object<string, string>} headers
  37. */
  38. function makeRes(body, status = 200, headers = {}) {
  39. headers['access-control-allow-origin'] = '*'
  40. return new Response(body, {status, headers})
  41. }
  42.  
  43.  
  44. /**
  45. * @param {string} urlStr
  46. */
  47. function newUrl(urlStr) {
  48. try {
  49. return new URL(urlStr)
  50. } catch (err) {
  51. return null
  52. }
  53. }
  54.  
  55.  
  56. addEventListener('fetch', e => {
  57. const ret = fetchHandler(e)
  58. .catch(err => makeRes('cfworker error:\n' + err.stack, 502))
  59. e.respondWith(ret)
  60. })
  61.  
  62.  
  63. function checkUrl(u) {
  64. for (let i of [exp1, exp2, exp3, exp4, exp5, exp6 ]) {
  65. if (u.search(i) === 0) {
  66. return true
  67. }
  68. }
  69. return false
  70. }
  71.  
  72. /**
  73. * @param {FetchEvent} e
  74. */
  75. async function fetchHandler(e) {
  76. const req = e.request
  77. const urlStr = req.url
  78. const urlObj = new URL(urlStr)
  79. let path = urlObj.searchParams.get('q')
  80. if (path) {
  81. return Response.redirect('https://' + urlObj.host + PREFIX + path, 301)
  82. }
  83. // cfworker 会把路径中的 `//` 合并成 `/`
  84. path = urlObj.href.substr(urlObj.origin.length + PREFIX.length).replace(/^https?:\/+/, 'https://')
  85. if (path.search(exp1) === 0 || path.search(exp5) === 0 || path.search(exp6) === 0 || !Config.cnpmjs && (path.search(exp3) === 0 || path.search(exp4) === 0)) {
  86. return httpHandler(req, path)
  87. } else if (path.search(exp2) === 0) {
  88. if (Config.jsdelivr) {
  89. const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh')
  90. return Response.redirect(newUrl, 302)
  91. } else {
  92. path = path.replace('/blob/', '/raw/')
  93. return httpHandler(req, path)
  94. }
  95. } else if (path.search(exp3) === 0) {
  96. const newUrl = path.replace(/^(?:https?:\/\/)?github\.com/, 'https://github.com.cnpmjs.org')
  97. return Response.redirect(newUrl, 302)
  98. } else if (path.search(exp4) === 0) {
  99. const newUrl = path.replace(/(?<=com\/.+?\/.+?)\/(.+?\/)/, '@$1').replace(/^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com/, 'https://cdn.jsdelivr.net/gh')
  100. return Response.redirect(newUrl, 302)
  101. } else {
  102. return fetch(ASSET_URL + path)
  103. }
  104. }
  105.  
  106.  
  107. /**
  108. * @param {Request} req
  109. * @param {string} pathname
  110. */
  111. function httpHandler(req, pathname) {
  112. const reqHdrRaw = req.headers
  113.  
  114. // preflight
  115. if (req.method === 'OPTIONS' &&
  116. reqHdrRaw.has('access-control-request-headers')
  117. ) {
  118. return new Response(null, PREFLIGHT_INIT)
  119. }
  120.  
  121. const reqHdrNew = new Headers(reqHdrRaw)
  122.  
  123. let urlStr = pathname
  124. if (urlStr.startsWith('github')) {
  125. urlStr = 'https://' + urlStr
  126. }
  127. const urlObj = newUrl(urlStr)
  128.  
  129. /** @type {RequestInit} */
  130. const reqInit = {
  131. method: req.method,
  132. headers: reqHdrNew,
  133. redirect: 'manual',
  134. body: req.body
  135. }
  136. return proxy(urlObj, reqInit)
  137. }
  138.  
  139.  
  140. /**
  141. *
  142. * @param {URL} urlObj
  143. * @param {RequestInit} reqInit
  144. */
  145. async function proxy(urlObj, reqInit) {
  146. const res = await fetch(urlObj.href, reqInit)
  147. const resHdrOld = res.headers
  148. const resHdrNew = new Headers(resHdrOld)
  149.  
  150. const status = res.status
  151.  
  152. if (resHdrNew.has('location')) {
  153. let _location = resHdrNew.get('location')
  154. if (checkUrl(_location))
  155. resHdrNew.set('location', PREFIX + _location)
  156. else {
  157. reqInit.redirect = 'follow'
  158. return proxy(newUrl(_location), reqInit)
  159. }
  160. }
  161. resHdrNew.set('access-control-expose-headers', '*')
  162. resHdrNew.set('access-control-allow-origin', '*')
  163.  
  164. resHdrNew.delete('content-security-policy')
  165. resHdrNew.delete('content-security-policy-report-only')
  166. resHdrNew.delete('clear-site-data')
  167.  
  168. return new Response(res.body, {
  169. status,
  170. headers: resHdrNew,
  171. })
  172. }

替换掉原来所有代码,先点下面的保存,等待保存成功后,再点发送

显示如下就成功了
代理收集

烟雨阁(烟雨大佬的代理)--> https://github.yanyuge.workers.dev/ 

我自己的 -->https://git.snakexgc.workers.dev/ 

上文中的-->https://github.diaobaol.workers.dev/ 

原文地址 https://www.kejiwanjia.com/jiaocheng/70055.html

« 上一篇 | 下一篇 »

发表评论

评论内容 (必填):