🎉 init: 初始化项目文件,包括环境变量、Dockerfile、Compose配置和启动脚本

This commit is contained in:
enoch 2025-05-05 13:31:47 +08:00
commit a678f4b3b8
4 changed files with 94 additions and 0 deletions

1
.env Normal file
View File

@ -0,0 +1 @@
SERVER_IP=1.2.3.4 # Replace with your server's IP address

9
Containerfile Normal file
View File

@ -0,0 +1,9 @@
FROM zerotier/zerotier:latest
EXPOSE 9993/udp
COPY startup.sh /startup.sh
ENTRYPOINT [ "/startup.sh" ]

16
compose.yaml Normal file
View File

@ -0,0 +1,16 @@
services:
zerotier-moon:
image: zerotier-moon:latest
build:
context: .
dockerfile: Containerfile
container_name: zerotier-moon
restart: always
ports:
- "9993:9993/udp"
volumes:
- ./zerotier-one:/var/lib/zerotier-one
command:
- -4
- ${SERVER_IP}

68
startup.sh Executable file
View File

@ -0,0 +1,68 @@
#!/bin/sh
# usage ./startup.sh -4 1.2.3.4 -6 2001:abcd:abcd::1 -p 9993
moon_port=9993 # default ZeroTier moon port
while getopts "4:6:p:" arg # handle args
do
case $arg in
4)
ipv4_address="$OPTARG"
echo "IPv4 address: $ipv4_address"
;;
6)
ipv6_address="$OPTARG"
echo "IPv6 address: $ipv6_address"
;;
p)
moon_port="$OPTARG"
echo "Moon port: $moon_port"
;;
?)
echo "unknown argument"
exit 1
;;
esac
done
stableEndpointsForSed=""
if [ -z ${ipv4_address+x} ]
then # ipv4 address is not set
if [ -z ${ipv6_address+x} ]
then # ipv6 address is not set
echo "Please set IPv4 address or IPv6 address."
exit 0
else # ipv6 address is set
stableEndpointsForSed="\"$ipv6_address\/$moon_port\""
fi
else # ipv4 address is set
if [ -z ${ipv6_address+x} ]
then # ipv6 address is not set
stableEndpointsForSed="\"$ipv4_address\/$moon_port\""
else # ipv6 address is set
stableEndpointsForSed="\"$ipv4_address\/$moon_port\",\"$ipv6_address\/$moon_port\""
fi
fi
if [ -d "/var/lib/zerotier-one/moons.d" ] # check if the moons conf has generated
then
moon_id=$(cat /var/lib/zerotier-one/identity.public | cut -d ':' -f1)
echo -e "Your ZeroTier moon id is \033[0;31m$moon_id\033[0m, you could orbit moon using \033[0;31m\"zerotier-cli orbit $moon_id $moon_id\"\033[0m"
/usr/sbin/zerotier-one
else
nohup /usr/sbin/zerotier-one >/dev/null 2>&1 &
# Waiting for identity generation...'
while [ ! -f /var/lib/zerotier-one/identity.secret ]; do
sleep 1
done
/usr/sbin/zerotier-idtool initmoon /var/lib/zerotier-one/identity.public >>/var/lib/zerotier-one/moon.json
sed -i 's/"stableEndpoints": \[\]/"stableEndpoints": ['$stableEndpointsForSed']/g' /var/lib/zerotier-one/moon.json
/usr/sbin/zerotier-idtool genmoon /var/lib/zerotier-one/moon.json > /dev/null
mkdir /var/lib/zerotier-one/moons.d
mv *.moon /var/lib/zerotier-one/moons.d/
pkill zerotier-one
moon_id=$(cat /var/lib/zerotier-one/moon.json | grep \"id\" | cut -d '"' -f4)
echo -e "Your ZeroTier moon id is \033[0;31m$moon_id\033[0m, you could orbit moon using \033[0;31m\"zerotier-cli orbit $moon_id $moon_id\"\033[0m"
exec /usr/sbin/zerotier-one
fi