2019-07-02 21:28:28 +00:00
|
|
|
#include <config/config.hpp> // for ensure_config
|
2019-10-02 18:08:45 +00:00
|
|
|
#include <constants/version.hpp>
|
2019-12-07 19:58:19 +00:00
|
|
|
#include <llarp.hpp>
|
2019-11-15 19:40:43 +00:00
|
|
|
#include <util/lokinet_init.h>
|
2019-01-11 01:59:44 +00:00
|
|
|
#include <util/fs.hpp>
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-09-16 19:40:31 +00:00
|
|
|
#include <util/logging/ostream_logger.hpp>
|
2020-03-23 20:53:42 +00:00
|
|
|
#include <util/str.hpp>
|
2020-06-29 20:09:59 +00:00
|
|
|
#include <util/thread/logic.hpp>
|
2019-01-11 01:59:44 +00:00
|
|
|
|
2019-07-02 21:28:28 +00:00
|
|
|
#include <csignal>
|
2019-01-30 17:24:02 +00:00
|
|
|
|
2019-04-19 18:24:33 +00:00
|
|
|
#include <cxxopts.hpp>
|
2018-07-20 04:50:28 +00:00
|
|
|
#include <string>
|
2018-09-19 13:02:55 +00:00
|
|
|
#include <iostream>
|
2019-10-04 18:10:58 +00:00
|
|
|
#include <future>
|
2018-05-20 16:15:16 +00:00
|
|
|
|
2020-05-21 14:18:23 +00:00
|
|
|
#ifdef USE_JEMALLOC
|
|
|
|
#include <new>
|
|
|
|
#include <jemalloc/jemalloc.h>
|
|
|
|
|
|
|
|
void*
|
|
|
|
operator new(std::size_t sz)
|
|
|
|
{
|
|
|
|
void* ptr = malloc(sz);
|
|
|
|
if (ptr)
|
|
|
|
return ptr;
|
|
|
|
else
|
|
|
|
throw std::bad_alloc{};
|
|
|
|
}
|
|
|
|
void
|
|
|
|
operator delete(void* ptr) noexcept
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
operator delete(void* ptr, size_t) noexcept
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define wmin(x, y) (((x) < (y)) ? (x) : (y))
|
|
|
|
#define MIN wmin
|
2019-08-02 03:25:48 +00:00
|
|
|
extern "C" LONG FAR PASCAL
|
2020-04-07 18:38:56 +00:00
|
|
|
win32_signal_handler(EXCEPTION_POINTERS*);
|
2018-07-30 04:38:14 +00:00
|
|
|
#endif
|
|
|
|
|
2020-06-29 19:55:59 +00:00
|
|
|
std::shared_ptr<llarp::Context> ctx;
|
2020-04-07 18:38:56 +00:00
|
|
|
std::promise<int> exit_code;
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
handle_signal(int sig)
|
2018-05-18 17:50:21 +00:00
|
|
|
{
|
2020-06-29 20:09:59 +00:00
|
|
|
if (ctx)
|
|
|
|
LogicCall(ctx->logic, std::bind(&llarp::Context::HandleSignal, ctx.get(), sig));
|
|
|
|
else
|
|
|
|
std::cerr << "Received signal " << sig << ", but have no context yet. Ignoring!" << std::endl;
|
2018-05-18 17:50:21 +00:00
|
|
|
}
|
|
|
|
|
2018-09-29 08:16:54 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
int
|
|
|
|
startWinsock()
|
|
|
|
{
|
|
|
|
WSADATA wsockd;
|
|
|
|
int err;
|
|
|
|
err = ::WSAStartup(MAKEWORD(2, 2), &wsockd);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (err)
|
2018-09-29 08:16:54 +00:00
|
|
|
{
|
|
|
|
perror("Failed to start Windows Sockets");
|
|
|
|
return err;
|
|
|
|
}
|
2020-01-30 07:36:03 +00:00
|
|
|
::CreateMutex(nullptr, FALSE, "lokinet_win32_daemon");
|
2018-09-29 08:16:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-12-14 12:50:45 +00:00
|
|
|
|
|
|
|
extern "C" BOOL FAR PASCAL
|
|
|
|
handle_signal_win32(DWORD fdwCtrlType)
|
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(fdwCtrlType);
|
|
|
|
handle_signal(SIGINT);
|
2018-12-23 13:29:11 +00:00
|
|
|
return TRUE; // probably unreachable
|
2018-12-14 12:50:45 +00:00
|
|
|
}
|
2018-09-29 08:16:54 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-26 12:55:08 +00:00
|
|
|
/// this sets up, configures and runs the main context
|
|
|
|
static void
|
2020-07-01 14:04:29 +00:00
|
|
|
run_main_context(const fs::path confFile, const llarp::RuntimeOptions opts)
|
2019-07-26 12:55:08 +00:00
|
|
|
{
|
2020-06-29 19:55:59 +00:00
|
|
|
try
|
2019-07-26 12:55:08 +00:00
|
|
|
{
|
2020-06-29 19:55:59 +00:00
|
|
|
// this is important, can downgrade from Info though
|
|
|
|
llarp::LogDebug("Running from: ", fs::current_path().string());
|
|
|
|
llarp::LogInfo("Using config file: ", confFile);
|
|
|
|
|
|
|
|
llarp::Config conf;
|
|
|
|
conf.Load(confFile, opts.isRouter, confFile.parent_path());
|
|
|
|
|
|
|
|
ctx = std::shared_ptr<llarp::Context>();
|
2020-07-01 14:38:56 +00:00
|
|
|
ctx->Configure(opts, {});
|
2020-06-29 19:55:59 +00:00
|
|
|
|
2019-07-26 12:55:08 +00:00
|
|
|
signal(SIGINT, handle_signal);
|
|
|
|
signal(SIGTERM, handle_signal);
|
|
|
|
#ifndef _WIN32
|
|
|
|
signal(SIGHUP, handle_signal);
|
|
|
|
#endif
|
2020-06-29 19:55:59 +00:00
|
|
|
|
2020-07-01 14:38:56 +00:00
|
|
|
ctx->Setup(opts);
|
2020-06-29 19:55:59 +00:00
|
|
|
|
2019-07-26 12:55:08 +00:00
|
|
|
llarp::util::SetThreadName("llarp-mainloop");
|
2020-06-29 19:55:59 +00:00
|
|
|
|
|
|
|
auto result = ctx->Run(opts);
|
|
|
|
exit_code.set_value(result);
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
llarp::LogError("Fatal: caught exception while running: ", e.what());
|
2020-07-01 14:38:33 +00:00
|
|
|
exit_code.set_exception(std::current_exception());
|
2020-06-29 19:55:59 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
llarp::LogError("Fatal: caught non-standard exception while running");
|
2020-07-01 14:38:33 +00:00
|
|
|
exit_code.set_exception(std::current_exception());
|
2019-07-26 12:55:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
int
|
2020-04-07 18:38:56 +00:00
|
|
|
main(int argc, char* argv[])
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2019-11-15 19:40:43 +00:00
|
|
|
auto result = Lokinet_INIT();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (result)
|
2019-11-15 19:40:43 +00:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
2020-06-29 19:55:59 +00:00
|
|
|
llarp::RuntimeOptions opts;
|
2018-07-27 00:21:57 +00:00
|
|
|
|
2018-09-29 08:16:54 +00:00
|
|
|
#ifdef _WIN32
|
2020-04-07 18:38:56 +00:00
|
|
|
if (startWinsock())
|
2018-09-29 08:16:54 +00:00
|
|
|
return -1;
|
2018-12-14 12:50:45 +00:00
|
|
|
SetConsoleCtrlHandler(handle_signal_win32, TRUE);
|
2019-08-02 03:25:48 +00:00
|
|
|
// SetUnhandledExceptionFilter(win32_signal_handler);
|
2018-09-29 08:16:54 +00:00
|
|
|
#endif
|
2020-04-07 20:41:11 +00:00
|
|
|
cxxopts::Options options(
|
|
|
|
"lokinet",
|
|
|
|
"LokiNET is a free, open source, private, "
|
|
|
|
"decentralized, \"market based sybil resistant\" "
|
|
|
|
"and IP based onion routing network");
|
|
|
|
options.add_options()("v,verbose", "Verbose", cxxopts::value<bool>())(
|
|
|
|
"h,help", "help", cxxopts::value<bool>())("version", "version", cxxopts::value<bool>())(
|
|
|
|
"g,generate", "generate client config", cxxopts::value<bool>())(
|
|
|
|
"r,relay", "run as relay instead of client", cxxopts::value<bool>())(
|
|
|
|
"f,force", "overwrite", cxxopts::value<bool>())(
|
|
|
|
"c,colour", "colour output", cxxopts::value<bool>()->default_value("true"))(
|
2020-02-26 21:07:07 +00:00
|
|
|
"b,background",
|
|
|
|
"background mode (start, but do not connect to the network)",
|
2020-04-07 18:38:56 +00:00
|
|
|
cxxopts::value<bool>())(
|
|
|
|
"config", "path to configuration file", cxxopts::value<std::string>());
|
2019-04-21 21:48:28 +00:00
|
|
|
|
|
|
|
options.parse_positional("config");
|
2019-04-19 18:24:33 +00:00
|
|
|
|
2019-10-08 14:52:01 +00:00
|
|
|
bool genconfigOnly = false;
|
2020-04-07 20:41:11 +00:00
|
|
|
bool overwrite = false;
|
2020-06-29 19:55:59 +00:00
|
|
|
fs::path configFile;
|
2019-04-19 18:24:33 +00:00
|
|
|
try
|
2018-09-19 13:02:55 +00:00
|
|
|
{
|
2019-04-19 18:24:33 +00:00
|
|
|
auto result = options.parse(argc, argv);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (result.count("verbose") > 0)
|
2018-09-19 13:02:55 +00:00
|
|
|
{
|
2019-04-19 18:24:33 +00:00
|
|
|
SetLogLevel(llarp::eLogDebug);
|
|
|
|
llarp::LogDebug("debug logging activated");
|
2018-09-19 13:02:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!result["colour"].as<bool>())
|
2019-09-16 19:40:31 +00:00
|
|
|
{
|
|
|
|
llarp::LogContext::Instance().logStream =
|
2020-04-07 18:38:56 +00:00
|
|
|
std::make_unique<llarp::OStreamLogStream>(false, std::cerr);
|
2019-09-16 19:40:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (result.count("help"))
|
2019-04-19 18:24:33 +00:00
|
|
|
{
|
2019-04-23 20:52:13 +00:00
|
|
|
std::cout << options.help() << std::endl;
|
|
|
|
return 0;
|
2019-04-19 18:24:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (result.count("version"))
|
2019-10-02 18:08:45 +00:00
|
|
|
{
|
2020-06-29 19:55:59 +00:00
|
|
|
std::cout << llarp::VERSION_FULL << std::endl;
|
2019-10-02 18:08:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (result.count("generate") > 0)
|
2019-04-19 18:24:33 +00:00
|
|
|
{
|
|
|
|
genconfigOnly = true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (result.count("background") > 0)
|
2019-05-28 00:19:25 +00:00
|
|
|
{
|
2019-10-04 18:10:58 +00:00
|
|
|
opts.background = true;
|
2019-05-28 00:19:25 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:55:59 +00:00
|
|
|
if (result.count("router") > 0)
|
2020-03-27 01:07:45 +00:00
|
|
|
{
|
2020-06-29 19:55:59 +00:00
|
|
|
opts.isRouter = true;
|
2020-03-27 01:07:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 20:41:11 +00:00
|
|
|
if (result.count("force") > 0)
|
2019-04-19 18:24:33 +00:00
|
|
|
{
|
2020-03-23 20:53:42 +00:00
|
|
|
overwrite = true;
|
2019-04-19 18:24:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 20:41:11 +00:00
|
|
|
if (result.count("config") > 0)
|
2019-04-19 18:24:33 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto arg = result["config"].as<std::string>();
|
|
|
|
if (!arg.empty())
|
2019-04-19 18:24:33 +00:00
|
|
|
{
|
2020-06-29 19:55:59 +00:00
|
|
|
configFile = arg;
|
2019-04-19 18:24:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
catch (const cxxopts::option_not_exists_exception& ex)
|
2019-04-19 18:24:33 +00:00
|
|
|
{
|
|
|
|
std::cerr << ex.what();
|
2019-04-23 20:52:13 +00:00
|
|
|
std::cout << options.help() << std::endl;
|
|
|
|
return 1;
|
2019-04-19 18:24:33 +00:00
|
|
|
}
|
2018-09-19 13:02:55 +00:00
|
|
|
|
2020-06-29 19:55:59 +00:00
|
|
|
if (!configFile.empty())
|
2018-09-19 13:02:55 +00:00
|
|
|
{
|
|
|
|
// when we have an explicit filepath
|
2020-06-29 19:55:59 +00:00
|
|
|
fs::path basedir = configFile.parent_path();
|
2019-01-29 11:23:40 +00:00
|
|
|
|
2020-04-07 20:41:11 +00:00
|
|
|
if (genconfigOnly)
|
2019-11-20 21:22:07 +00:00
|
|
|
{
|
2020-06-29 19:55:59 +00:00
|
|
|
llarp::ensureConfig(basedir, configFile, overwrite, opts.isRouter);
|
2019-11-20 21:22:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::error_code ec;
|
2020-06-29 19:55:59 +00:00
|
|
|
if (!fs::exists(configFile, ec))
|
2019-01-29 11:23:40 +00:00
|
|
|
{
|
2020-06-29 19:55:59 +00:00
|
|
|
llarp::LogError("Config file not found ", configFile);
|
2019-11-20 21:22:07 +00:00
|
|
|
return 1;
|
2019-01-29 11:23:40 +00:00
|
|
|
}
|
2020-03-23 20:53:42 +00:00
|
|
|
|
|
|
|
if (ec)
|
|
|
|
throw std::runtime_error(llarp::stringify("filesystem error: ", ec));
|
2018-09-19 13:10:14 +00:00
|
|
|
}
|
2018-09-19 13:02:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-07 20:41:11 +00:00
|
|
|
llarp::ensureConfig(
|
2020-06-29 19:55:59 +00:00
|
|
|
llarp::GetDefaultDataDir(), llarp::GetDefaultConfigPath(), overwrite, opts.isRouter);
|
|
|
|
configFile = llarp::GetDefaultConfigPath();
|
2018-09-19 13:02:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (genconfigOnly)
|
2019-01-16 20:57:45 +00:00
|
|
|
{
|
2018-09-19 13:02:55 +00:00
|
|
|
return 0;
|
2019-01-16 20:57:45 +00:00
|
|
|
}
|
2018-07-13 13:36:51 +00:00
|
|
|
|
2020-06-29 19:55:59 +00:00
|
|
|
std::thread main_thread{std::bind(&run_main_context, configFile, opts)};
|
2019-07-26 12:55:08 +00:00
|
|
|
auto ftr = exit_code.get_future();
|
|
|
|
do
|
2018-05-27 18:03:10 +00:00
|
|
|
{
|
2019-07-26 12:55:08 +00:00
|
|
|
// do periodic non lokinet related tasks here
|
2020-06-29 19:55:59 +00:00
|
|
|
if (ctx and ctx->IsUp() and not ctx->LooksAlive())
|
2019-12-07 19:58:19 +00:00
|
|
|
{
|
2020-06-29 19:55:59 +00:00
|
|
|
for (const auto& wtf : {"you have been visited by the mascott of the "
|
|
|
|
"deadlocked router.",
|
|
|
|
"⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣀⣴⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠄⠄⠄⠄",
|
|
|
|
"⠄⠄⠄⠄⠄⢀⣀⣀⡀⠄⠄⠄⡠⢲⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠄⠄",
|
|
|
|
"⠄⠄⠄⠔⣈⣀⠄⢔⡒⠳⡴⠊⠄⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⣿⣿⣧⠄⠄",
|
|
|
|
"⠄⢜⡴⢑⠖⠊⢐⣤⠞⣩⡇⠄⠄⠄⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣆⠄⠝⠛⠋⠐",
|
|
|
|
"⢸⠏⣷⠈⠄⣱⠃⠄⢠⠃⠐⡀⠄⠄⠄⠄⠙⠻⢿⣿⣿⣿⣿⣿⣿⣿⡿⠛⠸⠄⠄⠄⠄",
|
|
|
|
"⠈⣅⠞⢁⣿⢸⠘⡄⡆⠄⠄⠈⠢⡀⠄⠄⠄⠄⠄⠄⠉⠙⠛⠛⠛⠉⠉⡀⠄⠡⢀⠄⣀",
|
|
|
|
"⠄⠙⡎⣹⢸⠄⠆⢘⠁⠄⠄⠄⢸⠈⠢⢄⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠃⠄⠄⠄⠄⠄",
|
|
|
|
"⠄⠄⠑⢿⠈⢆⠘⢼⠄⠄⠄⠄⠸⢐⢾⠄⡘⡏⠲⠆⠠⣤⢤⢤⡤⠄⣖⡇⠄⠄⠄⠄⠄",
|
|
|
|
"⣴⣶⣿⣿⣣⣈⣢⣸⠄⠄⠄⠄⡾⣷⣾⣮⣤⡏⠁⠘⠊⢠⣷⣾⡛⡟⠈⠄⠄⠄⠄⠄⠄",
|
|
|
|
"⣿⣿⣿⣿⣿⠉⠒⢽⠄⠄⠄⠄⡇⣿⣟⣿⡇⠄⠄⠄⠄⢸⣻⡿⡇⡇⠄⠄⠄⠄⠄⠄⠄",
|
|
|
|
"⠻⣿⣿⣿⣿⣄⠰⢼⠄⠄⠄⡄⠁⢻⣍⣯⠃⠄⠄⠄⠄⠈⢿⣻⠃⠈⡆⡄⠄⠄⠄⠄⠄",
|
|
|
|
"⠄⠙⠿⠿⠛⣿⣶⣤⡇⠄⠄⢣⠄⠄⠈⠄⢠⠂⠄⠁⠄⡀⠄⠄⣀⠔⢁⠃⠄⠄⠄⠄⠄",
|
|
|
|
"⠄⠄⠄⠄⠄⣿⣿⣿⣿⣾⠢⣖⣶⣦⣤⣤⣬⣤⣤⣤⣴⣶⣶⡏⠠⢃⠌⠄⠄⠄⠄⠄⠄",
|
|
|
|
"⠄⠄⠄⠄⠄⠿⠿⠟⠛⡹⠉⠛⠛⠿⠿⣿⣿⣿⣿⣿⡿⠂⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄",
|
|
|
|
"⠠⠤⠤⠄⠄⣀⠄⠄⠄⠑⠠⣤⣀⣀⣀⡘⣿⠿⠙⠻⡍⢀⡈⠂⠄⠄⠄⠄⠄⠄⠄⠄⠄",
|
|
|
|
"⠄⠄⠄⠄⠄⠄⠑⠠⣠⣴⣾⣿⣿⣿⣿⣿⣿⣇⠉⠄⠻⣿⣷⣄⡀⠄⠄⠄⠄⠄⠄⠄⠄",
|
|
|
|
"file a bug report now or be cursed with this "
|
|
|
|
"annoying image in your syslog for all time."})
|
2019-12-07 19:58:19 +00:00
|
|
|
{
|
2020-06-29 19:55:59 +00:00
|
|
|
LogError(wtf);
|
|
|
|
llarp::LogContext::Instance().ImmediateFlush();
|
2019-12-07 19:58:19 +00:00
|
|
|
}
|
2020-06-29 19:55:59 +00:00
|
|
|
std::abort();
|
2019-12-07 19:58:19 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
} while (ftr.wait_for(std::chrono::seconds(1)) != std::future_status::ready);
|
2019-07-26 12:55:08 +00:00
|
|
|
|
|
|
|
main_thread.join();
|
2019-10-04 18:10:58 +00:00
|
|
|
const auto code = ftr.get();
|
2020-04-02 16:47:56 +00:00
|
|
|
|
|
|
|
llarp::LogContext::Instance().ImmediateFlush();
|
2018-09-29 08:16:54 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
::WSACleanup();
|
|
|
|
#endif
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ctx)
|
2019-12-10 15:21:52 +00:00
|
|
|
{
|
2020-06-29 19:55:59 +00:00
|
|
|
ctx.reset();
|
2019-12-10 15:21:52 +00:00
|
|
|
}
|
2018-05-27 19:13:25 +00:00
|
|
|
return code;
|
2017-09-28 17:02:05 +00:00
|
|
|
}
|