Remove NetConfig_t

pull/1246/head
Stephen Shelton 4 years ago committed by Jeff Becker
parent df01770466
commit 21ad442b55
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -72,6 +72,9 @@ namespace llarp
std::optional<bool> 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

@ -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

@ -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);

@ -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<RouterID> 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<fs::path> 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<std::string, std::string>& netConfig)
/// Populates some default values on networkConfig if they are absent
void
Router::PopulateNetworkConfigDefaults()
{
static const std::unordered_map<std::string, std::function<std::string(void)>>
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)
{

@ -4,6 +4,7 @@
#include <router/abstractrouter.hpp>
#include <bootstrap.hpp>
#include <config/config.hpp>
#include <config/key_manager.hpp>
#include <constants/link_layer.hpp>
#include <crypto/types.hpp>
@ -25,6 +26,7 @@
#include <routing/message_parser.hpp>
#include <rpc/rpc.hpp>
#include <service/context.hpp>
#include <stdexcept>
#include <util/buffer.hpp>
#include <util/fs.hpp>
#include <util/mem.hpp>
@ -42,11 +44,6 @@
#include <unordered_map>
#include <vector>
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<std::string, std::string>;
/// 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 <typename Config>
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

@ -135,32 +135,6 @@ namespace llarp
#endif
}
bool
Context::AddDefaultEndpoint(const std::unordered_multimap<std::string, std::string>&)
{
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"));

@ -35,13 +35,10 @@ namespace llarp
void
ForEachService(std::function<bool(const std::string&, const Endpoint_ptr&)> visit) const;
/// add default endpoint with options
bool
AddDefaultEndpoint(const std::unordered_multimap<std::string, std::string>& 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

@ -1,7 +1,9 @@
#include <exit/context.hpp>
#include <config/config.hpp>
#include <router/router.hpp>
#include <exit/context.hpp>
#include "config/config.hpp"
#include <gtest/gtest.h>
@ -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()

Loading…
Cancel
Save