diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index bf2f3dbf..8ad81394 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -28,7 +28,7 @@ jobs: runs-on: macos-latest steps: - name: depends - run: brew update && brew install fmt poco spdlog nlohmann-json + run: brew update && brew install fmt poco spdlog - name: checkout uses: actions/checkout@v4 - name: build @@ -57,7 +57,6 @@ jobs: mingw-w64-x86_64-gcc mingw-w64-x86_64-spdlog mingw-w64-x86_64-poco - mingw-w64-x86_64-nlohmann-json - name: checkout uses: actions/checkout@v4 - name: cache diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 9a9d54cd..379d695f 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -61,7 +61,6 @@ jobs: mingw-w64-x86_64-gcc mingw-w64-x86_64-spdlog mingw-w64-x86_64-poco - mingw-w64-x86_64-nlohmann-json - name: checkout uses: actions/checkout@v4 - name: cache diff --git a/CMakeLists.txt b/CMakeLists.txt index c76ea77b..0f7d9053 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,12 +91,6 @@ else() find_package(Poco REQUIRED COMPONENTS Foundation XML JSON Net NetSSL) endif() -if (${CANDY_STATIC_NLOHMANN_JSON}) - Fetch(nlohmann_json "https://github.com/nlohmann/json.git" "v3.11.3") -else() - find_package(nlohmann_json REQUIRED) -endif() - add_subdirectory(candy) add_subdirectory(candy-cli) add_subdirectory(candy-service) diff --git a/candy-cli/CMakeLists.txt b/candy-cli/CMakeLists.txt index af17a791..4e09471a 100644 --- a/candy-cli/CMakeLists.txt +++ b/candy-cli/CMakeLists.txt @@ -9,8 +9,7 @@ target_include_directories(candy-cli PUBLIC set_target_properties(candy-cli PROPERTIES OUTPUT_NAME "candy") target_link_libraries(candy-cli PRIVATE spdlog::spdlog) -target_link_libraries(candy-cli PRIVATE Poco::Foundation) -target_link_libraries(candy-cli PRIVATE nlohmann_json::nlohmann_json) +target_link_libraries(candy-cli PRIVATE Poco::Foundation Poco::JSON) target_link_libraries(candy-cli PRIVATE Candy::Library) install(TARGETS candy-cli) diff --git a/candy-cli/src/config.cc b/candy-cli/src/config.cc index 1c93025e..959e6a18 100644 --- a/candy-cli/src/config.cc +++ b/candy-cli/src/config.cc @@ -2,6 +2,7 @@ #include "config.h" #include "argparse.h" #include "candy/candy.h" +#include #include #include #include @@ -9,29 +10,34 @@ #include #include #include -#include #include #include #include -nlohmann::json arguments::json() { - nlohmann::json config = { - {"mode", this->mode}, - {"websocket", this->websocket}, - {"password", this->password}, - {"dhcp", this->dhcp}, - {"sdwan", this->sdwan}, - {"name", this->name}, - {"tun", this->tun}, - {"stun", this->stun}, - {"localhost", this->localhost}, - {"discovery", this->discovery}, - {"route", this->routeCost}, - {"mtu", this->mtu}, - {"port", this->port}, - {"vmac", virtualMac(this->name)}, - {"expt", loadTunAddress(this->name)}, - }; +Poco::JSON::Object arguments::json() { + Poco::JSON::Object config; + config.set("mode", this->mode); + config.set("websocket", this->websocket); + config.set("password", this->password); + + if (this->mode == "client") { + config.set("name", this->name); + config.set("tun", this->tun); + config.set("stun", this->stun); + config.set("localhost", this->localhost); + config.set("discovery", this->discovery); + config.set("route", this->routeCost); + config.set("mtu", this->mtu); + config.set("port", this->port); + config.set("vmac", virtualMac(this->name)); + config.set("expt", loadTunAddress(this->name)); + } + + if (this->mode == "server") { + config.set("dhcp", this->dhcp); + config.set("sdwan", this->sdwan); + } + return config; } diff --git a/candy-cli/src/config.h b/candy-cli/src/config.h index a023b45b..dbd5e5b6 100644 --- a/candy-cli/src/config.h +++ b/candy-cli/src/config.h @@ -2,13 +2,13 @@ #ifndef CANDY_CLI_CONFIG_H #define CANDY_CLI_CONFIG_H +#include #include -#include #include struct arguments { int parse(int argc, char *argv[]); - nlohmann::json json(); + Poco::JSON::Object json(); private: void parseFile(std::string cfgFile); diff --git a/candy-cli/src/main.cc b/candy-cli/src/main.cc index 12a57474..4e94df00 100644 --- a/candy-cli/src/main.cc +++ b/candy-cli/src/main.cc @@ -6,10 +6,9 @@ int main(int argc, char *argv[]) { arguments args; args.parse(argc, argv); - auto config = args.json(); - if (config["mode"] == "client") { + if (config.getValue("mode") == "client") { static const std::string id = "cli"; auto handler = [](int) -> void { candy::client::shutdown(id); }; @@ -21,14 +20,11 @@ int main(int argc, char *argv[]) { while (true) { std::this_thread::sleep_for(std::chrono::seconds(1)); auto status = candy::client::status(id); - if (status) { - auto it = (*status).find("address"); - if (it != (*status).end() && it->is_string()) { - auto address = it->get(); - if (!address.empty()) { - saveTunAddress(config["name"], address); - break; - } + if (status && (*status).has("address")) { + std::string address = (*status).getValue("address"); + if (!address.empty()) { + saveTunAddress(config.getValue("name"), address); + break; } } } @@ -38,7 +34,7 @@ int main(int argc, char *argv[]) { return 0; } - if (config["mode"] == "server") { + if (config.getValue("mode") == "server") { auto handler = [](int) -> void { candy::server::shutdown(); }; signal(SIGINT, handler); diff --git a/candy-service/CMakeLists.txt b/candy-service/CMakeLists.txt index d927e184..9ed60f0e 100644 --- a/candy-service/CMakeLists.txt +++ b/candy-service/CMakeLists.txt @@ -9,8 +9,7 @@ target_include_directories(candy-service PUBLIC set_target_properties(candy-service PROPERTIES OUTPUT_NAME "candy-service") target_link_libraries(candy-service PRIVATE spdlog::spdlog) -target_link_libraries(candy-service PRIVATE Poco::Foundation) -target_link_libraries(candy-service PRIVATE nlohmann_json::nlohmann_json) +target_link_libraries(candy-service PRIVATE Poco::Foundation Poco::JSON) target_link_libraries(candy-service PRIVATE Candy::Library) install(TARGETS candy-service) diff --git a/candy/CMakeLists.txt b/candy/CMakeLists.txt index f0d819b9..9df7f1c2 100644 --- a/candy/CMakeLists.txt +++ b/candy/CMakeLists.txt @@ -16,8 +16,7 @@ else() endif() target_link_libraries(candy-library PRIVATE spdlog::spdlog) -target_link_libraries(candy-library PRIVATE Poco::Foundation Poco::Net Poco::NetSSL) -target_link_libraries(candy-library PRIVATE nlohmann_json::nlohmann_json) +target_link_libraries(candy-library PRIVATE Poco::Foundation Poco::JSON Poco::Net Poco::NetSSL) set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) diff --git a/candy/include/candy/client.h b/candy/include/candy/client.h index 41718924..c112c631 100644 --- a/candy/include/candy/client.h +++ b/candy/include/candy/client.h @@ -2,16 +2,16 @@ #ifndef CANDY_CLIENT_H #define CANDY_CLIENT_H -#include +#include #include #include namespace candy { namespace client { -bool run(const std::string &id, const nlohmann::json &config); +bool run(const std::string &id, const Poco::JSON::Object &config); bool shutdown(const std::string &id); -std::optional status(const std::string &id); +std::optional status(const std::string &id); } // namespace client } // namespace candy diff --git a/candy/include/candy/server.h b/candy/include/candy/server.h index f7c36948..1f4fadb3 100644 --- a/candy/include/candy/server.h +++ b/candy/include/candy/server.h @@ -2,13 +2,13 @@ #ifndef CANDY_SERVER_H #define CANDY_SERVER_H -#include +#include #include namespace candy { namespace server { -bool run(const nlohmann::json &config); +bool run(const Poco::JSON::Object &config); bool shutdown(); } // namespace server diff --git a/candy/src/candy/client.cc b/candy/src/candy/client.cc index 8111ebd8..c2a1e647 100644 --- a/candy/src/candy/client.cc +++ b/candy/src/candy/client.cc @@ -2,10 +2,11 @@ #include "candy/client.h" #include "core/client.h" #include "utils/atomic.h" +#include +#include #include #include #include -#include #include #include #include @@ -29,10 +30,10 @@ class Instance { } } - nlohmann::json status() { - nlohmann::json data; + Poco::JSON::Object status() { + Poco::JSON::Object data; if (auto client = this->client.lock()) { - data["address"] = client->getTunCidr(); + data.set("address", client->getTunCidr()); } return data; } @@ -70,27 +71,33 @@ bool try_erase_instance(const std::string &id) { } // namespace -bool run(const std::string &id, const nlohmann::json &config) { +bool run(const std::string &id, const Poco::JSON::Object &config) { auto instance = try_create_instance(id); if (!instance) { return false; } - spdlog::info("run enter: id={} config={}", id, config.dump(4)); + auto toString = [](const Poco::JSON::Object &obj) -> std::string { + std::ostringstream oss; + Poco::JSON::Stringifier::stringify(obj, oss, 4); + return oss.str(); + }; + + spdlog::info("run enter: id={} config={}", id, toString(config)); while ((*instance)->is_running()) { std::this_thread::sleep_for(std::chrono::seconds(1)); auto client = (*instance)->create_client(); - client->setName(config["name"]); - client->setPassword(config["password"]); - client->setWebSocket(config["websocket"]); - client->setTunAddress(config["tun"]); - client->setVirtualMac(config["vmac"]); - client->setExptTunAddress(config["expt"]); - client->setStun(config["stun"]); - client->setDiscoveryInterval(config["discovery"]); - client->setRouteCost(config["route"]), client->setMtu(config["mtu"]); - client->setPort(config["port"]); - client->setLocalhost(config["localhost"]); + client->setName(config.getValue("name")); + client->setPassword(config.getValue("password")); + client->setWebSocket(config.getValue("websocket")); + client->setTunAddress(config.getValue("tun")); + client->setVirtualMac(config.getValue("vmac")); + client->setExptTunAddress(config.getValue("expt")); + client->setStun(config.getValue("stun")); + client->setDiscoveryInterval(config.getValue("discovery")); + client->setRouteCost(config.getValue("route")), client->setMtu(config.getValue("mtu")); + client->setPort(config.getValue("port")); + client->setLocalhost(config.getValue("localhost")); client->run(); } spdlog::info("run exit: id={} ", id); @@ -111,7 +118,7 @@ bool shutdown(const std::string &id) { return true; } -std::optional status(const std::string &id) { +std::optional status(const std::string &id) { std::shared_lock lock(instance_mutex); auto it = instance_map.find(id); if (it != instance_map.end()) { diff --git a/candy/src/candy/server.cc b/candy/src/candy/server.cc index eacb5eb7..bdd83476 100644 --- a/candy/src/candy/server.cc +++ b/candy/src/candy/server.cc @@ -11,14 +11,14 @@ Utils::Atomic running(true); std::shared_ptr server; } // namespace -bool run(const nlohmann::json &config) { +bool run(const Poco::JSON::Object &config) { while (running.load()) { std::this_thread::sleep_for(std::chrono::seconds(1)); server = std::make_shared(); - server->setWebSocket(config["websocket"]); - server->setPassword(config["password"]); - server->setDHCP(config["dhcp"]); - server->setSdwan(config["sdwan"]); + server->setWebSocket(config.getValue("websocket")); + server->setPassword(config.getValue("password")); + server->setDHCP(config.getValue("dhcp")); + server->setSdwan(config.getValue("sdwan")); server->run(); } return true; diff --git a/dockerfile b/dockerfile index 2c6a3e05..aea4ddd9 100644 --- a/dockerfile +++ b/dockerfile @@ -3,7 +3,7 @@ RUN apk update RUN apk add spdlog openssl poco FROM base AS build -RUN apk add git cmake ninja pkgconf g++ spdlog-dev openssl-dev poco-dev nlohmann-json linux-headers +RUN apk add git cmake ninja pkgconf g++ spdlog-dev openssl-dev poco-dev linux-headers COPY . candy RUN cd candy && cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release && cmake --build build && cmake --install build