2018-12-12 02:47:29 +01:00
|
|
|
#include <config.hpp> // for ensure_config
|
2018-05-27 20:03:10 +02:00
|
|
|
#include <llarp.h>
|
2019-01-11 02:59:44 +01:00
|
|
|
#include <util/fs.hpp>
|
2019-01-10 20:41:51 +01:00
|
|
|
#include <util/logger.hpp>
|
2019-01-11 02:59:44 +01:00
|
|
|
|
2018-07-13 15:36:51 +02:00
|
|
|
#include <signal.h>
|
2019-01-30 18:24:02 +01:00
|
|
|
|
2019-02-02 06:51:20 +01:00
|
|
|
#if !defined(_WIN32) && !defined(__OpenBSD__)
|
2019-01-29 12:23:40 +01:00
|
|
|
#include <wordexp.h>
|
2019-01-30 18:24:02 +01:00
|
|
|
#endif
|
2018-12-12 02:47:29 +01:00
|
|
|
|
2019-04-19 20:24:33 +02:00
|
|
|
#include <cxxopts.hpp>
|
2018-07-20 06:50:28 +02:00
|
|
|
#include <string>
|
2018-09-19 15:02:55 +02:00
|
|
|
#include <iostream>
|
2018-05-20 18:15:16 +02:00
|
|
|
|
2018-07-30 06:38:14 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define wmin(x, y) (((x) < (y)) ? (x) : (y))
|
|
|
|
#define MIN wmin
|
|
|
|
#endif
|
|
|
|
|
2018-05-27 20:03:10 +02:00
|
|
|
struct llarp_main *ctx = 0;
|
2018-04-30 18:14:20 +02:00
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
void
|
|
|
|
handle_signal(int sig)
|
2018-05-18 19:50:21 +02:00
|
|
|
{
|
2018-05-27 19:44:01 +02:00
|
|
|
if(ctx)
|
2018-05-27 20:03:10 +02:00
|
|
|
llarp_main_signal(ctx, sig);
|
2018-05-18 19:50:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 15:02:55 +02:00
|
|
|
int
|
|
|
|
printHelp(const char *argv0, int code = 1)
|
|
|
|
{
|
2018-11-08 13:31:50 +01:00
|
|
|
std::cout << "usage: " << argv0 << " [-h] [-v] [-g|-c] config.ini"
|
|
|
|
<< std::endl;
|
2018-09-19 15:02:55 +02:00
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2018-09-29 10:16:54 +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;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2018-09-29 10:16:54 +02:00
|
|
|
#endif
|
|
|
|
|
2019-01-29 12:23:40 +01: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 06:51:20 +01: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 12:23:40 +01:00
|
|
|
wordexp_t exp_result;
|
|
|
|
wordexp(conffname.c_str(), &exp_result, 0);
|
|
|
|
char *resolvedPath = realpath(exp_result.we_wordv[0], NULL);
|
|
|
|
if(!resolvedPath)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("Can't resolve path: ", exp_result.we_wordv[0]);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return resolvedPath;
|
2019-01-30 18:24:02 +01:00
|
|
|
#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 12:23:40 +01:00
|
|
|
}
|
|
|
|
|
2018-05-22 17:54:19 +02:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2018-07-20 06:50:28 +02:00
|
|
|
bool multiThreaded = true;
|
|
|
|
const char *singleThreadVar = getenv("LLARP_SHADOW");
|
|
|
|
if(singleThreadVar && std::string(singleThreadVar) == "1")
|
|
|
|
{
|
|
|
|
multiThreaded = false;
|
|
|
|
}
|
2018-07-27 02:21:57 +02:00
|
|
|
|
2018-09-29 10:16:54 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
if(startWinsock())
|
|
|
|
return -1;
|
2018-12-14 13:50:45 +01:00
|
|
|
SetConsoleCtrlHandler(handle_signal_win32, TRUE);
|
2018-09-29 10:16:54 +02:00
|
|
|
#endif
|
|
|
|
|
2019-03-03 16:01:05 +01:00
|
|
|
#ifdef LOKINET_DEBUG
|
|
|
|
absl::SetMutexDeadlockDetectionMode(absl::OnDeadlockCycle::kAbort);
|
|
|
|
#endif
|
|
|
|
|
2019-04-19 20:24:33 +02:00
|
|
|
// clang-format off
|
|
|
|
cxxopts::Options options(
|
|
|
|
"lokinet",
|
|
|
|
"Lokinet is a private, decentralized and IP based overlay network for the internet"
|
|
|
|
);
|
2019-04-21 23:48:28 +02:00
|
|
|
options.add_options()
|
2019-04-19 20:24:33 +02:00
|
|
|
("v,verbose", "Verbose", cxxopts::value<bool>())
|
|
|
|
("h,help", "help", cxxopts::value<bool>())
|
|
|
|
("g,generate", "generate config", cxxopts::value<bool>())
|
|
|
|
("r,router", "run as router", cxxopts::value<bool>())
|
2019-04-21 23:48:28 +02:00
|
|
|
("f,force", "overwrite", cxxopts::value<bool>())
|
|
|
|
("config","path to configuration file", cxxopts::value<std::string>());
|
|
|
|
|
|
|
|
options.parse_positional("config");
|
|
|
|
// clang-format on
|
2019-04-19 20:24:33 +02:00
|
|
|
|
2018-09-19 15:02:55 +02:00
|
|
|
bool genconfigOnly = false;
|
2018-10-27 14:41:04 +02:00
|
|
|
bool asRouter = false;
|
2018-10-01 11:58:30 +02:00
|
|
|
bool overWrite = false;
|
2019-04-19 20:24:33 +02:00
|
|
|
std::string conffname; // suggestions: confFName? conf_fname?
|
|
|
|
|
|
|
|
try
|
2018-09-19 15:02:55 +02:00
|
|
|
{
|
2019-04-19 20:24:33 +02:00
|
|
|
auto result = options.parse(argc, argv);
|
|
|
|
|
|
|
|
if(result.count("verbose") > 0)
|
2018-09-19 15:02:55 +02:00
|
|
|
{
|
2019-04-19 20:24:33 +02:00
|
|
|
SetLogLevel(llarp::eLogDebug);
|
|
|
|
llarp::LogDebug("debug logging activated");
|
2018-09-19 15:02:55 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 20:24:33 +02:00
|
|
|
if(result.count("help"))
|
|
|
|
{
|
|
|
|
return printHelp(argv[0], 0);
|
|
|
|
}
|
|
|
|
|
2019-04-21 23:48:28 +02:00
|
|
|
if(result.count("generate") > 0)
|
2019-04-19 20:24:33 +02:00
|
|
|
{
|
|
|
|
genconfigOnly = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result.count("force") > 0)
|
|
|
|
{
|
|
|
|
overWrite = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result.count("router") > 0)
|
|
|
|
{
|
|
|
|
asRouter = true;
|
|
|
|
}
|
|
|
|
|
2019-04-21 23:48:28 +02:00
|
|
|
if(result.count("config") > 0)
|
2019-04-19 20:24:33 +02:00
|
|
|
{
|
2019-04-22 00:15:47 +02:00
|
|
|
auto arg = result["config"].as< std::string >();
|
|
|
|
if(!arg.empty())
|
2019-04-19 20:24:33 +02:00
|
|
|
{
|
2019-04-22 00:15:47 +02:00
|
|
|
conffname = arg;
|
2019-04-19 20:24:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const cxxopts::option_not_exists_exception& ex)
|
|
|
|
{
|
|
|
|
std::cerr << ex.what();
|
|
|
|
return printHelp(argv[0]);
|
|
|
|
}
|
2018-09-19 15:02:55 +02:00
|
|
|
|
2019-04-19 20:24:33 +02:00
|
|
|
if(!conffname.empty())
|
2018-09-19 15:02:55 +02:00
|
|
|
{
|
|
|
|
// when we have an explicit filepath
|
2019-04-19 20:24:33 +02:00
|
|
|
fs::path fname = fs::path(conffname);
|
2018-09-19 15:10:14 +02:00
|
|
|
fs::path basedir = fname.parent_path();
|
2018-09-19 15:17:15 +02:00
|
|
|
conffname = fname.string();
|
2019-01-29 12:23:40 +01:00
|
|
|
conffname = resolvePath(conffname);
|
|
|
|
std::error_code ec;
|
|
|
|
|
|
|
|
// llarp::LogDebug("Basedir: ", basedir);
|
2018-09-19 15:15:07 +02:00
|
|
|
if(basedir.string().empty())
|
2018-09-19 15:10:14 +02:00
|
|
|
{
|
2019-01-29 12:23:40 +01:00
|
|
|
// relative path to config
|
|
|
|
|
|
|
|
// does this file exist?
|
|
|
|
if(genconfigOnly)
|
|
|
|
{
|
2019-04-22 09:30:11 +02:00
|
|
|
if(!llarp_ensure_config(conffname.c_str(), basedir.string().c_str(), overWrite,
|
2019-01-29 12:23:40 +01:00
|
|
|
asRouter))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!fs::exists(fname, ec))
|
|
|
|
{
|
|
|
|
llarp::LogError("Config file not found ", conffname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2018-09-19 15:15:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-29 12:23:40 +01:00
|
|
|
// absolute path to config
|
2018-09-19 15:15:07 +02:00
|
|
|
if(!fs::create_directories(basedir, ec))
|
|
|
|
{
|
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to create '", basedir.string(),
|
|
|
|
"': ", ec.message());
|
|
|
|
return 1;
|
|
|
|
}
|
2018-09-19 15:10:14 +02:00
|
|
|
}
|
2019-01-29 12:23:40 +01: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;
|
|
|
|
}
|
|
|
|
}
|
2018-09-19 15:10:14 +02:00
|
|
|
}
|
2018-09-19 15:02:55 +02:00
|
|
|
}
|
|
|
|
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 12:23:40 +01:00
|
|
|
// I don't think this is necessary with this condition
|
|
|
|
// conffname = resolvePath(conffname);
|
2018-09-19 15:02:55 +02:00
|
|
|
|
2019-01-16 21:57:45 +01:00
|
|
|
llarp::LogDebug("Find or create ", basepath.string());
|
2018-09-19 15:02:55 +02:00
|
|
|
std::error_code ec;
|
2018-10-01 04:08:03 +02:00
|
|
|
// These paths are guaranteed to exist - $APPDATA or $HOME
|
|
|
|
// so only create .lokinet/*
|
2018-09-29 10:16:54 +02:00
|
|
|
if(!fs::create_directory(basepath, ec))
|
2018-09-19 15:02:55 +02:00
|
|
|
{
|
2018-09-19 15:32:50 +02:00
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to create '", basepath.string(),
|
|
|
|
"': ", ec.message());
|
|
|
|
return 1;
|
|
|
|
}
|
2018-09-19 15:02:55 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 12:23:40 +01:00
|
|
|
// if using default INI file, we're create it even if you don't ask us too
|
2018-09-19 15:02:55 +02:00
|
|
|
if(!llarp_ensure_config(fpath.string().c_str(), basepath.string().c_str(),
|
2018-10-01 11:58:30 +02:00
|
|
|
overWrite, asRouter))
|
2018-09-19 15:02:55 +02:00
|
|
|
return 1;
|
2018-09-19 15:15:07 +02:00
|
|
|
conffname = fpath.string();
|
2018-09-19 15:02:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(genconfigOnly)
|
2019-01-16 21:57:45 +01:00
|
|
|
{
|
2018-09-19 15:02:55 +02:00
|
|
|
return 0;
|
2019-01-16 21:57:45 +01:00
|
|
|
}
|
2018-07-13 15:36:51 +02:00
|
|
|
|
2019-01-16 21:57:45 +01:00
|
|
|
// this is important, can downgrade from Info though
|
2019-02-02 09:51:33 +01:00
|
|
|
llarp::LogInfo("Running from: ", fs::current_path().string());
|
2019-01-16 21:57:45 +01:00
|
|
|
llarp::LogInfo("Using config file: ", conffname);
|
2018-09-19 15:15:07 +02:00
|
|
|
ctx = llarp_main_init(conffname.c_str(), multiThreaded);
|
2018-05-27 21:13:25 +02:00
|
|
|
int code = 1;
|
|
|
|
if(ctx)
|
2018-05-27 20:03:10 +02:00
|
|
|
{
|
|
|
|
signal(SIGINT, handle_signal);
|
2018-11-29 14:12:28 +01:00
|
|
|
signal(SIGTERM, handle_signal);
|
2018-09-19 13:57:07 +02:00
|
|
|
#ifndef _WIN32
|
2018-09-17 13:47:34 +02:00
|
|
|
signal(SIGHUP, handle_signal);
|
2018-09-19 13:57:07 +02:00
|
|
|
#endif
|
2019-03-25 03:08:52 +01:00
|
|
|
|
2018-12-03 03:17:36 +01:00
|
|
|
code = llarp_main_setup(ctx);
|
|
|
|
if(code == 0)
|
|
|
|
code = llarp_main_run(ctx);
|
2018-07-20 06:50:28 +02:00
|
|
|
llarp_main_free(ctx);
|
2018-05-27 20:03:10 +02:00
|
|
|
}
|
2018-09-29 10:16:54 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
::WSACleanup();
|
|
|
|
#endif
|
2018-08-12 19:22:29 +02:00
|
|
|
exit(code);
|
2018-05-27 21:13:25 +02:00
|
|
|
return code;
|
2017-09-28 19:02:05 +02:00
|
|
|
}
|