Files
mihomo-run/run.sh

114 lines
3.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
MIHOMO_CTL_AUTH="mihomo-run-123321"
MIHOMO_URL="https://github.com/MetaCubeX/mihomo/releases/download/v1.19.12/mihomo-linux-amd64-v3-v1.19.12.gz"
MIHOMO_UI_URL="https://github.com/MetaCubeX/metacubexd/releases/download/v1.190.1/compressed-dist.tgz"
echo "mihomo-run (version:1.0)"
echo "ui controller: http://localhost:19090/ui"
echo "ui secret: $MIHOMO_CTL_AUTH"
MH_DIR=$(dirname "$(readlink -f "$0")")
# 检查函数检查核心文件、UI文件和进程状态
mihomo_check_environment() {
# Core Check
if [ ! -f "$MH_DIR/bin/mihomo" ]; then
echo "=> Mihomo core is not found. Will download..."
mkdir -p "$MH_DIR/bin"
wget --show-progress -q "$MIHOMO_URL" -O "$MH_DIR/bin/mihomo.gz"
if [ $? -eq 0 ]; then
gunzip -f "$MH_DIR/bin/mihomo.gz"
chmod +x "$MH_DIR/bin/mihomo"
else
echo "=> Mihomo core was failed to download!"
exit 1
fi
fi
# UI Check
if [ ! -d "$MH_DIR/ui" ]; then
echo "=> UI static files are not found. Will download..."
wget --show-progress -q "$MIHOMO_UI_URL" -O "$MH_DIR/ui.tgz"
if [ $? -eq 0 ]; then
mkdir -p "$MH_DIR/ui"
tar -zxf "$MH_DIR/ui.tgz" -C "$MH_DIR/ui"
rm "$MH_DIR/ui.tgz*"
else
echo "=> Mihomo UI tar was failed to download!"
exit 1
fi
fi
# mihomo process Check
if pgrep -f "$MH_DIR/bin/mihomo" >/dev/null; then
echo "=> Mihomo is already running!"
exit 1
fi
}
# Args解析
case "$1" in
"")
# 执行环境检查
mihomo_check_environment
# 如果没有任何参数,则应用配置 default
CONFIG_FILE="$MH_DIR/profiles/default.yaml"
if [ ! -f "$CONFIG_FILE" ]; then
echo "=> Default config file not found: $CONFIG_FILE"
exit 1
fi
echo "=> Starting mihomo with default config..."
"$MH_DIR/bin/mihomo" -d "$MH_DIR/data" -f "$CONFIG_FILE" -ext-ui "$MH_DIR/ui" -ext-ctl 0.0.0.0:19090 -secret "$MIHOMO_CTL_AUTH"
;;
"use")
# 执行环境检查
mihomo_check_environment
# 如果有use则解析$2 对应的是profiles中的对应的配置文件(不需要带.yaml 后缀)
if [ -z "$2" ]; then
echo "=> Please specify a profile name"
exit 1
fi
CONFIG_FILE="$MH_DIR/profiles/$2.yaml"
if [ ! -f "$CONFIG_FILE" ]; then
echo "=> Config file not found: $CONFIG_FILE"
exit 1
fi
echo "=> Starting mihomo with profile: $2"
"$MH_DIR/bin/mihomo" -d "$MH_DIR/data" -f "$CONFIG_FILE" -ext-ui "$MH_DIR/ui" -ext-ctl 0.0.0.0:19090 -secret "$MIHOMO_CTL_AUTH"
;;
"list")
# list则为展示所有的profiles
echo "=> Available profiles:"
ls "$MH_DIR/profiles"/*.yaml | sed 's/.*profiles\/\(.*\)\.yaml/\1/'
;;
"stop")
# stop则是终止mihomo进程
echo "=> Stopping mihomo..."
pkill -f "$MH_DIR/bin/mihomo"
;;
"help")
# help 打印帮助信息
echo "Usage: $0 [command] [options]"
echo ""
echo "Commands:"
echo " (no args) Start mihomo with default config"
echo " use [name] Start mihomo with specified profile"
echo " list List all available profiles"
echo " stop Stop mihomo process"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " $0 Start with default config"
echo " $0 use myconf Start with 'myconf' profile"
echo " $0 list Show all profiles"
echo " $0 stop Stop mihomo"
;;
*)
echo "Invalid argument: $1"
echo "Use '$0 help' to see available commands"
exit 1
;;
esac