Clean up the logic around generating default confs

pull/1186/head
Stephen Shelton 4 years ago
parent c5ff672c79
commit 1653b73ee5
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -5,6 +5,7 @@
#include <util/fs.hpp> #include <util/fs.hpp>
#include <util/logging/logger.hpp> #include <util/logging/logger.hpp>
#include <util/logging/ostream_logger.hpp> #include <util/logging/ostream_logger.hpp>
#include <util/str.hpp>
#include <csignal> #include <csignal>
@ -123,7 +124,7 @@ main(int argc, char* argv[])
bool genconfigOnly = false; bool genconfigOnly = false;
bool asRouter = false; bool asRouter = false;
bool overWrite = false; bool overwrite = false;
std::string conffname; std::string conffname;
try try
{ {
@ -165,7 +166,7 @@ main(int argc, char* argv[])
if (result.count("force") > 0) if (result.count("force") > 0)
{ {
overWrite = true; overwrite = true;
} }
if (result.count("router") > 0) if (result.count("router") > 0)
@ -213,8 +214,10 @@ main(int argc, char* argv[])
if (genconfigOnly) if (genconfigOnly)
{ {
if (!llarp_ensure_config(conffname.c_str(), basedir.string().c_str(), overWrite, asRouter)) llarp::ensureConfig(llarp::GetDefaultConfigDir(),
return 1; llarp::GetDefaultConfigFilename(),
overwrite,
asRouter);
} }
else else
{ {
@ -224,6 +227,9 @@ main(int argc, char* argv[])
llarp::LogError("Config file not found ", conffname); llarp::LogError("Config file not found ", conffname);
return 1; return 1;
} }
if (ec)
throw std::runtime_error(llarp::stringify("filesystem error: ", ec));
} }
} }
else else
@ -243,13 +249,11 @@ main(int argc, char* argv[])
} }
} }
auto fpath = llarp::GetDefaultConfigPath(); llarp::ensureConfig(llarp::GetDefaultConfigDir(),
llarp::GetDefaultConfigFilename(),
// if using default INI file, we're create it even if you don't ask us too overwrite,
if (!llarp_ensure_config( asRouter);
fpath.string().c_str(), basepath.string().c_str(), overWrite, asRouter)) conffname = llarp::GetDefaultConfigPath().c_str();
return 1;
conffname = fpath.string();
} }
if (genconfigOnly) if (genconfigOnly)

