lokinet/daemon/lokinet-vpn.cpp

252 lines
6.7 KiB
C++
Raw Normal View History

#include <oxenmq/oxenmq.h>
2020-07-29 15:43:48 +00:00
#include <nlohmann/json.hpp>
#include <fmt/core.h>
2020-07-28 17:54:42 +00:00
#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
#include <CLI/App.hpp>
#include <CLI/Formatter.hpp>
#include <CLI/Config.hpp>
#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
2022-09-19 15:12:41 +00:00
/// do a oxenmq request on an omq instance blocking style
2020-07-29 15:43:48 +00:00
/// returns a json object parsed from the result
std::optional<nlohmann::json>
2022-09-19 15:12:41 +00:00
OMQ_Request(
oxenmq::OxenMQ& omq,
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())
{
2022-09-19 15:12:41 +00:00
omq.request(id, method, handleRequest, args->dump());
2020-07-29 15:43:48 +00:00
}
else
{
2022-09-19 15:12:41 +00:00
omq.request(id, method, handleRequest);
2020-07-29 15:43:48 +00:00
}
auto ftr = result_promise.get_future();
const auto str = ftr.get();
if (str.has_value())
return nlohmann::json::parse(*str);
return std::nullopt;
}
namespace
{
struct command_line_options
{
// bool options
bool verbose = false;
bool help = false;
bool vpnUp = false;
bool vpnDown = false;
bool printStatus = false;
bool killDaemon = false;
// string options
std::string exitAddress;
std::string rpc;
std::string endpoint = "default";
std::string token;
std::optional<std::string> range;
// oxenmq
oxenmq::address rpcURL{"tcp://127.0.0.1:1190"};
oxenmq::LogLevel logLevel = oxenmq::LogLevel::warn;
};
// Takes a code, prints a message, and returns the code. Intended use is:
// return exit_error(1, "blah: {}", 42);
// from within main().
template <typename... T>
[[nodiscard]] int
exit_error(int code, const std::string& format, T&&... args)
{
fmt::print(format, std::forward<T>(args)...);
fmt::print("\n");
return code;
}
// Same as above, but with code omitted (uses exit code 1)
template <typename... T>
[[nodiscard]] int
exit_error(const std::string& format, T&&... args)
{
return exit_error(1, format, std::forward<T>(args)...);
}
} // namespace
2020-07-28 17:54:42 +00:00
int
main(int argc, char* argv[])
{
CLI::App cli{"lokiNET vpn control utility", "lokinet-vpn"};
command_line_options options{};
// flags: boolean values in command_line_options struct
cli.add_flag("v,--verbose", options.verbose, "Verbose");
cli.add_flag("--up", options.vpnUp, "Put VPN up");
cli.add_flag("--down", options.vpnDown, "Put VPN down");
cli.add_flag("--status", options.printStatus, "Print VPN status and exit");
cli.add_flag("k,--kill", options.killDaemon, "Kill lokinet daemon");
// options: string values in command_line_options struct
cli.add_option("--exit", options.exitAddress, "Specify exit node address")->capture_default_str();
cli.add_option("--endpoint", options.endpoint, "Endpoint to use")->capture_default_str();
cli.add_option("--token", options.token, "Exit auth token to use")->capture_default_str();
// options: oxenmq values in command_line_options struct
cli.add_option("--rpc", options.rpc, "Specify RPC URL for lokinet")->capture_default_str();
cli.add_option(
"--log-level", options.logLevel, "Log verbosity level, see log levels for accepted values")
->type_name("LEVEL")
->capture_default_str();
2020-07-28 17:54:42 +00:00
try
{
cli.parse(argc, argv);
}
catch (const CLI::ParseError& e)
{
return cli.exit(e);
2020-07-28 17:54:42 +00:00
}
try
2020-07-28 17:54:42 +00:00
{
if (options.verbose)
options.logLevel = oxenmq::LogLevel::debug;
2020-07-28 17:54:42 +00:00
}
catch (const CLI::OptionNotFound& e)
2020-07-29 15:43:48 +00:00
{
cli.exit(e);
2020-07-28 17:54:42 +00:00
}
catch (const CLI::Error& e)
{
cli.exit(e);
};
int numCommands = options.vpnUp + options.vpnDown + options.printStatus + options.killDaemon;
switch (numCommands)
{
case 0:
return exit_error(3, "One of --up/--down/--status/--kill must be specified");
case 1:
return exit_error(3, "Only one of --up/--down/--status/--kill may be specified");
default:
break;
}
if (options.vpnUp and options.exitAddress.empty())
return exit_error("No exit address provided, must specify --exit <address>");
2020-07-28 17:54:42 +00:00
2022-09-19 15:12:41 +00:00
oxenmq::OxenMQ omq{
[](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;
},
options.logLevel};
2020-07-28 17:54:42 +00:00
2022-09-19 15:12:41 +00:00
omq.start();
2020-07-28 17:54:42 +00:00
std::promise<bool> connectPromise;
const auto connectionID = omq.connect_remote(
options.rpc,
2020-07-28 17:54:42 +00:00
[&connectPromise](auto) { connectPromise.set_value(true); },
[&connectPromise](auto, std::string_view msg) {
std::cout << "Failed to connect to lokinet RPC: " << msg << std::endl;
2020-07-28 17:54:42 +00:00
connectPromise.set_value(false);
});
auto ftr = connectPromise.get_future();
if (not ftr.get())
return 1;
if (options.killDaemon)
{
if (not OMQ_Request(omq, connectionID, "llarp.halt"))
return exit_error("Call to llarp.halt failed");
return 0;
}
if (options.printStatus)
2020-07-29 15:43:48 +00:00
{
const auto maybe_status = OMQ_Request(omq, connectionID, "llarp.status");
if (not maybe_status)
return exit_error("call to llarp.status failed");
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(options.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
{
return exit_error("failed to parse result: {}", ex.what());
2020-08-10 20:18:31 +00:00
}
return 0;
2020-07-29 15:43:48 +00:00
}
if (options.vpnUp)
2020-07-29 15:43:48 +00:00
{
nlohmann::json opts{{"exit", options.exitAddress}, {"token", options.token}};
if (options.range)
opts["range"] = *options.range;
auto maybe_result = OMQ_Request(omq, connectionID, "llarp.exit", std::move(opts));
2020-07-29 15:43:48 +00:00
if (not maybe_result)
return exit_error("could not add exit");
2020-07-29 15:43:48 +00:00
if (auto err_it = maybe_result->find("error");
err_it != maybe_result->end() and not err_it.value().is_null())
2020-07-29 15:43:48 +00:00
{
return exit_error("{}", err_it.value());
2020-07-29 15:43:48 +00:00
}
}
if (options.vpnDown)
2020-07-29 15:43:48 +00:00
{
nlohmann::json opts{{"unmap", true}};
if (options.range)
opts["range"] = *options.range;
if (not OMQ_Request(omq, connectionID, "llarp.exit", std::move(opts)))
return exit_error("failed to unmap exit");
2020-07-29 15:43:48 +00:00
}
2020-07-28 17:54:42 +00:00
return 0;
}