github 国内代理访问下载

本页内容

演示代理

  1. 前缀

https://github.jobcher.com/gh/

  1. 下载仓库

git clone https://github.jobcher.com/gh/<你要下载的GitHub地址>

1#例子
2git clone https://github.jobcher.com/gh/https://github.com/jobcher/blog.git

部署

复制js到cloudflare worker

  1'use strict'
  2
  3/**
  4 * static files (404.html, sw.js, conf.js)
  5 */
  6const ASSET_URL = 'https://jobcher.github.io/'
  7// 前缀,如果自定义路由为example.com/gh/*,将PREFIX改为 '/gh/',注意,少一个杠都会错!
  8const PREFIX = '/gh/'
  9// 分支文件使用jsDelivr镜像的开关,0为关闭,默认关闭
 10const Config = {
 11    jsdelivr: 0
 12}
 13
 14const whiteList = [] // 白名单,路径里面有包含字符的才会通过,e.g. ['/username/']
 15
 16/** @type {RequestInit} */
 17const 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
 27const exp1 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/i
 28const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob|raw)\/.*$/i
 29const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-).*$/i
 30const exp4 = /^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+?\/.+$/i
 31const exp5 = /^(?:https?:\/\/)?gist\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+$/i
 32const exp6 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/tags.*$/i
 33
 34/**
 35 * @param {any} body
 36 * @param {number} status
 37 * @param {Object<string, string>} headers
 38 */
 39function 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 */
 48function newUrl(urlStr) {
 49    try {
 50        return new URL(urlStr)
 51    } catch (err) {
 52        return null
 53    }
 54}
 55
 56
 57addEventListener('fetch', e => {
 58    const ret = fetchHandler(e)
 59        .catch(err => makeRes('cfworker error:\n' + err.stack, 502))
 60    e.respondWith(ret)
 61})
 62
 63
 64function 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/**
 74 * @param {FetchEvent} e
 75 */
 76async function fetchHandler(e) {
 77    const req = e.request
 78    const urlStr = req.url
 79    const urlObj = new URL(urlStr)
 80    let path = urlObj.searchParams.get('q')
 81    if (path) {
 82        return Response.redirect('https://' + urlObj.host + PREFIX + path, 301)
 83    }
 84    // cfworker 会把路径中的 `//` 合并成 `/`
 85    path = urlObj.href.substr(urlObj.origin.length + PREFIX.length).replace(/^https?:\/+/, 'https://')
 86    if (path.search(exp1) === 0 || path.search(exp5) === 0 || path.search(exp6) === 0 || path.search(exp3) === 0 || path.search(exp4) === 0) {
 87        return httpHandler(req, path)
 88    } else if (path.search(exp2) === 0) {
 89        if (Config.jsdelivr) {
 90            const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh')
 91            return Response.redirect(newUrl, 302)
 92        } else {
 93            path = path.replace('/blob/', '/raw/')
 94            return httpHandler(req, path)
 95        }
 96    } else if (path.search(exp4) === 0) {
 97        const newUrl = path.replace(/(?<=com\/.+?\/.+?)\/(.+?\/)/, '@$1').replace(/^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com/, 'https://cdn.jsdelivr.net/gh')
 98        return Response.redirect(newUrl, 302)
 99    } else {
100        return fetch(ASSET_URL + path)
101    }
102}
103
104
105/**
106 * @param {Request} req
107 * @param {string} pathname
108 */
109function httpHandler(req, pathname) {
110    const reqHdrRaw = req.headers
111
112    // preflight
113    if (req.method === 'OPTIONS' &&
114        reqHdrRaw.has('access-control-request-headers')
115    ) {
116        return new Response(null, PREFLIGHT_INIT)
117    }
118
119    const reqHdrNew = new Headers(reqHdrRaw)
120
121    let urlStr = pathname
122    let flag = !Boolean(whiteList.length)
123    for (let i of whiteList) {
124        if (urlStr.includes(i)) {
125            flag = true
126            break
127        }
128    }
129    if (!flag) {
130        return new Response("blocked", {status: 403})
131    }
132    if (urlStr.startsWith('github')) {
133        urlStr = 'https://' + urlStr
134    }
135    const urlObj = newUrl(urlStr)
136
137    /** @type {RequestInit} */
138    const reqInit = {
139        method: req.method,
140        headers: reqHdrNew,
141        redirect: 'manual',
142        body: req.body
143    }
144    return proxy(urlObj, reqInit)
145}
146
147
148/**
149 *
150 * @param {URL} urlObj
151 * @param {RequestInit} reqInit
152 */
153async function proxy(urlObj, reqInit) {
154    const res = await fetch(urlObj.href, reqInit)
155    const resHdrOld = res.headers
156    const resHdrNew = new Headers(resHdrOld)
157
158    const status = res.status
159
160    if (resHdrNew.has('location')) {
161        let _location = resHdrNew.get('location')
162        if (checkUrl(_location))
163            resHdrNew.set('location', PREFIX + _location)
164        else {
165            reqInit.redirect = 'follow'
166            return proxy(newUrl(_location), reqInit)
167        }
168    }
169    resHdrNew.set('access-control-expose-headers', '*')
170    resHdrNew.set('access-control-allow-origin', '*')
171
172    resHdrNew.delete('content-security-policy')
173    resHdrNew.delete('content-security-policy-report-only')
174    resHdrNew.delete('clear-site-data')
175
176    return new Response(res.body, {
177        status,
178        headers: resHdrNew,
179    })
180}

另外一种方法

在你有科学上网的前提下使用代理方式来连接github

1git config --global https.proxy http://127.0.0.1:1080
2#取消设置
3git config --global --unset https.proxy

http://127.0.0.1:1080 是你的代理服务地址