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
Removing intermediate container 13b4acf043ce
---> f66b3f73437e
Successfully built f66b3f73437e
Successfully tagged zerotier-planet:latest
启动服务 启动服务
WARNING: Published ports are discarded when using host network mode 3b59df95edabeabbf19aa6605b28704608710719bdf7c6e7612d75d01ede8f48
7782066b89fb217bf53ae57b139c73c3d6df79fecf887d1571aa8eed2433ba75 Preparing to copy...
Copying from container - 512B
Copying from container - 769B
Copying from container - 1.024kB
Copying from container - 1.536kB
Successfully copied 2.048kB to /tmp/planet
planet文件路径为 /tmp/planet
planet server端口为: 9994, 请在防火墙放行该端口的tcp和udp协议
enjoy~
``` ```

124
deploy.sh
View File

@ -2,55 +2,72 @@
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"
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
else else
ip=$c
# 要求用户手动输入IP地址
read -p "请输入IPv4地址: " ipv4
read -p "请输入IPv6地址可留空: " ipv6
fi fi
echo "----------------------------" # 输出使用的端口号和IP地址
echo "部署的ip为:$ip, 是否继续? y/n" ipv4_entry="${ipv4}/${port}"
read or if [[ -n "$ipv6" ]]; then
if [ "$or" = "y" ]; then ipv6_entry="${ipv6}/${port}"
echo "{ endpoints="[\"$ipv4_entry\",\"$ipv6_entry\"]"
\"stableEndpoints\": [
\"$ip/9993\"
]
}
" >./patch/patch.json
else else
exit -1 endpoints="[\"$ipv4_entry\"]"
fi fi
echo "{\"stableEndpoints\":$endpoints}" > ./patch/patch.json
# 开始安装程序 echo "配置内容为:"
echo "`cat ./patch/patch.json`"
echo "开始安装..."
echo "清除原有内容" echo "清除原有内容"
rm /tmp/planet rm -rf /tmp/planet
docker stop $imageName docker stop $imageName
docker rm $imageName docker rm $imageName
docker rmi $imageName docker rmi $imageName
echo "打包镜像" echo "打包镜像"
docker build --network host -t $imageName . echo "使用的端口为:${port}"
docker build --no-cache --build-arg ZT_PORT=$port --network host -t $imageName .
if [ $? -ne 0 ]; then
echo "镜像打包失败,请重试"
exit 1
fi
echo "启动服务" echo "启动服务"
for i in $(lsof -i:9993 -t); do kill -2 $i; done for i in $(lsof -i:$port -t); do kill -2 $i; done
docker run -d -p 9993:9993 -p 9993:9993/udp -p 3443:3443 --name $imageName --restart unless-stopped $imageName 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 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() {
@ -60,30 +77,27 @@ function upgrade(){
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 # 读取用户输入
read choice
# 根据用户输入执行相应操作
case "$choice" in
1)
echo "您选择了安装功能"
install
;;
2)
echo "您选择了更新功能"
upgrade upgrade
#elif [ "$n" = "3" ]; then ;;
# echo $n *)
elif [ "$n" = "q" ]; then echo "谢谢使用!"
echo 退出 exit 0
return ;;
else esac
echo "错误选项"
fi
}
menu

2
pull.sh Executable file
View File

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