tools: use yaml config file instead of default values
Change-Id: Ib9466e7b7a8f093620949ad4557817c26e52566e
diff --git a/tools/common.cpp b/tools/common.cpp
index 979274d..7d338e5 100644
--- a/tools/common.cpp
+++ b/tools/common.cpp
@@ -30,14 +30,10 @@
namespace dhtnet {
dht::crypto::Identity
-loadIdentity(const std::filesystem::path& path)
-{
- if (!std::filesystem::exists(path)) {
- std::filesystem::create_directory(path);
- }
+loadIdentity(const std::filesystem::path& path_id){
try {
- for (const auto& path : std::filesystem::directory_iterator(path)) {
- auto p = path.path();
+ for (const auto& path_id : std::filesystem::directory_iterator(path_id)) {
+ auto p = path_id.path();
if (p.extension() == ".pem") {
auto privateKey = std::make_unique<dht::crypto::PrivateKey>(fileutils::loadFile(p));
auto certificate = std::make_unique<dht::crypto::Certificate>(
@@ -45,14 +41,26 @@
return dht::crypto::Identity(std::move(privateKey), std::move(certificate));
}
}
- } catch (const std::exception& e) {
- fmt::print(stderr, "Error loadind key from .dhtnetTools: {}\n", e.what());
+ } catch (const std::exception& e) {}
+ return {};
+}
+dht::crypto::Identity
+loadIdentity(const std::filesystem::path& path_id, const std::filesystem::path& path_ca)
+{
+ if (!std::filesystem::exists(path_id)) {
+ std::filesystem::create_directory(path_id);
}
-
- auto ca = dht::crypto::generateIdentity("ca");
- auto id = dht::crypto::generateIdentity("dhtnc", ca);
+ // Load identity
+ auto id = loadIdentity(path_id);
+ if (!id.first or !id.second) {
+ // Load CA
+ auto ca_id = loadIdentity(path_ca);
+ if (!ca_id.first or !ca_id.second)
+ ca_id = dht::crypto::generateIdentity("dhtnet");
+ id = dht::crypto::generateIdentity("dhtnet", ca_id);
fmt::print("Generated new identity: {}\n", id.first->getPublicKey().getId());
- dht::crypto::saveIdentity(id, path / "id");
+ dht::crypto::saveIdentity(id, path_id / "id");
+}
return id;
}
@@ -102,14 +110,13 @@
config->factory = iceFactory;
config->cachePath = path;
config->logger = logger;
- if (!turn_host.empty())
+ if (!turn_host.empty()){
config->turnEnabled = true;
- config->turnServer = turn_host;
- config->turnServerUserName = turn_user;
- config->turnServerPwd = turn_pass;
- config->turnServerRealm = turn_realm;
-
-
+ config->turnServer = turn_host;
+ config->turnServerUserName = turn_user;
+ config->turnServerPwd = turn_pass;
+ config->turnServerRealm = turn_realm;
+ }
return std::move(config);
}
template<typename T>