feat: 🎸 支持更换端口 (#55)

Co-authored-by: xubiaolin <xubiaolin2014@xiaochuankeji.cn>
This commit is contained in:
Markxu 2023-02-26 15:17:17 +08:00 committed by GitHub
parent 5aa7aaa260
commit 460fa6dc1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 109 additions and 86 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.idea .idea
patch/patch.json

View File

@ -1,5 +1,7 @@
FROM alpine:latest FROM alpine:latest
ARG ZT_PORT
ENV TZ=Asia/Shanghai ENV TZ=Asia/Shanghai
WORKDIR /app WORKDIR /app
@ -17,6 +19,8 @@ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/re
&& echo 'HTTP_PORT=3443' >.env \ && echo 'HTTP_PORT=3443' >.env \
&& echo 'NODE_ENV=production' >>.env \ && echo 'NODE_ENV=production' >>.env \
&& echo 'HTTP_ALL_INTERFACES=true' >>.env \ && echo 'HTTP_ALL_INTERFACES=true' >>.env \
&& echo "ZT_ADDR=localhost:${ZT_PORT}" >>.env\
&& echo "${ZT_PORT}" >/app/zerotier-one.port \
&& cp -v etc/default.passwd etc/passwd && cp -v etc/default.passwd etc/passwd
RUN cd /app && git clone -v https://ghproxy.com/https://github.com/zerotier/ZeroTierOne.git --depth 1\ RUN cd /app && git clone -v https://ghproxy.com/https://github.com/zerotier/ZeroTierOne.git --depth 1\
@ -32,4 +36,4 @@ RUN cd /app && git clone -v https://ghproxy.com/https://github.com/zerotier/Zero
&& echo "ZT_TOKEN=$TOKEN">> /app/ztncui/src/.env && echo "ZT_TOKEN=$TOKEN">> /app/ztncui/src/.env
CMD /bin/sh -c "zerotier-one -d; cd /app/ztncui/src;npm start" CMD /bin/sh -c "cd /var/lib/zerotier-one && ./zerotier-one -p`cat /app/zerotier-one.port` -d; cd /app/ztncui/src;npm start"

View File

@ -70,11 +70,11 @@ cd docker-zerotier-planet
根据提示来选择即可,操作完成后会自动部署 根据提示来选择即可,操作完成后会自动部署
``` ```
------------------------------------------- 欢迎使用zerotier-planet脚本请选择需要执行的操作
您当前公网ip为8.8.8.8, 使用当前ip请输入:y 1. 安装
------------------------------------------- 2. 更新
使用其他ip请输入要使用的ip,例如1.1.1.1 其他任意键退出
-------------------------------------------
``` ```
整个脚本预计需要 3-10 分钟,具体需要看网络与机型 整个脚本预计需要 3-10 分钟,具体需要看网络与机型
@ -82,15 +82,17 @@ cd docker-zerotier-planet
当您看到类似如下字样时,表示安装成功 当您看到类似如下字样时,表示安装成功
``` ```
Step 6/6 : CMD /bin/sh -c "zerotier-one -d; cd /tmp/ztncui/src;npm start" 启动服务
---> Running in 13b4acf043ce 3b59df95edabeabbf19aa6605b28704608710719bdf7c6e7612d75d01ede8f48
Removing intermediate container 13b4acf043ce Preparing to copy...
---> f66b3f73437e Copying from container - 512B
Successfully built f66b3f73437e Copying from container - 769B
Successfully tagged zerotier-planet:latest Copying from container - 1.024kB
启动服务 Copying from container - 1.536kB
WARNING: Published ports are discarded when using host network mode Successfully copied 2.048kB to /tmp/planet
7782066b89fb217bf53ae57b139c73c3d6df79fecf887d1571aa8eed2433ba75 planet文件路径为 /tmp/planet
planet server端口为: 9994, 请在防火墙放行该端口的tcp和udp协议
enjoy~
``` ```

156
deploy.sh
View File

@ -2,88 +2,102 @@
imageName="zerotier-planet" imageName="zerotier-planet"
function deploy() { function install() {
read -p "请输入要使用的端口号(数字): " port
# 处理ip信息 # 确保端口号是数字
curr_ip=$(curl -s cip.cc | grep http | awk -F '/' '{print $4}') while ! [[ "$port" =~ ^[0-9]+$ ]]; do
read -p "端口号必须是数字,请重新输入端口号: " port
done
echo "-------------------------------------------" read -p "是否自动获取公网IP地址y/n" use_auto_ip
echo "支持使用域名或者ip默认端口为9993暂不支持修改"
echo "请输入 ip 或者 域名"
echo ""
echo "您当前公网ip为"$curr_ip",使用当前ip请输入:y"
echo "-------------------------------------------"
ip="" if [[ "$use_auto_ip" =~ ^[Yy]$ ]]; then
read c ipv4=$(curl -s https://ipv4.icanhazip.com/)
ipv6=$(curl -s https://ipv6.icanhazip.com/)
if [ "$c" = 'y' ]; then echo "获取到的IPv4地址为: $ipv4"
ip=$curr_ip echo "获取到的IPv6地址为: $ipv6"
else
ip=$c read -p "是否使用上面获取到的IP地址y/n" use_auto_ip_result
if [[ "$use_auto_ip_result" =~ ^[Nn]$ ]]; then
read -p "请输入IPv4地址: " ipv4
read -p "请输入IPv6地址可留空: " ipv6
fi fi
else
echo "----------------------------" # 要求用户手动输入IP地址
echo "部署的ip为:$ip, 是否继续? y/n" read -p "请输入IPv4地址: " ipv4
read or read -p "请输入IPv6地址可留空: " ipv6
if [ "$or" = "y" ]; then fi
echo "{
\"stableEndpoints\": [
\"$ip/9993\"
]
}
" >./patch/patch.json
else
exit -1
fi
# 开始安装程序 # 输出使用的端口号和IP地址
echo "清除原有内容" ipv4_entry="${ipv4}/${port}"
rm /tmp/planet if [[ -n "$ipv6" ]]; then
docker stop $imageName ipv6_entry="${ipv6}/${port}"
docker rm $imageName endpoints="[\"$ipv4_entry\",\"$ipv6_entry\"]"
docker rmi $imageName else
endpoints="[\"$ipv4_entry\"]"
fi
echo "{\"stableEndpoints\":$endpoints}" > ./patch/patch.json
echo "打包镜像" echo "配置内容为:"
docker build --network host -t $imageName . echo "`cat ./patch/patch.json`"
echo "启动服务" echo "开始安装..."
for i in $(lsof -i:9993 -t); do kill -2 $i; done echo "清除原有内容"
docker run -d -p 9993:9993 -p 9993:9993/udp -p 3443:3443 --name $imageName --restart unless-stopped $imageName rm -rf /tmp/planet
docker cp zerotier-planet:/app/bin/planet /tmp/planet docker stop $imageName
docker rm $imageName
docker rmi $imageName
echo "打包镜像"
echo "使用的端口为:${port}"
docker build --no-cache --build-arg ZT_PORT=$port --network host -t $imageName .
if [ $? -ne 0 ]; then
echo "镜像打包失败,请重试"
exit 1
fi
echo "启动服务"
for i in $(lsof -i:$port -t); do kill -2 $i; done
docker run -d -p $port:$port -p $port:$port/udp -p 3443:3443 --name $imageName --restart unless-stopped $imageName
docker cp zerotier-planet:/app/bin/planet /tmp/planet
echo "planet文件路径为 /tmp/planet"
echo "planet server端口为: $port, 请在防火墙放行该端口的tcp和udp协议"
echo "enjoy~"
} }
function upgrade(){ function upgrade() {
echo "准备更新zerotier服务" echo "准备更新zerotier服务"
docker exec $imageName sh -c "apt update && apt upgrade zerotier-one -y || apk upgrade zerotier-one" docker exec $imageName sh -c "apt update && apt upgrade zerotier-one -y || apk upgrade zerotier-one"
docker restart $imageName docker restart $imageName
echo "done!" echo "done!"
} }
function menu() { # 显示菜单
echo echo "欢迎使用zerotier-planet脚本请选择需要执行的操作"
echo "=============功能菜单=============" echo "1. 安装"
echo "| 1 - 安装" echo "2. 更新"
echo "| 2 - 更新" echo "其他任意键退出"
#echo "| 3 - 卸载"
echo "| q - 退出"
echo "---------------------------------"
printf "请选择菜单:"
read -n 1 n
echo
if [[ "$n" = "1" ]]; then
echo "安装"
deploy
elif [ "$n" = "2" ]; then # 读取用户输入
upgrade read choice
#elif [ "$n" = "3" ]; then
# echo $n # 根据用户输入执行相应操作
elif [ "$n" = "q" ]; then case "$choice" in
echo 退出 1)
return echo "您选择了安装功能"
else install
echo "错误选项" ;;
fi 2)
} echo "您选择了更新功能"
menu upgrade
;;
*)
echo "谢谢使用!"
exit 0
;;
esac

2
pull.sh Executable file
View File

@ -0,0 +1,2 @@
git checkout .
git pull