lokinet/daemon/lokinet-vpn.cpp

253 lines
6.4 KiB
C++
Raw Normal View History

#include <oxenmq/oxenmq.h>
2020-07-29 15:43:48 +00:00
#include <nlohmann/json.hpp>
2020-07-28 17:54:42 +00:00
#include <cxxopts.hpp>
#include <future>
2020-07-29 15:43:48 +00:00
#include <vector>
2020-08-10 15:07:33 +00:00
#include <array>
#include <llarp/net/net.hpp>
2020-07-29 15:43:48 +00:00
#ifdef _WIN32
// add the unholy windows headers for iphlpapi
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <strsafe.h>
2020-08-20 16:29:33 +00:00
#else
#include <sys/wait.h>
#endif
/// do a oxenmq request on an lmq instance blocking style
2020-07-29 15:43:48 +00:00
/// returns a json object parsed from the result
std::optional<nlohmann::json>
LMQ_Request(
oxenmq::OxenMQ& lmq,
const oxenmq::ConnectionID& id,
2020-07-29 15:43:48 +00:00
std::string_view method,
std::optional<nlohmann::json> args = std::nullopt)
{
std::promise<std::optional<std::string>> result_promise;
auto handleRequest = [&result_promise](bool success, std::vector<std::string> result) {
if ((not success) or result.empty())
{
result_promise.set_value(std::nullopt);
return;
}
result_promise.set_value(result[0]);
};
if (args.has_value())
{
lmq.request(id, method, handleRequest, args->dump());
}
else
{
lmq.request(id, method, handleRequest);
}
auto ftr = result_promise.get_future();
const auto str = ftr.get();
if (str.has_value())
return nlohmann::json::parse(*str);
return std::nullopt;
}
2020-07-28 17:54:42 +00:00
int
main(int argc, char* argv[])
{
cxxopts::Options opts("lokinet-vpn", "LokiNET vpn control utility");
// clang-format off
opts.add_options()
("v,verbose", "Verbose", cxxopts::value<bool>())
("h,help", "help", cxxopts::value<bool>())
("kill", "kill the daemon", cxxopts::value<bool>())
("up", "put vpn up", cxxopts::value<bool>())
("down", "put vpn down", cxxopts::value<bool>())
("exit", "specify exit node address", cxxopts::value<std::string>())
("rpc", "rpc url for lokinet", cxxopts::value<std::string>())
("endpoint", "endpoint to use", cxxopts::value<std::string>())
("token", "exit auth token to use", cxxopts::value<std::string>())
("auth", "exit auth token to use", cxxopts::value<std::string>())
("status", "print status and exit", cxxopts::value<bool>())
2021-02-16 15:59:18 +00:00
("range", "ip range to map", cxxopts::value<std::string>())
;
// clang-format on
oxenmq::address rpcURL("tcp://127.0.0.1:1190");
2020-07-28 17:54:42 +00:00
std::string exitAddress;
2020-07-29 15:43:48 +00:00
std::string endpoint = "default";
std::optional<std::string> token;
2021-02-16 15:59:18 +00:00
std::string range = "::/0";
oxenmq::LogLevel logLevel = oxenmq::LogLevel::warn;
2020-07-28 17:54:42 +00:00
bool goUp = false;
bool goDown = false;
bool printStatus = false;
bool killDaemon = false;
2020-07-28 17:54:42 +00:00
try
{
const auto result = opts.parse(argc, argv);
if (result.count("help") > 0)
{
std::cout << opts.help() << std::endl;
return 0;
}
if (result.count("verbose") > 0)
{
logLevel = oxenmq::LogLevel::debug;
2020-07-28 17:54:42 +00:00
}
if (result.count("rpc") > 0)
{
rpcURL = oxenmq::address(result["rpc"].as<std::string>());
2020-07-28 17:54:42 +00:00
}
if (result.count("exit") > 0)
{
exitAddress = result["exit"].as<std::string>();
}
goUp = result.count("up") > 0;
goDown = result.count("down") > 0;
printStatus = result.count("status") > 0;
killDaemon = result.count("kill") > 0;
2020-07-29 15:43:48 +00:00
if (result.count("endpoint") > 0)
{
endpoint = result["endpoint"].as<std::string>();
}
if (result.count("token") > 0)
{
token = result["token"].as<std::string>();
}
if (result.count("auth") > 0)
{
token = result["auth"].as<std::string>();
}
2021-02-16 15:59:18 +00:00
if (result.count("range") > 0)
{
range = result["range"].as<std::string>();
}
2020-07-28 17:54:42 +00:00
}
catch (const cxxopts::option_not_exists_exception& ex)
{
std::cerr << ex.what();
std::cout << opts.help() << std::endl;
return 1;
}
2020-07-29 15:43:48 +00:00
catch (std::exception& ex)
{
std::cout << ex.what() << std::endl;
return 1;
}
if ((not goUp) and (not goDown) and (not printStatus) and (not killDaemon))
2020-07-28 17:54:42 +00:00
{
std::cout << opts.help() << std::endl;
return 1;
}
if (goUp and exitAddress.empty())
{
std::cout << "no exit address provided" << std::endl;
return 1;
}
oxenmq::OxenMQ lmq{
[](oxenmq::LogLevel lvl, const char* file, int line, std::string msg) {
2021-03-05 17:31:52 +00:00
std::cout << lvl << " [" << file << ":" << line << "] " << msg << std::endl;
},
logLevel};
2020-07-28 17:54:42 +00:00
lmq.start();
std::promise<bool> connectPromise;
const auto connID = lmq.connect_remote(
rpcURL,
[&connectPromise](auto) { connectPromise.set_value(true); },
[&connectPromise](auto, std::string_view msg) {
std::cout << "failed to connect to lokinet RPC: " << msg << std::endl;
connectPromise.set_value(false);
});
auto ftr = connectPromise.get_future();
if (not ftr.get())
{
return 1;
}
if (killDaemon)
{
const auto maybe = LMQ_Request(lmq, connID, "llarp.halt");
if (not maybe.has_value())
{
std::cout << "call to llarp.admin.die failed" << std::endl;
return 1;
}
return 0;
}
if (printStatus)
2020-07-29 15:43:48 +00:00
{
const auto maybe_status = LMQ_Request(lmq, connID, "llarp.status");
if (not maybe_status.has_value())
{
std::cout << "call to llarp.status failed" << std::endl;
return 1;
}
2020-07-29 15:43:48 +00:00
try
2020-07-29 15:43:48 +00:00
{
const auto& ep = maybe_status->at("result").at("services").at(endpoint);
const auto exitMap = ep.at("exitMap");
if (exitMap.empty())
2020-07-29 15:43:48 +00:00
{
std::cout << "no exits" << std::endl;
}
else
{
for (const auto& [range, exit] : exitMap.items())
{
std::cout << range << " via " << exit.get<std::string>() << std::endl;
}
2020-07-29 15:43:48 +00:00
}
}
catch (std::exception& ex)
2020-08-10 20:18:31 +00:00
{
std::cout << "failed to parse result: " << ex.what() << std::endl;
return 1;
2020-08-10 20:18:31 +00:00
}
return 0;
2020-07-29 15:43:48 +00:00
}
if (goUp)
{
std::optional<nlohmann::json> maybe_result;
if (token.has_value())
{
maybe_result = LMQ_Request(
lmq,
connID,
"llarp.exit",
2021-02-16 15:59:18 +00:00
nlohmann::json{{"exit", exitAddress}, {"range", range}, {"token", *token}});
2020-07-29 15:43:48 +00:00
}
else
{
maybe_result = LMQ_Request(
2021-02-16 15:59:18 +00:00
lmq, connID, "llarp.exit", nlohmann::json{{"exit", exitAddress}, {"range", range}});
2020-07-29 15:43:48 +00:00
}
if (not maybe_result.has_value())
{
std::cout << "could not add exit" << std::endl;
return 1;
}
if (maybe_result->contains("error") and maybe_result->at("error").is_string())
{
std::cout << maybe_result->at("error").get<std::string>() << std::endl;
return 1;
}
}
if (goDown)
{
2021-02-16 15:59:18 +00:00
LMQ_Request(lmq, connID, "llarp.exit", nlohmann::json{{"range", range}, {"unmap", true}});
2020-07-29 15:43:48 +00:00
}
2020-07-28 17:54:42 +00:00
return 0;
}