Skip to content

Commit

Permalink
feat: 🎸 支持更换端口 (xubiaolin#55)
Browse files Browse the repository at this point in the history
Co-authored-by: xubiaolin <[email protected]>
  • Loading branch information
xubiaolin and xubiaolin authored Feb 26, 2023
1 parent 5aa7aaa commit 460fa6d
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 87 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
patch/patch.json
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM alpine:latest

ARG ZT_PORT

ENV TZ=Asia/Shanghai

WORKDIR /app
Expand All @@ -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 'NODE_ENV=production' >>.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

RUN cd /app && git clone -v https://ghproxy.com/https://github.com/zerotier/ZeroTierOne.git --depth 1\
Expand All @@ -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


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"
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,29 @@ cd docker-zerotier-planet

根据提示来选择即可,操作完成后会自动部署
```
-------------------------------------------
您当前公网ip为:8.8.8.8, 使用当前ip请输入:y
-------------------------------------------
使用其他ip请输入要使用的ip,例如1.1.1.1
-------------------------------------------
欢迎使用zerotier-planet脚本,请选择需要执行的操作:
1. 安装
2. 更新
其他任意键退出
```

整个脚本预计需要 3-10 分钟,具体需要看网络与机型


当您看到类似如下字样时,表示安装成功
```
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
7782066b89fb217bf53ae57b139c73c3d6df79fecf887d1571aa8eed2433ba75
启动服务
3b59df95edabeabbf19aa6605b28704608710719bdf7c6e7612d75d01ede8f48
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~
```


Expand Down
158 changes: 86 additions & 72 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,102 @@

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 "-------------------------------------------"
echo "支持使用域名或者ip,默认端口为9993,暂不支持修改"
echo "请输入 ip 或者 域名"
echo ""
echo "您当前公网ip为:"$curr_ip",使用当前ip请输入:y"
echo "-------------------------------------------"
read -p "是否自动获取公网IP地址?(y/n)" use_auto_ip

ip=""
read c
if [[ "$use_auto_ip" =~ ^[Yy]$ ]]; then
ipv4=$(curl -s https://ipv4.icanhazip.com/)
ipv6=$(curl -s https://ipv6.icanhazip.com/)

if [ "$c" = 'y' ]; then
ip=$curr_ip
else
ip=$c
fi
echo "获取到的IPv4地址为: $ipv4"
echo "获取到的IPv6地址为: $ipv6"

echo "----------------------------"
echo "部署的ip为:$ip, 是否继续? y/n"
read or
if [ "$or" = "y" ]; then
echo "{
\"stableEndpoints\": [
\"$ip/9993\"
]
}
" >./patch/patch.json
else
exit -1
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

# 开始安装程序
echo "清除原有内容"
rm /tmp/planet
docker stop $imageName
docker rm $imageName
docker rmi $imageName
# 要求用户手动输入IP地址
read -p "请输入IPv4地址: " ipv4
read -p "请输入IPv6地址(可留空): " ipv6
fi

echo "打包镜像"
docker build --network host -t $imageName .
# 输出使用的端口号和IP地址
ipv4_entry="${ipv4}/${port}"
if [[ -n "$ipv6" ]]; then
ipv6_entry="${ipv6}/${port}"
endpoints="[\"$ipv4_entry\",\"$ipv6_entry\"]"
else
endpoints="[\"$ipv4_entry\"]"
fi
echo "{\"stableEndpoints\":$endpoints}" > ./patch/patch.json

echo "启动服务"
for i in $(lsof -i:9993 -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 cp zerotier-planet:/app/bin/planet /tmp/planet
}
echo "配置内容为:"
echo "`cat ./patch/patch.json`"

echo "开始安装..."
echo "清除原有内容"
rm -rf /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

function upgrade(){
echo "准备更新zerotier服务"
docker exec $imageName sh -c "apt update && apt upgrade zerotier-one -y || apk upgrade zerotier-one"
docker restart $imageName
echo "done!"
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 menu() {
echo
echo "=============功能菜单============="
echo "| 1 - 安装"
echo "| 2 - 更新"
#echo "| 3 - 卸载"
echo "| q - 退出"
echo "---------------------------------"
printf "请选择菜单:"
read -n 1 n
echo
if [[ "$n" = "1" ]]; then
echo "安装"
deploy

elif [ "$n" = "2" ]; then
upgrade
#elif [ "$n" = "3" ]; then
# echo $n
elif [ "$n" = "q" ]; then
echo 退出
return
else
echo "错误选项"
fi
function upgrade() {
echo "准备更新zerotier服务"
docker exec $imageName sh -c "apt update && apt upgrade zerotier-one -y || apk upgrade zerotier-one"
docker restart $imageName
echo "done!"
}
menu

# 显示菜单
echo "欢迎使用zerotier-planet脚本,请选择需要执行的操作:"
echo "1. 安装"
echo "2. 更新"
echo "其他任意键退出"

# 读取用户输入
read choice

# 根据用户输入执行相应操作
case "$choice" in
1)
echo "您选择了安装功能"
install
;;
2)
echo "您选择了更新功能"
upgrade
;;
*)
echo "谢谢使用!"
exit 0
;;
esac
2 changes: 2 additions & 0 deletions pull.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
git checkout .
git pull

0 comments on commit 460fa6d

Please sign in to comment.