refactorcode

This commit is contained in:
Your Name 2024-05-18 20:04:25 +08:00
parent 6e636b6b74
commit 6c7bf182da

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
CONTAINER_NAME="myztplanet" CONTAINER_NAME="myztplanet"
ZEROTIER_PATH="$(pwd)/data/zerotier" ZEROTIER_PATH="$(pwd)/data/zerotier"
CONFIG_PATH="${ZEROTIER_PATH}/config" CONFIG_PATH="${ZEROTIER_PATH}/config"
@ -6,19 +7,25 @@ DIST_PATH="${ZEROTIER_PATH}/dist"
ZTNCUI_PATH="${ZEROTIER_PATH}/ztncui" ZTNCUI_PATH="${ZEROTIER_PATH}/ztncui"
DOCKER_IMAGE="xubiaolin/zerotier-planet:latest" DOCKER_IMAGE="xubiaolin/zerotier-planet:latest"
print_message() {
local message=$1
local color_code=$2
echo -e "\033[${color_code}m${message}\033[0m"
}
# 检查内核版本 # 检查内核版本
kernel_check() { kernel_check() {
os_name=$(grep ^ID= /etc/os-release | cut -d'=' -f2) os_name=$(grep ^ID= /etc/os-release | cut -d'=' -f2 | tr -d '"')
kernel_version=$(uname -r | cut -d'.' -f1) kernel_version=$(uname -r | cut -d'.' -f1)
if [[ "$kernel_version" -lt 5 ]]; then if ((kernel_version < 5)); then
if [[ "$os_name" == "\"centos\"" ]]; then if [[ "$os_name" == "centos" ]]; then
echo -e "\033[31m内核版本太低,请在菜单中选择CentOS内核升级\033[0m" print_message "内核版本太低,请在菜单中选择CentOS内核升级" "31"
else else
echo -e "\033[31m请自行升级系统内核到5.*及其以上版本\033[0m" print_message "请自行升级系统内核到5.*及其以上版本" "31"
fi fi
exit 1 exit 1
else else
echo -e "\033[32m系统和内核版本检查通过当前内核版本为$kernel_version\033[0m" print_message "系统和内核版本检查通过,当前内核版本为:$kernel_version" "32"
fi fi
} }
@ -50,11 +57,11 @@ update_centos_kernel() {
# 安装lsof工具 # 安装lsof工具
install_lsof() { install_lsof() {
if [ ! -f "/usr/bin/lsof" ]; then if ! command -v lsof &>/dev/null; then
echo "开始安装lsof工具..." echo "开始安装lsof工具..."
if [ -f "/usr/bin/apt" ]; then if command -v apt &>/dev/null; then
apt update && apt install -y lsof apt update && apt install -y lsof
elif [ -f "/usr/bin/yum" ]; then elif command -v yum &>/dev/null; then
yum install -y lsof yum install -y lsof
fi fi
fi fi
@ -63,7 +70,7 @@ install_lsof() {
# 检查端口是否被占用 # 检查端口是否被占用
check_port() { check_port() {
local port=$1 local port=$1
if [ $(lsof -i:${port} | wc -l) -gt 0 ]; then if lsof -i:${port} &>/dev/null; then
echo "端口${port}已被占用,请重新输入" echo "端口${port}已被占用,请重新输入"
exit 1 exit 1
fi fi
@ -73,9 +80,10 @@ check_port() {
read_port() { read_port() {
local port local port
local prompt=$1 local prompt=$1
read -p "${prompt}" port while :; do
while [[ ! "$port" =~ ^[0-9]+$ ]]; do read -p "${prompt}" port
read -p "端口号必须是数字,请重新输入: " port [[ "$port" =~ ^[0-9]+$ ]] && break
echo "端口号必须是数字,请重新输入: "
done done
check_port $port check_port $port
echo $port echo $port
@ -93,15 +101,14 @@ configure_ip() {
install() { install() {
kernel_check kernel_check
# 如果容器已经存在,检查是否有新版本 if docker inspect ${CONTAINER_NAME} &>/dev/null; then
docker inspect ${CONTAINER_NAME} >/dev/null 2>&1 && {
echo "容器${CONTAINER_NAME}已经存在" echo "容器${CONTAINER_NAME}已经存在"
read -p "是否更新版本?(y/n) " update_version read -p "是否更新版本?(y/n) " update_version
if [[ "$update_version" =~ ^[Yy]$ ]]; then if [[ "$update_version" =~ ^[Yy]$ ]]; then
upgrade upgrade
exit 0 exit 0
fi fi
} fi
echo "开始安装如果你已经安装了将会删除旧的数据10秒后开始安装..." echo "开始安装如果你已经安装了将会删除旧的数据10秒后开始安装..."
sleep 10 sleep 10
@ -175,7 +182,6 @@ install() {
} }
install_from_config() { install_from_config() {
# 判断CONFIG_PATH是否存在且不为空
if [ ! -d "${CONFIG_PATH}" ] || [ ! "$(ls -A ${CONFIG_PATH})" ]; then if [ ! -d "${CONFIG_PATH}" ] || [ ! "$(ls -A ${CONFIG_PATH})" ]; then
echo "配置文件目录不存在或为空,请先上传配置文件" echo "配置文件目录不存在或为空,请先上传配置文件"
exit 1 exit 1
@ -186,7 +192,6 @@ install_from_config() {
cat ${CONFIG_PATH}/${config_name} | tr -d '\r' cat ${CONFIG_PATH}/${config_name} | tr -d '\r'
} }
# 从data/zerotier/config里面取
ipv4=$(extract_config "ip_addr4") ipv4=$(extract_config "ip_addr4")
ipv6=$(extract_config "ip_addr6") ipv6=$(extract_config "ip_addr6")
API_PORT=$(extract_config "ztncui.port") API_PORT=$(extract_config "ztncui.port")
@ -225,22 +230,19 @@ install_from_config() {
} }
upgrade() { upgrade() {
#如果容器不存在,报错 if ! docker inspect ${CONTAINER_NAME} &>/dev/null; then
docker inspect ${CONTAINER_NAME} >/dev/null 2>&1 || {
echo "容器${CONTAINER_NAME}不存在,请先安装" echo "容器${CONTAINER_NAME}不存在,请先安装"
exit 1 exit 1
} fi
# 从dockerhub pull最新的镜像,比较镜像id,判断镜像是否有新版本
docker pull ${DOCKER_IMAGE} docker pull ${DOCKER_IMAGE}
new_image_id=$(docker inspect ${DOCKER_IMAGE} --format='{{.Id}}') new_image_id=$(docker inspect ${DOCKER_IMAGE} --format='{{.Id}}')
old_image_id=$(docker inspect ${CONTAINER_NAME} --format='{{.Image}}') old_image_id=$(docker inspect ${CONTAINER_NAME} --format='{{.Image}}')
if [ "$new_image_id" == "$old_image_id" ]; then if [ "$new_image_id" == "$old_image_id" ]; then
echo -e "\033[32m当前版本已经是最新版本\033[0m" print_message "当前版本已经是最新版本" "32"
exit 0 exit 0
else else
echo "发现新版本,开始升级...new_image_id:${new_image_id},old_image_id:${old_image_id}" echo "发现新版本,开始升级...new_image_id:${new_image_id},old_image_id:${old_image_id}"
# 提示数据备份
echo "更新可能存在风险请手动备份data目录中的数据,谨慎操作" echo "更新可能存在风险请手动备份data目录中的数据,谨慎操作"
read -p "是否继续升级?(y/n) " continue_upgrade read -p "是否继续升级?(y/n) " continue_upgrade
if [[ ! "$continue_upgrade" =~ ^[Yy]$ ]]; then if [[ ! "$continue_upgrade" =~ ^[Yy]$ ]]; then
@ -256,43 +258,38 @@ upgrade() {
install_from_config install_from_config
} }
# 查看信息
info() { info() {
docker inspect ${CONTAINER_NAME} >/dev/null 2>&1 || { if ! docker inspect ${CONTAINER_NAME} &>/dev/null; then
echo "容器${CONTAINER_NAME}不存在,请先安装" echo "容器${CONTAINER_NAME}不存在,请先安装"
exit 1 exit 1
} fi
extract_config() { extract_config() {
local config_name=$1 local config_name=$1
cat ${CONFIG_PATH}/${config_name} | tr -d '\r' cat ${CONFIG_PATH}/${config_name} | tr -d '\r'
# docker exec -it ${CONTAINER_NAME} sh -c "cat /app/config/${config_name}" | tr -d '\r'
} }
# 从data/zerotier/config里面取
ipv4=$(extract_config "ip_addr4") ipv4=$(extract_config "ip_addr4")
ipv6=$(extract_config "ip_addr6") ipv6=$(extract_config "ip_addr6")
API_PORT=$(extract_config "ztncui.port") API_PORT=$(extract_config "ztncui.port")
FILE_PORT=$(extract_config "file_server.port") FILE_PORT=$(extract_config "file_server.port")
ZT_PORT=$(extract_config "zerotier-one.port") ZT_PORT=$(extract_config "zerotier-one.port")
KEY=$(extract_config "file_server.key") KEY=$(extract_config "file_server.key")
MOON_NAME=$(ls ${DIST_PATH}/ | grep moon | tr -d '\r') MOON_NAME=$(ls ${DIST_PATH}/ | grep moon | tr -d '\r')
echo "---------------------------" echo "---------------------------"
echo "以下端口的tcp和udp协议请放行${ZT_PORT}${API_PORT}${FILE_PORT}" print_message "以下端口的tcp和udp协议请放行${ZT_PORT}${API_PORT}${FILE_PORT}" "32"
echo "---------------------------" echo "---------------------------"
echo "请访问 http://${ipv4}:${API_PORT} 进行配置" echo "请访问 http://${ipv4}:${API_PORT} 进行配置"
echo "默认用户名admin" echo "默认用户名admin"
echo "默认密码password" echo "默认密码password"
echo "请及时修改密码" echo "请及时修改密码"
echo "---------------------------" echo "---------------------------"
echo "moon配置和planet配置在 ${DIST_PATH} 目录下" print_message "moon配置和planet配置在 ${DIST_PATH} 目录下" "32"
echo "planet文件下载 http://${ipv4}:${FILE_PORT}/planet?key=${KEY} " print_message "planet文件下载 http://${ipv4}:${FILE_PORT}/planet?key=${KEY} " "32"
echo "moon文件下载 http://${ipv4}:${FILE_PORT}/${MOON_NAME}?key=${KEY} " print_message "moon文件下载 http://${ipv4}:${FILE_PORT}/${MOON_NAME}?key=${KEY} " "32"
} }
# 卸载zerotier-planet
uninstall() { uninstall() {
echo "开始卸载..." echo "开始卸载..."
@ -308,7 +305,6 @@ uninstall() {
echo "卸载完成" echo "卸载完成"
} }
# 重置密码
resetpwd() { resetpwd() {
docker exec -it ${CONTAINER_NAME} sh -c 'cp /app/ztncui/src/etc/default.passwd /app/ztncui/src/etc/passwd' docker exec -it ${CONTAINER_NAME} sh -c 'cp /app/ztncui/src/etc/default.passwd /app/ztncui/src/etc/passwd'
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -328,7 +324,6 @@ resetpwd() {
echo "--------------------------------" echo "--------------------------------"
} }
# 菜单
menu() { menu() {
echo "欢迎使用zerotier-planet脚本请选择需要执行的操作" echo "欢迎使用zerotier-planet脚本请选择需要执行的操作"
echo "1. 安装" echo "1. 安装"