This commit is contained in:
Ryan Tharp 2018-09-19 13:21:15 +00:00
commit f9936b43d3
5 changed files with 108 additions and 18 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,12 +37,82 @@ 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]);
}
}
ctx = llarp_main_init(conffname, multiThreaded);
std::string conffname;
if(optind < argc)
{
// when we have an explicit filepath
fs::path fname = fs::path(argv[optind]);
fs::path basedir = fname.parent_path();
conffname = fname.string();
if(basedir.string().empty())
{
if(!llarp_ensure_config(fname.string().c_str(), nullptr, genconfigOnly))
return 1;
}
else
{
std::error_code ec;
if(!fs::create_directories(basedir, ec))
{
if(ec)
{
llarp::LogError("failed to create '", basedir.string(),
"': ", ec.message());
return 1;
}
}
if(!llarp_ensure_config(fname.string().c_str(), basedir.string().c_str(),
genconfigOnly))
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";
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();
}
if(genconfigOnly)
return 0;
ctx = llarp_main_init(conffname.c_str(), multiThreaded);
int code = 1;
if(ctx)
{

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

@ -380,7 +380,7 @@ namespace llarp
}
bool
isPrivate()
isPrivate() const
{
uint32_t byte = getHostLong();
return this->isTenPrivate(byte) || this->isOneSevenPrivate(byte)
@ -388,7 +388,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

@ -88,6 +88,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