Paru的增强下载脚本ParuAxel

ArchLinux是一个优秀的Linux发行版,但是在使用 Paru 安装AUR中的软件时,很多软件依赖于 Github 上的软件,但有时候它不能访问,为此本人开发了脚本ParuAxel.sh.

配置Github镜像

为了提高Github的访问速度,可以设置git的下载镜像网站,于是首先配置git如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[user]
	email = YourEmail
	name = YourName
; [url "https://521github.com/"]
; [url "https://githubfast.com/"]
; [url "https://git.homegu.com/"]
; [url "https://kkgithub.com/"]
; [url "https://github.hscsec.cn/"]
; [url "https://gitclone.com/github.com/"]
[url "git@github.com:"]
	insteadOf = https://github.com/

ParuAxel 脚本

{% tabs ParuAxel %}

 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
#! /bin/sh
#
# Program: ParuAxel.sh
# Version: V1.4
# Author : Zhen-Hua Feng
# Email  : fengzhenhua@outlook.com
# Date   : 2024-11-06 14:08
# Copyright (C) 2023 feng <feng@arch>
#
# Distributed under terms of the MIT license.
#
GIT_DOMIN=`echo $2 | cut -f3 -d'/'`;
GIT_OTHER=`echo $2 | cut -f4- -d'/'`;
GIT_INIT="https://github.com/"
GCF=/home/$USER/.gitconfig
GIT_MIR=$(grep "url \"http" $GCF)
GIT_SIT=(${GIT_MIR[*]//" "/""})
GIT_SIT=(${GIT_SIT[*]//";"/""})
GIT_SIT=(${GIT_SIT[*]//"[url\""/""})
GIT_SIT=(${GIT_SIT[*]//"\"]"/""})
GIT_DETECT(){
    wget --spider -T 5 -q -t 2 $1
}
i=0 ; j=0
case "$GIT_DOMIN" in
    "github.com")
        if [ -e $GCF ]; then
            while [[ $i -lt "${#GIT_SIT[*]}" ]]; do
                GIT_DETECT ${GIT_SIT[$i]}
                if [[ $? = 0 ]]; then
                    GIT_URL=${GIT_SIT[$i]}$GIT_OTHER
                    i=${#GIT_SIT[*]} ; j=1
                else
                    let i+=1
                fi
            done
            if [[ $j -eq 0 ]]; then
                GIT_URL="$GIT_INIT$GIT_OTHER"
            fi
        else
            GIT_URL="$GIT_INIT$GIT_OTHER"
        fi
        echo "Download from mirror $GIT_URL";
        # Single-threaded, enabled by default
        /usr/bin/curl -gqb "" -fLC - --retry 3 --retry-delay 3 -o $1 $GIT_URL;
        # Multi-threading, which may not be supported by some mirror websites
		# /usr/bin/axel -n 15 -a -o $1 $GIT_URL;
        ;;
    *)
        GIT_URL=$2;
        /usr/bin/axel -n 15 -a -o $1 $GIT_URL;
        ;;
esac

{% note primary %} 编写思路:首先探测\~/.gitconfig文件,提取所有的http镜像地址,然后按顺序测试镜像地址,遇到可以使用的镜像则设置为下载地址,同时停止继续探测。如果全部探测完成后没有发现可用地址,则仍然使用https://github.com的原始地址。由于不是所有镜像都支持axel多线程下载,所以默认开启curl下载。为了提高效率,当探测到可用镜像时就停止了继续探测其他镜像,但是这不能保证探测到的镜像速度最快,所以在此版中应当尽量将速度快的镜像排在\~/.gitconfig的前面,下一版计划实现自动速度的探测。 {% endnote %}

1
2
3
4
5
6
DLAGENTS=('file::/usr/bin/curl -qgC - -o %o %u'
          'ftp::/usr/bin/axel -n 15 -a -o %o %u'
          'http::/usr/bin/axel -n 15 -a -o %o %u'
          'https::/usr/bin/ParuAxel  %o %u'
          'rsync::/usr/bin/rsync --no-motd -z %u %o'
          'scp::/usr/bin/scp -C %u %o')

{% endtabs %}

具体设置

  • 将脚本ParuAxel.sh复制到/usr/bin/ParuAxel
1
sudo cp ./ParuAxel.sh /usr/bin/ParuAxel