2021-03-15 12:46:19 +00:00
|
|
|
#include <oxenmq/oxenmq.h>
|
2020-07-29 15:43:48 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2022-09-19 15:17:01 +00:00
|
|
|
#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>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/net/net.hpp>
|
2020-07-29 15:43:48 +00:00
|
|
|
|
2023-01-05 21:05:18 +00:00
|
|
|
#include <CLI/App.hpp>
|
|
|
|
#include <CLI/Formatter.hpp>
|
|
|
|
#include <CLI/Config.hpp>
|
|
|
|
|
2020-08-10 15:06:51 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// add the unholy windows headers for iphlpapi
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <iphlpapi.h>
|
2020-08-11 22:27:50 +00:00
|
|
|
#include <strsafe.h>
|
2020-08-20 16:29:33 +00:00
|
|
|
#else
|
|
|
|
#include <sys/wait.h>
|
2020-08-10 15:06:51 +00:00
|
|
|
#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,
|
2021-03-15 12:46:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-19 15:17:01 +00:00
|
|
|
namespace
|
|
|
|
{
|
2023-01-05 21:05:18 +00:00
|
|
|
|
|
|
|
struct command_line_options
|
2022-09-19 15:17:01 +00:00
|
|
|
{
|
2023-01-05 21:05:18 +00:00
|
|
|
// 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;
|
|
|
|
};
|
2022-09-19 15:17:01 +00:00
|
|
|
|
|
|
|
// 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[])
|
|
|
|
{
|
2023-01-05 21:05:18 +00:00
|
|
|
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
|
|
|
|
{
|
2023-01-05 21:05:18 +00:00
|
|
|
cli.parse(argc, argv);
|
|
|
|
}
|
|
|
|
catch (const CLI::ParseError& e)
|
|
|
|
{
|
|
|
|
return cli.exit(e);
|
2020-07-28 17:54:42 +00:00
|
|
|
}
|
2023-01-05 21:05:18 +00:00
|
|
|
|
|
|
|
try
|
2020-07-28 17:54:42 +00:00
|
|
|
{
|
2023-01-05 21:05:18 +00:00
|
|
|
if (options.verbose)
|
|
|
|
options.logLevel = oxenmq::LogLevel::debug;
|
2020-07-28 17:54:42 +00:00
|
|
|
}
|
2023-01-05 21:05:18 +00:00
|
|
|
catch (const CLI::OptionNotFound& e)
|
2020-07-29 15:43:48 +00:00
|
|
|
{
|
2023-01-05 21:05:18 +00:00
|
|
|
cli.exit(e);
|
2020-07-28 17:54:42 +00:00
|
|
|
}
|
2023-01-05 21:05:18 +00:00
|
|
|
catch (const CLI::Error& e)
|
|
|
|
{
|
|
|
|
cli.exit(e);
|
|
|
|
};
|
2022-09-19 15:17:01 +00:00
|
|
|
|
2023-01-05 21:05:18 +00:00
|
|
|
int numCommands = options.vpnUp + options.vpnDown + options.printStatus + options.killDaemon;
|
2022-09-19 15:17:01 +00:00
|
|
|
|
2023-01-05 21:05:18 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-09-19 15:17:01 +00:00
|
|
|
|
2023-01-05 21:05:18 +00:00
|
|
|
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{
|
2021-03-15 12:46:19 +00:00
|
|
|
[](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;
|
|
|
|
},
|
2023-01-05 21:05:18 +00:00
|
|
|
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;
|
|
|
|
|
2023-01-05 21:05:18 +00:00
|
|
|
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) {
|
2023-01-05 21:05:18 +00:00
|
|
|
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;
|
|
|
|
|
2023-01-05 21:05:18 +00:00
|
|
|
if (options.killDaemon)
|
2020-11-20 17:37:30 +00:00
|
|
|
{
|
2023-01-05 21:05:18 +00:00
|
|
|
if (not OMQ_Request(omq, connectionID, "llarp.halt"))
|
|
|
|
return exit_error("Call to llarp.halt failed");
|
2020-11-20 17:37:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-05 21:05:18 +00:00
|
|
|
if (options.printStatus)
|
2020-07-29 15:43:48 +00:00
|
|
|
{
|
2023-01-05 21:05:18 +00:00
|
|
|
const auto maybe_status = OMQ_Request(omq, connectionID, "llarp.status");
|
2022-09-19 15:17:01 +00:00
|
|
|
if (not maybe_status)
|
|
|
|
return exit_error("call to llarp.status failed");
|
2020-07-29 15:43:48 +00:00
|
|
|
|
2020-10-05 15:50:59 +00:00
|
|
|
try
|
2020-07-29 15:43:48 +00:00
|
|
|
{
|
2023-01-05 21:05:18 +00:00
|
|
|
const auto& ep = maybe_status->at("result").at("services").at(options.endpoint);
|
2020-10-05 15:50:59 +00:00
|
|
|
const auto exitMap = ep.at("exitMap");
|
|
|
|
if (exitMap.empty())
|
2020-07-29 15:43:48 +00:00
|
|
|
{
|
2020-10-05 15:50:59 +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
|
|
|
}
|
|
|
|
}
|
2020-10-05 15:50:59 +00:00
|
|
|
catch (std::exception& ex)
|
2020-08-10 20:18:31 +00:00
|
|
|
{
|
2022-09-19 15:17:01 +00:00
|
|
|
return exit_error("failed to parse result: {}", ex.what());
|
2020-08-10 20:18:31 +00:00
|
|
|
}
|
2020-10-05 15:50:59 +00:00
|
|
|
return 0;
|
2020-07-29 15:43:48 +00:00
|
|
|
}
|
2023-01-05 21:05:18 +00:00
|
|
|
if (options.vpnUp)
|
2020-07-29 15:43:48 +00:00
|
|
|
{
|
2023-01-05 21:05:18 +00:00
|
|
|
nlohmann::json opts{{"exit", options.exitAddress}, {"token", options.token}};
|
|
|
|
if (options.range)
|
|
|
|
opts["range"] = *options.range;
|
2022-09-19 14:34:27 +00:00
|
|
|
|
2023-01-05 21:05:18 +00:00
|
|
|
auto maybe_result = OMQ_Request(omq, connectionID, "llarp.exit", std::move(opts));
|
2020-07-29 15:43:48 +00:00
|
|
|
|
2022-09-19 15:17:01 +00:00
|
|
|
if (not maybe_result)
|
|
|
|
return exit_error("could not add exit");
|
2020-07-29 15:43:48 +00:00
|
|
|
|
2022-11-02 19:42:49 +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
|
|
|
{
|
2022-11-02 19:42:49 +00:00
|
|
|
return exit_error("{}", err_it.value());
|
2020-07-29 15:43:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-05 21:05:18 +00:00
|
|
|
if (options.vpnDown)
|
2020-07-29 15:43:48 +00:00
|
|
|
{
|
2022-09-19 14:34:27 +00:00
|
|
|
nlohmann::json opts{{"unmap", true}};
|
2023-01-05 21:05:18 +00:00
|
|
|
if (options.range)
|
|
|
|
opts["range"] = *options.range;
|
|
|
|
if (not OMQ_Request(omq, connectionID, "llarp.exit", std::move(opts)))
|
2022-09-19 15:17:01 +00:00
|
|
|
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;
|
|
|
|
}
|