lokinet/daemon/main.cpp

322 lines
7.8 KiB
C++
Raw Normal View History

#include <config/config.hpp> // for ensure_config
2018-05-27 18:03:10 +00:00
#include <llarp.h>
2019-01-11 01:59:44 +00:00
#include <util/fs.hpp>
#include <util/logger.hpp>
2019-01-11 01:59:44 +00:00
#include <csignal>
2019-02-02 05:51:20 +00:00
#if !defined(_WIN32) && !defined(__OpenBSD__)
2019-01-29 11:23:40 +00:00
#include <wordexp.h>
#endif
2018-12-12 01:47:29 +00:00
#include <cxxopts.hpp>
2018-07-20 04:50:28 +00:00
#include <string>
#include <iostream>
2018-05-20 16:15:16 +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
win32_signal_handler(EXCEPTION_POINTERS *);
#endif
2018-05-27 18:03:10 +00:00
struct llarp_main *ctx = 0;
std::promise< int > exit_code;
2018-04-30 16:14:20 +00:00
void
handle_signal(int sig)
2018-05-18 17:50:21 +00:00
{
if(ctx)
2018-05-27 18:03:10 +00:00
llarp_main_signal(ctx, sig);
2018-05-18 17:50:21 +00: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;
}
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
}
#endif
2019-01-29 11:23:40 +00:00
/// resolve ~ and symlinks into actual paths (so we know the real path on disk,
/// to remove assumptions and confusion with permissions)
std::string
resolvePath(std::string conffname)
{
2019-02-02 05:51:20 +00:00
// implemented in netbsd, removed downstream for security reasons
// even though it is defined by POSIX.1-2001+
#if !defined(_WIN32) && !defined(__OpenBSD__)
2019-01-29 11:23:40 +00:00
wordexp_t exp_result;
wordexp(conffname.c_str(), &exp_result, 0);
char *resolvedPath = realpath(exp_result.we_wordv[0], NULL);
if(!resolvedPath)
{
2019-04-24 00:40:44 +00:00
// relative paths don't need to be resolved
// llarp::LogWarn("Can't resolve path: ", exp_result.we_wordv[0]);
return conffname;
2019-01-29 11:23:40 +00:00
}
return resolvedPath;
#else
// TODO(despair): dig through LLVM local patch set
// one of these exists deep in the bowels of LLVMSupport
return conffname; // eww, easier said than done outside of cygwin
#endif
2019-01-29 11:23:40 +00:00
}
/// this sets up, configures and runs the main context
static void
run_main_context(std::string conffname, bool multiThreaded, bool debugMode)
{
// 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(), multiThreaded);
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, debugMode);
llarp::util::SetThreadName("llarp-mainloop");
if(code == 0)
code = llarp_main_run(ctx);
llarp_main_free(ctx);
}
exit_code.set_value(code);
}
int
main(int argc, char *argv[])
{
2018-07-20 04:50:28 +00:00
bool multiThreaded = true;
const char *singleThreadVar = getenv("LLARP_SHADOW");
if(singleThreadVar && std::string(singleThreadVar) == "1")
{
multiThreaded = false;
}
#ifdef _WIN32
if(startWinsock())
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);
#endif
#ifdef LOKINET_DEBUG
absl::SetMutexDeadlockDetectionMode(absl::OnDeadlockCycle::kAbort);
#endif
// clang-format off
cxxopts::Options options(
"lokinet",
2019-04-23 23:15:42 +00:00
"LokiNET is a free, open source, private, decentralized, \"market based sybil resistant\" and IP based onion routing network"
);
2019-04-21 21:48:28 +00:00
options.add_options()
("v,verbose", "Verbose", cxxopts::value<bool>())
("h,help", "help", cxxopts::value<bool>())
("g,generate", "generate client config", cxxopts::value<bool>())
("r,router", "generate router config", cxxopts::value<bool>())
2019-04-21 21:48:28 +00:00
("f,force", "overwrite", cxxopts::value<bool>())
2019-05-28 00:19:25 +00:00
("d,debug", "debug mode - UNENCRYPTED TRAFFIC", cxxopts::value<bool>())
2019-04-21 21:48:28 +00:00
("config","path to configuration file", cxxopts::value<std::string>());
options.parse_positional("config");
2019-04-23 20:52:13 +00:00
// clang-format on
bool genconfigOnly = false;
bool asRouter = false;
bool overWrite = false;
2019-05-28 00:19:25 +00:00
bool debugMode = false;
std::string conffname; // suggestions: confFName? conf_fname?
try
{
auto result = options.parse(argc, argv);
if(result.count("verbose") > 0)
{
SetLogLevel(llarp::eLogDebug);
llarp::LogDebug("debug logging activated");
}
if(result.count("help"))
{
2019-04-23 20:52:13 +00:00
std::cout << options.help() << std::endl;
return 0;
}
2019-04-21 21:48:28 +00:00
if(result.count("generate") > 0)
{
genconfigOnly = true;
}
2019-05-28 00:19:25 +00:00
if(result.count("debug") > 0)
{
debugMode = true;
}
if(result.count("force") > 0)
{
overWrite = true;
}
if(result.count("router") > 0)
{
asRouter = true;
2019-04-24 00:03:42 +00:00
// we should generate and exit (docker needs this, so we don't write a
// config each time on startup)
genconfigOnly = true;
}
2019-04-23 20:52:13 +00:00
if(result.count("config") > 0)
{
2019-04-21 22:15:47 +00:00
auto arg = result["config"].as< std::string >();
if(!arg.empty())
{
2019-04-21 22:15:47 +00:00
conffname = arg;
}
}
}
2019-04-23 20:52:13 +00:00
catch(const cxxopts::option_not_exists_exception &ex)
{
std::cerr << ex.what();
2019-04-23 20:52:13 +00: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();
2018-09-19 13:17:15 +00:00
conffname = fname.string();
2019-01-29 11:23:40 +00:00
conffname = resolvePath(conffname);
std::error_code ec;
// llarp::LogDebug("Basedir: ", basedir);
2018-09-19 13:15:07 +00:00
if(basedir.string().empty())
{
2019-01-29 11:23:40 +00:00
// relative path to config
// does this file exist?
if(genconfigOnly)
{
2019-04-23 20:52:13 +00:00
if(!llarp_ensure_config(conffname.c_str(), basedir.string().c_str(),
overWrite, asRouter))
2019-01-29 11:23:40 +00:00
return 1;
}
else
{
if(!fs::exists(fname, ec))
{
llarp::LogError("Config file not found ", conffname);
return 1;
}
}
2018-09-19 13:15:07 +00:00
}
else
{
2019-01-29 11:23:40 +00:00
// absolute path to config
2018-09-19 13:15:07 +00:00
if(!fs::create_directories(basedir, ec))
{
if(ec)
{
llarp::LogError("failed to create '", basedir.string(),
"': ", ec.message());
return 1;
}
}
2019-01-29 11:23:40 +00:00
if(genconfigOnly)
{
// find or create file
if(!llarp_ensure_config(conffname.c_str(), basedir.string().c_str(),
overWrite, asRouter))
return 1;
}
else
{
// does this file exist?
if(!fs::exists(conffname, ec))
{
llarp::LogError("Config file not found ", conffname);
return 1;
}
}
}
}
else
{
// no explicit config file provided
#ifdef _WIN32
fs::path homedir = fs::path(getenv("APPDATA"));
#else
fs::path homedir = fs::path(getenv("HOME"));
#endif
fs::path basepath = homedir / fs::path(".lokinet");
fs::path fpath = basepath / "lokinet.ini";
2019-01-29 11:23:40 +00:00
// I don't think this is necessary with this condition
// conffname = resolvePath(conffname);
llarp::LogDebug("Find or create ", basepath.string());
std::error_code ec;
// These paths are guaranteed to exist - $APPDATA or $HOME
// so only create .lokinet/*
if(!fs::create_directory(basepath, ec))
{
2018-09-19 13:32:50 +00:00
if(ec)
{
llarp::LogError("failed to create '", basepath.string(),
"': ", ec.message());
return 1;
}
}
2019-01-29 11:23:40 +00:00
// if using default INI file, we're create it even if you don't ask us too
if(!llarp_ensure_config(fpath.string().c_str(), basepath.string().c_str(),
overWrite, asRouter))
return 1;
2018-09-19 13:15:07 +00:00
conffname = fpath.string();
}
if(genconfigOnly)
{
return 0;
}
2018-07-13 13:36:51 +00:00
std::thread main_thread{
std::bind(&run_main_context, conffname, multiThreaded, debugMode)};
auto ftr = exit_code.get_future();
do
2018-05-27 18:03:10 +00:00
{
// do periodic non lokinet related tasks here
} while(ftr.wait_for(std::chrono::seconds(1)) != std::future_status::ready);
main_thread.join();
2019-03-25 02:08:52 +00:00
#ifdef _WIN32
::WSACleanup();
#endif
const auto code = ftr.get();
2018-08-12 17:22:29 +00:00
exit(code);
2018-05-27 19:13:25 +00:00
return code;
2017-09-28 17:02:05 +00:00
}