diff --git a/daemon/main.cpp b/daemon/main.cpp index ee56fc2ba..71e58eb00 100644 --- a/daemon/main.cpp +++ b/daemon/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -123,7 +124,7 @@ main(int argc, char* argv[]) bool genconfigOnly = false; bool asRouter = false; - bool overWrite = false; + bool overwrite = false; std::string conffname; try { @@ -165,7 +166,7 @@ main(int argc, char* argv[]) if (result.count("force") > 0) { - overWrite = true; + overwrite = true; } if (result.count("router") > 0) @@ -213,8 +214,10 @@ main(int argc, char* argv[]) if (genconfigOnly) { - if (!llarp_ensure_config(conffname.c_str(), basedir.string().c_str(), overWrite, asRouter)) - return 1; + llarp::ensureConfig(llarp::GetDefaultConfigDir(), + llarp::GetDefaultConfigFilename(), + overwrite, + asRouter); } else { @@ -224,6 +227,9 @@ main(int argc, char* argv[]) llarp::LogError("Config file not found ", conffname); return 1; } + + if (ec) + throw std::runtime_error(llarp::stringify("filesystem error: ", ec)); } } else @@ -243,13 +249,11 @@ main(int argc, char* argv[]) } } - auto fpath = llarp::GetDefaultConfigPath(); - - // if using default INI file, we're create it even if you don't ask us too - if (!llarp_ensure_config( - fpath.string().c_str(), basepath.string().c_str(), overWrite, asRouter)) - return 1; - conffname = fpath.string(); + llarp::ensureConfig(llarp::GetDefaultConfigDir(), + llarp::GetDefaultConfigFilename(), + overwrite, + asRouter); + conffname = llarp::GetDefaultConfigPath().c_str(); } if (genconfigOnly) diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index f91f396a7..3325345e0 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -17,6 +17,7 @@ #include #include #include +#include "ghc/filesystem.hpp" namespace llarp { @@ -513,87 +514,89 @@ namespace llarp #else const fs::path homedir = fs::path(getenv("HOME")); #endif - return homedir / fs::path(".lokinet"); + return homedir / fs::path(".lokinet/"); } fs::path - GetDefaultConfigPath() + GetDefaultConfigFilename() { - return GetDefaultConfigDir() / "lokinet.ini"; + return fs::path("lokinet.ini"); } -} // namespace llarp - -/// 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) + fs::path + GetDefaultConfigPath() { - llarp::LogError(ec); - return false; + return GetDefaultConfigDir() / GetDefaultConfigFilename(); } - std::string basepath; - if (basedir) + void + ensureConfig(const fs::path& dir, const fs::path& filename, bool overwrite, bool asRouter) { - basepath = basedir; -#ifndef _WIN32 - basepath += "/"; -#else - basepath += "\\"; -#endif - } + fs::path fullPath = dir / filename; - llarp::LogInfo("Attempting to create config file ", fname); + std::error_code ec; - // abort if config already exists - if (!asRouter) - { - if (fs::exists(fname, ec) && !overwrite) - { - llarp::LogError(fname, " currently exists, please use -f to overwrite"); - return true; - } - if (ec) + // fail to overwrite if not instructed to do so + if(fs::exists(fullPath, ec) && !overwrite) + throw std::invalid_argument(stringify("Config file ", fullPath, " already exists")); + + if (ec) throw std::runtime_error(stringify("filesystem error: ", ec)); + + // create parent dir if it doesn't exist + if (not fs::exists(dir, ec)) { - llarp::LogError(ec); - return false; + if (not fs::create_directory(dir)) + throw std::runtime_error(stringify("Failed to create parent directory ", dir)); } - } + if (ec) throw std::runtime_error(stringify("filesystem error: ", ec)); - // write fname ini - auto optional_f = llarp::util::OpenFileStream(fname, std::ios::binary); - if (!optional_f || !optional_f.value().is_open()) - { - llarp::LogError("failed to open ", fname, " for writing"); - return false; + llarp::LogInfo("Attempting to create config file ", fullPath); + + llarp::Config config; + std::string confStr; + if (asRouter) + confStr = config.generateBaseClientConfig(); + else + confStr = config.generateBaseRouterConfig(); + + // open a filestream + auto stream = llarp::util::OpenFileStream(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); - if (asRouter) + + std::string + 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 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 << "# change these values as desired\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-node=/path/to/alternative/self.signed\n"; f << "\n\n"; + */ } void 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]\n"; f << "enabled=false\n"; @@ -721,11 +734,23 @@ llarp_ensure_router_config(std::ofstream& f, std::string basepath) } f << std::endl; + */ } bool 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 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 // don't revert me const static std::string ip = "10.33.0.1/16"; - /* - std::string ip = llarp::findFreePrivateRange(); - if(ip == "") - { - llarp::LogError( - "Couldn't easily detect a private range to map lokinet onto"); - return false; - } - */ + + // std::string ip = llarp::findFreePrivateRange(); + // if(ip == "") + // { + // llarp::LogError( + // "Couldn't easily detect a private range to map lokinet onto"); + // return false; + // } example_f << "# this is an example configuration for a snapp\n"; example_f << "[example-snapp]\n"; 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 f << "enabled=true\n"; return true; + */ } diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 30deb2332..23ebae09e 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -248,11 +248,6 @@ namespace llarp struct Config { - private: - void - initializeConfig(Configuration& conf); - - public: RouterConfig router; NetworkConfig network; ConnectConfig connect; @@ -266,26 +261,38 @@ namespace llarp BootstrapConfig bootstrap; LoggingConfig logging; + // Initialize config definition + void + initializeConfig(Configuration& conf); + + // Load a config from the given file bool Load(const char* fname); + // Load a config from the given string bool LoadFromStr(string_view str); - llarp_config* - Copy() const; + std::string + generateBaseClientConfig(); + + std::string + generateBaseRouterConfig(); }; fs::path GetDefaultConfigDir(); + fs::path + GetDefaultConfigFilename(); + fs::path GetDefaultConfigPath(); -} // namespace llarp + void + ensureConfig(const fs::path& dir, const fs::path& filename, bool overwrite, bool asRouter); -void -llarp_generic_ensure_config(std::ofstream& f, std::string basepath, bool isRouter); +} // namespace llarp void llarp_ensure_router_config(std::ofstream& f, std::string basepath);