tools/dnc: add verbose option to enable the log
Change-Id: If2efb47ecc2c8701ecc41e3318f716e5f584508f
diff --git a/tools/dnc/README.md b/tools/dnc/README.md
index fffb379..4aef7fd 100644
--- a/tools/dnc/README.md
+++ b/tools/dnc/README.md
@@ -28,6 +28,7 @@
- `-p, --privateKey`: Provide a private key.
- `-d, --configuration`: Define the dnc configuration with a YAML file path.
- `-a, --anonymous_cnx`: Activate anonymous connection mode.
+- `-vv, --verbose`: Enable verbose mode.
For additional options, use the `-d` flag with a YAML configuration file:
```shell
diff --git a/tools/dnc/dnc.1 b/tools/dnc/dnc.1
index 26f847c..a24cf67 100644
--- a/tools/dnc/dnc.1
+++ b/tools/dnc/dnc.1
@@ -60,6 +60,9 @@
.TP
.B \-a, \-\-anonymous_cnx
Activate anonymous connection mode.
+.TP
+.B \-vv, \-\-verbose
+Enable verbose mode.
.SH "ESTABLISHING SSH CONNECTIONS"
To facilitate SSH connections, \fBdnc\fR establishes a DHT network connection followed by socket creation on port 22. This requires an operational OpenSSH server.
diff --git a/tools/dnc/dnc.cpp b/tools/dnc/dnc.cpp
index bce0388..764945c 100644
--- a/tools/dnc/dnc.cpp
+++ b/tools/dnc/dnc.cpp
@@ -59,9 +59,10 @@
const std::string& turn_user,
const std::string& turn_pass,
const std::string& turn_realm,
- const bool anonymous)
- : logger(dht::log::getStdLogger())
- , ioContext(std::make_shared<asio::io_context>()),
+ const bool anonymous,
+ const bool verbose)
+ :logger(verbose ? dht::log::getStdLogger() : nullptr),
+ ioContext(std::make_shared<asio::io_context>()),
iceFactory(std::make_shared<IceTransportFactory>(logger))
{
ioContextRunner = std::thread([context = ioContext, logger = logger] {
@@ -105,8 +106,7 @@
connectionManager->onChannelRequest(
[&](const std::shared_ptr<dht::crypto::Certificate>&, const std::string& name) {
// handle channel request
- if (logger)
- logger->debug("Channel request received: {}", name);
+ fmt::print("Channel request received: {}\n", name);
return true;
});
@@ -119,8 +119,7 @@
}
try {
auto parsedName = parseName(name);
- if (logger)
- logger->debug("Connecting to {}:{}", parsedName.first, parsedName.second);
+ fmt::print("Connecting to {}:{}", parsedName.first, parsedName.second);
asio::ip::tcp::resolver resolver(*ioContext);
asio::ip::tcp::resolver::results_type endpoints = resolver.resolve(parsedName.first,
@@ -136,8 +135,7 @@
[this, socket, mtlxSocket](const std::error_code& error,
const asio::ip::tcp::endpoint& ep) {
if (!error) {
- if (logger)
- logger->debug("Connected!");
+ fmt::print("Connected!\n");
mtlxSocket->setOnRecv([socket, this](const uint8_t* data, size_t size) {
auto data_copy = std::make_shared<std::vector<uint8_t>>(data,
data + size);
@@ -146,10 +144,9 @@
[data_copy, this](const std::error_code& error,
std::size_t bytesWritten) {
if (error) {
- if (logger)
- logger->error("Write error: {}",
- error.message());
+ fmt::print("Write error: {}\n", error.message());
}
+
});
return size;
});
@@ -157,15 +154,13 @@
auto buffer = std::make_shared<std::vector<uint8_t>>(BUFFER_SIZE);
readFromPipe(mtlxSocket, socket, buffer);
} else {
- if (logger)
- logger->error("Connection error: {}", error.message());
+ fmt::print("Connection error: {}\n", error.message());
mtlxSocket->shutdown();
}
});
} catch (std::exception& e) {
- if (logger)
- logger->error("Exception: {}", e.what());
+ fmt::print("Exception: {}\n", e.what());
}
});
}
@@ -178,8 +173,9 @@
const std::string& turn_host,
const std::string& turn_user,
const std::string& turn_pass,
- const std::string& turn_realm)
- : Dnc(identity, bootstrap,turn_host,turn_user,turn_pass, turn_realm, true)
+ const std::string& turn_realm,
+ const bool verbose)
+ : Dnc(identity, bootstrap,turn_host,turn_user,turn_pass, turn_realm, true, verbose)
{
std::condition_variable cv;
auto name = fmt::format("nc://{:s}:{:d}", remote_host, remote_port);
@@ -201,8 +197,7 @@
readFromPipe(socket, stdinPipe, buffer);
socket->onShutdown([this]() {
- if (logger)
- logger->debug("Exit program");
+ fmt::print("Exit program\n");
ioContext->stop();
});
}
@@ -210,8 +205,7 @@
connectionManager->onConnectionReady(
[&](const DeviceId&, const std::string& name, std::shared_ptr<ChannelSocket> mtlxSocket) {
- if (logger)
- logger->debug("Connected!");
+ fmt::print("Connected!\n");
});
}
diff --git a/tools/dnc/dnc.h b/tools/dnc/dnc.h
index 0f8e24e..665d2a0 100644
--- a/tools/dnc/dnc.h
+++ b/tools/dnc/dnc.h
@@ -39,7 +39,8 @@
const std::string& turn_user,
const std::string& turn_pass,
const std::string& turn_realm,
- const bool anonymous);
+ const bool anonymous,
+ const bool verbose);
// Build a client
Dnc(
dht::crypto::Identity identity,
@@ -50,7 +51,8 @@
const std::string& turn_host,
const std::string& turn_user,
const std::string& turn_pass,
- const std::string& turn_realm);
+ const std::string& turn_realm,
+ const bool verbose);
~Dnc();
void run();
diff --git a/tools/dnc/dnc.yaml b/tools/dnc/dnc.yaml
index adf3be1..661ecc5 100644
--- a/tools/dnc/dnc.yaml
+++ b/tools/dnc/dnc.yaml
@@ -7,4 +7,5 @@
ip: "127.0.0.1"
# certificate: "to/your/certificate.crt"
# privateKey: "to/your/privatekey.pem"
-anonymous: true
\ No newline at end of file
+anonymous: true
+verbose: false
\ No newline at end of file
diff --git a/tools/dnc/main.cpp b/tools/dnc/main.cpp
index 239307d..441ef56 100644
--- a/tools/dnc/main.cpp
+++ b/tools/dnc/main.cpp
@@ -49,6 +49,7 @@
std::string turn_realm {};
std::string configuration {};
bool anonymous_cnx {false};
+ bool verbose {false};
};
static const constexpr struct option long_options[]
@@ -73,13 +74,20 @@
{
dhtnc_params params;
int opt;
+ int v_count = 0;
while ((opt = getopt_long(argc, argv, "ahvlw:r:u:t:P:b:p:i:c:d:", long_options, nullptr)) != -1) {
switch (opt) {
case 'h':
params.help = true;
break;
case 'v':
- params.version = true;
+ v_count++;
+ if (v_count == 1) {
+ params.version = true;
+ }else if (v_count == 2) {
+ params.version = false;
+ params.verbose = true;
+ }
break;
case 'P':
params.remote_port = std::stoi(optarg);
@@ -172,6 +180,9 @@
if (config["anonymous"] && !params.anonymous_cnx) {
params.anonymous_cnx = config["anonymous"].as<bool>();
}
+ if (config["verbose"] && !params.verbose) {
+ params.verbose = config["verbose"].as<bool>();
+ }
}
}
return params;
@@ -214,7 +225,8 @@
" -c, --certificate Specify the certificate option with an argument.\n"
" -d, --configuration Specify the configuration option with an argument.\n"
" -p, --privateKey Specify the privateKey option with an argument.\n"
- " -a, --anonymous_cnx Enable the anonymous mode.\n");
+ " -a, --anonymous_cnx Enable the anonymous mode.\n"
+ " -vv, --verbose Enable verbose mode.\n");
return EXIT_SUCCESS;
}
@@ -227,7 +239,6 @@
fmt::print("Loaded identity: {}\n", identity.second->getId());
fmt::print("dnc 1.0\n");
-
std::unique_ptr<dhtnet::Dnc> dhtnc;
if (params.listen) {
// create dnc instance
@@ -237,7 +248,8 @@
params.turn_user,
params.turn_pass,
params.turn_realm,
- params.anonymous_cnx);
+ params.anonymous_cnx,
+ params.verbose);
} else {
dhtnc = std::make_unique<dhtnet::Dnc>(identity,
params.bootstrap,
@@ -247,7 +259,8 @@
params.turn_host,
params.turn_user,
params.turn_pass,
- params.turn_realm);
+ params.turn_realm,
+ params.verbose);
}
dhtnc->run();
return EXIT_SUCCESS;