Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 1 addition & 2 deletions candy-cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 25 additions & 19 deletions candy-cli/src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,42 @@
#include "config.h"
#include "argparse.h"
#include "candy/candy.h"
#include <Poco/JSON/Object.h>
#include <Poco/Platform.h>
#include <Poco/String.h>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <nlohmann/json.hpp>
#include <spdlog/spdlog.h>
#include <sstream>
#include <string>

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;
}

Expand Down
4 changes: 2 additions & 2 deletions candy-cli/src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#ifndef CANDY_CLI_CONFIG_H
#define CANDY_CLI_CONFIG_H

#include <Poco/JSON/Object.h>
#include <map>
#include <nlohmann/json.hpp>
#include <string>

struct arguments {
int parse(int argc, char *argv[]);
nlohmann::json json();
Poco::JSON::Object json();

private:
void parseFile(std::string cfgFile);
Expand Down
18 changes: 7 additions & 11 deletions candy-cli/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>("mode") == "client") {
static const std::string id = "cli";

auto handler = [](int) -> void { candy::client::shutdown(id); };
Expand All @@ -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<std::string>();
if (!address.empty()) {
saveTunAddress(config["name"], address);
break;
}
if (status && (*status).has("address")) {
std::string address = (*status).getValue<std::string>("address");
if (!address.empty()) {
saveTunAddress(config.getValue<std::string>("name"), address);
break;
}
}
}
Expand All @@ -38,7 +34,7 @@ int main(int argc, char *argv[]) {
return 0;
}

if (config["mode"] == "server") {
if (config.getValue<std::string>("mode") == "server") {
auto handler = [](int) -> void { candy::server::shutdown(); };

signal(SIGINT, handler);
Expand Down
3 changes: 1 addition & 2 deletions candy-service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions candy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions candy/include/candy/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#ifndef CANDY_CLIENT_H
#define CANDY_CLIENT_H

#include <nlohmann/json.hpp>
#include <Poco/JSON/Object.h>
#include <optional>
#include <string>

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<nlohmann::json> status(const std::string &id);
std::optional<Poco::JSON::Object> status(const std::string &id);

} // namespace client
} // namespace candy
Expand Down
4 changes: 2 additions & 2 deletions candy/include/candy/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#ifndef CANDY_SERVER_H
#define CANDY_SERVER_H

#include <nlohmann/json.hpp>
#include <Poco/JSON/Object.h>
#include <string>

namespace candy {
namespace server {

bool run(const nlohmann::json &config);
bool run(const Poco::JSON::Object &config);
bool shutdown();

} // namespace server
Expand Down
43 changes: 25 additions & 18 deletions candy/src/candy/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#include "candy/client.h"
#include "core/client.h"
#include "utils/atomic.h"
#include <Poco/JSON/Object.h>
#include <Poco/JSON/Stringifier.h>
#include <map>
#include <memory>
#include <mutex>
#include <nlohmann/json.hpp>
#include <optional>
#include <shared_mutex>
#include <spdlog/spdlog.h>
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<std::string>("name"));
client->setPassword(config.getValue<std::string>("password"));
client->setWebSocket(config.getValue<std::string>("websocket"));
client->setTunAddress(config.getValue<std::string>("tun"));
client->setVirtualMac(config.getValue<std::string>("vmac"));
client->setExptTunAddress(config.getValue<std::string>("expt"));
client->setStun(config.getValue<std::string>("stun"));
client->setDiscoveryInterval(config.getValue<int>("discovery"));
client->setRouteCost(config.getValue<int>("route")), client->setMtu(config.getValue<int>("mtu"));
client->setPort(config.getValue<int>("port"));
client->setLocalhost(config.getValue<std::string>("localhost"));
client->run();
}
spdlog::info("run exit: id={} ", id);
Expand All @@ -111,7 +118,7 @@ bool shutdown(const std::string &id) {
return true;
}

std::optional<nlohmann::json> status(const std::string &id) {
std::optional<Poco::JSON::Object> status(const std::string &id) {
std::shared_lock lock(instance_mutex);
auto it = instance_map.find(id);
if (it != instance_map.end()) {
Expand Down
10 changes: 5 additions & 5 deletions candy/src/candy/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Utils::Atomic<bool> running(true);
std::shared_ptr<Server> 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>();
server->setWebSocket(config["websocket"]);
server->setPassword(config["password"]);
server->setDHCP(config["dhcp"]);
server->setSdwan(config["sdwan"]);
server->setWebSocket(config.getValue<std::string>("websocket"));
server->setPassword(config.getValue<std::string>("password"));
server->setDHCP(config.getValue<std::string>("dhcp"));
server->setSdwan(config.getValue<std::string>("sdwan"));
server->run();
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down