跳过正文

AUR 中 GitHub 资源拉取缓慢,无法拉取使用镜像的方法

·571 字·2 分钟
Arch Linux Archlinux Aur Aria2 Bash Github
沉渊覆雪
作者
沉渊覆雪
故事的结局早已定下,但途中的故事还未曾书写
目录

aurspeed脚本
#

保存以下代码到/usr/bin/aurspeed 记得chmod +x /usr/bin/aurspeed (脚本调用aria2)

#!/bin/bash
output_file="$1"
url="$2"

# 镜像站点列表, 每个镜像一行
MIRRORS=(
"https://gh-proxy.com"
"https://ghfast.top"
"https://ghproxy.net"
"https://ghproxy.cc"
"https://hub.gitmirror.com"
"https://cf.ghproxy.cc"
)
MIRRORS2=(
  "https://originfastly.jsdelivr.net"
  "https://testingcf.jsdelivr.net"
  "https://gcore.jsdelivr.net"
  "https://fastly.jsdelivr.net"
)
# Aria2 UA
USER_AGENT="curl/8.11.1"
# 重新下载的最大重试次数
MAX_RETRIES=3
RETRY_COUNT=0

download_file() {
  # 替换 github.com 链接
  local modified_url=$(echo "$url" | sed -e "s|https://github.com|$RANDOM_MIRROR/https://github.com|")

  # 处理 raw.githubusercontent.com 的链接,替换为 jsdelivr 镜像
  modified_url=$(echo "$modified_url" | sed -E "s|https://raw.githubusercontent.com/([^/]+)/([^/]+)/([^/]+)/|$RANDOM_MIRROR2/gh/\1/\2@\3/|")

  echo "正在下载: $modified_url"
  aria2c -c -j 8 -x 8 -s 8 --user-agent="$USER_AGENT" --out="$output_file" "$modified_url"

  # 检查下载是否成功
  if [[ $? -ne 0 ]]; then
    echo "下载失败, 重试..."
    return 1  # 下载失败,返回1
  else
    echo "下载成功"
    return 0  # 下载成功,返回0
  fi
}

# 重试下载过程
while [[ $RETRY_COUNT -lt $MAX_RETRIES ]]; do
  # 在每次重试之前重新随机选择镜像
  RANDOM_MIRROR=${MIRRORS[$RANDOM % ${#MIRRORS[@]}]}
  RANDOM_MIRROR2=${MIRRORS2[$RANDOM % ${#MIRRORS2[@]}]}

  if download_file; then
    break  # 如果下载成功,退出循环
  else
    # 如果下载失败,增加重试次数
    RETRY_COUNT=$((RETRY_COUNT + 1))
    echo "第 $RETRY_COUNT 次重试..."
  fi
done

# 如果下载依然失败,提示
if [[ $RETRY_COUNT -eq $MAX_RETRIES ]]; then
  echo "下载失败,已尝试 $MAX_RETRIES 次,请检查链接或网络状况。"
fi

配置makepkg
#

备份/etc/makepkg.conf sudo cp /etc/makepkg.conf /etc/makepkg.conf.bak 然后编辑/etc/makepkg.conf sudo nano /etc/makepkg.conf 修改以下部分:

DLAGENTS=('file::/usr/bin/curl -qgC - -o %o %u'
           'ftp::/usr/bin/curl -qgfC - --ftp-pasv --retry 3 --retry-delay 3 -o %o %u'
           'http::/usr/bin/curl -qgb "" -fLC - --retry 3 --retry-delay 3 -o %o %u'
           'https::/usr/bin/curl -qgb "" -fLC - --retry 3 --retry-delay 3 -o %o %u'
           'rsync::/usr/bin/rsync --no-motd -z %u %o'
           'scp::/usr/bin/scp -C %u %o')

修改为这样:

DLAGENTS=('file::/usr/bin/aurspeed %o %u'
          'ftp::/usr/bin/aurspeed %o %u'
          'http::/usr/bin/aurspeed %o %u'
          'https::/usr/bin/aurspeed %o %u'
          'rsync::/usr/bin/rsync --no-motd -z %u %o'
          'scp::/usr/bin/scp -C %u %o')

大功告成!

git clone配置镜像
#

tips:你还可以使用 git config --global url."https://ghfast.top/https://github.com/".insteadOf "https://github.com/" 来设置git clone使用镜像