1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00

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

This commit is contained in:
Jeff Becker 2018-09-19 09:02:55 -04:00
parent 842761d569
commit 58198656a3
No known key found for this signature in database
GPG key ID: F357B3B42F6F9B05
5 changed files with 86 additions and 17 deletions

View file

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

View file

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

View file

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

View file

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

View file

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