Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions public/help/scripts/change-source/change-source.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/bin/bash

MIRROR_URL=${MIRROR_URL:-"mirrors.lcpu.dev"}
declare -g ZH_MODE=0
declare -g INTERACTIVE=0

# 设置临时目录
TMP_DIR=$(mktemp -d)
cleanup() {
rm -rf "$TMP_DIR"
}
trap cleanup EXIT

# 初始化语言设置
set_language() {
if [[ "$LANG" =~ "zh_CN" ]] || [[ "$1" == "zh" ]]; then
ZH_MODE=1
fi
}

# 消息打印函数
msg() {
if [ $ZH_MODE -eq 1 ]; then
echo -e "\033[34m$1\033[0m"
else
echo -e "\033[34m$2\033[0m"
fi
}

# 错误处理
error_exit() {
if [ $ZH_MODE -eq 1 ]; then
echo -e "\033[31m错误:$1\033[0m" >&2
else
echo -e "\033[31mError: $1\033[0m" >&2
fi
exit 1
}

# 确认提示
confirm() {
if [ $INTERACTIVE -eq 1 ]; then
local prompt_en=$1
local prompt_zh=$2
if [ $ZH_MODE -eq 1 ]; then
read -p "$prompt_zh [y/N] " -n 1 -r
else
read -p "$prompt_en [y/N] " -n 1 -r
fi
echo
[[ $REPLY =~ ^[Yy]$ ]]
else
return 0
fi
}

# 下载必要临时文件
download() {
if ! curl -fsSL "https://${MIRROR_URL}/distros.zip" -o "${TMP_DIR}/distros.zip"; then
error_exit "无法下载资源文件" "Failed to download resources"
fi
if ! unzip -q "${TMP_DIR}/distros.zip" -d "${TMP_DIR}"; then
error_exit "解压资源文件失败" "Failed to extract resources"
fi
}

# 分发到对应的发行版脚本
dispatch() {
source /etc/os-release
local script_path
case $ID in
ubuntu)
major_ver=$(echo $VERSION_ID | cut -d. -f1)
if [ $major_ver -ge 24 ]; then
script_path="${TMP_DIR}/distros/ubuntu24.sh"
elif [ $major_ver -ge 22 || $major_ver -ge 20 ]; then
script_path="${TMP_DIR}/distros/ubuntu.sh"
else
error_exit "不支持的 Ubuntu 版本:${VERSION_ID}" "Unsupported Ubuntu version: ${VERSION_ID}"
fi
;;
rocky)
major_ver=$(echo $VERSION_ID | cut -d. -f1)
if [ $major_ver -eq 8 ]; then
script_path="${TMP_DIR}/distros/rocky8.sh"
elif [ $major_ver -eq 9 ]; then
script_path="${TMP_DIR}/distros/rocky9.sh"
else
error_exit "不支持的 Rocky Linux 版本:${VERSION_ID}" "Unsupported Rocky Linux version: ${VERSION_ID}"
fi
;;
debian)
major_ver=$(echo $VERSION_ID | cut -d. -f1)
if [ $major_ver -eq 12 ]; then
script_path="${TMP_DIR}/distros/debian12.sh"
elif [ $major_ver -eq 11 ]; then
script_path="${TMP_DIR}/distros/debian11.sh"
else
error_exit "不支持的 Debian 版本:${VERSION_ID}" "Unsupported Debian version: ${VERSION_ID}"
fi
;;
arch)
script_path="${TMP_DIR}/distros/${ID}.sh"
;;
*)
error_exit "不支持的系统:${ID}" "Unsupported distribution: ${ID}"
;;
esac
if [ -f "$script_path" ]; then
msg "检测到 ${PRETTY_NAME}" "Detected ${PRETTY_NAME}"
source "$script_path"
else
error_exit "找不到发行版脚本:${script_path}" "Missing distribution script: ${script_path}"
fi
}

# 解析参数
while [[ $# -gt 0 ]]; do
case $1 in
--interactive) INTERACTIVE=1; shift ;;
--zh) set_language zh; shift ;;
*) shift ;;
esac
done

# 检查 root 权限
if [ $EUID -ne 0 ]; then
error_exit "需要 root 权限,请使用 sudo 执行" "Requires root privileges, please use sudo"
fi

download
dispatch
49 changes: 49 additions & 0 deletions public/help/scripts/change-source/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# 全局配置
MIRROR_URL=${MIRROR_URL:-"mirrors.lcpu.dev"}
declare -g ZH_MODE=0
declare -g INTERACTIVE=0

# 初始化语言设置
set_language() {
if [[ "$LANG" =~ "zh_CN" ]] || [[ "$1" == "zh" ]]; then
ZH_MODE=1
fi
}

# 消息打印函数
msg() {
if [ $ZH_MODE -eq 1 ]; then
echo -e "\033[34m$1\033[0m"
else
echo -e "\033[34m$2\033[0m"
fi
}

# 错误处理
error_exit() {
if [ $ZH_MODE -eq 1 ]; then
echo -e "\033[31m错误:$1\033[0m" >&2
else
echo -e "\033[31mError: $1\033[0m" >&2
fi
exit 1
}

# 确认提示
confirm() {
if [ $INTERACTIVE -eq 1 ]; then
local prompt_en=$1
local prompt_zh=$2
if [ $ZH_MODE -eq 1 ]; then
read -p "$prompt_zh [y/N] " -n 1 -r
else
read -p "$prompt_en [y/N] " -n 1 -r
fi
echo
[[ $REPLY =~ ^[Yy]$ ]]
else
return 0
fi
}
Binary file added public/help/scripts/change-source/distros.zip
Binary file not shown.
27 changes: 27 additions & 0 deletions public/help/scripts/change-source/distros/arch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

prepare() {
BACKUP_FILE="/etc/pacman.d/mirrorlist.bak"
TARGET_FILE="/etc/pacman.d/mirrorlist"
}

execute() {
msg "开始更换北大镜像源(Arch Linux)..." "Changing to PKU mirror for Arch Linux..."
if [ $INTERACTIVE -eq 1 ]; then
confirm "备份原始文件?" "Backup original files?" || return
fi
cp -f "$TARGET_FILE" "$BACKUP_FILE"
if [ $INTERACTIVE -eq 1 ]; then
confirm "应用镜像配置?" "Apply mirror configuration?" || return
fi
sed -i "1i Server = https://${MIRROR_URL}/archlinux/\\\$repo/os/\\\$arch" "$TARGET_FILE"
if pacman -Syy --noconfirm >/dev/null 2>&1; then
msg "镜像源更换成功!" "Mirror changed successfully!"
else
mv -f "$BACKUP_FILE" "$TARGET_FILE"
error_exit "换源失败,已恢复原配置" "Mirror change failed, restored original configuration"
fi
}

prepare
execute
28 changes: 28 additions & 0 deletions public/help/scripts/change-source/distros/debian11.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

prepare() {
BACKUP_FILE="/etc/apt/sources.list.bak"
TARGET_FILE="/etc/apt/sources.list"
}

execute() {
msg "开始更换北大镜像源(Debian 11)..." "Changing to PKU mirror for Debian 11..."
if [ $INTERACTIVE -eq 1 ]; then
confirm "备份原始文件?" "Backup original files?" || return
fi
cp -f "$TARGET_FILE" "$BACKUP_FILE"
if [ $INTERACTIVE -eq 1 ]; then
confirm "应用镜像配置?" "Apply mirror configuration?" || return
fi
sed -i "s|http://deb\.debian\.org|https://${MIRROR_URL}|g" $TARGET_FILE
sed -i "s|http://security\.debian\.org|https://${MIRROR_URL}|g" $TARGET_FILE
if apt-get update -yqq >/dev/null 2>&1; then
msg "镜像源更换成功!" "Mirror changed successfully!"
else
mv -f "$BACKUP_FILE" "$TARGET_FILE"
error_exit "换源失败,已恢复原配置" "Mirror change failed, restored original configuration"
fi
}

prepare
execute
35 changes: 35 additions & 0 deletions public/help/scripts/change-source/distros/debian12.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

prepare() {
TARGET_FILES=(
"/etc/apt/mirrors/debian.list"
"/etc/apt/mirrors/debian-security.list"
)
BACKUP_DIR="/etc/apt/mirrors/backup.d"
}

