diff --git a/Dockerfile b/Dockerfile index ef9e77b..8eb57f7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,9 +5,9 @@ ARG TAG=main ENV TAG=${TAG} WORKDIR /app -ADD ./entrypoint.sh /app/entrypoint.sh -ADD ./http_server.js /app/http_server.js -ADD ./mkmoonworld-x86_64 /app/mkmoonworld-x86_64 +ADD ./patch/entrypoint.sh /app/entrypoint.sh +ADD ./patch/http_server.js /app/http_server.js +ADD ./patch/mkworld_custom.cpp /app/patch/mkworld_custom.cpp # init tool RUN set -x\ @@ -29,7 +29,14 @@ RUN set -x\ && 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!" + && echo "zerotier-one init success!"\ + && cp /app/patch/mkworld_custom.cpp ./attic/world\ + && mv ./attic/world/mkworld.cpp ./attic/world/mkworld.cpp.bak \ + && mv ./attic/world/mkworld_custom.cpp ./attic/world/mkworld.cpp \ + && sh ./attic/world/build.sh \ + && mv ./attic/world/mkworld /var/lib/zerotier-one\ + && echo "mkworld build success!" + #make ztncui @@ -53,7 +60,7 @@ ENV ZT_PORT=9994 ENV API_PORT=3443 ENV FILE_SERVER_PORT=3000 -ENV GH_MIRROR="https://ghproxy.imoyuapp.win/" +ENV GH_MIRROR="https://mirror.ghproxy.com/" ENV FILE_KEY='' ENV TZ=Asia/Shanghai @@ -63,9 +70,9 @@ 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 -COPY --from=builder /app/mkmoonworld-x86_64 /app/mkmoonworld-x86_64 +COPY --from=builder /app/ZeroTierOne/attic/world/mkworld /var/lib/zerotier-one/mkworld -RUN set -x ;sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \ +RUN set -x \ && apk update \ && apk add --no-cache npm curl jq openssl\ && mkdir /app/config -p diff --git a/build.sh b/build.sh index ef64dd8..33f26f9 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,10 @@ #!/bin/bash -docker build --build-arg TAG=1.12.2 -t xubiaolin/zerotier-planet:latest . \ No newline at end of file +USER=zerotier +REPO=ZeroTierOne + +latest_tag=$(curl -s "https://api.github.com/repos/$USER/$REPO/tags" | jq -r '.[].name' | grep -E "^[0-9]+\.[0-9]+\.[0-9]+$" | sort -V | tail -n 1) + +echo "Latest tag for $USER/$REPO matching the format digit.digit.digit is: $latest_tag" + +docker build --build-arg TAG="$latest_tag" -t "xubiaolin/zerotier-planet:$latest_tag" . diff --git a/mkmoonworld-x86_64 b/mkmoonworld-x86_64 deleted file mode 100755 index db80c95..0000000 Binary files a/mkmoonworld-x86_64 and /dev/null differ diff --git a/entrypoint.sh b/patch/entrypoint.sh similarity index 96% rename from entrypoint.sh rename to patch/entrypoint.sh index 4185168..23618c0 100755 --- a/entrypoint.sh +++ b/patch/entrypoint.sh @@ -64,9 +64,7 @@ function check_zerotier() { 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 - cp /app/mkmoonworld-x86_64 ./mkmoonworld-x86_64 - chmod +x ./mkmoonworld-x86_64 - ./mkmoonworld-x86_64 moon.json + ./mkworld moon.json if [ $? -ne 0 ]; then echo "mkmoonworld failed!" exit 1 diff --git a/http_server.js b/patch/http_server.js similarity index 100% rename from http_server.js rename to patch/http_server.js diff --git a/patch/mkworld_custom.cpp b/patch/mkworld_custom.cpp new file mode 100644 index 0000000..55651db --- /dev/null +++ b/patch/mkworld_custom.cpp @@ -0,0 +1,232 @@ +/* + * 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 +#include + +using namespace ZeroTier; +using json = nlohmann::json; + +void printHelp() { + printf("Usage: mkworld [options]\n"); + printf("Options:\n"); + printf(" -h, --help Display this help message\n"); + printf(" -j, --json2bin Convert from JSON file to world.bin\n"); + printf(" -b, --bin2json Convert from world.bin to JSON format\n"); +} + +int jsonToBinary() { + 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 + + std::string fileContent; + if (!OSUtils::readFile("config.json", fileContent)) { + fprintf(stderr, "Failed to open config file." ZT_EOL_S); + return 1; + } + + // 解析JSON数据 + json config = json::parse(fileContent); + + + for (auto& root : config["roots"]) { + roots.push_back(World::Root()); + roots.back().identity = Identity(root["identity"].get().c_str()); + for (auto& endpoint : root["stableEndpoints"]) { + roots.back().stableEndpoints.push_back(InetAddress(endpoint.get().c_str())); + } + } + + 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 < 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; +} + +void binaryToJson() { + // Read world.bin file into memory + std::string binContent; + if (!OSUtils::readFile("world.bin", binContent)) { + fprintf(stderr, "Failed to open world.bin file." ZT_EOL_S); + return; + } + + // Deserialize the binary data into a World object + Buffer binBuffer(binContent.data(), binContent.size()); + World world; + if (!world.deserialize(binBuffer, 0)) { + fprintf(stderr, "Failed to deserialize world.bin content." ZT_EOL_S); + return; + } + + // Create a JSON object to store the world data + json worldJson; + + + // Add roots array to the JSON object + json rootsJson; + for (const auto& root : world.roots()) { + json rootJson; + + // Add identity to the root JSON object + char identityStr[ZT_IDENTITY_STRING_BUFFER_LENGTH]; + root.identity.toString(true, identityStr); // Include private key + rootJson["identity"] = identityStr; + + // Add stableEndpoints array to the root JSON object + json stableEndpointsJson; + for (const auto& endpoint : root.stableEndpoints) { + char ipStr[64]; + endpoint.toString(ipStr); + stableEndpointsJson.push_back(ipStr); + } + rootJson["stableEndpoints"] = stableEndpointsJson; + + rootsJson.push_back(rootJson); + } + worldJson["roots"] = rootsJson; + std::string jsonStr = worldJson.dump(4); + printf("World JSON:\n%s\n", jsonStr.c_str()); + if (!OSUtils::writeFile("config.json", jsonStr.c_str(), jsonStr.size())) { + fprintf(stderr, "Failed to write JSON data to config.json." ZT_EOL_S); + } + else { + printf("JSON data successfully written to config.json." ZT_EOL_S); + } +} + + +int main(int argc, char** argv) +{ + bool json2bin = false; + bool bin2json = false; + + for (int i = 1; i < argc; ++i) { + std::string arg = argv[i]; + if (arg == "-h" || arg == "--help") { + printHelp(); + return 0; + } + else if (arg == "-j" || arg == "--json2bin") { + json2bin = true; + } + else if (arg == "-b" || arg == "--bin2json") { + bin2json = true; + } + } + + if (!(json2bin || bin2json)) { + // Default behavior: convert from JSON to world.bin + json2bin = true; + } + + if (json2bin && bin2json) { + printf("Error: Cannot specify both JSON to binary and binary to JSON conversion options.\n"); + printHelp(); + return 1; + } + + if (json2bin) { + jsonToBinary(); + } + else if (bin2json) { + binaryToJson(); + } + + return 0; +}