From 58198656a366f3c347b8c2cdeeab9ca0bde3f562 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 19 Sep 2018 09:02:55 -0400 Subject: [PATCH] configurable base directory for config and add option for only generating config --- daemon/main.cpp | 68 +++++++++++++++++++++++++++++++++++++++--- include/llarp/config.h | 8 +++-- include/llarp/net.hpp | 4 +-- llarp/config.cpp | 20 ++++++++----- readme.md | 3 +- 5 files changed, 86 insertions(+), 17 deletions(-) diff --git a/daemon/main.cpp b/daemon/main.cpp index 33a00f01d..42fbd9f91 100644 --- a/daemon/main.cpp +++ b/daemon/main.cpp @@ -1,7 +1,11 @@ #include -#include +#include #include +#include #include +#include +#include +#include "fs.hpp" #ifdef _WIN32 #define wmin(x, y) (((x) < (y)) ? (x) : (y)) @@ -17,6 +21,13 @@ handle_signal(int sig) llarp_main_signal(ctx, sig); } +int +printHelp(const char *argv0, int code = 1) +{ + std::cout << "usage: " << argv0 << " [-h] [-g] config.ini" << std::endl; + return code; +} + int main(int argc, char *argv[]) { @@ -26,10 +37,59 @@ main(int argc, char *argv[]) { multiThreaded = false; } - const char *conffname = handleBaseCmdLineArgs(argc, argv); - if(!llarp_ensure_config(conffname)) - return 1; + int opt = 0; + bool genconfigOnly = false; + while((opt = getopt(argc, argv, "hg")) != -1) + { + switch(opt) + { + case 'h': + return printHelp(argv[0], 0); + case 'g': + genconfigOnly = true; + break; + default: + return printHelp(argv[0]); + } + } + + const char *conffname = nullptr; + + if(optind < argc) + { + // when we have an explicit filepath + if(!llarp_ensure_config(argv[optind], dirname(argv[optind]), genconfigOnly)) + return 1; + conffname = argv[optind]; + } + 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"; + + std::error_code ec; + if(!fs::create_directories(basepath, ec)) + { + llarp::LogError("failed to create '", basepath.string(), + "': ", ec.message()); + return 1; + } + + if(!llarp_ensure_config(fpath.string().c_str(), basepath.string().c_str(), + genconfigOnly)) + return 1; + conffname = fpath.string().c_str(); + } + + if(genconfigOnly) + return 0; ctx = llarp_main_init(conffname, multiThreaded); int code = 1; diff --git a/include/llarp/config.h b/include/llarp/config.h index cf7f40e74..dae2d105f 100644 --- a/include/llarp/config.h +++ b/include/llarp/config.h @@ -43,10 +43,14 @@ extern "C" struct llarp_config_iterator *iter); /// ensure configuration exists - /// populate with defaults if it does not exist + /// populate with defaults /// return if this succeeded + /// if overwrite is true then overwrite old config file + /// if basedir is not nullptr then use basedir as an absolute + /// base path for all files in config bool - llarp_ensure_config(const char *fname); + llarp_ensure_config(const char *fname, const char *basedir = nullptr, + bool overwrite = false); #ifdef __cplusplus } diff --git a/include/llarp/net.hpp b/include/llarp/net.hpp index 772669165..b79aedad1 100644 --- a/include/llarp/net.hpp +++ b/include/llarp/net.hpp @@ -310,7 +310,7 @@ namespace llarp } bool - isPrivate() + isPrivate() const { in_addr_t addr = this->addr4()->s_addr; unsigned byte = ntohl(addr); @@ -321,7 +321,7 @@ namespace llarp } bool - isLoopback() + isLoopback() const { return (ntohl(addr4()->s_addr)) >> 24 == 127; } diff --git a/llarp/config.cpp b/llarp/config.cpp index 264f4349b..db692f638 100644 --- a/llarp/config.cpp +++ b/llarp/config.cpp @@ -88,16 +88,22 @@ extern "C" } bool - llarp_ensure_config(const char *fname) + llarp_ensure_config(const char *fname, const char *basedir, bool overwrite) { std::error_code ec; - if(fs::exists(fname, ec)) + if(fs::exists(fname, ec) && !overwrite) return true; if(ec) { llarp::LogError(ec); return false; } + std::string basepath = ""; + if(basedir) + { + basepath = basedir; + basepath += "/"; + } std::ofstream f(fname); if(!f.is_open()) @@ -123,13 +129,13 @@ extern "C" f << "# number of crypto worker threads " << std::endl; f << "threads=4" << std::endl; f << "# path to store signed RC" << std::endl; - f << "contact-file=self.signed" << std::endl; + f << "contact-file=" << basepath << "self.signed" << std::endl; f << "# path to store transport private key" << std::endl; - f << "transport-privkey=transport.private" << std::endl; + f << "transport-privkey=" << basepath << "transport.private" << std::endl; f << "# path to store identity signing key" << std::endl; - f << "identity-privkey=identity.private" << std::endl; + f << "identity-privkey=" << basepath << "identity.private" << std::endl; f << "# path to store signed RC" << std::endl; - f << "contact-file=self.signed" << std::endl; + f << "contact-file=" << basepath << "self.signed" << std::endl; f << std::endl; f << "# uncomment following line to set router nickname to 'lokinet'" << std::endl; @@ -162,7 +168,7 @@ extern "C" f << "# network database settings block " << std::endl; f << "[netdb]" << std::endl; f << "# directory for network database skiplist storage" << std::endl; - f << "dir=netdb" << std::endl; + f << "dir=" << basepath << "netdb" << std::endl; f << std::endl << std::endl; f << "# publish network interfaces for handling inbound traffic" << std::endl; diff --git a/readme.md b/readme.md index fb55bafb7..8804a44ee 100644 --- a/readme.md +++ b/readme.md @@ -52,6 +52,5 @@ Alternatively: # mkdir /usr/local/lokinet # cd /usr/local/lokinet - # lokinet --genconf daemon.ini - # lokinet --check daemon.ini + # lokinet -g /usr/local/lokinet/daemon.ini # lokinet /usr/local/lokinet/daemon.ini