优化解决github脚本安装无法clone库问题

为什么有这个项目?

我经常切换设备的时候,每次都需要手动安装各种依赖:nvm、oh-my-zsh、pyenv等
每次都会发现安装脚本由于是github的地址,每次要不是很慢就是clone失败。
为了成功安装脚本要不是去找国内的版本,要不就是设置github的ddns。
就很麻烦。这就是这个项目诞生的缘由

如何使用?

例如:安装oh-my-zsh为例子
这是官网提供给我们的地址:

 sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" 

只需要在https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh地址前面加上https://shell.oimi.space即可,完整实例如下

 sh -c "$(curl -fsSL https://shell.oimi.space/https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" 

实现原理以及效果

本质上你请求https://shell.oimi.space/带上github地址之后,服务会去获取到请求地址,然后去请求脚本的内容,并将脚本内的关于github的仓库的地址,都加上https://nn.oimi.space这个前缀,这个前缀是一个github的加速clone服务走的是cloudflare的CDN。以此来加速脚本的安装。

部分代码展示以及github的源码地址:

shell-cn Github 服务是托管在vercel上面的。
Clone加速服务项目:gh-proxy

import {
    
     Response, Request } from "express"
import * as express from 'express'
import fetch from 'node-fetch';
import 'dotenv/config'

const PORT = process.env.PORT || 3000
const ORIGIN_PROXY = process.env.ORIGIN_PROXY || 'https://nn.oimi.space'
const app = express()
const isUrl = (url: string) => {
    
    
    try {
    
    
        new URL(url)
        return true
    } catch (e) {
    
    
        return false
    }
}
const handlerGithubURL = (content: string, proxy: string) => {
    
    
    const handerKeys = ['https://github.com', 'https://raw.githubusercontent.com']
    handerKeys.forEach(key => {
    
    
        content = content.replaceAll(key, `${
      
      proxy}/${
      
      key}`)
    })
    return content
}
const getSourceData = (url: string, proxy: string) => {
    
    
    return new Promise(async (resolve, reject) => {
    
    
        try {
    
    
            const response = await fetch(`${
      
      proxy}/${
      
      url}`)
            const text = await response.text();
            resolve(handlerGithubURL(text, proxy))
        } catch(e) {
    
    
            reject(e)
        }
    })
}


app.use('/', async (req: Request, res: Response, next: Function) => {
    
    
    let baseURL = req.originalUrl.slice(1)
    if (baseURL === 'favicon.ico') {
    
    
        next()
    } else {
    
    
        console.log(req.headers['accept-language'])
        if (!isUrl(baseURL)) {
    
    
            res.end("please provide correct url")
        } else {
    
    
            const BASEURL = new URL(baseURL)
            const sourceURL = baseURL.replace(BASEURL.search, '')
            const searchParams = new URLSearchParams(BASEURL.search);
            const PROXY_WEBURL = searchParams.get('proxy') ?? ORIGIN_PROXY
            if (!sourceURL) {
    
    
                res.send("please provide correct shell script url:" + sourceURL)
            } else {
    
    
                try {
    
    
                    const shellContent = await getSourceData(sourceURL, PROXY_WEBURL)
                    res.end(shellContent)
                } catch(e) {
    
    
                    res.status(400)
                    res.send('cant resolve this url: ' + sourceURL + '\Nerror:' + e)
                }
            }
        }
    }
})

app.listen(PORT, () => {
    
    
    console.log(`server is running on: ${
      
      PORT}`);
})