当前位置:   article > 正文

利用CF搭建Github下载代理_利用cfcdn代理githu加速下载

利用cfcdn代理githu加速下载

1.注册cloudflare账号

如题,先去cloudflare.com注册一个账号,有账户的可以忽略

2.创建worker

 

名称随意

3.配置文件 

然后把左侧代码全部替换成下面的代码

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

点击下面保存并部署

4.测试

点击中间发送按钮并有右侧显示200 OK即搭建成功

点击下面箭头链接即可进入网站

PS:

默认配置下项目文件会走jsDelivr,如需走worker,修改Config变量即可

ASSET_URL是静态资源的url(实际上就是现在显示出来的那个输入框单页面)

PREFIX是前缀,默认(根路径情况为"/"),如果自定义路由为example.com/gh/*,请将PREFIX改为 '/gh/',注意,少一个杠都会错!

Cloudflare Workers计费

到 overview 页面可参看使用情况。免费版每天有 10 万次免费请求,并且有每分钟1000次请求的限制。

如果不够用,可升级到 $5 的高级版本,每月可用 1000 万次请求(超出部分 $0.5/百万次请求)。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/716688
推荐阅读
相关标签
  

闽ICP备14008679号