Zerotier 自动更换 Moon 的 IP 配置

Posted by koocyton on 2023-11-15
Estimated Reading Time 1 Minutes
Words 271 In Total
Viewed Times

创建 moon.json

1
2
3
4
# zerotier-cli info
200 info 860dcfb934 1.12.2 ONLINE

# zerotier-idtool initmoon identity.public >> moon.json.source

编辑 /var/lib/zerotier-one/moon.json.source

  • stableEndpoints 内编辑如下,插入占位字符串 $endPoint1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"id": "860dcfb934",
"objtype": "world",
"roots": [
{
"identity": "860dcfb934:0:.......",
"stableEndpoints": [
"$endPoint1/9993"
]
}
],
"signingKey": ".....",
"signingKey_SECRET": "......",
"updatesMustBeSignedBy": ".....",
"worldType": "moon"
}

刷新脚本

  • /var/lib/zerotier-one/refresh_moon.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

cd /var/lib/zerotier-one
# current ip
CURR_IP=`curl -s http://ip.3322.net`
# last ip
LAST_IP=`cat moon.json | tr '\n' ' ' | sed 's/.*"\([^"]*\)\/\(9993\).*/\1/g'`
# eq 如果 IP 不变,则退出
if [ "$CURR_IP" == "$LAST_IP" ]
then
exit 0
fi
# replact ip, get new moon.json,将占位字符串替换成新的 IP
sed 's/$endPoint1/'${CURR_IP}'/g' /var/lib/zerotier-one/moon.json.source > /var/lib/zerotier-one/moon.json

# create /var/lib/zerotier-one/000000860dcfb934.moon
zerotier-idtool genmoon /var/lib/zerotier-one/moon.json
# move to moons.d
mv /var/lib/zerotier-one/000000860dcfb934.moon /var/lib/zerotier-one/moons.d/
# restart zerotier
systemctl stop zerotier-one
sleep 1
systemctl start zerotier-one
sleep 1
# print info
zerotier-cli listmoons

每十分钟执行一次

  • # crontab -e
1
0 */10 * * * /var/lib/zerotier-one/refresh_moon.sh

如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !