Advertisement
Guest User

gh-proxy的cloudflare worker版本,魔改代理所有api接口

a guest
Jul 11th, 2023
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. // 分支文件使用jsDelivr镜像的开关,0为关闭,默认关闭
  10. const Config = {
  11.     jsdelivr: 0
  12. }
  13.  
  14. const whiteList = [] // 白名单,路径里面有包含字符的才会通过,e.g. ['/username/']
  15.  
  16. /** @type {RequestInit} */
  17. const PREFLIGHT_INIT = {
  18.     status: 204,
  19.     headers: new Headers({
  20.         'access-control-allow-origin': '*',
  21.         'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS',
  22.         'access-control-max-age': '1728000',
  23.     }),
  24. }
  25.  
  26.  
  27. const exp1 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/i
  28. const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob|raw)\/.*$/i
  29. const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-).*$/i
  30. const exp4 = /^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+?\/.+$/i
  31. const exp5 = /^(?:https?:\/\/)?gist\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+$/i
  32. const exp6 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/tags.*$/i
  33.  
  34. /**
  35.  * @param {any} body
  36.  * @param {number} status
  37.  * @param {Object<string, string>} headers
  38.  */
  39. function makeRes(body, status = 200, headers = {}) {
  40.     headers['access-control-allow-origin'] = '*'
  41.     return new Response(body, {status, headers})
  42. }
  43.  
  44.  
  45. /**
  46.  * @param {string} urlStr
  47.  */
  48. function newUrl(urlStr) {
  49.     try {
  50.         return new URL(urlStr)
  51.     } catch (err) {
  52.         return null
  53.     }
  54. }
  55.  
  56.  
  57. addEventListener('fetch', e => {
  58.     const ret = fetchHandler(e)
  59.         .catch(err => makeRes('cfworker error:\n' + err.stack, 502))
  60.     e.respondWith(ret)
  61. })
  62.  
  63.  
  64. function checkUrl(u) {
  65.     for (let i of [exp1, exp2, exp3, exp4, exp5, exp6]) {
  66.         if (u.search(i) === 0) {
  67.             return true
  68.         }
  69.     }
  70.     return false
  71. }
  72.  
  73. const customProxyDomainArray=[
  74.     // 代理所有的http或https开头的东西
  75.     /^(?:https?:\/\/).*$/i
  76. ]
  77.  
  78. function checkCustomUrl(u) {
  79.     for (let i of customProxyDomainArray ) {
  80.         if (u.search(i) === 0) {
  81.             return true
  82.         }
  83.     }
  84.     return false
  85. }
  86.  
  87. /**
  88.  * @param {FetchEvent} e
  89.  */
  90. async function fetchHandler(e) {
  91.     const req = e.request
  92.     const urlStr = req.url
  93.     const urlObj = new URL(urlStr)
  94.     let path = urlObj.searchParams.get('q')
  95.     if (path) {
  96.         return Response.redirect('https://' + urlObj.host + PREFIX + path, 301)
  97.     }
  98.     // cfworker 会把路径中的 `//` 合并成 `/`
  99.     path = urlObj.href.substr(urlObj.origin.length + PREFIX.length).replace(/^https?:\/+/, 'https://')
  100.     if (path.search(exp1) === 0 || path.search(exp5) === 0 || path.search(exp6) === 0 || path.search(exp3) === 0 || path.search(exp4) === 0) {
  101.         return httpHandler(req, path)
  102.     } else if (path.search(exp2) === 0) {
  103.         if (Config.jsdelivr) {
  104.             const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh')
  105.             return Response.redirect(newUrl, 302)
  106.         } else {
  107.             path = path.replace('/blob/', '/raw/')
  108.             return httpHandler(req, path)
  109.         }
  110.     } else if (path.search(exp4) === 0) {
  111.         const newUrl = path.replace(/(?<=com\/.+?\/.+?)\/(.+?\/)/, '@$1').replace(/^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com/, 'https://cdn.jsdelivr.net/gh')
  112.         return Response.redirect(newUrl, 302)
  113.     } else if (checkCustomUrl(path)) {
  114.         return httpHandler(req, path)
  115.     } else {
  116.         return fetch(ASSET_URL + path)
  117.     }
  118. }
  119.  
  120.  
  121. /**
  122.  * @param {Request} req
  123.  * @param {string} pathname
  124.  */
  125. function httpHandler(req, pathname) {
  126.     const reqHdrRaw = req.headers
  127.  
  128.     // preflight
  129.     if (req.method === 'OPTIONS' &&
  130.         reqHdrRaw.has('access-control-request-headers')
  131.     ) {
  132.         return new Response(null, PREFLIGHT_INIT)
  133.     }
  134.  
  135.     const reqHdrNew = new Headers(reqHdrRaw)
  136.  
  137.     let urlStr = pathname
  138.     let flag = !Boolean(whiteList.length)
  139.     for (let i of whiteList) {
  140.         if (urlStr.includes(i)) {
  141.             flag = true
  142.             break
  143.         }
  144.     }
  145.     if (!flag) {
  146.         return new Response("blocked", {status: 403})
  147.     }
  148.     if (urlStr.startsWith('github')) {
  149.         urlStr = 'https://' + urlStr
  150.     }
  151.     const urlObj = newUrl(urlStr)
  152.  
  153.     /** @type {RequestInit} */
  154.     const reqInit = {
  155.         method: req.method,
  156.         headers: reqHdrNew,
  157.         redirect: 'manual',
  158.         body: req.body
  159.     }
  160.     return proxy(urlObj, reqInit)
  161. }
  162.  
  163.  
  164. /**
  165.  *
  166.  * @param {URL} urlObj
  167.  * @param {RequestInit} reqInit
  168.  */
  169. async function proxy(urlObj, reqInit) {
  170.     const res = await fetch(urlObj.href, reqInit)
  171.     const resHdrOld = res.headers
  172.     const resHdrNew = new Headers(resHdrOld)
  173.  
  174.     const status = res.status
  175.  
  176.     if (resHdrNew.has('location')) {
  177.         let _location = resHdrNew.get('location')
  178.         if (checkUrl(_location))
  179.             resHdrNew.set('location', PREFIX + _location)
  180.         else {
  181.             reqInit.redirect = 'follow'
  182.             return proxy(newUrl(_location), reqInit)
  183.         }
  184.     }
  185.     resHdrNew.set('access-control-expose-headers', '*')
  186.     resHdrNew.set('access-control-allow-origin', '*')
  187.  
  188.     resHdrNew.delete('content-security-policy')
  189.     resHdrNew.delete('content-security-policy-report-only')
  190.     resHdrNew.delete('clear-site-data')
  191.  
  192.     return new Response(res.body, {
  193.         status,
  194.         headers: resHdrNew,
  195.     })
  196. }
  197.  
Tags: worker.js
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement