diff --git a/daemon/lokinetctl.cpp b/daemon/lokinetctl.cpp index 98fc3ab4b..e84b2ff86 100644 --- a/daemon/lokinetctl.cpp +++ b/daemon/lokinetctl.cpp @@ -57,7 +57,7 @@ namespace curl_global_init(CURL_GLOBAL_ALL); llarp::Config config; - if (!config.Load(configFile.c_str())) + if(!config.Load(configFile.c_str(), false)) { llarp::LogError("Failed to load from config file: ", configFile); return false; diff --git a/daemon/main.cpp b/daemon/main.cpp index 71e58eb00..612797c8d 100644 --- a/daemon/main.cpp +++ b/daemon/main.cpp @@ -65,7 +65,7 @@ run_main_context(std::string conffname, llarp_main_runtime_opts opts) // this is important, can downgrade from Info though llarp::LogDebug("Running from: ", fs::current_path().string()); llarp::LogInfo("Using config file: ", conffname); - ctx = llarp_main_init(conffname.c_str()); + ctx = llarp_main_init(conffname.c_str(), opts.isRelay); int code = 1; if (ctx) { @@ -164,12 +164,18 @@ main(int argc, char* argv[]) opts.background = true; } - if (result.count("force") > 0) + if(result.count("relay") > 0) + { + opts.isRelay = true; + } + + if(result.count("force") > 0) { overwrite = true; } - if (result.count("router") > 0) + // TODO: remove this + if(result.count("router") > 0) { asRouter = true; // we should generate and exit (docker needs this, so we don't write a diff --git a/include/llarp.h b/include/llarp.h index 9b6ff2f17..9ed071167 100644 --- a/include/llarp.h +++ b/include/llarp.h @@ -26,6 +26,7 @@ extern "C" bool background = false; bool debug = false; bool singleThreaded = false; + bool isRelay = false; }; /// llarp_application config @@ -138,44 +139,44 @@ extern "C" /// allocates new config and puts it into c /// return false on failure bool - llarp_config_load_file(const char* fname, struct llarp_config** c); + llarp_config_load_file(const char *fname, struct llarp_config **c, bool isRelay); /// loads config from file by name /// uses already allocated config /// return false on failure bool - llarp_config_read_file(struct llarp_config* c, const char* f); + llarp_config_read_file(struct llarp_config *c, const char *f, bool isRelay); /// make a main context from configuration /// copies config contents - struct llarp_main* - llarp_main_init_from_config(struct llarp_config* conf); + struct llarp_main * + llarp_main_init_from_config(struct llarp_config *conf, bool isRelay); /// initialize application context and load config - static struct llarp_main* - llarp_main_init(const char* fname) + static struct llarp_main * + llarp_main_init(const char *fname, bool isRelay) { - struct llarp_main* m = 0; - struct llarp_config* conf = 0; - if (!llarp_config_load_file(fname, &conf)) + struct llarp_main *m = 0; + struct llarp_config *conf = 0; + if(!llarp_config_load_file(fname, &conf, isRelay)) return 0; if (conf == NULL) return 0; - m = llarp_main_init_from_config(conf); + m = llarp_main_init_from_config(conf, isRelay); llarp_config_free(conf); return m; } /// initialize applicatin context with all defaults - static struct llarp_main* - llarp_main_default_init() + static struct llarp_main * + llarp_main_default_init(bool isRelay) { struct llarp_main* m; struct llarp_config* conf; conf = llarp_default_config(); if (conf == 0) return 0; - m = llarp_main_init_from_config(conf); + m = llarp_main_init_from_config(conf, isRelay); llarp_config_free(conf); return m; } @@ -183,7 +184,7 @@ extern "C" /// (re)configure main context /// return true if (re)configuration was successful bool - llarp_main_configure(struct llarp_main* ptr, struct llarp_config* conf); + llarp_main_configure(struct llarp_main *ptr, struct llarp_config *conf, bool isRelay); /// return true if this main context is running /// return false otherwise diff --git a/include/llarp.hpp b/include/llarp.hpp index 1d3394cd6..209b24564 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -55,7 +55,7 @@ namespace llarp std::string nodedb_dir; bool - LoadConfig(const std::string& fname); + LoadConfig(const std::string &fname, bool isRelay); void Close(); @@ -73,7 +73,7 @@ namespace llarp HandleSignal(int sig); bool - Configure(); + Configure(bool isRelay); bool IsUp() const; @@ -113,9 +113,6 @@ namespace llarp void SigINT(); - bool - ReloadConfig(); - std::string configfile; std::string pidfile; std::unique_ptr> closeWaiter; diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index 4a040f6b9..387287feb 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -92,7 +92,7 @@ namespace llarp } void - RouterConfig::defineConfigOptions(Configuration& conf) + RouterConfig::defineConfigOptions(Configuration& conf, bool isRelay) { conf.defineOption("router", "job-queue-size", false, m_JobQueueSize, [this](int arg) { @@ -216,8 +216,10 @@ namespace llarp } void - NetworkConfig::defineConfigOptions(Configuration& conf) + NetworkConfig::defineConfigOptions(Configuration& conf, bool isRelay) { + (void)isRelay; + // TODO: review default value conf.defineOption("network", "profiling", false, m_enableProfiling, [this](bool arg) { @@ -238,8 +240,10 @@ namespace llarp } void - NetdbConfig::defineConfigOptions(Configuration& conf) + NetdbConfig::defineConfigOptions(Configuration& conf, bool isRelay) { + (void)isRelay; + conf.defineOption("netdb", "dir", false, m_nodedbDir, [this](std::string arg) { m_nodedbDir = str(arg); @@ -247,8 +251,10 @@ namespace llarp } void - DnsConfig::defineConfigOptions(Configuration& conf) + DnsConfig::defineConfigOptions(Configuration& conf, bool isRelay) { + (void)isRelay; + // TODO: this was previously a multi-value option conf.defineOption("dns", "upstream", false, "", [this](std::string arg) { @@ -288,8 +294,10 @@ namespace llarp } void - LinksConfig::defineConfigOptions(Configuration& conf) + LinksConfig::defineConfigOptions(Configuration& conf, bool isRelay) { + (void)isRelay; + conf.addUndeclaredHandler("bind", [&](string_view, string_view name, string_view value) { LinkInfo info = LinkInfoFromINIValues(name, value); @@ -312,8 +320,10 @@ namespace llarp } void - ConnectConfig::defineConfigOptions(Configuration& conf) + ConnectConfig::defineConfigOptions(Configuration& conf, bool isRelay) { + (void)isRelay; + conf.addUndeclaredHandler("connect", [this](string_view section, string_view name, @@ -326,8 +336,10 @@ namespace llarp } void - ServicesConfig::defineConfigOptions(Configuration& conf) + ServicesConfig::defineConfigOptions(Configuration& conf, bool isRelay) { + (void)isRelay; + conf.addUndeclaredHandler("services", [this](string_view section, string_view name, string_view value) { @@ -338,8 +350,10 @@ namespace llarp } void - SystemConfig::defineConfigOptions(Configuration& conf) + SystemConfig::defineConfigOptions(Configuration& conf, bool isRelay) { + (void)isRelay; + conf.defineOption("system", "pidfile", false, pidfile, [this](std::string arg) { pidfile = std::move(arg); @@ -347,8 +361,10 @@ namespace llarp } void - ApiConfig::defineConfigOptions(Configuration& conf) + ApiConfig::defineConfigOptions(Configuration& conf, bool isRelay) { + (void)isRelay; + conf.defineOption("api", "enabled", false, m_enableRPCServer, [this](bool arg) { m_enableRPCServer = arg; @@ -363,8 +379,10 @@ namespace llarp } void - LokidConfig::defineConfigOptions(Configuration& conf) + LokidConfig::defineConfigOptions(Configuration& conf, bool isRelay) { + (void)isRelay; + conf.defineOption("lokid", "service-node-seed", false, "", [this](std::string arg) { if (not arg.empty()) @@ -397,8 +415,10 @@ namespace llarp } void - BootstrapConfig::defineConfigOptions(Configuration& conf) + BootstrapConfig::defineConfigOptions(Configuration& conf, bool isRelay) { + (void)isRelay; + conf.addUndeclaredHandler("bootstrap", [&](string_view, string_view name, string_view value) { if (name != "add-node") { @@ -413,8 +433,10 @@ namespace llarp } void - LoggingConfig::defineConfigOptions(Configuration& conf) + LoggingConfig::defineConfigOptions(Configuration& conf, bool isRelay) { + (void)isRelay; + conf.defineOption("logging", "type", false, "file", [this](std::string arg) { LoggingConfig::LogType type = LogTypeFromString(arg); @@ -440,13 +462,13 @@ namespace llarp } bool - Config::Load(const char *fname) + Config::Load(const char *fname, bool isRelay) { // TODO: DRY try { Configuration conf; - initializeConfig(conf); + initializeConfig(conf, isRelay); ConfigParser parser; if(!parser.LoadFile(fname)) @@ -472,25 +494,25 @@ namespace llarp } void - Config::initializeConfig(Configuration& conf) + Config::initializeConfig(Configuration& conf, bool isRelay) { // TODO: this seems like a random place to put this, should this be closer // to main() ? if(Lokinet_INIT()) throw std::runtime_error("Can't initializeConfig() when Lokinet_INIT() == true"); - router.defineConfigOptions(conf); - network.defineConfigOptions(conf); - connect.defineConfigOptions(conf); - netdb.defineConfigOptions(conf); - dns.defineConfigOptions(conf); - links.defineConfigOptions(conf); - services.defineConfigOptions(conf); - system.defineConfigOptions(conf); - api.defineConfigOptions(conf); - lokid.defineConfigOptions(conf); - bootstrap.defineConfigOptions(conf); - logging.defineConfigOptions(conf); + router.defineConfigOptions(conf, isRelay); + network.defineConfigOptions(conf, isRelay); + connect.defineConfigOptions(conf, isRelay); + netdb.defineConfigOptions(conf, isRelay); + dns.defineConfigOptions(conf, isRelay); + links.defineConfigOptions(conf, isRelay); + services.defineConfigOptions(conf, isRelay); + system.defineConfigOptions(conf, isRelay); + api.defineConfigOptions(conf, isRelay); + lokid.defineConfigOptions(conf, isRelay); + bootstrap.defineConfigOptions(conf, isRelay); + logging.defineConfigOptions(conf, isRelay); } fs::path @@ -564,7 +586,7 @@ namespace llarp Config::generateBaseClientConfig() { llarp::Configuration def; - initializeConfig(def); + initializeConfig(def, false); // TODO: pass these in const std::string basepath = ""; @@ -683,7 +705,7 @@ namespace llarp Config::generateBaseRouterConfig() { llarp::Configuration def; - initializeConfig(def); + initializeConfig(def, true); // lokid def.addSectionComment("lokid", "Lokid configuration (settings for talking to lokid"); diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 66acea408..a3b2385ad 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -91,7 +91,7 @@ namespace llarp // clang-format on void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; class NetworkConfig @@ -114,7 +114,7 @@ namespace llarp // clang-format on void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; class NetdbConfig @@ -128,7 +128,7 @@ namespace llarp // clang-format on void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; struct DnsConfig @@ -136,7 +136,7 @@ namespace llarp std::unordered_multimap netConfig; void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; class LinksConfig @@ -165,7 +165,7 @@ namespace llarp // clang-format on void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; struct ConnectConfig @@ -173,14 +173,14 @@ namespace llarp std::vector routers; void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; struct ServicesConfig { std::vector< std::pair< std::string, std::string > > services; void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; struct SystemConfig @@ -188,7 +188,7 @@ namespace llarp std::string pidfile; void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; class ApiConfig @@ -204,7 +204,7 @@ namespace llarp // clang-format on void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; struct LokidConfig @@ -217,14 +217,14 @@ namespace llarp std::string lokidRPCPassword; void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; struct BootstrapConfig { std::vector< std::string > routers; void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; struct LoggingConfig @@ -243,7 +243,7 @@ namespace llarp std::string m_logFile; void - defineConfigOptions(Configuration& conf); + defineConfigOptions(Configuration& conf, bool isRelay); }; struct Config @@ -263,11 +263,11 @@ namespace llarp // Initialize config definition void - initializeConfig(Configuration& conf); + initializeConfig(Configuration& conf, bool isRelay); // Load a config from the given file bool - Load(const char* fname); + Load(const char* fname, bool isRelay); std::string generateBaseClientConfig(); @@ -290,10 +290,4 @@ namespace llarp } // namespace llarp -void -llarp_ensure_router_config(std::ofstream& f, std::string basepath); - -bool -llarp_ensure_client_config(std::ofstream& f, std::string basepath); - #endif diff --git a/llarp/context.cpp b/llarp/context.cpp index 9da283f43..120c9f09b 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -28,12 +28,12 @@ namespace llarp } bool - Context::Configure() + Context::Configure(bool isRelay) { // llarp::LogInfo("loading config at ", configfile); if (configfile.size()) { - if (!config->Load(configfile.c_str())) + if(!config->Load(configfile.c_str(), isRelay)) { config.release(); llarp::LogError("failed to load config file ", configfile); @@ -257,11 +257,11 @@ namespace llarp } bool - Context::LoadConfig(const std::string& fname) + Context::LoadConfig(const std::string &fname, bool isRelay) { config = std::make_unique(); configfile = fname; - return Configure(); + return Configure(isRelay); } #ifdef LOKINET_HIVE @@ -335,31 +335,31 @@ extern "C" delete conf; } - struct llarp_main* - llarp_main_init_from_config(struct llarp_config* conf) + struct llarp_main * + llarp_main_init_from_config(struct llarp_config *conf, bool isRelay) { if (conf == nullptr) return nullptr; - llarp_main* m = new llarp_main(conf); - if (m->ctx->Configure()) + llarp_main *m = new llarp_main(conf); + if(m->ctx->Configure(isRelay)) return m; delete m; return nullptr; } bool - llarp_config_read_file(struct llarp_config* conf, const char* fname) + llarp_config_read_file(struct llarp_config *conf, const char *fname, bool isRelay) { if (conf == nullptr) return false; - return conf->impl.Load(fname); + return conf->impl.Load(fname, isRelay); } bool - llarp_config_load_file(const char* fname, struct llarp_config** conf) + llarp_config_load_file(const char *fname, struct llarp_config **conf, bool isRelay) { - llarp_config* c = new llarp_config(); - if (c->impl.Load(fname)) + llarp_config *c = new llarp_config(); + if(c->impl.Load(fname, isRelay)) { *conf = c; return true; @@ -496,13 +496,13 @@ extern "C" } bool - llarp_main_configure(struct llarp_main* ptr, struct llarp_config* conf) + llarp_main_configure(struct llarp_main *ptr, struct llarp_config *conf, bool isRelay) { if (ptr == nullptr || conf == nullptr) return false; // give new config ptr->ctx->config.reset(new llarp::Config(conf->impl)); - return ptr->ctx->Configure(); + return ptr->ctx->Configure(isRelay); } bool