lokinet/daemon/main.cpp

284 lines
8.5 KiB
C++
Raw Normal View History

#include <config/config.hpp> // for ensure_config
#include <constants/version.hpp>
#include <llarp.hpp>
#include <util/lokinet_init.h>
2019-01-11 02:59:44 +01:00
#include <util/fs.hpp>
2019-09-01 14:10:49 +02:00
#include <util/logging/logger.hpp>
2019-09-16 21:40:31 +02:00
#include <util/logging/ostream_logger.hpp>
#include <util/str.hpp>
2019-01-11 02:59:44 +01:00
#include <csignal>
#include <cxxopts.hpp>
2018-07-20 06:50:28 +02:00
#include <string>
#include <iostream>
#include <future>
2018-05-20 18:15:16 +02:00
#ifdef _WIN32
#define wmin(x, y) (((x) < (y)) ? (x) : (y))
#define MIN wmin
2019-08-02 05:25:48 +02:00
extern "C" LONG FAR PASCAL
win32_signal_handler(EXCEPTION_POINTERS*);
#endif
struct llarp_main* ctx = 0;
std::promise<int> exit_code;
2018-04-30 18:14:20 +02:00
void
handle_signal(int sig)
2018-05-18 19:50:21 +02:00
{
if (ctx)
2019-09-04 14:41:07 +02:00
{
2018-05-27 20:03:10 +02:00
llarp_main_signal(ctx, sig);
2019-09-04 14:41:07 +02:00
}
2018-05-18 19:50:21 +02:00
}
#ifdef _WIN32
int
startWinsock()
{
WSADATA wsockd;
int err;
err = ::WSAStartup(MAKEWORD(2, 2), &wsockd);
if (err)
{
perror("Failed to start Windows Sockets");
return err;
}
::CreateMutex(nullptr, FALSE, "lokinet_win32_daemon");
return 0;
}
2018-12-14 13:50:45 +01:00
extern "C" BOOL FAR PASCAL
handle_signal_win32(DWORD fdwCtrlType)
{
UNREFERENCED_PARAMETER(fdwCtrlType);
handle_signal(SIGINT);
2018-12-23 14:29:11 +01:00
return TRUE; // probably unreachable
2018-12-14 13:50:45 +01:00
}
#endif
/// this sets up, configures and runs the main context
static void
run_main_context(std::string conffname, llarp_main_runtime_opts opts)
{
// this is important, can downgrade from Info though
llarp::LogDebug("Running from: ", fs::current_path().string());
llarp::LogInfo("Using config file: ", conffname);
ctx = llarp_main_init(conffname.c_str(), opts.isRelay);
int code = 1;
if (ctx)
{
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
#ifndef _WIN32
signal(SIGHUP, handle_signal);
#endif
code = llarp_main_setup(ctx);
llarp::util::SetThreadName("llarp-mainloop");
if (code == 0)
code = llarp_main_run(ctx, opts);
}
exit_code.set_value(code);
}
int
main(int argc, char* argv[])
{
auto result = Lokinet_INIT();
if (result)
{
return result;
}
llarp_main_runtime_opts opts;
const char* singleThreadVar = getenv("LLARP_SHADOW");
if (singleThreadVar && std::string(singleThreadVar) == "1")
2018-07-20 06:50:28 +02:00
{
opts.singleThreaded = true;
2018-07-20 06:50:28 +02:00
}
#ifdef _WIN32
if (startWinsock())
return -1;
2018-12-14 13:50:45 +01:00
SetConsoleCtrlHandler(handle_signal_win32, TRUE);
2019-08-02 05:25:48 +02:00
// SetUnhandledExceptionFilter(win32_signal_handler);
#endif
2020-03-30 23:23:29 +02: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"))(
"b,background",
"background mode (start, but do not connect to the network)",
cxxopts::value<bool>())(
"config", "path to configuration file", cxxopts::value<std::string>());
2019-04-21 23:48:28 +02:00
options.parse_positional("config");
2019-10-08 16:52:01 +02:00
bool genconfigOnly = false;
2020-03-30 23:23:29 +02:00
bool overwrite = false;
std::string conffname;
try
{
auto result = options.parse(argc, argv);
if (result.count("verbose") > 0)
{
SetLogLevel(llarp::eLogDebug);
llarp::LogDebug("debug logging activated");
}
if (!result["colour"].as<bool>())
2019-09-16 21:40:31 +02:00
{
llarp::LogContext::Instance().logStream =
std::make_unique<llarp::OStreamLogStream>(false, std::cerr);
2019-09-16 21:40:31 +02:00
}
if (result.count("help"))
{
2019-04-23 22:52:13 +02:00
std::cout << options.help() << std::endl;
return 0;
}
if (result.count("version"))
{
std::cout << llarp_version() << std::endl;
return 0;
}
if (result.count("generate") > 0)
{
genconfigOnly = true;
}
if (result.count("background") > 0)
2019-05-28 02:19:25 +02:00
{
opts.background = true;
2019-05-28 02:19:25 +02:00
}
if(result.count("relay") > 0)
{
opts.isRelay = true;
}
if(result.count("force") > 0)
{
overwrite = true;
}
2020-03-30 23:23:29 +02:00
if(result.count("config") > 0)
{
auto arg = result["config"].as<std::string>();
if (!arg.empty())
{
2019-04-22 00:15:47 +02:00
conffname = arg;
}
}
}
catch (const cxxopts::option_not_exists_exception& ex)
{
std::cerr << ex.what();
2019-04-23 22:52:13 +02:00
std::cout << options.help() << std::endl;
return 1;
}
if (!conffname.empty())
{
// when we have an explicit filepath
fs::path fname = fs::path(conffname);
fs::path basedir = fname.parent_path();
2019-01-29 12:23:40 +01:00
if(genconfigOnly)
{
llarp::ensureConfig(llarp::GetDefaultDataDir(),
llarp::GetDefaultConfigPath(),
overwrite,
2020-03-30 23:23:29 +02:00
opts.isRelay);
}
else
{
std::error_code ec;
if (!fs::exists(fname, ec))
2019-01-29 12:23:40 +01:00
{
llarp::LogError("Config file not found ", conffname);
return 1;
2019-01-29 12:23:40 +01:00
}
if (ec)
throw std::runtime_error(llarp::stringify("filesystem error: ", ec));
}
}
else
{
llarp::ensureConfig(llarp::GetDefaultDataDir(),
llarp::GetDefaultConfigPath(),
overwrite,
2020-03-30 23:23:29 +02:00
opts.isRelay);
conffname = llarp::GetDefaultConfigPath().c_str();
}
if (genconfigOnly)
{
return 0;
}
2018-07-13 15:36:51 +02:00
2019-10-08 16:52:01 +02:00
std::thread main_thread{std::bind(&run_main_context, conffname, opts)};
auto ftr = exit_code.get_future();
do
2018-05-27 20:03:10 +02:00
{
// do periodic non lokinet related tasks here
if (ctx != nullptr)
{
auto ctx_pp = llarp::Context::Get(ctx);
if (ctx_pp != nullptr)
{
if (ctx_pp->IsUp() and not ctx_pp->LooksAlive())
{
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."})
{
LogError(wtf);
}
std::abort();
}
}
}
} while (ftr.wait_for(std::chrono::seconds(1)) != std::future_status::ready);
main_thread.join();
const auto code = ftr.get();
#ifdef _WIN32
::WSACleanup();
#endif
if (ctx)
{
llarp_main_free(ctx);
}
2018-05-27 21:13:25 +02:00
return code;
2017-09-28 19:02:05 +02:00
}