mirror of https://github.com/hunshcn/gh-proxy
				
				
				
			增加raw.githubusercontent.com支持
							parent
							
								
									327d22a30d
								
							
						
					
					
						commit
						4a3fe663f4
					
				| 
						 | 
				
			
			@ -68,6 +68,7 @@ python版本的机器如果无法正常访问github.io会启动报错,请自
 | 
			
		|||
 | 
			
		||||
## Changelog
 | 
			
		||||
 | 
			
		||||
* 2020.04.10 增加对`raw.githubusercontent.com`文件的支持
 | 
			
		||||
* 2020.04.09 增加Python版本(使用Flask)
 | 
			
		||||
* 2020.03.23 新增了clone的支持
 | 
			
		||||
* 2020.03.22 初始版本
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										15
									
								
								index.js
								
								
								
								
							
							
						
						
									
										15
									
								
								index.js
								
								
								
								
							| 
						 | 
				
			
			@ -58,23 +58,26 @@ async function fetchHandler(e) {
 | 
			
		|||
    const urlStr = req.url
 | 
			
		||||
    const urlObj = new URL(urlStr)
 | 
			
		||||
    let path = urlObj.searchParams.get('q')
 | 
			
		||||
    if(path)
 | 
			
		||||
    {
 | 
			
		||||
    if (path) {
 | 
			
		||||
        return Response.redirect('https://' + urlObj.host + '/' + path, 301)
 | 
			
		||||
    }
 | 
			
		||||
    // cfworker 会把路径中的 `//` 合并成 `/`
 | 
			
		||||
    path = urlObj.href.substr(urlObj.origin.length + 1).replace(/^https?:\/+/, 'https://')
 | 
			
		||||
    const exp = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/
 | 
			
		||||
    const exp1 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:releases|archive)\/.*$/
 | 
			
		||||
    const exp2 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:blob)\/.*$/
 | 
			
		||||
    const exp3 = /^(?:https?:\/\/)?github\.com\/.+?\/.+?\/(?:info|git-upload-pack).*$/
 | 
			
		||||
    if (path.search(exp)===0 || !Config.jsdelivr && path.search(exp2)===0 || !Config.cnpmjs && path.search(exp3)===0) {
 | 
			
		||||
    const exp4 = /^(?:https?:\/\/)?raw\.githubusercontent\.com\/.+?\/.+?\/.+?\/.+$/
 | 
			
		||||
    if (path.search(exp1) === 0 || !Config.jsdelivr && path.search(exp2) === 0 || !Config.cnpmjs && (path.search(exp3) === 0 || path.search(exp4))) {
 | 
			
		||||
        return httpHandler(req, path)
 | 
			
		||||
    }else if(path.search(exp2)===0) {
 | 
			
		||||
    } else if (path.search(exp2) === 0) {
 | 
			
		||||
        const newUrl = path.replace('/blob/', '@').replace(/^(?:https?:\/\/)?github\.com/, 'https://cdn.jsdelivr.net/gh')
 | 
			
		||||
        return Response.redirect(newUrl, 302)
 | 
			
		||||
    }else if (path.search(exp3)===0){
 | 
			
		||||
    } else if (path.search(exp3) === 0) {
 | 
			
		||||
        const newUrl = path.replace(/^(?:https?:\/\/)?github\.com/, 'https://github.com.cnpmjs.org')
 | 
			
		||||
        return Response.redirect(newUrl, 302)
 | 
			
		||||
    } else if (path.search(exp4) === 0) {
 | 
			
		||||
        const newUrl = path.replace(/(?<=com\/.+?\/.+?)\/(.+?\/)/, '@$1').replace(/^(?:https?:\/\/)?raw\.githubusercontent\.com/, 'https://cdn.jsdelivr.net/gh')
 | 
			
		||||
        return Response.redirect(newUrl, 302)
 | 
			
		||||
    } else {
 | 
			
		||||
        return fetch(ASSET_URL + path)
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue