cf-worker/reverse-proxy/github-releases.js

31 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export default {
async fetch(request) {
const url = new URL(request.url);
const { hostname, pathname, search } = url;
if (checkUrl(pathname)) {
return fetch(new Request("https://github.com" + pathname + search, {
body: request.body,
headers: request.headers,
method: request.method,
redirect: request.redirect
}));
} else {
let tip = `GitHub 下载代理,支持 releases、archive、tags、raw 等。
使用方法:将 Github 原链接中的 【github.com】 改为 【${hostname}】。
【https://github.com/a/a/releases】改为【https://${hostname}/a/a/releases】。`
return new Response(tip);
}
}
};
function checkUrl(u) {
const exp1 = "/.+?\/.+?\/(?:releases|archive|tags).*"
const exp2 = "/.+?\/.+?\/raw\/.*"
for (let ind of [exp1, exp2]) {
if (u.search(ind) === 0) {
return true
}
}
return false
}