lokinet/daemon/lokinetctl.cpp

185 lines
4.7 KiB
C++
Raw Normal View History

#include <config/config.hpp>
2018-12-12 01:55:30 +00:00
#include <router_contact.hpp>
2019-09-01 12:10:49 +00:00
#include <util/logging/logger.hpp>
#include <util/logging/ostream_logger.hpp>
2018-07-24 03:40:34 +00:00
2019-08-13 00:26:25 +00:00
#include <absl/synchronization/mutex.h>
2019-08-12 21:47:30 +00:00
#include <cxxopts.hpp>
#include <string>
#include <vector>
#ifdef WITH_CURL
#include <curl/curl.h>
#endif
namespace
2018-05-21 12:51:10 +00:00
{
bool
dumpRc(const std::vector< std::string >& files)
2019-08-12 22:10:07 +00:00
{
nlohmann::json result;
for(const auto& file : files)
2019-08-12 22:10:07 +00:00
{
llarp::RouterContact rc;
const bool ret = rc.Read(file.c_str());
if(ret)
2019-08-12 22:10:07 +00:00
{
result[file] = rc.ToJson();
}
else
{
std::cerr << "file = " << file << " was not a valid rc file\n";
2019-08-12 22:10:07 +00:00
}
}
std::cout << result << "\n";
return true;
}
#ifdef WITH_CURL
size_t
curlCallback(void* contents, size_t size, size_t nmemb, void* userp) noexcept
{
auto* str = static_cast< std::string* >(userp);
size_t realsize = size * nmemb;
char* asChar = static_cast< char* >(contents);
std::copy(asChar, asChar + realsize, std::back_inserter(*str));
return realsize;
}
bool
executeJsonRpc(const std::string& command, const std::string& configFile)
{
// Do init (on windows this will do socket initialisation)
curl_global_init(CURL_GLOBAL_ALL);
llarp::Config config;
if(!config.Load(configFile.c_str()))
2019-08-12 22:10:07 +00:00
{
llarp::LogError("Failed to load from config file: ", configFile);
return false;
}
if(!config.api.enableRPCServer())
{
llarp::LogError("Config does not have RPC enabled");
return false;
}
std::string address = config.api.rpcBindAddr() + "/jsonrpc";
const nlohmann::json request{{"method", command},
{"params", nlohmann::json::object()},
{"id", "foo"}};
const std::string requestStr = request.dump();
std::unique_ptr< curl_slist, void (*)(curl_slist*) > chunk(
curl_slist_append(nullptr, "content-type: application/json"),
&curl_slist_free_all);
std::unique_ptr< CURL, void (*)(CURL*) > curl(curl_easy_init(),
&curl_easy_cleanup);
curl_easy_setopt(curl.get(), CURLOPT_URL, address.c_str());
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, requestStr.c_str());
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDSIZE, requestStr.size());
curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, chunk.get());
std::string result;
curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, curlCallback);
curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &result);
auto res = curl_easy_perform(curl.get());
if(res != CURLE_OK)
{
llarp::LogError("Failed to curl endpoint, ", curl_easy_strerror(res));
return false;
2018-05-21 12:51:10 +00:00
}
2019-08-12 22:10:07 +00:00
std::cout << result << "\n";
2019-08-12 22:09:44 +00:00
return true;
}
#endif
} // namespace
2019-08-12 21:47:30 +00:00
int
2019-08-12 22:10:07 +00:00
main(int argc, char* argv[])
2019-08-12 21:47:30 +00:00
{
#ifdef LOKINET_DEBUG
absl::SetMutexDeadlockDetectionMode(absl::OnDeadlockCycle::kAbort);
#endif
2019-08-12 21:47:30 +00:00
// clang-format off
cxxopts::Options options(
2019-10-07 10:15:45 +00:00
"lokinetctl",
2019-08-12 21:47:30 +00:00
"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>())
("c,config", "config file", cxxopts::value<std::string>()->default_value(llarp::GetDefaultConfigPath().string()))
#ifdef WITH_CURL
("j,jsonrpc", "hit json rpc endpoint", cxxopts::value<std::string>())
#endif
2019-08-12 22:10:07 +00:00
("dump", "dump rc file", cxxopts::value<std::vector<std::string> >(), "FILE");
2019-08-12 21:47:30 +00:00
// clang-format on
2018-08-31 12:46:54 +00:00
2019-08-12 22:10:07 +00:00
try
{
2019-08-12 21:47:30 +00:00
const auto result = options.parse(argc, argv);
2019-08-12 21:47:30 +00:00
if(result.count("verbose") > 0)
{
SetLogLevel(llarp::eLogDebug);
2019-08-12 22:10:07 +00:00
llarp::LogContext::Instance().logStream =
std::make_unique< llarp::OStreamLogStream >(true, std::cerr);
2019-08-12 21:47:30 +00:00
llarp::LogDebug("debug logging activated");
}
else
{
SetLogLevel(llarp::eLogError);
llarp::LogContext::Instance().logStream =
std::make_unique< llarp::OStreamLogStream >(true, std::cerr);
}
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)
{
if(!dumpRc(result["dump"].as< std::vector< std::string > >()))
2018-08-31 12:46:54 +00:00
{
2019-08-12 22:10:07 +00:00
return 1;
2018-08-31 12:46:54 +00:00
}
}
#ifdef WITH_CURL
if(result.count("jsonrpc") > 0)
{
if(!executeJsonRpc(result["jsonrpc"].as< std::string >(),
result["config"].as< std::string >()))
{
return 1;
}
}
#endif
2018-05-21 12:51:10 +00:00
}
2019-08-12 22:10:07 +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
}