diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 406c90d00..0a170e78c 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -72,6 +72,9 @@ namespace llarp std::optional m_enableProfiling; std::string m_routerProfilesFile; std::string m_strictConnect; + std::string m_ifname; + std::string m_ifaddr; + std::string m_localDNS; FreehandOptions m_options; void diff --git a/llarp/exit/context.cpp b/llarp/exit/context.cpp index 07df13d92..7e98898c4 100644 --- a/llarp/exit/context.cpp +++ b/llarp/exit/context.cpp @@ -97,8 +97,13 @@ namespace llarp } bool - Context::AddExitEndpoint(const std::string& name, const Config_t& conf) + Context::AddExitEndpoint( + const std::string& name, const NetworkConfig& networkConfig, const DnsConfig& dnsConfig) { + throw std::runtime_error("FIXME"); + /* + * TODO: no more SetOption() + * // check for duplicate exit by name { auto itr = m_Exits.find(name); @@ -132,6 +137,7 @@ namespace llarp } m_Exits.emplace(name, std::move(endpoint)); return true; + */ } } // namespace exit diff --git a/llarp/exit/context.hpp b/llarp/exit/context.hpp index dbd58bf4b..df57b95da 100644 --- a/llarp/exit/context.hpp +++ b/llarp/exit/context.hpp @@ -32,7 +32,8 @@ namespace llarp Stop(); bool - AddExitEndpoint(const std::string& name, const Config_t& config); + AddExitEndpoint( + const std::string& name, const NetworkConfig& networkConfig, const DnsConfig& dnsConfig); bool ObtainNewExit(const PubKey& remote, const PathID_t& path, bool permitInternet); diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index e0598b8c2..80f7f03cc 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -375,6 +375,9 @@ namespace llarp bool Router::FromConfig(Config* conf) { + networkConfig = conf->network; + dnsConfig = conf->dns; + // Set netid before anything else if (!conf->router.m_netId.empty() && strcmp(conf->router.m_netId.c_str(), llarp::DEFAULT_NETID)) { @@ -421,36 +424,25 @@ namespace llarp m_isServiceNode = true; } + /// build a set of strictConnectPubkeys ( + /// TODO: make this consistent with config -- do we support multiple strict connections + // or not? std::set strictConnectPubkeys; - - if (!conf->network.m_strictConnect.empty()) + if (not networkConfig.m_strictConnect.empty()) { - const auto& val = conf->network.m_strictConnect; + const auto& val = networkConfig.m_strictConnect; if (IsServiceNode()) - { throw std::runtime_error("cannot use strict-connect option as service node"); - } + + // try as a RouterID and as a PubKey, convert to RouterID if needed llarp::RouterID snode; llarp::PubKey pk; if (pk.FromString(val)) - { - if (strictConnectPubkeys.emplace(pk).second) - llarp::LogInfo("added ", pk, " to strict connect list"); - else - llarp::LogWarn("duplicate key for strict connect: ", pk); - } + strictConnectPubkeys.emplace(pk); else if (snode.FromString(val)) - { - if (strictConnectPubkeys.insert(snode).second) - { - llarp::LogInfo("added ", snode, " to strict connect list"); - netConfig.emplace("strict-connect", val); - } - else - llarp::LogWarn("duplicate key for strict connect: ", snode); - } + strictConnectPubkeys.insert(snode); else - llarp::LogError("invalid key for strict-connect: ", val); + throw std::invalid_argument(stringify("invalid key for strict-connect: ", val)); } std::vector configRouters = conf->connect.routers; @@ -583,14 +575,23 @@ namespace llarp enableRPCServer = conf->api.m_enableRPCServer; rpcBindAddr = conf->api.m_rpcBindAddr; - // load SNApps - for (const auto& pairs : conf->snapps) + // create endpoint + // TODO: ensure that preconditions are met, e.g. net ifname/ifaddr/etc is sane + if (conf->snapps.size() > 1) { - // TODO: this was previously incorporating the "sane defaults" for netConfig - // netConfig should be refactored to use strongly typed member variables - // instead of an ad-hoc multimap - const EndpointConfig& endpointConfig = pairs.second; - hiddenServiceContext().AddEndpoint(endpointConfig); + // TODO: refactor config to only allow one + throw std::runtime_error("Only one endpoint allowed (including SNApps"); + } + else if (conf->snapps.size() == 1) + { + const auto itr = conf->snapps.begin(); + const EndpointConfig& endpointConfig = itr->second; + hiddenServiceContext().AddEndpoint(endpointConfig, networkConfig); + } + else + { + // TODO: service context had logic that if keyfile was empty, reachable is false + hiddenServiceContext().AddEndpoint({}, networkConfig); } // Logging config @@ -854,27 +855,18 @@ namespace llarp _rcLookupHandler.SetRouterWhitelist(routers); } - /// this function ensure there are sane defualts in a net config - static void - EnsureNetConfigDefaultsSane(std::unordered_multimap& netConfig) + /// Populates some default values on networkConfig if they are absent + void + Router::PopulateNetworkConfigDefaults() { - static const std::unordered_map> - netConfigDefaults = {{"ifname", llarp::FindFreeTun}, - {"ifaddr", llarp::FindFreeRange}, - {"local-dns", []() -> std::string { return "127.0.0.1:53"; }}}; - // populate with fallback defaults if values not present - auto itr = netConfigDefaults.begin(); - while (itr != netConfigDefaults.end()) - { - auto found = netConfig.find(itr->first); - if (found == netConfig.end() || found->second.empty()) - { - auto val = itr->second(); - if (!val.empty()) - netConfig.emplace(itr->first, std::move(val)); - } - ++itr; - } + if (networkConfig.m_ifname.empty()) + networkConfig.m_ifname = llarp::FindFreeTun(); + + if (networkConfig.m_ifaddr.empty()) + networkConfig.m_ifaddr = llarp::FindFreeRange(); + + if (networkConfig.m_localDNS.empty()) + networkConfig.m_localDNS = "127.0.0.1:53"; } bool @@ -1001,7 +993,7 @@ namespace llarp return false; } - EnsureNetConfigDefaultsSane(netConfig); + PopulateNetworkConfigDefaults(); if (IsServiceNode()) { @@ -1031,12 +1023,6 @@ namespace llarp LogError("failed to regenerate keys and sign RC"); return false; } - - if (!CreateDefaultHiddenService()) - { - LogError("failed to set up default network endpoint"); - return false; - } } LogInfo("starting hidden service context..."); @@ -1181,7 +1167,7 @@ namespace llarp LogInfo("accepting transit traffic"); paths.AllowTransit(); llarp_dht_allow_transit(dht()); - return _exitContext.AddExitEndpoint("default-connectivity", netConfig); + return _exitContext.AddExitEndpoint("default-connectivity", networkConfig, dnsConfig); } bool @@ -1248,13 +1234,6 @@ namespace llarp stringify("Failed to init AF_INET and AF_INET6 on port ", m_OutboundPort)); } - bool - Router::CreateDefaultHiddenService() - { - // add endpoint - return hiddenServiceContext().AddDefaultEndpoint(netConfig); - } - void Router::MessageSent(const RouterID& remote, SendStatus status) { diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index 4fb47c43a..f9953d6ef 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -25,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -42,11 +44,6 @@ #include #include -namespace llarp -{ - struct Config; -} // namespace llarp - namespace llarp { struct Router final : public AbstractRouter @@ -225,29 +222,27 @@ namespace llarp return now <= _lastTick || (now - _lastTick) <= llarp_time_t{30000}; } - using NetConfig_t = std::unordered_multimap; - - /// default network config for default network interface - NetConfig_t netConfig; - /// bootstrap RCs BootstrapList bootstrapRCList; bool ExitEnabled() const { + throw std::runtime_error("FIXME: this needs to be derived from config"); + /* // TODO: use equal_range ? auto itr = netConfig.find("exit"); if (itr == netConfig.end()) return false; return IsTrueValue(itr->second.c_str()); + */ } void PumpLL() override; - bool - CreateDefaultHiddenService(); + NetworkConfig networkConfig; + DnsConfig dnsConfig; const std::string DefaultRPCBindAddr = "127.0.0.1:1190"; bool enableRPCServer = false; @@ -499,21 +494,14 @@ namespace llarp bool UpdateOurRC(bool rotateKeys = false); - template - void - mergeHiddenServiceConfig(const Config& in, Config& out) - { - for (const auto& item : netConfig) - out.push_back({item.first, item.second}); - for (const auto& item : in) - out.push_back({item.first, item.second}); - } - bool FromConfig(Config* conf); void MessageSent(const RouterID& remote, SendStatus status); + + void + PopulateNetworkConfigDefaults(); }; } // namespace llarp diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index 1effd24a4..17e29e0ee 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -135,32 +135,6 @@ namespace llarp #endif } - bool - Context::AddDefaultEndpoint(const std::unordered_multimap&) - { - throw std::runtime_error("FIXME"); - /* - Config::section_values_t configOpts; - configOpts.push_back({"type", DefaultEndpointType()}); - // non reachable by default as this is the default endpoint - // but only if no keyfile option provided - if (opts.count("keyfile") == 0) - { - configOpts.push_back({"reachable", "false"}); - } - - { - auto itr = opts.begin(); - while (itr != opts.end()) - { - configOpts.push_back({itr->first, itr->second}); - ++itr; - } - } - return AddEndpoint({"default", configOpts}); - */ - } - bool Context::StartAll() { @@ -198,7 +172,8 @@ namespace llarp } void - Context::AddEndpoint(const EndpointConfig& conf, bool autostart) + Context::AddEndpoint( + const EndpointConfig& conf, const NetworkConfig& networkConfig, bool autostart) { if (m_Endpoints.find(conf.m_name) != m_Endpoints.end()) throw std::invalid_argument(stringify("Snapp ", conf.m_name, " already exists")); diff --git a/llarp/service/context.hpp b/llarp/service/context.hpp index 5c743c586..de2dce464 100644 --- a/llarp/service/context.hpp +++ b/llarp/service/context.hpp @@ -35,13 +35,10 @@ namespace llarp void ForEachService(std::function visit) const; - /// add default endpoint with options - bool - AddDefaultEndpoint(const std::unordered_multimap& opts); - /// add endpoint via config void - AddEndpoint(const EndpointConfig& conf, bool autostart = false); + AddEndpoint( + const EndpointConfig& conf, const NetworkConfig& networkConfig, bool autostart = false); /// inject endpoint instance void diff --git a/test/exit/test_llarp_exit_context.cpp b/test/exit/test_llarp_exit_context.cpp index 6a0d4b699..b91ab26ee 100644 --- a/test/exit/test_llarp_exit_context.cpp +++ b/test/exit/test_llarp_exit_context.cpp @@ -1,7 +1,9 @@ #include +#include #include #include +#include "config/config.hpp" #include @@ -22,12 +24,16 @@ TEST_F(ExitTest, AddMultipleIP) llarp::PathID_t firstPath, secondPath; firstPath.Randomize(); secondPath.Randomize(); - llarp::exit::Context::Config_t conf; - conf.emplace("exit", "true"); - conf.emplace("type", "null"); - conf.emplace("ifaddr", "10.0.0.1/24"); - ASSERT_TRUE(context.AddExitEndpoint("test-exit", conf)); + // TODO: exit and type + // llarp::exit::Context::Config_t conf; + // conf.emplace("exit", "true"); + // conf.emplace("type", "null"); + + llarp::NetworkConfig networkConfig; + networkConfig.m_ifaddr = "10.0.0.1/24"; + + ASSERT_TRUE(context.AddExitEndpoint("test-exit", networkConfig, {})); ASSERT_TRUE(context.ObtainNewExit(pk, firstPath, true)); ASSERT_TRUE(context.ObtainNewExit(pk, secondPath, true)); ASSERT_TRUE(context.FindEndpointForPath(firstPath)->LocalIP()