跳转到内容

参考

视频: ZeroTier Planet搭建记录

视频: 自建zerotier planet服务器教程

视频网盘备份: 自建zerotier planet服务器教程 和 ZeroTier Planet搭建记录

搭建教程从视频的11分开始

ZeroTierOne: ZeroTierOne - A Smart Ethernet Switch for Earth

ZeroTier: ZeroTier | Global Networking Solution for IoT, SD-WAN, and VPN

Key Networks: Key Networks

GitHub项目: 一分钟私有部署zerotier-planet服务

搭建流程

安装必要的组件

bash
# 安装unzip
apt install unzip
# 安装g++
apt install g++
# 安装json库
apt install nlohmann-json3-dev

Planet搭建

  • 安装ZeroTier

放行服务器9993(TCP/UDP)端口,TCP/UDP都要放行

bash
curl -s https://install.zerotier.com | sudo bash
# 成功安装会输出如下内容
Success! You are ZeroTier address [ 7777777777 ].
  • 记录安全认证信息
bash
# 记录identity.public和authtoken.secret中的值,储存在/var/lib/zerotier-one/目录下
vim /var/lib/zerotier-one/identity.public
vim /var/lib/zerotier-one/authtoken.secret

# 或者直接在/var/lib/zerotier-one/目录下找到identity.public和authtoken.secret文件查看
  • 下载源码
bash
# 下载ZeroTierOne源码
# 项目地址: https://github.com/zerotier/ZeroTierOne
wget https://github.com/zerotier/ZeroTierOne/archive/refs/tags/1.14.2.zip

# 加镜像站
wget https://ghproxy.net/https://github.com/zerotier/ZeroTierOne/archive/refs/tags/1.14.2.zip
  • 解压源码
bash
# 解压命令
unzip 1.14.2.zip

# 解压之后使用
ls
# 可以看到文件夹下多一个ZeroTierOne-1.14.2文件夹
  • 修改源码
bash
# 进入ZeroTierOne-1.14.2/attic/world/文件夹
cd ZeroTierOne-1.14.2/attic/world/
# 编辑目录下的mkworld.cpp文件
vim mkworld.cpp

mkworld.cpp源码如下

