42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
export default {
|
|
async fetch(request) {
|
|
const url = new URL(request.url);
|
|
const { hostname, pathname, search } = url;
|
|
if (checkUrl(pathname)) {
|
|
const response = await fetch(new Request("https://github.com" + pathname + search, {
|
|
body: request.body,
|
|
headers: request.headers,
|
|
method: request.method,
|
|
redirect: "follow"
|
|
}));
|
|
const responseText = await response.text();
|
|
const modifiedText = responseText.replace(/github\.com/g, hostname);
|
|
|
|
return new Response(modifiedText, {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
headers: response.headers
|
|
});
|
|
} else {
|
|
let tip = `GitHub 下载代理,支持 releases、archive、tags、raw 等。\n\n\n\n
|
|
使用方法:
|
|
将 Github 原链接中的 【github.com】 改为 【${hostname}】。\n
|
|
例如:
|
|
【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
|
|
} |