lokinet/llarp/config/config.hpp

233 lines
5.7 KiB
C++
Raw Normal View History

#ifndef LLARP_CONFIG_HPP
#define LLARP_CONFIG_HPP
#include <chrono>
#include <crypto/types.hpp>
#include <router_contact.hpp>
#include <util/fs.hpp>
#include <util/str.hpp>
#include <config/ini.hpp>
#include <config/definition.hpp>
#include <constants/files.hpp>
2020-05-06 20:38:44 +00:00
#include <net/ip_address.hpp>
2020-05-21 14:18:23 +00:00
#include <net/net_int.hpp>
#include <service/address.hpp>
#include <cstdlib>
#include <functional>
#include <string>
#include <utility>
#include <vector>
#include <unordered_set>
struct llarp_config;
namespace llarp
{
using SectionValues_t = llarp::ConfigParser::SectionValues_t;
using Config_impl_t = llarp::ConfigParser::Config_impl_t;
2019-07-06 13:46:21 +00:00
2020-03-31 15:25:29 +00:00
// TODO: don't use these maps. they're sloppy and difficult to follow
/// Small struct to gather all parameters needed for config generation to reduce the number of
/// parameters that need to be passed around.
struct ConfigGenParameters
{
bool isRelay = false;
fs::path defaultDataDir;
};
struct RouterConfig
{
size_t m_minConnectedRouters = 0;
size_t m_maxConnectedRouters = 0;
2020-03-31 15:51:24 +00:00
std::string m_netId;
2019-07-01 23:55:02 +00:00
std::string m_nickname;
fs::path m_dataDir;
2019-07-01 23:55:02 +00:00
bool m_blockBogons = false;
2019-08-26 23:29:17 +00:00
2020-05-06 20:38:44 +00:00
IpAddress m_publicAddress;
int m_workerThreads = -1;
int m_numNetThreads = -1;
size_t m_JobQueueSize = 0;
2019-11-25 21:30:34 +00:00
std::string m_routerContactFile;
std::string m_encryptionKeyFile;
std::string m_identityKeyFile;
std::string m_transportKeyFile;
void
defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params);
};
struct NetworkConfig
{
std::optional<bool> m_enableProfiling;
std::string m_routerProfilesFile;
2019-07-02 00:20:58 +00:00
std::string m_strictConnect;
2020-04-27 15:24:05 +00:00
std::string m_ifname;
std::string m_ifaddr;
2020-06-08 12:42:10 +00:00
std::optional<fs::path> m_keyfile;
std::string m_endpointType;
bool m_reachable = false;
2020-06-08 12:42:10 +00:00
std::optional<int> m_Hops;
std::optional<int> m_Paths;
2020-05-21 14:18:23 +00:00
bool m_AllowExit = false;
std::set<RouterID> m_snodeBlacklist;
2020-05-21 14:18:23 +00:00
std::optional<service::Address> m_exitNode;
std::unordered_map<huint128_t, service::Address> m_mapAddrs;
2020-06-02 21:10:42 +00:00
std::optional<std::string> m_AuthUrl;
std::optional<std::string> m_AuthMethod;
// TODO:
// on-up
// on-down
// on-ready
void
defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params);
};
struct DnsConfig
{
2020-05-06 20:38:44 +00:00
IpAddress m_bind;
std::vector<IpAddress> m_upstreamDNS;
void
defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params);
};
struct LinksConfig
{
2020-03-19 22:20:45 +00:00
struct LinkInfo
{
std::string interface;
int addressFamily = -1;
uint16_t port = -1;
2020-03-19 22:20:45 +00:00
};
/// Create a LinkInfo from the given string.
/// @throws if str does not represent a LinkInfo.
LinkInfo
LinkInfoFromINIValues(std::string_view name, std::string_view value);
2019-08-07 16:33:29 +00:00
LinkInfo m_OutboundLink;
2020-03-19 22:20:45 +00:00
std::vector<LinkInfo> m_InboundLinks;
void
defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params);
};
struct ConnectConfig
{
std::vector<fs::path> routers;
void
defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params);
};
struct ApiConfig
{
bool m_enableRPCServer = false;
2020-03-31 15:51:24 +00:00
std::string m_rpcBindAddr;
void
defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params);
};
struct LokidConfig
{
bool usingSNSeed = false;
bool whitelistRouters = false;
fs::path ident_keyfile;
2020-03-31 15:51:24 +00:00
std::string lokidRPCAddr;
std::string lokidRPCUser;
std::string lokidRPCPassword;
void
defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params);
};
struct BootstrapConfig
{
2020-06-08 12:42:10 +00:00
std::vector<fs::path> routers;
/// for unit tests
bool skipBootstrap = false;
void
defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params);
};
struct LoggingConfig
{
LogType m_logType = LogType::Unknown;
LogLevel m_logLevel = eLogNone;
std::string m_logFile;
2020-03-30 16:25:02 +00:00
void
defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params);
2020-03-30 16:25:02 +00:00
};
struct Config
{
RouterConfig router;
NetworkConfig network;
ConnectConfig connect;
DnsConfig dns;
2019-08-07 16:33:29 +00:00
LinksConfig links;
ApiConfig api;
LokidConfig lokid;
BootstrapConfig bootstrap;
LoggingConfig logging;
// Initialize config definition
void
initializeConfig(ConfigDefinition& conf, const ConfigGenParameters& params);
/// Insert config entries for backwards-compatibility (e.g. so that the config system will
/// tolerate old values that are no longer accepted)
///
/// @param conf is the config to modify
void
addBackwardsCompatibleConfigOptions(ConfigDefinition& conf);
// Load a config from the given file
bool
2020-06-08 12:42:10 +00:00
Load(const fs::path fname, bool isRelay, fs::path defaultDataDir);
2019-07-06 13:46:21 +00:00
/// Load (initialize) a default config.
///
/// This delegates to the ConfigDefinition to generate a default config,
/// as though an empty config were specified.
///
/// If using Config without the intention of loading from file (or string), this is necessary
/// in order to obtain sane defaults.
///
/// @param isRelay determines whether the config will reflect that of a relay or client
/// @param dataDir is a path representing a directory to be used as the data dir
/// @return true on success, false otherwise
bool
LoadDefault(bool isRelay, fs::path dataDir);
std::string
generateBaseClientConfig(fs::path defaultDataDir);
std::string
generateBaseRouterConfig(fs::path defaultDataDir);
llarp_config*
Copy() const;
};
void
ensureConfig(
const fs::path& defaultDataDir, const fs::path& confFile, bool overwrite, bool asRouter);
} // namespace llarp
#endif