commit f1ce5c916b9e9ab574bbb6c492f17e9ef01a5e03 Author: hunshnet <337490703@qq.com> Date: Sun Mar 22 08:57:39 2020 +0800 init diff --git a/README.md b/README.md new file mode 100644 index 0000000..185c4f3 --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# gh-proxy +## 简介 + +利用Cloudflare Workers对github release、archive以及项目文件进行加速,部署无需服务器且自带cdn。 + +## 使用 + +直接在copy出来的url前加`https://shrill-pond-3e81.hunsh.workers.dev/`即可 + +也可以直接访问,在input输入 + +注意,如果是项目文件会302到jsdeliver,由国内加速会更快。 + +***大量使用请自行部署,以上域名仅为演示使用。*** + +以下都是合法输入: + + - 分支源码:https://github.com/hunshcn/project/archive/master.zip + + - release源码:https://github.com/hunshcn/project/archive/v0.1.0.tar.gz + + - release文件:https://github.com/hunshcn/project/releases/download/v0.1.0/example.zip + + - 分支文件:https://github.com/hunshcn/project/blob/master/filename + + - commit文件:https://github.com/hunshcn/project/blob/1111111111111111111111111111/filename + +## 部署 + +首页:https://workers.cloudflare.com + +注册,登陆,`Start building`,取一个子域名,`Create a Worker`。 + +复制 [index.js](https://cdn.jsdeliver.net/hunshcn/gh-proxy@master/index.js) 到左侧代码框,`Save and deploy`。如果正常,右侧应显示首页。 + +## 计费 + +到 `overview` 页面可参看使用情况。免费版每天有 10 万次免费请求,并且有每分钟1000次请求的限制。 + +如果不够用,可升级到 $5 的高级版本,每月可用 1000 万次请求(超出部分 $0.5/百万次请求)。 + +## 链接 + +[我的博客](https://hunsh.net) + +## 参考 + +[jsproxy](https://github.com/EtherDream/jsproxy/) diff --git a/index.js b/index.js new file mode 100644 index 0000000..466116e --- /dev/null +++ b/index.js @@ -0,0 +1,146 @@ +'use strict' + +/** + * static files (404.html, sw.js, conf.js) + */ +const ASSET_URL = 'https://hunshcn.github.io/gh-proxy' + +/** @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} headers + */ +function makeRes(body, status = 200, headers = {}) { + 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) + let path = urlObj.searchParams.get('q') + if(path) + { + return Response.redirect('https://' + urlObj.host + '/' + path, 301) + } + // cfworker 会把路径中的 `//` 合并成 `/` + path = urlObj.href.substr(urlObj.origin.length + 1).replace(/^https?:\/+/, 'https://') + const exp = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/ + const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob)\/.*$/ + if (path.search(exp)===0) { + return httpHandler(req, path) + }else if(path.search(exp2)===0){ + const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh') + return Response.redirect(newUrl, 302) + } else { + return fetch(ASSET_URL + path) + } +} + + +/** + * @param {Request} req + * @param {string} pathname + */ +function httpHandler(req, pathname) { + const reqHdrRaw = req.headers + + // preflight + if (req.method === 'OPTIONS' && + reqHdrRaw.has('access-control-request-headers') + ) { + return new Response(null, PREFLIGHT_INIT) + } + + let rawLen = '' + + const reqHdrNew = new Headers(reqHdrRaw) + + const refer = reqHdrNew.get('referer') + + let urlStr = pathname + if (urlStr.startsWith('github')) { + urlStr = 'https://' + urlStr + } + const urlObj = newUrl(urlStr) + + /** @type {RequestInit} */ + const reqInit = { + method: req.method, + headers: reqHdrNew, + redirect: 'follow', + } + return proxy(urlObj, reqInit, rawLen, 0) +} + + +/** + * + * @param {URL} urlObj + * @param {RequestInit} reqInit + */ +async function proxy(urlObj, reqInit, rawLen) { + const res = await fetch(urlObj.href, reqInit) + const resHdrOld = res.headers + const resHdrNew = new Headers(resHdrOld) + + // verify + if (rawLen) { + const newLen = resHdrOld.get('content-length') || '' + const badLen = (rawLen !== newLen) + + if (badLen) { + return makeRes(res.body, 400, { + '--error': `bad len: ${newLen}, except: ${rawLen}`, + 'access-control-expose-headers': '--error', + }) + } + } + const status = res.status + resHdrNew.set('access-control-expose-headers', '*') + resHdrNew.set('access-control-allow-origin', '*') + + resHdrNew.delete('content-security-policy') + resHdrNew.delete('content-security-policy-report-only') + resHdrNew.delete('clear-site-data') + + return new Response(res.body, { + status, + headers: resHdrNew, + }) +} +