lokinet/daemon/rcutil.cpp

96 lines
2.2 KiB
C++
Raw Normal View History

2018-12-12 01:55:30 +00:00
#include <router_contact.hpp>
2019-08-12 22:09:44 +00:00
#include <util/logger.hpp>
#include <util/ostream_logger.hpp>
2018-07-24 03:40:34 +00:00
2019-08-12 21:47:30 +00:00
#include <cxxopts.hpp>
#include <string>
#include <vector>
2019-08-12 22:09:44 +00:00
bool dumpRc(const std::vector<std::string>& files, bool json)
2018-05-21 12:51:10 +00:00
{
2019-08-12 22:09:44 +00:00
nlohmann::json result;
2019-08-12 21:47:30 +00:00
for(const auto& file : files)
{
2019-08-12 21:47:30 +00:00
llarp::RouterContact rc;
const bool ret = rc.Read(file.c_str());
if (ret)
{
2019-08-12 22:09:44 +00:00
if (json)
{
result[file] = rc.ToJson();
}
else
{
2019-08-12 21:47:30 +00:00
std::cout << "file = " << file << "\n";
std::cout << rc << "\n\n";
2019-08-12 22:09:44 +00:00
}
}
2019-08-12 21:47:30 +00:00
else
{
2019-08-12 21:47:30 +00:00
std::cerr << "file = " << file << " was not a valid rc file\n";
}
2018-05-21 12:51:10 +00:00
}
2019-08-12 22:09:44 +00:00
if (json)
std::cout << result << "\n";
2019-08-12 21:47:30 +00:00
return true;
}
2019-08-12 21:47:30 +00:00
int
main(int argc, char *argv[])
{
#ifdef LOKINET_DEBUG
absl::SetMutexDeadlockDetectionMode(absl::OnDeadlockCycle::kAbort);
#endif
2019-08-12 21:47:30 +00:00
// clang-format off
cxxopts::Options options(
"lokinet-rcutil",
"LokiNET is a free, open source, private, decentralized, \"market based sybil resistant\" and IP based onion routing network"
);
2019-08-12 21:47:30 +00:00
options.add_options()
("v,verbose", "Verbose", cxxopts::value<bool>())
("h,help", "help", cxxopts::value<bool>())
2019-08-12 22:09:44 +00:00
("j,json", "output in json", cxxopts::value<bool>())
2019-08-12 21:47:30 +00:00
("dump", "dump rc file", cxxopts::value<std::vector<std::string>>(), "FILE");
// clang-format on
2018-08-31 12:46:54 +00:00
2019-08-12 21:47:30 +00:00
try {
const auto result = options.parse(argc, argv);
2019-08-12 22:09:44 +00:00
const bool json = result["json"].as<bool>();
2019-08-12 21:47:30 +00:00
if(result.count("verbose") > 0)
{
SetLogLevel(llarp::eLogDebug);
2019-08-12 22:09:44 +00:00
llarp::LogContext::Instance().logStream = std::make_unique<llarp::OStreamLogStream>(std::cerr);
2019-08-12 21:47:30 +00:00
llarp::LogDebug("debug logging activated");
}
2019-08-12 21:47:30 +00:00
if(result.count("help") > 0)
2018-08-31 12:46:54 +00:00
{
2019-08-12 21:47:30 +00:00
std::cout << options.help() << std::endl;
return 0;
2018-08-31 12:46:54 +00:00
}
2019-08-12 21:47:30 +00:00
if(result.count("dump") > 0)
{
2019-08-12 22:09:44 +00:00
if (!dumpRc(result["dump"].as<std::vector<std::string>>(), json))
2018-08-31 12:46:54 +00:00
{
2019-08-12 21:47:30 +00:00
return 1;
2018-08-31 12:46:54 +00:00
}
}
2018-05-21 12:51:10 +00:00
}
2019-08-12 21:47:30 +00:00
catch(const cxxopts::OptionParseException &ex)
{
2019-08-12 21:47:30 +00:00
std::cerr << ex.what() << std::endl;
std::cout << options.help() << std::endl;
return 1;
}
2018-08-02 23:30:34 +00:00
2019-08-12 21:47:30 +00:00
return 0;
2018-05-21 12:51:10 +00:00
}