1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
const ASSET_URL = 'https://raw.githubusercontent.com/'
const DEFAULT_PREFIX = '/'
const DEFAULT_JSDELIVR = 0
const DEFAULT_WHITE_LIST = ['/poychang/']
const PREFLIGHT_INIT = { status: 204, headers: { 'access-control-allow-origin': '*', 'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS', 'access-control-max-age': '1728000', }, }
const RE_RELEASES_ARCHIVE = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/i const RE_BLOB_RAW = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob|raw)\/.*$/i const RE_INFO_GIT = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-).*$/i const RE_RAW_HOST = /^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+?\/.+$/i const RE_GIST = /^(?:https?:\/\/)?gist\.(?:githubusercontent|github)\.com\/.+?\/.+?\/.+$/i const RE_TAGS = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/tags.*$/i
const ALL_PATTERNS = [ RE_RELEASES_ARCHIVE, RE_BLOB_RAW, RE_INFO_GIT, RE_RAW_HOST, RE_GIST, RE_TAGS, ]
function makeRes(body, status = 200, headers = {}) { return new Response(body, { status, headers: { ...headers, 'access-control-allow-origin': '*' }, }) }
function safeNewUrl(urlStr) { try { return new URL(urlStr) } catch { return null } }
function checkUrl(u) { return ALL_PATTERNS.some((re) => re.test(u)) }
async function fetchHandler(req) { const urlObj = new URL(req.url)
const q = urlObj.searchParams.get('q') if (q) { return Response.redirect('https://' + urlObj.host + DEFAULT_PREFIX + q, 301) }
const path = urlObj.href .slice(urlObj.origin.length + DEFAULT_PREFIX.length) .replace(/^https?:\/+/, 'https://')
if ( RE_RELEASES_ARCHIVE.test(path) || RE_INFO_GIT.test(path) || RE_GIST.test(path) || RE_TAGS.test(path) ) { return httpHandler(req, path) }
if (RE_BLOB_RAW.test(path)) { if (DEFAULT_JSDELIVR) { const redirectUrl = path .replace('/blob/', '@') .replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh') return Response.redirect(redirectUrl, 302) } return httpHandler(req, path.replace('/blob/', '/raw/')) }
if (RE_RAW_HOST.test(path)) { if (DEFAULT_JSDELIVR) { const redirectUrl = path .replace(/(?<=com\/.+?\/.+?)\/(.+?\/)/, '@$1') .replace( /^(?:https?:\/\/)?raw\.(?:githubusercontent|github)\.com/, 'https://cdn.jsdelivr.net/gh' ) return Response.redirect(redirectUrl, 302) } return httpHandler(req, path) }
if (/^https?:\/\//i.test(path)) { return makeRes('not found', 404) } return fetch(ASSET_URL + path) }
function httpHandler(req, pathname) { const reqHdrRaw = req.headers
if ( req.method === 'OPTIONS' && reqHdrRaw.has('access-control-request-headers') ) { return new Response(null, PREFLIGHT_INIT) }
if (DEFAULT_WHITE_LIST.length > 0 && !DEFAULT_WHITE_LIST.some((w) => pathname.includes(w))) { return new Response('blocked', { status: 403 }) }
const urlStr = /^https?:\/\//i.test(pathname) ? pathname : 'https://' + pathname const urlObj = safeNewUrl(urlStr) if (!urlObj) { return makeRes('invalid url: ' + urlStr, 400) }
const reqInit = { method: req.method, headers: new Headers(reqHdrRaw), redirect: 'manual', body: req.body, } if (req.body) { reqInit.duplex = 'half' }
return proxy(urlObj, reqInit) }
async function proxy(urlObj, reqInit) { const res = await fetch(urlObj.href, reqInit) const resHdrNew = new Headers(res.headers)
const location = resHdrNew.get('location') if (location) { if (checkUrl(location)) { resHdrNew.set('location', DEFAULT_PREFIX + location) } else { return fetch(location, { ...reqInit, redirect: 'follow' }) } }
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: res.status, headers: resHdrNew, }) }
export default {
async fetch(request) { try { return await fetchHandler(request) } catch (err) { const stack = err instanceof Error ? err.stack : String(err) return makeRes('cfworker error:\n' + stack, 502) } }, }
|