From dd9ab0f1d5d37bd84790a828927fa2c20133a3ad Mon Sep 17 00:00:00 2001 From: Stephen Shelton Date: Thu, 30 Apr 2020 13:40:20 -0600 Subject: [PATCH] Remove ability to have multiple endpoints/snodes from config --- llarp/config/config.cpp | 125 ++++++++----------------------- llarp/config/config.hpp | 8 +- llarp/handlers/null.hpp | 4 +- llarp/handlers/tun.cpp | 9 +-- llarp/handlers/tun.hpp | 1 - llarp/router/router.cpp | 19 +---- llarp/service/context.cpp | 59 ++++++--------- llarp/service/context.hpp | 5 +- llarp/service/endpoint.cpp | 10 +-- llarp/service/endpoint.hpp | 7 +- llarp/service/endpoint_state.cpp | 1 - llarp/service/endpoint_state.hpp | 1 - 12 files changed, 76 insertions(+), 173 deletions(-) diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index 3d203ac37..13564ccc3 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -361,69 +361,49 @@ namespace llarp { (void)params; - static constexpr bool ReachableDefault = true; + static constexpr bool ReachableDefault = false; static constexpr int HopsDefault = 4; static constexpr int PathsDefault = 6; - static constexpr bool BundleRCDefault = false; - static constexpr int MinLatencyDefault = 5000; - if (m_name.empty()) - throw std::runtime_error("Cannot create EndpointConfig with empty name"); - - const std::string section = m_name + "-snapp"; - - conf.defineOption(section, "keyfile", false, "", [this](std::string arg) { - // TODO: validate as valid .loki / .snode address - m_keyfile = arg; - }); - - // TODO: m_endpointType -- this is used downstream, but was it ever supported in config file? + conf.defineOption( + "endpoint", "keyfile", false, "", [this](std::string arg) { m_keyfile = arg; }); conf.defineOption( - section, "reachable", false, ReachableDefault, AssignmentAcceptor(m_reachable)); + "endpoint", "reachable", false, ReachableDefault, AssignmentAcceptor(m_reachable)); - conf.defineOption(section, "hops", false, HopsDefault, [this](int arg) { + conf.defineOption("endpoint", "hops", false, HopsDefault, [this](int arg) { if (arg < 1 or arg > 8) - throw std::invalid_argument("[snapp]:hops must be >= 1 and <= 8"); + throw std::invalid_argument("[endpoint]:hops must be >= 1 and <= 8"); }); - conf.defineOption(section, "paths", false, PathsDefault, [this](int arg) { + conf.defineOption("endpoint", "paths", false, PathsDefault, [this](int arg) { if (arg < 1 or arg > 8) - throw std::invalid_argument("[snapp]:paths must be >= 1 and <= 8"); + throw std::invalid_argument("[endpoint]:paths must be >= 1 and <= 8"); }); - conf.defineOption(section, "exit-node", false, "", [this](std::string arg) { +#ifdef LOKINET_EXITS + conf.defineOption("endpoint", "exit-node", false, "", [this](std::string arg) { // TODO: validate as valid .loki / .snode address + // probably not .snode...? m_exitNode = arg; }); +#endif - conf.defineOption(section, "local-dns", false, "", [this](std::string arg) { - // TODO: validate as IP address - m_localDNS = arg; - }); - - conf.defineOption(section, "upstream-dns", false, "", [this](std::string arg) { - // TODO: validate as IP address - m_upstreamDNS = arg; - }); - - conf.defineOption(section, "mapaddr", false, "", [this](std::string arg) { + conf.defineOption("endpoint", "mapaddr", false, "", [this](std::string arg) { // TODO: parse / validate as loki_addr : IP addr pair m_mapAddr = arg; }); - conf.defineOption( - section, "bundle-rc", false, BundleRCDefault, AssignmentAcceptor(m_bundleRC)); - - conf.defineOption(section, "blacklist-snode", true, "", [this](std::string arg) { - RouterID id; - if (not id.FromString(arg)) - throw std::invalid_argument(stringify("Invalide RouterID: ", arg)); + conf.defineOption( + "endpoint", "blacklist-snode", false, true, "", [this](std::string arg) { + RouterID id; + if (not id.FromString(arg)) + throw std::invalid_argument(stringify("Invalide RouterID: ", arg)); - auto itr = m_snodeBlacklist.emplace(std::move(id)); - if (itr.second) - throw std::invalid_argument(stringify("Duplicate blacklist-snode: ", arg)); - }); + auto itr = m_snodeBlacklist.emplace(std::move(id)); + if (itr.second) + throw std::invalid_argument(stringify("Duplicate blacklist-snode: ", arg)); + }); } bool @@ -445,30 +425,6 @@ namespace llarp return false; } - // first pass: find any "foo-snapp" sections and configure them as config options - // this will cause the ConfigDefinition to handle them as values are fed to it during - // the second pass below - parser.IterAll([&](std::string_view section, const SectionValues_t& values) { - (void)values; - - const static string_view suffix = "-snapp"; - if (section.size() > suffix.size()) - { - string_view snappName = section.substr(0, section.size() - suffix.size()); - string_view ending = section.substr(snappName.size()); - - if (ending == suffix) - { - EndpointConfig snappConf; - snappConf.m_name = str(snappName); - snappConf.defineConfigOptions(conf, params); - - snapps[str(snappName)] = std::move(snappConf); - } - } - }); - - // second pass: feed all k:v pairs to ConfigDefinition for processing parser.IterAll([&](std::string_view section, const SectionValues_t& values) { for (const auto& pair : values) { @@ -526,6 +482,7 @@ namespace llarp lokid.defineConfigOptions(conf, params); bootstrap.defineConfigOptions(conf, params); logging.defineConfigOptions(conf, params); + endpoint.defineConfigOptions(conf, params); } void @@ -795,13 +752,13 @@ namespace llarp // snapp def.addSectionComments( - "example-snapp", + "endpoint", { "Snapp settings", }); def.addOptionComments( - "example-snapp", + "endpoint", "keyfile", { "The private key to persist address with. If not specified the address will be", @@ -810,60 +767,44 @@ namespace llarp // TODO: is this redundant with / should be merged with basic client config? def.addOptionComments( - "example-snapp", + "endpoint", "reachable", { "Determines whether we will publish our snapp's introset to the DHT.", }); - // TODO: merge with client conf? def.addOptionComments( - "example-snapp", + "endpoint", "hops", { "Number of hops in a path. Min 1, max 8.", }); - // TODO: is this actually different than client's paths min/max config? def.addOptionComments( - "example-snapp", + "endpoint", "paths", { "Number of paths to maintain at any given time.", }); def.addOptionComments( - "example-snapp", + "endpoint", "blacklist-snode", { "Adds a `.snode` address to the blacklist.", }); +#ifdef LOKINET_EXITS def.addOptionComments( - "example-snapp", + "endpoint", "exit-node", { "Specify a `.snode` or `.loki` address to use as an exit broker.", }); - - // TODO: merge with client conf? - def.addOptionComments( - "example-snapp", - "local-dns", - { - "Address to bind local DNS resolver to. Ex: `127.3.2.1:53`. Iif port is omitted, port", - }); - - def.addOptionComments( - "example-snapp", - "upstream-dns", - { - "Address to forward non-lokinet related queries to. If not set, lokinet DNS will reply", - "with `srvfail`.", - }); +#endif def.addOptionComments( - "example-snapp", + "endpoint", "mapaddr", { "Permanently map a `.loki` address to an IP owned by the snapp. Example:", diff --git a/llarp/config/config.hpp b/llarp/config/config.hpp index 83ccdaa40..e54d1481a 100644 --- a/llarp/config/config.hpp +++ b/llarp/config/config.hpp @@ -153,17 +153,15 @@ namespace llarp struct EndpointConfig { - std::string m_name; std::string m_keyfile; std::string m_endpointType; bool m_reachable; int m_hops; int m_paths; - bool m_bundleRC; std::set m_snodeBlacklist; +#ifdef LOKINET_EXITS std::string m_exitNode; - std::string m_localDNS; - std::string m_upstreamDNS; +#endif std::string m_mapAddr; // TODO: @@ -186,7 +184,7 @@ namespace llarp LokidConfig lokid; BootstrapConfig bootstrap; LoggingConfig logging; - std::unordered_map snapps; + EndpointConfig endpoint; // Initialize config definition void diff --git a/llarp/handlers/null.hpp b/llarp/handlers/null.hpp index 84dab0ab0..2e64542f9 100644 --- a/llarp/handlers/null.hpp +++ b/llarp/handlers/null.hpp @@ -10,8 +10,8 @@ namespace llarp struct NullEndpoint final : public llarp::service::Endpoint, public std::enable_shared_from_this { - NullEndpoint(const EndpointConfig& conf, AbstractRouter* r, llarp::service::Context* parent) - : llarp::service::Endpoint(conf, r, parent) + NullEndpoint(AbstractRouter* r, llarp::service::Context* parent) + : llarp::service::Endpoint(r, parent) { } diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 04fb78bd6..44b8cb7d8 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -49,11 +49,10 @@ namespace llarp } } - TunEndpoint::TunEndpoint( - const EndpointConfig& conf, AbstractRouter* r, service::Context* parent, bool lazyVPN) - : service::Endpoint(conf, r, parent) - , m_UserToNetworkPktQueue(conf.m_name + "_sendq", r->netloop(), r->netloop()) - , m_NetworkToUserPktQueue(conf.m_name + "_recvq", r->netloop(), r->netloop()) + TunEndpoint::TunEndpoint(AbstractRouter* r, service::Context* parent, bool lazyVPN) + : service::Endpoint(r, parent) + , m_UserToNetworkPktQueue("endpoint_sendq", r->netloop(), r->netloop()) + , m_NetworkToUserPktQueue("endpoint_recvq", r->netloop(), r->netloop()) , m_Resolver(std::make_shared( r->netloop(), r->logic(), r->netloop(), r->logic(), this)) { diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index f00794915..1c572cb56 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -21,7 +21,6 @@ namespace llarp public std::enable_shared_from_this { TunEndpoint( - const EndpointConfig& conf, AbstractRouter* r, llarp::service::Context* parent, bool lazyVPN = false); diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 862e0dcbb..918d93312 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -575,24 +575,7 @@ namespace llarp enableRPCServer = conf->api.m_enableRPCServer; rpcBindAddr = conf->api.m_rpcBindAddr; - // create endpoint - // TODO: ensure that preconditions are met, e.g. net ifname/ifaddr/etc is sane - if (conf->snapps.size() > 1) - { - // 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); - } + hiddenServiceContext().AddEndpoint(conf->endpoint); // Logging config LogContext::Instance().Initialize( diff --git a/llarp/service/context.cpp b/llarp/service/context.cpp index 17e29e0ee..44e6949b8 100644 --- a/llarp/service/context.cpp +++ b/llarp/service/context.cpp @@ -13,25 +13,25 @@ namespace llarp { namespace { - using EndpointConstructor = std::function; + using EndpointConstructor = + std::function; using EndpointConstructors = std::map; static EndpointConstructors endpointConstructors = { {"tun", - [](const EndpointConfig& conf, AbstractRouter* r, service::Context* c) { - return std::make_shared(conf, r, c, false); + [](AbstractRouter* r, service::Context* c) { + return std::make_shared(r, c, false); }}, {"android", - [](const EndpointConfig& conf, AbstractRouter* r, service::Context* c) { - return std::make_shared(conf, r, c, true); + [](AbstractRouter* r, service::Context* c) { + return std::make_shared(r, c, true); }}, {"ios", - [](const EndpointConfig& conf, AbstractRouter* r, service::Context* c) { - return std::make_shared(conf, r, c, true); + [](AbstractRouter* r, service::Context* c) { + return std::make_shared(r, c, true); }}, - {"null", [](const EndpointConfig& conf, AbstractRouter* r, service::Context* c) { - return std::make_shared(conf, r, c); + {"null", [](AbstractRouter* r, service::Context* c) { + return std::make_shared(r, c); }}}; } // namespace @@ -172,30 +172,25 @@ namespace llarp } void - Context::AddEndpoint( - const EndpointConfig& conf, const NetworkConfig& networkConfig, bool autostart) + Context::AddEndpoint(const EndpointConfig& conf, bool autostart) { - if (m_Endpoints.find(conf.m_name) != m_Endpoints.end()) - throw std::invalid_argument(stringify("Snapp ", conf.m_name, " already exists")); + // TODO: refactor Context to only contain one endpoint + constexpr auto endpointName = "endpoint"; - // use specified type, fall back to default - std::string endpointType = conf.m_name; - if (endpointType.empty()) - endpointType = DefaultEndpointType(); + if (m_Endpoints.find(endpointName) != m_Endpoints.end()) + throw std::invalid_argument("service::Context only supports one endpoint now"); + + // TODO: make endpoint type configurable [again] + std::string endpointType = DefaultEndpointType(); // use factory to create endpoint - const auto itr = endpointConstructors.find(endpointType); + const auto itr = endpointConstructors.find(DefaultEndpointType()); if (itr == endpointConstructors.end()) throw std::invalid_argument(stringify("Endpoint type ", endpointType, " does not exist")); - auto service = itr->second(conf, m_Router, this); + auto service = itr->second(m_Router, this); if (not service) - throw std::runtime_error(stringify( - "Failed to create endpoint service for ", - conf.m_name, - "(type: ", - conf.m_endpointType, - ")")); + throw std::runtime_error(stringify("Failed to construct endpoint of type ", endpointType)); // pass conf to service service->Configure(conf); @@ -203,21 +198,13 @@ namespace llarp // autostart if requested if (autostart) { - // start if (service->Start()) - { LogInfo("autostarting hidden service endpoint ", service->Name()); - m_Endpoints.emplace(conf.m_name, service); - } else - { - throw std::runtime_error( - stringify("failed to start hidden service endpoint ", conf.m_name)); - } + throw std::runtime_error("failed to start hidden service endpoint"); } - LogInfo("added hidden service endpoint ", service->Name()); - m_Endpoints.emplace(conf.m_name, service); + m_Endpoints.emplace(endpointName, service); } } // namespace service } // namespace llarp diff --git a/llarp/service/context.hpp b/llarp/service/context.hpp index de2dce464..7a6430a67 100644 --- a/llarp/service/context.hpp +++ b/llarp/service/context.hpp @@ -13,6 +13,8 @@ namespace llarp namespace service { /// holds all the hidden service endpoints we own + /// TODO: this should be refactored (removed entirely...?) now that lokinet + /// only supports one endpoint per instance struct Context { explicit Context(AbstractRouter* r); @@ -37,8 +39,7 @@ namespace llarp /// add endpoint via config void - AddEndpoint( - const EndpointConfig& conf, const NetworkConfig& networkConfig, bool autostart = false); + AddEndpoint(const EndpointConfig& conf, bool autostart = false); /// inject endpoint instance void diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index cefd0ba43..a03782f56 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -33,12 +33,12 @@ namespace llarp { namespace service { - Endpoint::Endpoint(const EndpointConfig& conf, AbstractRouter* r, Context* parent) + Endpoint::Endpoint(AbstractRouter* r, Context* parent) : path::Builder(r, 3, path::default_len), context(parent), m_RecvQueue(128) { m_state = std::make_unique(); m_state->m_Router = r; - m_state->m_Name = conf.m_name; + m_state->m_Name = "endpoint"; m_RecvQueue.enable(); } @@ -635,12 +635,6 @@ namespace llarp [](const exit::BaseSession_ptr& ptr) -> RouterID { return ptr->Endpoint(); }); } - bool - Endpoint::ShouldBundleRC() const - { - return m_state->m_BundleRC; - } - void Endpoint::PutNewOutboundContext(const service::IntroSet& introset) { diff --git a/llarp/service/endpoint.hpp b/llarp/service/endpoint.hpp index d5ab07dda..1ba328861 100644 --- a/llarp/service/endpoint.hpp +++ b/llarp/service/endpoint.hpp @@ -69,7 +69,7 @@ namespace llarp { static const size_t MAX_OUTBOUND_CONTEXT_COUNT = 4; - Endpoint(const EndpointConfig& conf, AbstractRouter* r, Context* parent); + Endpoint(AbstractRouter* r, Context* parent); ~Endpoint() override; /// return true if we are ready to recv packets from the void @@ -256,7 +256,10 @@ namespace llarp MarkAddressOutbound(const Address&) override; bool - ShouldBundleRC() const override; + ShouldBundleRC() const + { + return false; + } /// return true if we have a convotag as an exit session /// or as a hidden service session diff --git a/llarp/service/endpoint_state.cpp b/llarp/service/endpoint_state.cpp index 6d885e365..91f465d25 100644 --- a/llarp/service/endpoint_state.cpp +++ b/llarp/service/endpoint_state.cpp @@ -14,7 +14,6 @@ namespace llarp EndpointState::Configure(const EndpointConfig& conf) { m_Keyfile = conf.m_keyfile; - m_BundleRC = conf.m_bundleRC; m_SnodeBlacklist = conf.m_snodeBlacklist; // TODO: diff --git a/llarp/service/endpoint_state.hpp b/llarp/service/endpoint_state.hpp index c62db598a..1d24e4887 100644 --- a/llarp/service/endpoint_state.hpp +++ b/llarp/service/endpoint_state.hpp @@ -52,7 +52,6 @@ namespace llarp std::string m_Keyfile; std::string m_Name; std::string m_NetNS; - bool m_BundleRC = false; util::Mutex m_SendQueueMutex; // protects m_SendQueue std::deque m_SendQueue GUARDED_BY(m_SendQueueMutex);