c++
/*
 * ZeroTier One - Network Virtualization Everywhere
 * Copyright (C) 2011-2016  ZeroTier, Inc.  https://www.zerotier.com/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * This utility makes the World from the configuration specified below.
 * It probably won't be much use to anyone outside ZeroTier, Inc. except
 * for testing and experimentation purposes.
 *
 * If you want to make your own World you must edit this file.
 *
 * When run, it expects two files in the current directory:
 *
 * previous.c25519 - key pair to sign this world (key from previous world)
 * current.c25519 - key pair whose public key should be embedded in this world
 *
 * If these files do not exist, they are both created with the same key pair
 * and a self-signed initial World is born.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#include <string>
#include <vector>
#include <algorithm>

#include <node/Constants.hpp>
#include <node/World.hpp>
#include <node/C25519.hpp>
#include <node/Identity.hpp>
#include <node/InetAddress.hpp>
#include <osdep/OSUtils.hpp>

using namespace ZeroTier;

int main(int argc,char **argv)
{
        std::string previous,current;
        if ((!OSUtils::readFile("previous.c25519",previous))||(!OSUtils::readFile("current.c25519",current))) {
                C25519::Pair np(C25519::generate());
                previous = std::string();
                previous.append((const char *)np.pub.data,ZT_C25519_PUBLIC_KEY_LEN);
                previous.append((const char *)np.priv.data,ZT_C25519_PRIVATE_KEY_LEN);
                current = previous;
                OSUtils::writeFile("previous.c25519",previous);
                OSUtils::writeFile("current.c25519",current);
                fprintf(stderr,"INFO: created initial world keys: previous.c25519 and current.c25519 (both initially the same)" ZT_EOL_S);
        }

        if ((previous.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN))||(current.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN))) {
                fprintf(stderr,"FATAL: previous.c25519 or current.c25519 empty or invalid" ZT_EOL_S);
                return 1;
        }
        C25519::Pair previousKP;
        memcpy(previousKP.pub.data,previous.data(),ZT_C25519_PUBLIC_KEY_LEN);
        memcpy(previousKP.priv.data,previous.data() + ZT_C25519_PUBLIC_KEY_LEN,ZT_C25519_PRIVATE_KEY_LEN);
        C25519::Pair currentKP;
        memcpy(currentKP.pub.data,current.data(),ZT_C25519_PUBLIC_KEY_LEN);
        memcpy(currentKP.priv.data,current.data() + ZT_C25519_PUBLIC_KEY_LEN,ZT_C25519_PRIVATE_KEY_LEN);

        // =========================================================================
        // EDIT BELOW HERE

        std::vector<World::Root> roots;

        const uint64_t id = ZT_WORLD_ID_EARTH;
        const uint64_t ts = 1567191349589ULL; // August 30th, 2019

        // Los Angeles
        roots.push_back(World::Root());
        roots.back().identity = Identity("3a46f1bf30:0:76e66fab33e28549a62ee2064d1843273c2c300ba45c3f20bef02dbad225723bb59a9bb4b13535730961aeecf5a163ace477cceb0727025b99ac14a5166a09a3");
        roots.back().stableEndpoints.push_back(InetAddress("185.180.13.82/9993"));
        roots.back().stableEndpoints.push_back(InetAddress("2a02:6ea0:c815::/9993"));

        // Miami
        roots.push_back(World::Root());
        roots.back().identity = Identity("de8950a8b2:0:1b3ada8251b91b6b6fa6535b8c7e2460918f4f729abdec97d3c7f3796868fb02f0de0b0ee554b2d59fc3524743eebfcf5315e790ed6d92db5bd10c28c09b40ef");
        roots.back().stableEndpoints.push_back(InetAddress("207.246.73.245/443"));
        roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:9002:5cb:ec4:7aff:fe8f:69d9/443"));

        // Tokyo
        roots.push_back(World::Root());
        roots.back().identity = Identity("34e0a5e174:0:93efb50934788f856d5cfb9ca5be88e85b40965586b75befac900df77352c145a1ba7007569d37c77bfe52c0999f3bdc67a47a4a6000b720a883ce47aa2fb7f8");
        roots.back().stableEndpoints.push_back(InetAddress("147.75.92.2/443"));
        roots.back().stableEndpoints.push_back(InetAddress("2604:1380:3000:7100::1/443"));

        // Amsterdam
        roots.push_back(World::Root());
        roots.back().identity = Identity("992fcf1db7:0:206ed59350b31916f749a1f85dffb3a8787dcbf83b8c6e9448d4e3ea0e3369301be716c3609344a9d1533850fb4460c50af43322bcfc8e13d3301a1f1003ceb6");
        roots.back().stableEndpoints.push_back(InetAddress("195.181.173.159/443"));
        roots.back().stableEndpoints.push_back(InetAddress("2a02:6ea0:c024::/443"));

        // Alice
        //roots.push_back(World::Root());
        //roots.back().identity = Identity("9d219039f3:0:01f0922a98e3b34ebcbff333269dc265d7a020aab69d72be4d4acc9c8c9294785771256cd1d942a90d1bd1d2dca3ea84ef7d85afe6611fb43ff0b74126d90a6e");
        //roots.back().stableEndpoints.push_back(InetAddress("188.166.94.177/9993")); // Amsterdam
        //roots.back().stableEndpoints.push_back(InetAddress("2a03:b0c0:2:d0::7d:1/9993")); // Amsterdam
        //roots.back().stableEndpoints.push_back(InetAddress("154.66.197.33/9993")); // Johannesburg
        //roots.back().stableEndpoints.push_back(InetAddress("2c0f:f850:154:197::33/9993")); // Johannesburg
        //roots.back().stableEndpoints.push_back(InetAddress("159.203.97.171/9993")); // New York
        //roots.back().stableEndpoints.push_back(InetAddress("2604:a880:800:a1::54:6001/9993")); // New York
        //roots.back().stableEndpoints.push_back(InetAddress("131.255.6.16/9993")); // Buenos Aires
        //roots.back().stableEndpoints.push_back(InetAddress("2803:eb80:0:e::2/9993")); // Buenos Aires
        //roots.back().stableEndpoints.push_back(InetAddress("107.170.197.14/9993")); // San Francisco
        //roots.back().stableEndpoints.push_back(InetAddress("2604:a880:1:20::200:e001/9993")); // San Francisco
        //roots.back().stableEndpoints.push_back(InetAddress("128.199.197.217/9993")); // Singapore
        //roots.back().stableEndpoints.push_back(InetAddress("2400:6180:0:d0::b7:4001/9993")); // Singapore

        // Bob
        //roots.push_back(World::Root());
        //roots.back().identity = Identity("8841408a2e:0:bb1d31f2c323e264e9e64172c1a74f77899555ed10751cd56e86405cde118d02dffe555d462ccf6a85b5631c12350c8d5dc409ba10b9025d0f445cf449d92b1c");
        //roots.back().stableEndpoints.push_back(InetAddress("45.32.198.130/9993")); // Dallas
        //roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:6400:81c3:5400:00ff:fe18:1d61/9993")); // Dallas
        //roots.back().stableEndpoints.push_back(InetAddress("46.101.160.249/9993")); // Frankfurt
        //roots.back().stableEndpoints.push_back(InetAddress("2a03:b0c0:3:d0::6a:3001/9993")); // Frankfurt
        //roots.back().stableEndpoints.push_back(InetAddress("107.191.46.210/9993")); // Paris
        //roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:6800:83a4::64/9993")); // Paris
        //roots.back().stableEndpoints.push_back(InetAddress("45.32.246.179/9993")); // Sydney
        //roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:5800:8bf8:5400:ff:fe15:b39a/9993")); // Sydney
        //roots.back().stableEndpoints.push_back(InetAddress("45.32.248.87/9993")); // Tokyo
        //roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:7000:9bc9:5400:00ff:fe15:c4f5/9993")); // Tokyo
        //roots.back().stableEndpoints.push_back(InetAddress("159.203.2.154/9993")); // Toronto
        //roots.back().stableEndpoints.push_back(InetAddress("2604:a880:cad:d0::26:7001/9993")); // Toronto

        // END WORLD DEFINITION
        // =========================================================================

        fprintf(stderr,"INFO: generating and signing id==%llu ts==%llu" ZT_EOL_S,(unsigned long long)id,(unsigned long long)ts);

        World nw = World::make(World::TYPE_PLANET,id,ts,currentKP.pub,roots,previousKP);

        Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> outtmp;
        nw.serialize(outtmp,false);
        World testw;
        testw.deserialize(outtmp,0);
        if (testw != nw) {
                fprintf(stderr,"FATAL: serialization test failed!" ZT_EOL_S);
                return 1;
        }

        OSUtils::writeFile("world.bin",std::string((const char *)outtmp.data(),outtmp.size()));
        fprintf(stderr,"INFO: world.bin written with %u bytes of binary world data." ZT_EOL_S,outtmp.size());

        fprintf(stdout,ZT_EOL_S);
        fprintf(stdout,"#define ZT_DEFAULT_WORLD_LENGTH %u" ZT_EOL_S,outtmp.size());
        fprintf(stdout,"static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {");
        for(unsigned int i=0;i<outtmp.size();++i) {
                const unsigned char *d = (const unsigned char *)outtmp.data();
                if (i > 0)
                        fprintf(stdout,",");
                fprintf(stdout,"0x%.2x",(unsigned int)d[i]);
        }
        fprintf(stdout,"};" ZT_EOL_S);

        return 0;
}

主要修改如下部分

c++
// 需要将下面的官方Planet至少注释掉一个,原因如下
// 原因是代码里默认只允许4台Planet服务器
// 可以在World.hpp里看到如下定义#define ZT_WORLD_MAX_ROOTS 4
// 最好是全注释掉,毕竟都有自己的了
        // Los Angeles
        roots.push_back(World::Root());
        roots.back().identity = Identity("3a46f1bf30:0:76e66fab33e28549a62ee2064d1843273c2c300ba45c3f20bef02dbad225723bb59a9bb4b13535730961aeecf5a163ace477cceb0727025b99ac14a5166a09a3");
        roots.back().stableEndpoints.push_back(InetAddress("185.180.13.82/9993"));
        roots.back().stableEndpoints.push_back(InetAddress("2a02:6ea0:c815::/9993"));

        // Miami
        roots.push_back(World::Root());
        roots.back().identity = Identity("de8950a8b2:0:1b3ada8251b91b6b6fa6535b8c7e2460918f4f729abdec97d3c7f3796868fb02f0de0b0ee554b2d59fc3524743eebfcf5315e790ed6d92db5bd10c28c09b40ef");
        roots.back().stableEndpoints.push_back(InetAddress("207.246.73.245/443"));
        roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:9002:5cb:ec4:7aff:fe8f:69d9/443"));

        // Tokyo
        roots.push_back(World::Root());
        roots.back().identity = Identity("34e0a5e174:0:93efb50934788f856d5cfb9ca5be88e85b40965586b75befac900df77352c145a1ba7007569d37c77bfe52c0999f3bdc67a47a4a6000b720a883ce47aa2fb7f8");
        roots.back().stableEndpoints.push_back(InetAddress("147.75.92.2/443"));
        roots.back().stableEndpoints.push_back(InetAddress("2604:1380:3000:7100::1/443"));

        // Amsterdam
        roots.push_back(World::Root());
        roots.back().identity = Identity("992fcf1db7:0:206ed59350b31916f749a1f85dffb3a8787dcbf83b8c6e9448d4e3ea0e3369301be716c3609344a9d1533850fb4460c50af43322bcfc8e13d3301a1f1003ceb6");
        roots.back().stableEndpoints.push_back(InetAddress("195.181.173.159/443"));
        roots.back().stableEndpoints.push_back(InetAddress("2a02:6ea0:c024::/443"));

// 将
roots.back().identity = Identity("xxxxxxx");
// 中的xxxxxxx换成自己的
// 通过
vim /var/lib/zerotier-one/identity.public
// 命令获取
  • 开始编译
bash
# 运行 build.sh 脚本
source ./build.sh
# 执行当前目录下的 mkworld 脚本
./mkworld
# 将 world.bin 文件重命名为 planet
mv ./world.bin ./planet
# 将 planet 文件复制到 /var/lib/zerotier-one/ 中
cp -r ./planet /var/lib/zerotier-one/
# 将 planet 文件复制到 /root 中
cp -r ./planet /root
  • 重启服务端
bash
systemctl restart zerotier-one.service

安装节点控制器

放行服务器3000/3443(TCP)端口,两个端口2选1,根据自己实际情况放行TCP协议。

bash
curl -O https://s3-us-west-1.amazonaws.com/key-networks/deb/ztncui/1/x86_64/ztncui_0.8.14_amd64.deb
dpkg -i ztncui_0.8.14_amd64.deb
sh -c "echo ZT_TOKEN=`sudo cat /var/lib/zerotier-one/authtoken.secret` > /opt/key-networks/ztncui/.env"
sh -c "echo NODE_ENV=production >> /opt/key-networks/ztncui/.env"
sh -c "echo 'ZT_ADDR=127.0.0.1:9993' >> /opt/key-networks/ztncui/.env"
sh -c "echo HTTPS_PORT=3443 >> /opt/key-networks/ztncui/.env"
chmod 400 /opt/key-networks/ztncui/.env
######################################################################
# 以下两条命令任选其一即可,官方命令不出错就用官方命令,否则就用另外一条命令
# 官方命令
chown ztncui.ztncui /opt/key-networks/ztncui/.env
# 有些系统出现警告就是用此条命令
chown ztncui:ztncui /opt/key-networks/ztncui/.env
######################################################################
systemctl enable ztncui
systemctl restart ztncui

访问 1.1.1.1:3000 进入WEB UI,将1.1.1.1替换为服务器ipv4

初始账号: admin

初始密码: password

  • 安装节点控制器操作步骤

登陆到ztncui,首次登陆会要求你更改密码,更改好密码以后。点击ADD-NETWORK——输入网络名提交后建立完成——点击导航栏networks——找到刚才建立的网络——点击easy setup——点击Generate network address会自动建立一个虚拟网段,然后Submit。其他的选项不要去动,否则会无法分配IPV4地址。

记录一下dpkg -i ztncui_0.8.14_amd64.deb命令后输出

bash
root@iZ2ze2zpmojw0pf4c893lxZ:~# dpkg -i ztncui_0.8.14_amd64.deb                                                                      
Selecting previously unselected package ztncui.                                                                                      
(Reading database ... 45727 files and directories currently installed.)                                                              
Preparing to unpack ztncui_0.8.14_amd64.deb ...                                                                                      
Adding user ztncui to group zerotier-one...                                                                                          
Unpacking ztncui (0.8.14) ...                                                                                                        
Setting up ztncui (0.8.14) ...                                                                                                       
Copying default password file...                                                                                                     
'/opt/key-networks/ztncui/etc/default.passwd' -> '/opt/key-networks/ztncui/etc/passwd'                                               
Generating new TLS key and self-signed certificate...                                                                                
......+...+..+...+......+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.+....+........+.+..............+.+.....+
......+..........+...+...+........+.......+...+......+.........+..+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++*......+...+..+.+..+............+..........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                       
...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.+....+.....+......+...+..........+..+++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++*........+...+.......+...+..............+...+......+.......+...+..+...+.+.....+.........+......
....+.....+....+.....+.+...........+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                                 
-----                                                                                                                                
chown: warning: '.' should be ':': ‘ztncui.ztncui’                                                                                   
Enabling and starting ztncui service...                                                                                              
Created symlink /etc/systemd/system/multi-user.target.wants/ztncui.service /lib/systemd/system/ztncui.service.

使用教程

  • Windows

进入以下文件夹,将生成的Planet文件粘贴替换官方Planet

Bash
C:\ProgramData\ZeroTier\One

之后去服务中重启ZeroTier

Bash
# 方式1
# 使用管理员身份打开PowerShell
# 执行如下命令,看到join ok字样就成功了
# 网络id就是在网页里面创建的那个网络
zerotier-cli.bat join 网络id
# 方式2
# 桌面右下角有个ZeroTier 的小图标,
Join New NetWork...
# 输入 ZtnCuI 面板里面的 网络ID就可以
# 加入网络后,记得去网页授权
  • Linux
Bash
# 安装zerotier
curl -s https://install.zerotier.com | sudo bash
# 查看运行状态
zerotier-cli info
# 配置自启动
sudo systemctl enable zerotier-one.service
# 将Planet复制到如下目录
/var/lib/zerotier-one/planet
# 重启服务
systemctl restart zerotier-one
# 加入网络
# 网络id就是在网页里面创建的那个网络
zerotier-cli join 网络id
  • 安卓和iOS在网上查教程

安卓和iOS参考教程

ZeroTier安装及Planet自建教程:iOS、Windows、macOS、安卓、Linux、iStoreOS

zerotier自建planet

一分钟私有部署zerotier-planet服务