execute() {
msg "开始更换北大镜像源(Debian 12)..." "Changing to PKU mirror for Debian 12..."
if [ $INTERACTIVE -eq 1 ]; then
confirm "备份原始文件?" "Backup original files?" || return
fi
mkdir -p "$BACKUP_DIR"
msg "备份配置文件中..." "Backing up repository files..."
cp -f "${TARGET_FILES[@]}" "$BACKUP_DIR"
if [ $INTERACTIVE -eq 1 ]; then
confirm "应用镜像配置?" "Apply mirror configuration?" || return
fi
for file in "${TARGET_FILES[@]}"; do
sed -i "s/deb.debian.org/${MIRROR_URL}/g" "$file"
done
if apt-get update -yqq >/dev/null 2>&1; then
msg "镜像源更换成功!" "Mirror changed successfully!"
else
msg "正在恢复备份..." "Restoring backups..."
cp -f "$BACKUP_DIR"/* /etc/apt/mirrors
error_exit "换源失败,已恢复原配置" "Mirror change failed, restored original configuration"
fi
}

prepare
execute
39 changes: 39 additions & 0 deletions public/help/scripts/change-source/distros/rocky8.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

prepare() {
TARGET_FILES=(
"/etc/yum.repos.d/Rocky-AppStream.repo"
"/etc/yum.repos.d/Rocky-BaseOS.repo"
"/etc/yum.repos.d/Rocky-Extras.repo"
"/etc/yum.repos.d/Rocky-PowerTools.repo"
)
BACKUP_DIR="/etc/yum.repos.d/backup.d"
}

execute() {
msg "开始更换北大镜像源(Rocky Linux 8)..." "Changing to PKU mirror for Rocky Linux 8..."
if [ $INTERACTIVE -eq 1 ]; then
confirm "备份原始文件?" "Backup original sources?" || return
fi
mkdir -p "$BACKUP_DIR"
msg "备份配置文件中..." "Backing up repository files..."
cp -f "${TARGET_FILES[@]}" "$BACKUP_DIR"
if [ $INTERACTIVE -eq 1 ]; then
confirm "应用镜像配置?" "Apply mirror configuration?" || return
fi
for file in "${TARGET_FILES[@]}"; do
sed -i -e 's|^mirrorlist=|#mirrorlist=|g' \
-e "s|^#baseurl=http://dl.rockylinux.org|baseurl=https://${MIRROR_URL}|g" \
"$file"
done
if dnf clean all >/dev/null 2>&1 && dnf makecache >/dev/null 2>&1; then
msg "镜像源更换成功!" "Mirror changed successfully!"
else
msg "正在恢复备份..." "Restoring backups..."
cp -f "$BACKUP_DIR"/* /etc/yum.repos.d/
error_exit "换源失败,已恢复原配置" "Mirror change failed, restored original configuration"
fi
}

prepare
execute
37 changes: 37 additions & 0 deletions public/help/scripts/change-source/distros/rocky9.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

prepare() {
TARGET_FILES=(
"/etc/yum.repos.d/rocky.repo"
"/etc/yum.repos.d/rocky-extras.repo"
)
BACKUP_DIR="/etc/yum.repos.d/backup.d"
}

execute() {
msg "开始更换北大镜像源(Rocky Linux 9)..." "Changing to PKU mirror for Rocky Linux 9..."
if [ $INTERACTIVE -eq 1 ]; then
confirm "备份原始文件?" "Backup original sources?" || return
fi
mkdir -p "$BACKUP_DIR"
msg "备份配置文件中..." "Backing up repository files..."
cp -f "${TARGET_FILES[@]}" "$BACKUP_DIR"
if [ $INTERACTIVE -eq 1 ]; then
confirm "应用镜像配置?" "Apply mirror configuration?" || return
fi
for file in "${TARGET_FILES[@]}"; do
sed -i -e 's|^mirrorlist=|#mirrorlist=|g' \
-e "s|^#baseurl=http://dl.rockylinux.org|baseurl=https://${MIRROR_URL}|g" \
"$file"
done
if dnf clean all >/dev/null 2>&1 && dnf makecache >/dev/null 2>&1; then
msg "镜像源更换成功!" "Mirror changed successfully!"
else
msg "正在恢复备份..." "Restoring backups..."
cp -f "$BACKUP_DIR"/* /etc/yum.repos.d/
error_exit "换源失败,已恢复原配置" "Mirror change failed, restored original configuration"
fi
}

prepare
execute
27 changes: 27 additions & 0 deletions public/help/scripts/change-source/distros/ubuntu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

prepare() {
BACKUP_FILE="/etc/apt/sources.list.bak"
TARGET_FILE="/etc/apt/sources.list"
}

execute() {
msg "开始更换北大镜像源(Ubuntu 22.04 & 20.04)..." "Changing to PKU mirror for Ubuntu 22.04 & 20.04..."
if [ $INTERACTIVE -eq 1 ]; then
confirm "备份原始文件?" "Backup original sources?" || return
fi
cp -f "$TARGET_FILE" "$BACKUP_FILE"
if [ $INTERACTIVE -eq 1 ]; then
confirm "应用镜像配置?" "Apply mirror configuration?" || return
fi
sed -i "s@//.*archive.ubuntu.com@//${MIRROR_URL}@g" "$TARGET_FILE"
if apt-get update -yqq >/dev/null 2>&1; then
msg "镜像源更换成功!" "Mirror changed successfully!"
else
mv -f "$BACKUP_FILE" "$TARGET_FILE"
error_exit "换源失败,已恢复原配置" "Mirror change failed, restored original configuration"
fi
}

prepare
execute
Loading