Last active
June 6, 2025 14:32
-
-
Save lockcp/ae231bb84b79fbe5ccf70929a966df0f to your computer and use it in GitHub Desktop.
Revisions
-
lockcp revised this gist
Jun 6, 2025 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ CONFIG_PATH="$INSTALL_DIR/config.yaml" VERSION="v4.5.3" SERVICE_NAME="mosdns" SCRIPT_PATH="$(readlink -f "$0" 2>/dev/null || realpath "$0" 2>/dev/null || echo "$0")" CONFIG_URL="https://ghfast.top/https://gist.github.com/lockcp/99b6c9b4d4bdce70e49652eb40e4427e/raw/10cb89532ebbce27c9c7afd2c437fdb18ec1c6d4/config.yaml" # 检测初始化系统类型 function detect_init_system() { @@ -36,7 +36,7 @@ case "$ARCH" in *) echo "不支持的架构: $ARCH"; exit 1 ;; esac DOWNLOAD_URL="https://ghfast.top/https://github.com/IrineSistiana/mosdns/releases/download/${VERSION}/mosdns-linux-${MOSDNS_ARCH}.zip" # 判断是否需要sudo SUDO="" -
lockcp revised this gist
Jun 6, 2025 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ CONFIG_PATH="$INSTALL_DIR/config.yaml" VERSION="v4.5.3" SERVICE_NAME="mosdns" SCRIPT_PATH="$(readlink -f "$0" 2>/dev/null || realpath "$0" 2>/dev/null || echo "$0")" CONFIG_URL="https://ghproxy.com/https://gist.github.com/lockcp/99b6c9b4d4bdce70e49652eb40e4427e/raw/10cb89532ebbce27c9c7afd2c437fdb18ec1c6d4/config.yaml" # 检测初始化系统类型 function detect_init_system() { @@ -36,7 +36,7 @@ case "$ARCH" in *) echo "不支持的架构: $ARCH"; exit 1 ;; esac DOWNLOAD_URL="https://ghproxy.com/https://github.com/IrineSistiana/mosdns/releases/download/${VERSION}/mosdns-linux-${MOSDNS_ARCH}.zip" # 判断是否需要sudo SUDO="" -
lockcp created this gist
Jun 6, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,285 @@ #!/bin/bash set -e INSTALL_DIR="/opt/mosdns" BIN_PATH="$INSTALL_DIR/mosdns" CONFIG_PATH="$INSTALL_DIR/config.yaml" VERSION="v4.5.3" SERVICE_NAME="mosdns" SCRIPT_PATH="$(readlink -f "$0" 2>/dev/null || realpath "$0" 2>/dev/null || echo "$0")" CONFIG_URL="https://gist.github.com/lockcp/99b6c9b4d4bdce70e49652eb40e4427e/raw/10cb89532ebbce27c9c7afd2c437fdb18ec1c6d4/config.yaml" # 检测初始化系统类型 function detect_init_system() { if [ -f "/proc/1/comm" ] && grep -q "systemd" /proc/1/comm; then echo "systemd" elif [ -f "/etc/openwrt_release" ]; then echo "openwrt" elif [ -d "/etc/init.d" ]; then echo "init" else echo "unknown" fi } INIT_SYSTEM=$(detect_init_system) # 自动识别架构 ARCH=$(uname -m) case "$ARCH" in x86_64) MOSDNS_ARCH="amd64" ;; aarch64|arm64) MOSDNS_ARCH="arm64" ;; armv7l|armv7) MOSDNS_ARCH="armv7" ;; i386|i686) MOSDNS_ARCH="386" ;; mips64el) MOSDNS_ARCH="mips64le" ;; *) echo "不支持的架构: $ARCH"; exit 1 ;; esac DOWNLOAD_URL="https://github.com/IrineSistiana/mosdns/releases/download/${VERSION}/mosdns-linux-${MOSDNS_ARCH}.zip" # 判断是否需要sudo SUDO="" if command -v sudo &> /dev/null && [ "$(id -u)" -ne 0 ]; then SUDO="sudo" fi # 服务文件路径 if [ "$INIT_SYSTEM" = "systemd" ]; then SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" elif [ "$INIT_SYSTEM" = "openwrt" ] || [ "$INIT_SYSTEM" = "init" ]; then SERVICE_FILE="/etc/init.d/${SERVICE_NAME}" else SERVICE_FILE="" fi # 检查服务是否存在 function service_exists() { [ -n "$SERVICE_FILE" ] && [ -f "$SERVICE_FILE" ] } # 安装/重建系统服务 function install_service() { if [ -z "$SERVICE_FILE" ]; then echo "不支持的初始化系统,无法创建服务。" return 1 fi if [ "$INIT_SYSTEM" = "systemd" ]; then $SUDO tee "$SERVICE_FILE" >/dev/null <<EOF [Unit] Description=mosdns Service After=network.target [Service] Type=simple ExecStart=$BIN_PATH start --config $CONFIG_PATH Restart=on-failure User=root [Install] WantedBy=multi-user.target EOF $SUDO systemctl daemon-reload $SUDO systemctl enable "$SERVICE_NAME" elif [ "$INIT_SYSTEM" = "openwrt" ] || [ "$INIT_SYSTEM" = "init" ]; then $SUDO tee "$SERVICE_FILE" >/dev/null <<EOF #!/bin/sh /etc/rc.common START=99 STOP=10 BIN="$BIN_PATH" CONF="$CONFIG_PATH" start() { \$BIN start --config \$CONF & } stop() { killall mosdns 2>/dev/null } status() { if pidof mosdns >/dev/null; then echo "mosdns is running." return 0 else echo "mosdns is not running." return 1 fi } EOF $SUDO chmod +x "$SERVICE_FILE" $SUDO "$SERVICE_FILE" enable else echo "无法创建服务:未知的初始化系统" return 1 fi # 检查创建是否成功 if [ -f "$SERVICE_FILE" ]; then echo "mosdns 系统服务创建成功!" return 0 else echo "mosdns 系统服务创建失败!" return 1 fi } # 卸载系统服务 function uninstall_service() { if ! service_exists; then echo "服务文件不存在,无需卸载。" return 0 fi if [ "$INIT_SYSTEM" = "systemd" ]; then $SUDO systemctl stop "$SERVICE_NAME" 2>/dev/null || true $SUDO systemctl disable "$SERVICE_NAME" 2>/dev/null || true $SUDO rm -f "$SERVICE_FILE" $SUDO systemctl daemon-reload elif [ "$INIT_SYSTEM" = "openwrt" ] || [ "$INIT_SYSTEM" = "init" ]; then $SUDO "$SERVICE_FILE" stop 2>/dev/null || true $SUDO "$SERVICE_FILE" disable 2>/dev/null || true $SUDO rm -f "$SERVICE_FILE" fi echo "mosdns 系统服务已卸载。" } # 彻底清除 function full_cleanup() { uninstall_service $SUDO rm -rf "$INSTALL_DIR" [ -f "$SCRIPT_PATH" ] && rm -f "$SCRIPT_PATH" echo "mosdns 及相关文件已彻底清除。" exit 0 } # 首次安装检测(仅安装二进制,不自动装服务) if [ ! -f "$BIN_PATH" ]; then echo "首次运行,正在下载安装 mosdns v4.5.3 ..." WORKDIR="/tmp/mosdns_install" mkdir -p "$WORKDIR" cd "$WORKDIR" echo "下载: $DOWNLOAD_URL" if ! curl -L -o mosdns.zip "$DOWNLOAD_URL"; then echo "下载失败,请检查网络" exit 1 fi # OpenWrt需手动安装unzip if ! command -v unzip >/dev/null 2>&1; then if [ "$INIT_SYSTEM" = "openwrt" ]; then opkg update && opkg install unzip else echo "请先安装 unzip" exit 1 fi fi $SUDO mkdir -p "$INSTALL_DIR" $SUDO unzip -o mosdns.zip -d "$INSTALL_DIR" $SUDO chmod +x "$BIN_PATH" # 覆盖 config.yaml 并赋予 755 权限(仅首次安装) echo "正在下载并覆盖 config.yaml ..." if curl -L -o "$WORKDIR/config.yaml" "$CONFIG_URL"; then $SUDO cp "$WORKDIR/config.yaml" "$CONFIG_PATH" $SUDO chmod 755 "$CONFIG_PATH" echo "config.yaml 已覆盖并赋予 755 权限。" else echo "config.yaml 下载失败,请检查网络或手动配置。" fi echo "首次安装完成。config.yaml 已覆盖。" fi # 菜单 while true; do clear echo "========= mosdns v4.5.3 管理 =========" echo "1. 启动服务" echo "2. 重启服务" echo "3. 停止服务" echo "4. 查看状态" echo "5. 安装/重建 mosdns 系统服务" echo "6. 卸载 mosdns 系统服务" echo "7. 彻底清除 mosdns(含服务、程序、脚本本身)" echo "0. 退出" echo "=====================================" if service_exists; then echo "服务文件已存在: $SERVICE_FILE" else echo "服务文件不存在,可通过选项5安装/重建。" fi read -p "请输入选项 [0-7]: " choice case "$choice" in 1) if service_exists; then if [ "$INIT_SYSTEM" = "systemd" ]; then $SUDO systemctl start "$SERVICE_NAME" else $SUDO /etc/init.d/$SERVICE_NAME start fi else echo "服务文件不存在,请先用选项5安装/重建服务。" fi ;; 2) if service_exists; then if [ "$INIT_SYSTEM" = "systemd" ]; then $SUDO systemctl restart "$SERVICE_NAME" else $SUDO /etc/init.d/$SERVICE_NAME restart fi else echo "服务文件不存在,请先用选项5安装/重建服务。" fi ;; 3) if service_exists; then if [ "$INIT_SYSTEM" = "systemd" ]; then $SUDO systemctl stop "$SERVICE_NAME" else $SUDO /etc/init.d/$SERVICE_NAME stop fi else echo "服务文件不存在,请先用选项5安装/重建服务。" fi ;; 4) if service_exists; then if [ "$INIT_SYSTEM" = "systemd" ]; then $SUDO systemctl status "$SERVICE_NAME" else $SUDO /etc/init.d/$SERVICE_NAME status fi else echo "服务文件不存在,请先用选项5安装/重建服务。" fi ;; 5) echo "正在安装/重建 mosdns 系统服务..." if install_service; then echo "服务创建成功。" else echo "服务创建失败,请检查上方报错信息。" fi ;; 6) echo "正在卸载 mosdns 系统服务..." uninstall_service ;; 7) echo "正在彻底清除 mosdns 及相关文件..." full_cleanup ;; 0) echo "退出脚本。" exit 0 ;; *) echo "无效选项,请重新输入。" ;; esac echo read -p "按回车键继续..." done