Skip to content

launcher/usage: Serval inprovement to the new script #607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
71 changes: 49 additions & 22 deletions .vuepress/public/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@ log_W() { echo -e "\e[33m[!] $1\e[0m"; }
log_E() { echo -e "\e[31m[-] $1\e[0m"; }

ask_for_creds() {
read -e -p "请输入 SakuraFrp 的 访问密钥: " api_key
if [[ ${#api_key} -lt 16 ]]; then
log_E "访问密钥至少需要 16 字符, 请从管理面板直接复制粘贴"
exit 1
fi

read -e -p "请输入您希望使用的远程管理密码 (至少八个字符): " remote_pass
if [[ ${#remote_pass} -lt 8 ]]; then
log_E "远程管理密码至少需要 8 字符"
exit 1
fi

read -e -p "请再次输入远程管理密码: " remote_pass_confirm
if [[ $remote_pass != $remote_pass_confirm ]]; then
while true; do
read -e -s -p "请输入 SakuraFrp 的访问密钥, 请到 https://www.natfrp.com/user/ 获取" api_key
echo
api_key_respond=$(curl -LI -X 'GET' "https://api.natfrp.com/v4/user/info?token=${api_key}" -o /dev/null -w '%{http_code}\n' -s)
echo ${api_key_respond}
if [[ ${api_key_respond} -eq 200 ]]; then break; fi
log_E "访问密钥错误, 请到 https://www.natfrp.com/user/ 获取"
done

while true; do
while true; do
read -e -s -p "请输入远程管理密码(至少八个字符): " remote_pass
echo
if [[ ${#remote_pass} -ge 8 ]]; then break; fi
log_E "远程管理密码至少需要 8 字符"
done

read -e -s -p "请再次输入远程管理密码: " remote_pass_confirm
echo
if [[ "$remote_pass" == "$remote_pass_confirm" ]]; then break; fi
log_E "两次输入的远程管理密码不一致, 请确认知晓自己正在输入的内容"
exit 1
fi
done
}

check_executable() {
Expand Down Expand Up @@ -62,17 +67,36 @@ docker_install() {
log_W "已存在名为 natfrp-service 的容器"
read -p " - 是否移除已存在的容器? [y/N] " -r choice
if [[ $choice =~ ^[Yy]$ ]]; then
docker kill natfrp-service || log_W "无法停止 natfrp-service 容器, 将尝试直接移除"
docker stop natfrp-service &>/dev/null || docker kill natfrp-service &>/dev/null
docker rm natfrp-service
else
log_E "请手动移除已存在的容器后重新运行脚本"
exit 1
fi
fi

docker run -d --network=host --restart=on-failure:5 --pull=always --name=natfrp-service -v ${CONFIG_BASE}:/run -e "NATFRP_TOKEN=$api_key" -e "NATFRP_REMOTE=$remote_pass" natfrp.com/launcher || \
log_I "Docker 安装模式选择"
echo "请选择镜像来源:"
echo "1. 官方 (natfrp.com/launcher)"
echo "2. GitHub (ghcr.io/natfrp/launcher)"
echo "3. Docker Hub (natfrp/launcher)"
read -p "请输入选项 [1-3] (默认1): " source
case $source in
2) image="ghcr.io/natfrp/launcher" ;;
3) image="natfrp/launcher" ;;
*) image="natfrp.com/launcher" ;;
esac

docker run -d --network=host \
--restart=on-failure:5 \
--pull=always \
--name=natfrp-service \
-v ${CONFIG_BASE}:/run \
-e "NATFRP_TOKEN=$api_key" \
-e "NATFRP_REMOTE=$remote_pass" \
$image || \
(
log_E "Docker 模式安装失败, 请检查 Docker 在是否正常运行以及是否能正常访问 natfrp.com 拉取镜像"
log_E "Docker 模式安装失败, 请检查 Docker 在是否正常运行以及是否能正常访问镜像"
exit 1
)

Expand Down Expand Up @@ -198,7 +222,7 @@ EOF
- 查看日志\tjournalctl -u natfrp.service
\n请登录远程管理界面启动隧道: https://www.natfrp.com/remote/v2\n"

log_I "下方将输出启动器日志, 如需退出请按 Ctrl+C"
log_I "下方将输出启动器日志, 如需退出请按 Ctrl+C"
journalctl -u natfrp.service -f
}

Expand All @@ -221,8 +245,11 @@ uninstall() {
read -p " - 确认要卸载 SakuraFrp 启动器吗? [y/N] " -r choice
if [[ $choice =~ ^[Yy]$ ]]; then
if docker ps -a --format '{{.Names}}' | grep -q '^natfrp-service$'; then
docker kill natfrp-service &>/dev/null || log_W "无法停止 natfrp-service 容器, 将尝试直接删除"
docker stop natfrp-service &>/dev/null || docker kill natfrp-service &>/dev/null
docker rm natfrp-service &>/dev/null && log_I "已删除 Docker 容器"
else
log_E "无法删除 Docker 容器, 脚本已退出"
exit 1
fi

if [[ -f /etc/systemd/system/natfrp.service ]]; then
Expand Down Expand Up @@ -271,7 +298,7 @@ if docker info &>/dev/null && [[ $1 != "direct" ]]; then
fi

log_W "您正在使用非 Docker 安装模式"
echo "我们建议您总是使用 Docker 安装模式, 以便于管理和减少兼容性问题"
log_I "我们建议您总是使用 Docker 安装模式, 以便于管理和减少兼容性问题"

# Check SELinux
if command -v getenforce &>/dev/null; then
Expand Down