当前位置:首页 > 教程 > 正文内容

自建github加速网站

2026-05-05教程27

让gemini写的,CF的Workers,我是用在DPanel拉1Panel的商店仓库,成功了,直接用就好,域名替换一下。

const PROXY_DOMAIN = '这里是替换的域名' 
const HUB_DOMAINS = [
    'github.com',
    'raw.githubusercontent.com',
    'assets-cdn.github.com',
    'objects.githubusercontent.com',
    'codeload.github.com',
    'gist.github.com'
]
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url)
let actualPath = url.pathname.replace(/^\/+/, '')
let targetUrlStr = ''
    // 识别目标 URL
const startsWithDomain = HUB_DOMAINS.find(domain => actualPath.startsWith(domain))
if (startsWithDomain) {
targetUrlStr = 'https://' + actualPath + url.search
} else if (actualPath.startsWith('http')) {
targetUrlStr = actualPath + url.search
} else if (actualPath.length > 0) {
targetUrlStr = 'https://github.com/' + actualPath + url.search
} else {
return new Response('GitHub Proxy is Running', { headers: { 'content-type': 'text/plain' } })
}
try {
const targetUrl = new URL(targetUrlStr)
const newHeaders = new Headers(request.headers)
newHeaders.set('Host', targetUrl.hostname)
        // --- 核心修复:识别 Git 协议请求 ---
        // Git 克隆会包含 info/refs 或 git-upload-pack 路径
const isGitTraffic = url.pathname.includes('info/refs') || 
url.pathname.includes('git-upload-pack') || 
url.pathname.includes('git-receive-pack');
const response = await fetch(targetUrlStr, {
            method: request.method,
            headers: newHeaders,
            body: request.body, // 转发 POST 请求的 body
            redirect: isGitTraffic ? 'follow' : 'manual' 
        })
        // 如果是 Git 流量,绝对不能调用 response.text(),必须直接流式返回二进制数据
if (isGitTraffic) {
return new Response(response.body, {
status: response.status,
headers: response.headers
})
}
        // --- 处理重定向 (非 Git 流量) ---
if ([301, 302, 303, 307, 308].includes(response.status)) {
let location = response.headers.get('Location')
if (location) {
let newLocation = location.replace('https://github.com', `https://${PROXY_DOMAIN}`)
HUB_DOMAINS.forEach(d => {
if (d !== 'github.com') {
newLocation = newLocation.replace(new RegExp(`https?:\/\/${d.replace(/\./g, '\\.')}`, 'g'), `https://${PROXY_DOMAIN}/${d}`)
}
})
return new Response(null, { status: response.status, headers: { 'Location': newLocation } })
}
}
        // --- 处理网页内容替换 (仅限 HTML) ---
const contentType = response.headers.get('content-type') || ''
if (contentType.includes('text/html')) {
let body = await response.text()
body = body.replace(/https?:\/\/github\.com/g, `https://${PROXY_DOMAIN}`)
HUB_DOMAINS.forEach(d => {
if (d !== 'github.com') {
body = body.replace(new RegExp(`https?:\/\/${d.replace(/\./g, '\\.')}`, 'g'), `https://${PROXY_DOMAIN}/${d}`)
}
})
return new Response(body, { status: response.status, headers: response.headers })
}
        // 其他资源(图片、Release 二进制文件)直接返回流
return new Response(response.body, {
status: response.status,
headers: response.headers
})
} catch (e) {
return new Response(`Proxy Error: ${e.message}`, { status: 500 })
}
}


打赏 支付宝打赏 微信打赏

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。