diff --git a/.gitignore b/.gitignore index dffd33b..485dee6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ .idea -patch/patch.json diff --git a/Dockerfile b/Dockerfile index cff3552..e19db46 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,53 +1,70 @@ -FROM alpine:3.17 as builder - -ARG ZT_PORT +FROM alpine:3.14 as builder ENV TZ=Asia/Shanghai WORKDIR /app -ADD . /app +ADD ./entrypoint.sh /app/entrypoint.sh +ADD ./http_server.js /app/http_server.js -RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \ +# init tool +RUN set -x\ && apk update\ - && mkdir -p /usr/include/nlohmann/ && cd /usr/include/nlohmann/ && wget https://ghproxy.markxu.online/https://github.com/nlohmann/json/releases/download/v3.10.5/json.hpp \ - && apk add --no-cache git python3 npm make g++ zerotier-one linux-headers\ - && mkdir /app -p && cd /app && git clone --progress https://ghproxy.markxu.online/https://github.com/key-networks/ztncui.git\ + && apk add --no-cache git python3 npm make g++ linux-headers curl pkgconfig openssl-dev jq build-base gcc \ + && echo "env prepare success!" + +# make zerotier-one +RUN set -x\ + && curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y\ + && source "$HOME/.cargo/env"\ + && git clone https://github.com/zerotier/ZeroTierOne.git\ + && cd ZeroTierOne\ + && make ZT_SYMLINK=1 \ + && make\ + && make install\ + && echo "make success!"\ + ; zerotier-one -d \ + ; sleep 5s && ps -ef |grep zerotier-one |grep -v grep |awk '{print $1}' |xargs kill -9\ + && echo "zerotier-one init success!" + + +#make ztncui +RUN set -x \ + && mkdir /app -p \ + && cd /app \ + && git clone --progress https://ghproxy.markxu.online/https://github.com/key-networks/ztncui.git\ && cd /app/ztncui/src \ - && cp /app/patch/binding.gyp .\ - && echo "开始配置npm环境"\ - && npm install -g --progress --verbose node-gyp --registry=https://registry.npmmirror.com\ - && npm install --registry=https://registry.npmmirror.com\ - && 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 + && npm config set registry https://registry.npmmirror.com\ + && npm install -g node-gyp\ + && npm install -RUN cd /app && git clone --progress https://ghproxy.markxu.online/https://github.com/zerotier/ZeroTierOne.git --depth 1\ - && zerotier-one -d && sleep 5s && ps -ef |grep zerotier-one |grep -v grep |awk '{print $1}' |xargs kill -9 \ - && cd /var/lib/zerotier-one && zerotier-idtool initmoon identity.public >moon.json\ - && cd /app/patch && python3 patch.py \ - && cd /var/lib/zerotier-one && zerotier-idtool genmoon moon.json && mkdir moons.d && cp ./*.moon ./moons.d \ - && cd /app/ZeroTierOne/attic/world/ && sh build.sh \ - && sleep 5s \ - && cd /app/ZeroTierOne/attic/world/ && ./mkworld \ - && mkdir /app/bin -p && cp world.bin /app/bin/planet \ - && TOKEN=$(cat /var/lib/zerotier-one/authtoken.secret) \ - && echo "ZT_TOKEN=$TOKEN">> /app/ztncui/src/.env +FROM alpine:3.14 -FROM alpine:3.17 WORKDIR /app -COPY --from=builder /app/ztncui /app/ztncui -COPY --from=builder /app/bin /app/bin -COPY --from=builder /app/zerotier-one.port /app/zerotier-one.port -COPY --from=builder /var/lib/zerotier-one /var/lib/zerotier-one +ENV IP_ADDR4='' +ENV IP_ADDR6='' -RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \ - && apk update\ - && apk add --no-cache npm zerotier-one +ENV ZT_PORT=9994 +ENV API_PORT=3443 +ENV FILE_SERVER_PORT=3000 -VOLUME [ "/app","/var/lib/zerotier-one" ] +ENV GH_MIRROR="https://ghproxy.markxu.online/" +ENV FILE_KEY='' +ENV TZ=Asia/Shanghai -CMD /bin/sh -c "cd /var/lib/zerotier-one && ./zerotier-one -p`cat /app/zerotier-one.port` -d; cd /app/ztncui/src;npm start" +COPY --from=builder /app/ztncui /bak/ztncui +COPY --from=builder /var/lib/zerotier-one /bak/zerotier-one + +COPY --from=builder /app/ZeroTierOne/zerotier-one /usr/sbin/zerotier-one +COPY --from=builder /app/entrypoint.sh /app/entrypoint.sh +COPY --from=builder /app/http_server.js /app/http_server.js + +RUN set -x ;sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \ + && apk update \ + && apk add --no-cache npm curl jq\ + && mkdir /app/config -p + + +VOLUME [ "/app/dist","/app/ztncui","/var/lib/zerotier-one","/app/config"] + +CMD ["/bin/sh","/app/entrypoint.sh"] diff --git a/deploy.sh b/deploy.sh index c69dca0..aa8527d 100755 --- a/deploy.sh +++ b/deploy.sh @@ -3,11 +3,10 @@ imageName="zerotier-planet" function install() { - read -p "请输入zerotier-planet要使用的端口号,例如9994(数字): " port - - # 确保端口号是数字 - while ! [[ "$port" =~ ^[0-9]+$ ]]; do - read -p "端口号必须是数字,请重新输入端口号: " port + read -p "请输入zerotier-planet要使用的端口号,例如9994: " ZT_PORT + #port必须是数字 + while [[ ! "$ZT_PORT" =~ ^[0-9]+$ ]]; do + read -p "端口号必须是数字,请重新输入: " ZT_PORT done read -p "是否自动获取公网IP地址?(y/n)" use_auto_ip diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..f99b616 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,106 @@ +#!/bin/sh + +set -x + +function start() { + echo "start ztncui and zerotier" + cd /var/lib/zerotier-one && ./zerotier-one -p$(cat /app/config/zerotier-one.port) -d || exit 1 + nohup node /app/http_server.js &> /app/server.log & + cd /app/ztncui/src && npm start || exit 1 +} + +function check_ztncui() { + mkdir -p /app/ztncui + if [ "$(ls -A /app/ztncui)" ]; then + echo "${API_PORT}" >/app/config/ztncui.port + echo "/app/ztncui is not empty, start directly" + else + echo "/app/ztncui is empty, init data" + cp -r /bak/ztncui/* /app/ztncui/ + + echo "config ztncui" + mkdir -p /app/config + echo "${API_PORT}" >/app/config/ztncui.port + cd /app/ztncui/src + echo "HTTP_PORT=${API_PORT}" >.env && + echo 'NODE_ENV=production' >>.env && + echo 'HTTP_ALL_INTERFACES=true' >>.env && + echo "ZT_ADDR=localhost:${ZT_PORT}" >>.env && echo "${ZT_PORT}" >/app/config/zerotier-one.port && + cp -v etc/default.passwd etc/passwd && TOKEN=$(cat /var/lib/zerotier-one/authtoken.secret) && + echo "ZT_TOKEN=$TOKEN" >>.env && + echo "make ztncui success!" + fi +} + +function check_zerotier() { + mkdir -p /var/lib/zerotier-one + if [ "$(ls -A /var/lib/zerotier-one)" ]; then + echo "/var/lib/zerotier-one is not empty, start directly" + else + mkdir -p /app/config + echo "/var/lib/zerotier-one is empty, init data" + echo "${ZT_PORT}" >/app/config/zerotier-one.port + cp -r /bak/zerotier-one/* /var/lib/zerotier-one/ + + cd /var/lib/zerotier-one + echo "start mkmoonworld" + + ./zerotier-idtool initmoon identity.public >moon.json + + if [ -z "$IP_ADDR4" ]; then IP_ADDR4=$(curl -s https://ipv4.icanhazip.com/); fi + if [ -z "$IP_ADDR6" ]; then IP_ADDR6=$(curl -s https://ipv6.icanhazip.com/); fi + + echo "IP_ADDR4=$IP_ADDR4" + echo "IP_ADDR6=$IP_ADDR6" + + ZT_PORT=$(cat /app/config/zerotier-one.port) + API_PORT=$(cat /app/config/ztncui.port) + + echo "ZT_PORT=$ZT_PORT" + echo "API_PORT=$API_PORT" + + if [ -z "$IP_ADDR4" ]; then stableEndpoints="[\"$IP_ADDR6/${ZT_PORT}\"]"; fi + if [ -z "$IP_ADDR6" ]; then stableEndpoints="[\"$IP_ADDR4/${ZT_PORT}\"]"; fi + if [ -n "$IP_ADDR4" ] && [ -n "$IP_ADDR6" ]; then stableEndpoints="[\"$IP_ADDR4/${ZT_PORT}\",\"$IP_ADDR6/${ZT_PORT}\"]"; fi + if [ -z "$IP_ADDR4" ] && [ -z "$IP_ADDR6" ]; then + echo "IP_ADDR4 and IP_ADDR6 are both empty!" + exit 1 + fi + + echo "$IP_ADDR4">/app/config/ip_addr4 + echo "$IP_ADDR6">/app/config/ip_addr6 + + echo "stableEndpoints=$stableEndpoints" + + jq --argjson newEndpoints "$stableEndpoints" '.roots[0].stableEndpoints = $newEndpoints' moon.json >temp.json && mv temp.json moon.json + ./zerotier-idtool genmoon moon.json && mkdir -p moons.d && cp ./*.moon ./moons.d + + wget "${GH_MIRROR}https://github.com/kaaass/ZeroTierOne/releases/download/mkmoonworld-1.0/mkmoonworld-x86_64" + chmod +x ./mkmoonworld-x86_64 + ./mkmoonworld-x86_64 moon.json + + mkdir -p /app/dist/ + mv world.bin /app/dist/planet + cp *.moon /app/dist/ + echo -e "mkmoonworld success!\n" + fi +} + +function check_file_server(){ + if [ ! -f "/app/config/file_server.port" ]; then + echo "file_server.port is not exist, generate it" + echo "${FILE_SERVER_PORT}" >/app/config/file_server.port + echo "${FILE_SERVER_PORT}" + else + echo "file_server.port is exist, read it" + FILE_SERVER_PORT=$(cat /app/config/file_server.port) + echo "${FILE_SERVER_PORT}" + fi +} + + +check_file_server +check_ztncui +check_zerotier + +start \ No newline at end of file diff --git a/http_server.js b/http_server.js new file mode 100644 index 0000000..3a994db --- /dev/null +++ b/http_server.js @@ -0,0 +1,71 @@ +const http = require('http'); +const fs = require('fs'); +const path = require('path'); +const url = require('url'); +const crypto = require('crypto'); +const port = process.env.FILE_SERVER_PORT; + +const SECRET_KEY = process.env.SECRET_KEY || crypto.randomBytes(8).toString('hex'); +const APP_PATH='/app' +const DIST_PATH = '/app/dist' + +// write to file +const secretKeyPath = '/app/config/file_server.key'; +fs.writeFile(secretKeyPath, SECRET_KEY, (err) => { + if (err) { + console.error('Error writing SECRET_KEY to file:', err); + } else { + console.log(`SECRET_KEY written to ${secretKeyPath}`); + } +}); + +const server = http.createServer((req, res) => { + const parsedUrl = url.parse(req.url, true); + + // check key + const key = parsedUrl.query.key; + if (!key || key !== SECRET_KEY) { + res.writeHead(401, { 'Content-Type': 'text/plain' }); + return res.end('Unauthorized'); + } + + let filePath = path.join(DIST_PATH, parsedUrl.pathname); + let extname = String(path.extname(filePath)).toLowerCase(); + let mimeTypes = { + '.html': 'text/html', + '.js': 'text/javascript', + '.css': 'text/css', + '.json': 'application/json', + '.png': 'image/png', + '.jpg': 'image/jpg', + '.gif': 'image/gif', + '.svg': 'image/svg+xml', + '.wav': 'audio/wav', + '.mp4': 'video/mp4', + '.woff': 'application/font-woff', + '.ttf': 'application/font-ttf', + '.eot': 'application/vnd.ms-fontobject', + '.otf': 'application/font-otf', + '.wasm': 'application/wasm' + }; + let contentType = mimeTypes[extname] || 'application/octet-stream'; + + fs.readFile(filePath, (err, content) => { + if (err) { + if (err.code == 'ENOENT') { + res.writeHead(404, { 'Content-Type': 'text/html' }); + res.end("404 - File Not Found"); + } else { + res.writeHead(500); + res.end(`Server Error: ${err.code}`); + } + } else { + res.writeHead(200, { 'Content-Type': contentType }); + res.end(content, 'utf-8'); + } + }); +}); + +server.listen(port, () => { + console.log(`Server running at http://localhost:${port}/`); +}); diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..73b6270 --- /dev/null +++ b/install.sh @@ -0,0 +1,249 @@ +#!/bin/bash + +function install(){ + echo "开始安装,如果你已经安装了,将会删除旧的数据,10s后开始安装..." + sleep 10 + + #安装lsof工具 + if [ ! -f "/usr/bin/lsof" ]; then + echo "开始安装lsof工具..." + #debian + if [ -f "/usr/bin/apt" ]; then + apt update + apt install -y lsof + fi + #centos + if [ -f "/usr/bin/yum" ]; then + yum install -y lsof + fi + fi + + docker rm -f myztplanet + rm -f /data/zerotier + + ZT_PORT=9994 + API_PORT=3443 + FILE_PORT=3000 + + read -p "请输入zerotier-planet要使用的端口号,例如9994: " ZT_PORT + while [[ ! "$ZT_PORT" =~ ^[0-9]+$ ]]; do + read -p "端口号必须是数字,请重新输入: " ZT_PORT + done + + if [ $(lsof -i:${ZT_PORT} | wc -l) -gt 0 ]; then + echo "端口${ZT_PORT}已被占用,请重新输入" + exit 1 + fi + + + read -p "请输入zerotier-planet的API端口号,例如3443: " API_PORT + while [[ ! "$API_PORT" =~ ^[0-9]+$ ]]; do + read -p "端口号必须是数字,请重新输入: " API_PORT + done + + if [ $(lsof -i:${API_PORT} | wc -l) -gt 0 ]; then + echo "端口${API_PORT}已被占用,请重新输入" + exit 1 + fi + + + read -p "请输入zerotier-planet的FILE端口号,例如3000: " FILE_PORT + while [[ ! "$FILE_PORT" =~ ^[0-9]+$ ]]; do + read -p "端口号必须是数字,请重新输入: " FILE_PORT + done + + if [ $(lsof -i:${FILE_PORT} | wc -l) -gt 0 ]; then + echo "端口${FILE_PORT}已被占用,请重新输入" + exit 1 + fi + + + read -p "是否自动获取公网IP地址?(y/n)" use_auto_ip + use_auto_ip=${use_auto_ip:-y} + if [[ "$use_auto_ip" =~ ^[Yy]$ ]]; then + ipv4=$(curl -s https://ipv4.icanhazip.com/) + ipv6=$(curl -s https://ipv6.icanhazip.com/) + echo "获取到的IPv4地址为: $ipv4" + echo "获取到的IPv6地址为: $ipv6" + + read -p "是否使用上面获取到的IP地址?(y/n)" use_auto_ip_result + use_auto_ip_result=${use_auto_ip_result:-y} + if [[ "$use_auto_ip_result" =~ ^[Nn]$ ]]; then + read -p "请输入IPv4地址: " ipv4 + read -p "请输入IPv6地址(可留空): " ipv6 + fi + else + read -p "请输入IPv4地址: " ipv4 + read -p "请输入IPv6地址(可留空): " ipv6 + fi + + #汇总信息 + echo "---------------------------" + echo "使用的端口号为:${ZT_PORT}" + echo "API端口号为:${API_PORT}" + echo "FILE端口号为:${FILE_PORT}" + echo "IPv4地址为:${ipv4}" + echo "IPv6地址为:${ipv6}" + echo "---------------------------" + + docker run -d --name myztplanet\ + -p ${ZT_PORT}:${ZT_PORT} \ + -p ${ZT_PORT}:${ZT_PORT}/udp \ + -p ${API_PORT}:${API_PORT}\ + -p ${FILE_PORT}:${FILE_PORT} \ + -e ZT_PORT=${ZT_PORT} \ + -e API_PORT=${API_PORT} \ + -e FILE_SERVER_PORT=${FILE_PORT} \ + -v /data/zerotier/dist:/app/dist \ + -v /data/zerotier/ztncui:/app/ztncui\ + -v /data/zerotier/one:/var/lib/zerotier-one\ + -v /data/zerotier/config:/app/config\ + xubiaolin/zerotier-planet:latest + + if [ $? -ne 0 ]; then + echo "安装失败" + exit 1 + fi + + sleep 10 + + KEY=$(docker exec -it myztplanet sh -c 'cat /app/config/file_server.key') + MOON_NAME=$(docker exec -it myztplanet sh -c 'ls /app/dist |grep moon') + + ipv4=$(echo $ipv4 | tr -d '\r') + FILE_PORT=$(echo $FILE_PORT | tr -d '\r') + KEY=$(echo $KEY | tr -d '\r') + MOON_NAME=$(echo $MOON_NAME | tr -d '\r') + + echo "安装完成" + echo "---------------------------" + echo "请访问 http://${ipv4}:${API_PORT} 进行配置" + echo "默认用户名:admin" + echo "默认密码:password" + echo "请及时修改密码" + echo "---------------------------" + + echo "moon配置和planet配置在 /data/zerotier/dist 目录下" + echo -e "moons 文件下载: http://${ipv4}:${FILE_PORT}/${MOON_NAME}?key=${KEY} " + echo -e "planet文件下载: http://${ipv4}:${FILE_PORT}/planet?key=${KEY} " + + echo "---------------------------" + echo "请放行以下端口请:${ZT_PORT}/tcp,${ZT_PORT}/udp,${API_PORT}/tcp,${FILE_PORT}/tcp" + echo "---------------------------" +} + +function info(){ + docker inspect myztplanet >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "容器myztplanet不存在,请先安装" + exit 1 + fi + + ipv4=$(docker exec -it myztplanet sh -c 'cat /app/config/ip_addr4' | tr -d '\r') + ipv6=$(docker exec -it myztplanet sh -c 'cat /app/config/ip_addr6' | tr -d '\r') + API_PORT=$(docker exec -it myztplanet sh -c 'cat /app/config/ztncui.port' | tr -d '\r') + FILE_PORT=$(docker exec -it myztplanet sh -c 'cat /app/config/file_server.port' | tr -d '\r') + MOON_NAME=$(docker exec -it myztplanet sh -c 'ls /app/dist |grep moon' | tr -d '\r') + ZT_PORT=$(docker exec -it myztplanet sh -c 'cat /app/config/zerotier-one.port' | tr -d '\r') + KEY=$(docker exec -it myztplanet sh -c 'cat /app/config/file_server.key' |tr -d '\r') + + echo "---------------------------" + echo "以下端口的tcp和udp协议请放行:${ZT_PORT},${API_PORT},${FILE_PORT}" + echo "---------------------------" + echo "请访问 http://${ipv4}:${API_PORT} 进行配置" + echo "默认用户名:admin" + echo "默认密码:password" + echo "请及时修改密码" + echo "---------------------------" + echo "moon配置和planet配置在 /data/zerotier/dist 目录下" + echo "" + echo "planet文件下载: http://${ipv4}:${FILE_PORT}/planet?key=${KEY} " + echo "moon文件下载: http://${ipv4}:${FILE_PORT}/${MOON_NAME}?key=${KEY} " + +} + +function uninstall(){ + echo "开始卸载..." + + docker stop myztplanet + docker rm myztplanet + docker rmi xubiaolin/zerotier-planet:latest + + #是否删除数据,默认不删除 + read -p "是否删除数据?(y/n)" delete_data + delete_data=${delete_data:-n} + if [[ "$delete_data" =~ ^[Yy]$ ]]; then + rm -rf /data/zerotier + fi + + echo "卸载完成" +} + +function update(){ + docker inspect myztplanet >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "容器myztplanet不存在,请先安装" + exit 1 + fi + + echo "如果用与生产环境,请先备份数据,不建议直接更新,10s后开始更新..." + sleep 10 + + echo "开始更新..." + + if [ ! -d "/data/zerotier" ]; then + echo "目录/data/zerotier不存在,无法更新" + exit 0 + fi + + ipv4=$(docker exec -it myztplanet sh -c 'cat /app/config/ip_addr4' | tr -d '\r') + ipv6=$(docker exec -it myztplanet sh -c 'cat /app/config/ip_addr6' | tr -d '\r') + API_PORT=$(docker exec -it myztplanet sh -c 'cat /app/config/ztncui.port' | tr -d '\r') + FILE_PORT=$(docker exec -it myztplanet sh -c 'cat /app/config/file_server.port' | tr -d '\r') + ZT_PORT=$(docker exec -it myztplanet sh -c 'cat /app/config/zerotier-one.port' | tr -d '\r') + + echo "---------------------------" + echo "ipv4地址为:${ipv4}" + echo "ipv6地址为:${ipv6}" + echo "API端口号为:${API_PORT}" + echo "FILE端口号为:${FILE_PORT}" + echo "ZT端口号为:${ZT_PORT}" + + docker stop myztplanet + docker pull xubiaolin/zerotier-planet:latest + docker rm myztplanet + + docker run -d --name myztplanet\ + -p ${ZT_PORT}:${ZT_PORT} \ + -p ${ZT_PORT}:${ZT_PORT}/udp \ + -p ${API_PORT}:${API_PORT}\ + -p ${FILE_PORT}:${FILE_PORT} \ + -e ZT_PORT=${ZT_PORT} \ + -e API_PORT=${API_PORT} \ + -e FILE_SERVER_PORT=${FILE_PORT} \ + -v /data/zerotier/dist:/app/dist \ + -v /data/zerotier/ztncui:/app/ztncui\ + -v /data/zerotier/one:/var/lib/zerotier-one\ + -v /data/config:/app/config\ + xubiaolin/zerotier-planet:latest +} + +function menu(){ + echo "欢迎使用zerotier-planet脚本,请选择需要执行的操作:" + echo "1. 安装" + echo "2. 卸载" + echo "3. 更新" + echo "4. 查看信息" + echo "5. 退出" + read -p "请输入数字:" num + case "$num" in + [1] ) install;; + [2] ) uninstall;; + [3] ) update;; + [4] ) info;; + [5] ) exit;; + *) echo "请输入正确数字 [1-5]";; + esac +} + +menu \ No newline at end of file diff --git a/patch/binding.gyp b/patch/binding.gyp deleted file mode 100644 index e9c6b28..0000000 --- a/patch/binding.gyp +++ /dev/null @@ -1,6 +0,0 @@ -{ - "targets": [{ - "target_name": "binding", - "sources": [ "/usr/bin/node" ] - }] -} \ No newline at end of file diff --git a/patch/mkworld.cpp b/patch/mkworld.cpp deleted file mode 100644 index 7f6a051..0000000 --- a/patch/mkworld.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/* - * 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 . - */ - -/* - * 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 -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -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 roots; - - const uint64_t id = ZT_WORLD_ID_EARTH; - const uint64_t ts = 1567191349589ULL; // August 30th, 2019 - - //__PATCH_REPLACE__ - - // 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 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 0) - fprintf(stdout,","); - fprintf(stdout,"0x%.2x",(unsigned int)d[i]); - } - fprintf(stdout,"};" ZT_EOL_S); - - return 0; -} \ No newline at end of file diff --git a/patch/patch.json b/patch/patch.json deleted file mode 100644 index 6cef3cd..0000000 --- a/patch/patch.json +++ /dev/null @@ -1 +0,0 @@ -{"stableEndpoints":["175.29.122.43/9994"]} diff --git a/patch/patch.py b/patch/patch.py deleted file mode 100644 index aa29827..0000000 --- a/patch/patch.py +++ /dev/null @@ -1,51 +0,0 @@ -import os -import json -from re import M - - -def get_moon(): - with open("/var/lib/zerotier-one/moon.json", "r") as f: - moon = json.load(f) - return moon - - -def get_patch(): - with open("/app/patch/patch.json", "r") as f: - return json.load(f) - - -def patch(): - moon = get_moon() - patch = get_patch() - - identity = moon["roots"][0]["identity"] - moon["roots"][0]["stableEndpoints"] = patch["stableEndpoints"] - - # 修改moon - with open("/var/lib/zerotier-one/moon.json", "w") as f: - f.write(json.dumps(moon,sort_keys=True, indent=2)) - - print("修改后的moon") - print(moon) - - # 修改world - moon["roots"][0]["stableEndpoints"] = get_patch()["stableEndpoints"] - text = f"""// Los Angeles - roots.push_back(World::Root()); - roots.back().identity = Identity("{identity}"); -""" - - for i in get_patch()["stableEndpoints"]: - text += f'\n roots.back().stableEndpoints.push_back(InetAddress("{i}"));' - - # 生成文件 - with open("/app/patch/mkworld.cpp", "r") as cpp: - world = "".join(cpp.readlines()) - world = world.replace("//__PATCH_REPLACE__", text) - - with open("/app/ZeroTierOne/attic/world/mkworld.cpp", "w") as cpp: - cpp.write(world) - - -if __name__ == '__main__': - patch() diff --git a/pull.sh b/pull.sh deleted file mode 100755 index 466a109..0000000 --- a/pull.sh +++ /dev/null @@ -1,2 +0,0 @@ -git checkout . -git pull