优化解决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}`);
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值