@ -17,6 +17,7 @@
#include <fstream> #include <fstream>
#include <ios> #include <ios>
#include <iostream> #include <iostream>
#include "ghc/filesystem.hpp"
namespace llarp namespace llarp
{ {
@ -513,87 +514,89 @@ namespace llarp
#else #else
const fs::path homedir = fs::path(getenv("HOME")); const fs::path homedir = fs::path(getenv("HOME"));
#endif #endif
return homedir / fs::path(".lokinet"); return homedir / fs::path(".lokinet/");
} }
fs::path fs::path
GetDefaultConfigPath() GetDefaultConfigFilename()
{ {
return GetDefaultConfigDir() / "lokinet.ini"; return fs::path("lokinet.ini");
} }
} // namespace llarp fs::path
GetDefaultConfigPath()
/// fname should be a relative path (from CWD) or absolute path to the config
/// file
extern "C" bool
llarp_ensure_config(const char* fname, const char* basedir, bool overwrite, bool asRouter)
{
if (Lokinet_INIT())
return false;
std::error_code ec;
if (fs::exists(fname, ec) && !overwrite)
{
return true;
}
if (ec)
{ {
llarp::LogError(ec); return GetDefaultConfigDir() / GetDefaultConfigFilename();
return false;
} }
std::string basepath; void
if (basedir) ensureConfig(const fs::path& dir, const fs::path& filename, bool overwrite, bool asRouter)
{ {
basepath = basedir; fs::path fullPath = dir / filename;
#ifndef _WIN32
basepath += "/";
#else
basepath += "\\";
#endif
}
llarp::LogInfo("Attempting to create config file ", fname); std::error_code ec;
// abort if config already exists // fail to overwrite if not instructed to do so
if (!asRouter) if(fs::exists(fullPath, ec) && !overwrite)
{ throw std::invalid_argument(stringify("Config file ", fullPath, " already exists"));
if (fs::exists(fname, ec) && !overwrite)
{ if (ec) throw std::runtime_error(stringify("filesystem error: ", ec));
llarp::LogError(fname, " currently exists, please use -f to overwrite");
return true; // create parent dir if it doesn't exist
} if (not fs::exists(dir, ec))
if (ec)
{ {
llarp::LogError(ec); if (not fs::create_directory(dir))
return false; throw std::runtime_error(stringify("Failed to create parent directory ", dir));
} }
} if (ec) throw std::runtime_error(stringify("filesystem error: ", ec));
// write fname ini llarp::LogInfo("Attempting to create config file ", fullPath);
auto optional_f = llarp::util::OpenFileStream<std::ofstream>(fname, std::ios::binary);
if (!optional_f || !optional_f.value().is_open()) llarp::Config config;
{ std::string confStr;
llarp::LogError("failed to open ", fname, " for writing"); if (asRouter)
return false; confStr = config.generateBaseClientConfig();
else
confStr = config.generateBaseRouterConfig();
// open a filestream
auto stream = llarp::util::OpenFileStream<std::ofstream>(fullPath.c_str(), std::ios::binary);
if (not stream.has_value() or not stream.value().is_open())
throw std::runtime_error(stringify("Failed to open file ", fullPath, " for writing"));
stream.value() << confStr;
stream.value().flush();
llarp::LogInfo("Generated new config ", fullPath);
} }
auto& f = optional_f.value();
llarp_generic_ensure_config(f, basepath, asRouter); std::string
if (asRouter) Config::generateBaseClientConfig()
{ {
llarp_ensure_router_config(f, basepath); throw std::runtime_error("fixme");
} }
else
std::string
Config::generateBaseRouterConfig()
{ {
llarp_ensure_client_config(f, basepath); throw std::runtime_error("fixme");
} }
llarp::LogInfo("Generated new config ", fname);
return true; } // namespace llarp
}
void void
llarp_generic_ensure_config(std::ofstream& f, std::string basepath, bool isRouter) llarp_generic_ensure_config(std::ofstream& f, std::string basepath, bool isRouter)
{ {
llarp::Configuration def;
llarp::Config conf;
conf.initializeConfig(def);
std::string confStr = def.generateINIConfig();
f << confStr;
f.flush();
/*
f << "# this configuration was auto generated with 'sane' defaults\n"; f << "# this configuration was auto generated with 'sane' defaults\n";
f << "# change these values as desired\n"; f << "# change these values as desired\n";
f << "\n\n"; f << "\n\n";
@ -682,11 +685,21 @@ llarp_generic_ensure_config(std::ofstream& f, std::string basepath, bool isRoute
// f << "# add another bootstrap node\n"; // f << "# add another bootstrap node\n";
// f << "#add-node=/path/to/alternative/self.signed\n"; // f << "#add-node=/path/to/alternative/self.signed\n";
f << "\n\n"; f << "\n\n";
*/
} }
void void
llarp_ensure_router_config(std::ofstream& f, std::string basepath) llarp_ensure_router_config(std::ofstream& f, std::string basepath)
{ {
llarp::Configuration def;
llarp::Config conf;
conf.initializeConfig(def);
std::string confStr = def.generateINIConfig();
/*
f << confStr;
f.flush();
f << "# lokid settings (disabled by default)\n"; f << "# lokid settings (disabled by default)\n";
f << "[lokid]\n"; f << "[lokid]\n";
f << "enabled=false\n"; f << "enabled=false\n";
@ -721,11 +734,23 @@ llarp_ensure_router_config(std::ofstream& f, std::string basepath)
} }
f << std::endl; f << std::endl;
*/
} }
bool bool
llarp_ensure_client_config(std::ofstream& f, std::string basepath) llarp_ensure_client_config(std::ofstream& f, std::string basepath)
{ {
llarp::Configuration def;
llarp::Config conf;
conf.initializeConfig(def);
std::string confStr = def.generateINIConfig();
f << confStr;
f.flush();
return true;
/*
// write snapp-example.ini // write snapp-example.ini
const std::string snappExample_fpath = basepath + "snapp-example.ini"; const std::string snappExample_fpath = basepath + "snapp-example.ini";
{ {
@ -740,15 +765,14 @@ llarp_ensure_client_config(std::ofstream& f, std::string basepath)
// pick ip // pick ip
// don't revert me // don't revert me
const static std::string ip = "10.33.0.1/16"; const static std::string ip = "10.33.0.1/16";
/*
std::string ip = llarp::findFreePrivateRange(); // std::string ip = llarp::findFreePrivateRange();
if(ip == "") // if(ip == "")
{ // {
llarp::LogError( // llarp::LogError(
"Couldn't easily detect a private range to map lokinet onto"); // "Couldn't easily detect a private range to map lokinet onto");
return false; // return false;
} // }
*/
example_f << "# this is an example configuration for a snapp\n"; example_f << "# this is an example configuration for a snapp\n";
example_f << "[example-snapp]\n"; example_f << "[example-snapp]\n";
example_f << "# keyfile is the path to the private key of the snapp, " example_f << "# keyfile is the path to the private key of the snapp, "
@ -793,4 +817,5 @@ llarp_ensure_client_config(std::ofstream& f, std::string basepath)
// probably auto in case they want to set up a hidden service // probably auto in case they want to set up a hidden service
f << "enabled=true\n"; f << "enabled=true\n";
return true; return true;
*/
} }

@ -248,11 +248,6 @@ namespace llarp
struct Config struct Config
{ {
private:
void
initializeConfig(Configuration& conf);
public:
RouterConfig router; RouterConfig router;
NetworkConfig network; NetworkConfig network;
ConnectConfig connect; ConnectConfig connect;
@ -266,26 +261,38 @@ namespace llarp
BootstrapConfig bootstrap; BootstrapConfig bootstrap;
LoggingConfig logging; LoggingConfig logging;
// Initialize config definition
void
initializeConfig(Configuration& conf);
// Load a config from the given file
bool bool
Load(const char* fname); Load(const char* fname);
// Load a config from the given string
bool bool
LoadFromStr(string_view str); LoadFromStr(string_view str);
llarp_config* std::string
Copy() const; generateBaseClientConfig();
std::string
generateBaseRouterConfig();
}; };
fs::path fs::path
GetDefaultConfigDir(); GetDefaultConfigDir();
fs::path
GetDefaultConfigFilename();
fs::path fs::path
GetDefaultConfigPath(); GetDefaultConfigPath();
} // namespace llarp void
ensureConfig(const fs::path& dir, const fs::path& filename, bool overwrite, bool asRouter);
void } // namespace llarp
llarp_generic_ensure_config(std::ofstream& f, std::string basepath, bool isRouter);
void void
llarp_ensure_router_config(std::ofstream& f, std::string basepath); llarp_ensure_router_config(std::ofstream& f, std::string basepath);

Loading…
Cancel
Save