mirror of https://github.com/oxen-io/lokinet
Use libcurl (optional dependency) to hit jsonrpc
This commit is contained in:
parent
0721ce4d34
commit
3371da98cf
|
@ -133,6 +133,7 @@ addons:
|
|||
- gcc-mingw-w64-base
|
||||
- git
|
||||
- libcap-dev
|
||||
- libcurl-dev
|
||||
- libuv1-dev
|
||||
- mingw-w64 mingw-w64-common
|
||||
- ninja-build
|
||||
|
@ -140,6 +141,7 @@ addons:
|
|||
packages:
|
||||
- ccache
|
||||
- cmake
|
||||
- curl
|
||||
- libuv
|
||||
- llvm
|
||||
- make
|
||||
|
@ -148,7 +150,7 @@ addons:
|
|||
|
||||
before_install:
|
||||
- if [ "$TRAVIS_OS_NAME" == "windows" ]; then
|
||||
choco install make ninja;
|
||||
choco install curl make ninja;
|
||||
choco upgrade cmake.install;
|
||||
export CC="/c/Program Files/LLVM/bin/clang-cl";
|
||||
export CXX="/c/Program Files/LLVM/bin/clang-cl";
|
||||
|
|
|
@ -42,4 +42,13 @@ else()
|
|||
endif()
|
||||
target_link_libraries(${EXE} PUBLIC ${EXE_LIBS} ${LIBS})
|
||||
target_link_libraries(lokinet-rcutil PUBLIC ${EXE_LIBS} ${LIBS})
|
||||
|
||||
find_package(CURL)
|
||||
|
||||
if(CURL_FOUND)
|
||||
target_include_directories(lokinet-rcutil PRIVATE ${CURL_INCLUDE_DIRS})
|
||||
target_link_libraries(lokinet-rcutil PRIVATE ${CURL_LIBRARIES})
|
||||
target_compile_definitions(lokinet-rcutil PRIVATE -DWITH_CURL=1)
|
||||
endif(CURL_FOUND)
|
||||
|
||||
endif(SHADOW)
|
||||
|
|
|
@ -283,14 +283,7 @@ main(int argc, char *argv[])
|
|||
}
|
||||
else
|
||||
{
|
||||
// no explicit config file provided
|
||||
#ifdef _WIN32
|
||||
fs::path homedir = fs::path(getenv("APPDATA"));
|
||||
#else
|
||||
fs::path homedir = fs::path(getenv("HOME"));
|
||||
#endif
|
||||
fs::path basepath = homedir / fs::path(".lokinet");
|
||||
fs::path fpath = basepath / "lokinet.ini";
|
||||
auto basepath = llarp::GetDefaultConfigDir();
|
||||
// I don't think this is necessary with this condition
|
||||
// conffname = resolvePath(conffname);
|
||||
|
||||
|
@ -308,6 +301,8 @@ main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
auto fpath = llarp::GetDefaultConfigPath();
|
||||
|
||||
// if using default INI file, we're create it even if you don't ask us too
|
||||
if(!llarp_ensure_config(fpath.string().c_str(), basepath.string().c_str(),
|
||||
overWrite, asRouter))
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <config/config.hpp>
|
||||
#include <router_contact.hpp>
|
||||
#include <util/logging/logger.hpp>
|
||||
#include <util/logging/ostream_logger.hpp>
|
||||
|
@ -7,38 +8,104 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
bool
|
||||
dumpRc(const std::vector< std::string >& files, bool json)
|
||||
{
|
||||
nlohmann::json result;
|
||||
for(const auto& file : files)
|
||||
{
|
||||
llarp::RouterContact rc;
|
||||
const bool ret = rc.Read(file.c_str());
|
||||
#ifdef WITH_CURL
|
||||
#include <curl/curl.h>
|
||||
#endif
|
||||
|
||||
if(ret)
|
||||
namespace
|
||||
{
|
||||
bool
|
||||
dumpRc(const std::vector< std::string >& files)
|
||||
{
|
||||
nlohmann::json result;
|
||||
for(const auto& file : files)
|
||||
{
|
||||
if(json)
|
||||
llarp::RouterContact rc;
|
||||
const bool ret = rc.Read(file.c_str());
|
||||
|
||||
if(ret)
|
||||
{
|
||||
result[file] = rc.ToJson();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "file = " << file << "\n";
|
||||
std::cout << rc << "\n\n";
|
||||
std::cerr << "file = " << file << " was not a valid rc file\n";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "file = " << file << " was not a valid rc file\n";
|
||||
}
|
||||
std::cout << result << "\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
if(json)
|
||||
#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()))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
std::cout << result << "\n";
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
} // namespace
|
||||
|
||||
int
|
||||
main(int argc, char* argv[])
|
||||
|
@ -55,9 +122,11 @@ main(int argc, char* argv[])
|
|||
|
||||
options.add_options()
|
||||
("v,verbose", "Verbose", cxxopts::value<bool>())
|
||||
("c,colour", "colour output", cxxopts::value<bool>()->default_value("true"))
|
||||
("h,help", "help", cxxopts::value<bool>())
|
||||
("j,json", "output in json", 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
|
||||
("dump", "dump rc file", cxxopts::value<std::vector<std::string> >(), "FILE");
|
||||
// clang-format on
|
||||
|
||||
|
@ -65,16 +134,19 @@ main(int argc, char* argv[])
|
|||
{
|
||||
const auto result = options.parse(argc, argv);
|
||||
|
||||
const bool json = result["json"].as< bool >();
|
||||
|
||||
if(result.count("verbose") > 0)
|
||||
{
|
||||
SetLogLevel(llarp::eLogDebug);
|
||||
llarp::LogContext::Instance().logStream =
|
||||
std::make_unique< llarp::OStreamLogStream >(
|
||||
result["colour"].as< bool >(), std::cerr);
|
||||
std::make_unique< llarp::OStreamLogStream >(true, std::cerr);
|
||||
llarp::LogDebug("debug logging activated");
|
||||
}
|
||||
else
|
||||
{
|
||||
SetLogLevel(llarp::eLogError);
|
||||
llarp::LogContext::Instance().logStream =
|
||||
std::make_unique< llarp::OStreamLogStream >(true, std::cerr);
|
||||
}
|
||||
|
||||
if(result.count("help") > 0)
|
||||
{
|
||||
|
@ -84,11 +156,22 @@ main(int argc, char* argv[])
|
|||
|
||||
if(result.count("dump") > 0)
|
||||
{
|
||||
if(!dumpRc(result["dump"].as< std::vector< std::string > >(), json))
|
||||
if(!dumpRc(result["dump"].as< std::vector< std::string > >()))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WITH_CURL
|
||||
if(result.count("jsonrpc") > 0)
|
||||
{
|
||||
if(!executeJsonRpc(result["jsonrpc"].as< std::string >(),
|
||||
result["config"].as< std::string >()))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
catch(const cxxopts::OptionParseException& ex)
|
||||
{
|
||||
|
|
|
@ -475,6 +475,23 @@ namespace llarp
|
|||
return true;
|
||||
}
|
||||
|
||||
fs::path
|
||||
GetDefaultConfigDir()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
fs::path homedir = fs::path(getenv("APPDATA"));
|
||||
#else
|
||||
fs::path homedir = fs::path(getenv("HOME"));
|
||||
#endif
|
||||
return homedir / fs::path(".lokinet");
|
||||
}
|
||||
|
||||
fs::path
|
||||
GetDefaultConfigPath()
|
||||
{
|
||||
return GetDefaultConfigDir() / "lokinet.ini";
|
||||
}
|
||||
|
||||
} // namespace llarp
|
||||
|
||||
/// fname should be a relative path (from CWD) or absolute path to the config
|
||||
|
|
|
@ -132,7 +132,7 @@ namespace llarp
|
|||
std::string transportKeyfile() const { return fromEnv(m_transportKeyfile, "TRANSPORT_KEYFILE"); }
|
||||
std::string identKeyfile() const { return fromEnv(m_identKeyfile, "IDENT_KEYFILE"); }
|
||||
std::string netId() const { return fromEnv(m_netId, "NETID"); }
|
||||
std::string nickname() const { return fromEnv(m_nickname, "NICKNAME"); }
|
||||
std::string nickname() const { return fromEnv(m_nickname, "NICKNAME"); }
|
||||
bool publicOverride() const { return fromEnv(m_publicOverride, "PUBLIC_OVERRIDE"); }
|
||||
const struct sockaddr_in& ip4addr() const { return m_ip4addr; }
|
||||
const AddressInfo& addrInfo() const { return m_addrInfo; }
|
||||
|
@ -326,6 +326,12 @@ namespace llarp
|
|||
LoadFromStr(string_view str);
|
||||
};
|
||||
|
||||
fs::path
|
||||
GetDefaultConfigDir();
|
||||
|
||||
fs::path
|
||||
GetDefaultConfigPath();
|
||||
|
||||
} // namespace llarp
|
||||
|
||||
void
|
||||
|
|
Loading…
Reference in New Issue