configurable base directory for config and add option for only generating config

pull/15/head
Jeff Becker 6 years ago
parent 842761d569
commit 58198656a3
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -1,7 +1,11 @@
#include <llarp.h>
#include <llarp/logger.h>
#include <llarp/logger.hpp>
#include <signal.h>
#include <getopt.h>
#include <string>
#include <iostream>
#include <libgen.h>
#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;

@ -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
}

@ -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;
}

@ -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;

@ -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

Loading…
Cancel
Save