2019-07-02 21:28:28 +00:00
|
|
|
#include <config/config.hpp>
|
|
|
|
|
|
|
|
#include <config/ini.hpp>
|
|
|
|
#include <constants/defaults.hpp>
|
|
|
|
#include <net/net.hpp>
|
2019-08-26 23:29:17 +00:00
|
|
|
#include <router_contact.hpp>
|
2020-03-13 20:45:33 +00:00
|
|
|
#include <stdexcept>
|
2019-07-02 21:28:28 +00:00
|
|
|
#include <util/fs.hpp>
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger_syslog.hpp>
|
|
|
|
#include <util/logging/logger.hpp>
|
2019-07-02 21:28:28 +00:00
|
|
|
#include <util/mem.hpp>
|
|
|
|
#include <util/str.hpp>
|
2019-11-15 19:40:43 +00:00
|
|
|
#include <util/lokinet_init.h>
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2019-07-13 15:13:10 +00:00
|
|
|
#include <cstdlib>
|
2019-07-02 21:28:28 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <ios>
|
|
|
|
#include <iostream>
|
2020-03-23 20:53:42 +00:00
|
|
|
#include "ghc/filesystem.hpp"
|
2019-07-02 21:28:28 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2020-03-27 00:49:05 +00:00
|
|
|
|
|
|
|
// constants for config file default values
|
|
|
|
constexpr int DefaultMinConnectionsForRouter = 6;
|
|
|
|
constexpr int DefaultMaxConnectionsForRouter = 60;
|
|
|
|
|
|
|
|
constexpr int DefaultMinConnectionsForClient = 4;
|
|
|
|
constexpr int DefaultMaxConnectionsForClient = 6;
|
|
|
|
|
2020-03-27 21:39:40 +00:00
|
|
|
LoggingConfig::LogType
|
|
|
|
LoggingConfig::LogTypeFromString(const std::string& str)
|
2020-03-19 21:51:22 +00:00
|
|
|
{
|
|
|
|
if (str == "unknown") return LogType::Unknown;
|
|
|
|
else if (str == "file") return LogType::File;
|
|
|
|
else if (str == "json") return LogType::Json;
|
|
|
|
else if (str == "syslog") return LogType::Syslog;
|
|
|
|
|
|
|
|
return LogType::Unknown;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
RouterConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-18 18:24:23 +00:00
|
|
|
conf.defineOption<int>("router", "job-queue-size", false, m_JobQueueSize,
|
|
|
|
[this](int arg) {
|
|
|
|
if (arg < 1024)
|
|
|
|
throw std::invalid_argument("job-queue-size must be 1024 or greater");
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
m_JobQueueSize = arg;
|
|
|
|
});
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-25 22:41:22 +00:00
|
|
|
// TODO: we don't support other protocols now; remove
|
2020-03-18 18:24:23 +00:00
|
|
|
conf.defineOption<std::string>("router", "default-protocol", false, m_DefaultLinkProto,
|
|
|
|
[this](std::string arg) {
|
|
|
|
m_DefaultLinkProto = arg;
|
|
|
|
});
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
conf.defineOption<std::string>("router", "netid", true, m_netId,
|
|
|
|
[this](std::string arg) {
|
|
|
|
if(arg.size() > NetID::size())
|
|
|
|
throw std::invalid_argument(stringify(
|
|
|
|
"netid is too long, max length is ", NetID::size()));
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
m_netId = std::move(arg);
|
|
|
|
});
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-27 21:39:40 +00:00
|
|
|
int minConnections = (params.isRelay ? DefaultMinConnectionsForRouter
|
2020-03-27 00:49:05 +00:00
|
|
|
: DefaultMinConnectionsForClient);
|
|
|
|
conf.defineOption<int>("router", "min-connections", false, minConnections,
|
|
|
|
[=](int arg) {
|
|
|
|
if (arg < minConnections)
|
|
|
|
throw std::invalid_argument(stringify("min-connections must be >= ", minConnections));
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-25 22:41:22 +00:00
|
|
|
m_minConnectedRouters = arg;
|
2020-03-18 18:24:23 +00:00
|
|
|
});
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-27 21:39:40 +00:00
|
|
|
int maxConnections = (params.isRelay ? DefaultMaxConnectionsForRouter
|
2020-03-27 00:49:05 +00:00
|
|
|
: DefaultMaxConnectionsForClient);
|
|
|
|
conf.defineOption<int>("router", "max-connections", false, maxConnections,
|
|
|
|
[=](int arg) {
|
|
|
|
if (arg < maxConnections)
|
|
|
|
throw std::invalid_argument(stringify("max-connections must be >= ", maxConnections));
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-25 22:41:22 +00:00
|
|
|
m_maxConnectedRouters = arg;
|
2020-03-18 18:24:23 +00:00
|
|
|
});
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
// additional check that min <= max
|
|
|
|
// TODO: where can we perform this check now?
|
|
|
|
// if (m_minConnectedRouters > m_maxConnectedRouters)
|
|
|
|
// throw std::invalid_argument("[router]:min-connections must be less than [router]:max-connections");
|
|
|
|
|
|
|
|
conf.defineOption<std::string>("router", "nickname", false, m_nickname,
|
|
|
|
[this](std::string arg) {
|
|
|
|
m_nickname = std::move(arg);
|
|
|
|
});
|
|
|
|
|
2020-03-27 21:54:13 +00:00
|
|
|
conf.defineOption<std::string>("router", "data-dir", false, GetDefaultDataDir(),
|
2020-03-18 18:24:23 +00:00
|
|
|
[this](std::string arg) {
|
2020-03-27 21:54:13 +00:00
|
|
|
m_dataDir = std::move(arg);
|
2020-03-18 18:24:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<std::string>("router", "public-address", false, "",
|
|
|
|
[this](std::string arg) {
|
|
|
|
llarp::LogInfo("public ip ", arg, " size ", arg.size());
|
|
|
|
if(arg.size() < 17)
|
|
|
|
{
|
|
|
|
// assume IPv4
|
|
|
|
llarp::Addr a(arg);
|
|
|
|
llarp::LogInfo("setting public ipv4 ", a);
|
|
|
|
m_addrInfo.ip = *a.addr6();
|
|
|
|
m_publicOverride = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<int>("router", "public-port", false, 1090,
|
|
|
|
[this](int arg) {
|
|
|
|
if (arg <= 0)
|
|
|
|
throw std::invalid_argument("public-port must be > 0");
|
|
|
|
|
|
|
|
// Not needed to flip upside-down - this is done in llarp::Addr(const AddressInfo&)
|
|
|
|
m_ip4addr.sin_port = arg;
|
|
|
|
m_addrInfo.port = arg;
|
|
|
|
m_publicOverride = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<int>("router", "worker-threads", false, m_workerThreads,
|
|
|
|
[this](int arg) {
|
|
|
|
if (arg <= 0)
|
|
|
|
throw std::invalid_argument("worker-threads must be > 0");
|
|
|
|
|
|
|
|
m_workerThreads = arg;
|
|
|
|
});
|
|
|
|
|
2020-03-19 20:06:57 +00:00
|
|
|
conf.defineOption<int>("router", "net-threads", false, m_numNetThreads,
|
2020-03-18 18:24:23 +00:00
|
|
|
[this](int arg) {
|
|
|
|
if (arg <= 0)
|
2020-03-19 20:06:57 +00:00
|
|
|
throw std::invalid_argument("net-threads must be > 0");
|
2020-03-18 18:24:23 +00:00
|
|
|
|
|
|
|
m_numNetThreads = arg;
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<bool>("router", "block-bogons", false, m_blockBogons,
|
|
|
|
[this](bool arg) {
|
|
|
|
m_blockBogons = arg;
|
|
|
|
});
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
2019-07-02 00:20:58 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
NetworkConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
(void)params;
|
2020-03-27 01:07:45 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
// TODO: review default value
|
|
|
|
conf.defineOption<bool>("network", "profiling", false, m_enableProfiling,
|
|
|
|
[this](bool arg) {
|
|
|
|
m_enableProfiling = arg;
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<std::string>("network", "profiles", false, m_routerProfilesFile,
|
|
|
|
[this](std::string arg) {
|
|
|
|
m_routerProfilesFile = std::move(arg);
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<std::string>("network", "strict-connect", false, m_strictConnect,
|
|
|
|
[this](std::string arg) {
|
|
|
|
m_strictConnect = std::move(arg);
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: NetConfig was collecting all other k:v pairs here
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
NetdbConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
(void)params;
|
2020-03-27 01:07:45 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
conf.defineOption<std::string>("netdb", "dir", false, m_nodedbDir,
|
|
|
|
[this](std::string arg) {
|
|
|
|
m_nodedbDir = str(arg);
|
|
|
|
});
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
DnsConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
(void)params;
|
2020-03-27 01:07:45 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
// TODO: this was previously a multi-value option
|
|
|
|
conf.defineOption<std::string>("dns", "upstream", false, "",
|
|
|
|
[this](std::string arg) {
|
|
|
|
netConfig.emplace("upstream-dns", std::move(arg));
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: this was previously a multi-value option
|
|
|
|
conf.defineOption<std::string>("dns", "bind", false, "",
|
|
|
|
[this](std::string arg) {
|
|
|
|
netConfig.emplace("local-dns", std::move(arg));
|
|
|
|
});
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 16:15:00 +00:00
|
|
|
LinksConfig::LinkInfo
|
|
|
|
LinksConfig::LinkInfoFromINIValues(string_view name, string_view value)
|
|
|
|
{
|
|
|
|
// we treat the INI k:v pair as:
|
|
|
|
// k: interface name, * indicating outbound
|
|
|
|
// v: a comma-separated list of values, an int indicating port (everything else ignored)
|
|
|
|
// this is somewhat of a backwards- and forwards-compatibility thing
|
|
|
|
|
|
|
|
LinkInfo info;
|
|
|
|
info.addressFamily = AF_INET;
|
|
|
|
info.interface = str(name);
|
|
|
|
|
|
|
|
std::vector<string_view> splits = split(value, ',');
|
|
|
|
for (string_view str : splits)
|
|
|
|
{
|
|
|
|
int asNum = std::atoi(str.data());
|
|
|
|
if (asNum > 0)
|
|
|
|
info.port = asNum;
|
|
|
|
|
|
|
|
// otherwise, ignore ("future-proofing")
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
LinksConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
(void)params;
|
2020-03-27 01:07:45 +00:00
|
|
|
|
2020-03-23 16:15:00 +00:00
|
|
|
conf.addUndeclaredHandler("bind", [&](string_view, string_view name, string_view value) {
|
|
|
|
LinkInfo info = LinkInfoFromINIValues(name, value);
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2020-03-23 16:15:00 +00:00
|
|
|
if (info.port <= 0)
|
|
|
|
throw std::invalid_argument(stringify("Invalid [bind] port specified on interface", name));
|
|
|
|
|
|
|
|
if(name == "*")
|
|
|
|
m_OutboundLink = std::move(info);
|
2019-08-07 16:33:29 +00:00
|
|
|
else
|
2020-03-23 16:15:00 +00:00
|
|
|
m_InboundLinks.emplace_back(std::move(info));
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2020-03-23 16:15:00 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
ConnectConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
(void)params;
|
2020-03-27 01:07:45 +00:00
|
|
|
|
2020-03-23 16:15:00 +00:00
|
|
|
|
|
|
|
conf.addUndeclaredHandler("connect", [this](string_view section,
|
|
|
|
string_view name,
|
|
|
|
string_view value) {
|
|
|
|
(void)section;
|
|
|
|
(void)name;
|
|
|
|
routers.emplace_back(value);
|
|
|
|
return true;
|
|
|
|
});
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
ServicesConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
(void)params;
|
2020-03-27 01:07:45 +00:00
|
|
|
|
2020-03-23 16:15:00 +00:00
|
|
|
conf.addUndeclaredHandler("services", [this](string_view section,
|
|
|
|
string_view name,
|
|
|
|
string_view value) {
|
|
|
|
(void)section;
|
|
|
|
services.emplace_back(name, value);
|
|
|
|
return true;
|
|
|
|
});
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
SystemConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
(void)params;
|
2020-03-27 01:07:45 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
conf.defineOption<std::string>("system", "pidfile", false, pidfile,
|
|
|
|
[this](std::string arg) {
|
|
|
|
pidfile = std::move(arg);
|
|
|
|
});
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
ApiConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
(void)params;
|
2020-03-27 01:07:45 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
conf.defineOption<bool>("api", "enabled", false, m_enableRPCServer,
|
|
|
|
[this](bool arg) {
|
|
|
|
m_enableRPCServer = arg;
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<std::string>("api", "bind", false, m_rpcBindAddr,
|
|
|
|
[this](std::string arg) {
|
|
|
|
m_rpcBindAddr = std::move(arg);
|
|
|
|
});
|
|
|
|
// TODO: this was from pre-refactor:
|
2019-07-02 21:28:28 +00:00
|
|
|
// TODO: add pubkey to whitelist
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
LokidConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
(void)params;
|
2020-03-27 01:07:45 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
conf.defineOption<std::string>("lokid", "service-node-seed", false, "",
|
|
|
|
[this](std::string arg) {
|
|
|
|
if (not arg.empty())
|
|
|
|
{
|
|
|
|
usingSNSeed = true;
|
|
|
|
ident_keyfile = std::move(arg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<bool>("lokid", "enabled", false, whitelistRouters,
|
|
|
|
[this](bool arg) {
|
|
|
|
whitelistRouters = arg;
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: was also aliased as "addr" -- presumably because loki-launcher
|
|
|
|
conf.defineOption<std::string>("lokid", "jsonrpc", false, lokidRPCAddr,
|
|
|
|
[this](std::string arg) {
|
|
|
|
lokidRPCAddr = arg;
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<std::string>("lokid", "username", false, lokidRPCUser,
|
|
|
|
[this](std::string arg) {
|
|
|
|
lokidRPCUser = arg;
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<std::string>("lokid", "password", false, lokidRPCPassword,
|
|
|
|
[this](std::string arg) {
|
|
|
|
lokidRPCPassword = arg;
|
|
|
|
});
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
BootstrapConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
(void)params;
|
2020-03-27 01:07:45 +00:00
|
|
|
|
2020-03-23 16:41:23 +00:00
|
|
|
conf.addUndeclaredHandler("bootstrap", [&](string_view, string_view name, string_view value) {
|
|
|
|
if (name != "add-node")
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
routers.emplace_back(str(value));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
LoggingConfig::defineConfigOptions(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
(void)params;
|
2020-03-27 01:07:45 +00:00
|
|
|
|
2020-03-19 21:51:22 +00:00
|
|
|
conf.defineOption<std::string>("logging", "type", false, "file",
|
|
|
|
[this](std::string arg) {
|
|
|
|
LoggingConfig::LogType type = LogTypeFromString(arg);
|
|
|
|
if (type == LogType::Unknown)
|
|
|
|
throw std::invalid_argument(stringify("invalid log type: ", arg));
|
|
|
|
|
|
|
|
m_logType = type;
|
|
|
|
});
|
2020-03-18 18:24:23 +00:00
|
|
|
|
2020-03-19 21:51:22 +00:00
|
|
|
conf.defineOption<std::string>("logging", "level", false, "info",
|
2020-03-18 18:24:23 +00:00
|
|
|
[this](std::string arg) {
|
2020-03-19 21:51:22 +00:00
|
|
|
nonstd::optional<LogLevel> level = LogLevelFromString(arg);
|
|
|
|
if (not level.has_value())
|
|
|
|
throw std::invalid_argument(stringify( "invalid log level value: ", arg));
|
|
|
|
|
|
|
|
m_logLevel = level.value();
|
2020-03-18 18:24:23 +00:00
|
|
|
});
|
|
|
|
|
2020-03-19 21:51:22 +00:00
|
|
|
conf.defineOption<std::string>("logging", "file", false, "stdout",
|
|
|
|
[this](std::string arg) {
|
|
|
|
m_logFile = arg;
|
|
|
|
});
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 20:07:56 +00:00
|
|
|
bool
|
2020-03-27 21:39:40 +00:00
|
|
|
Config::Load(const char *fname, bool isRelay, fs::path defaultDataDir)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-19 20:07:56 +00:00
|
|
|
try
|
2019-08-26 23:29:17 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
ConfigGenParameters params;
|
|
|
|
params.isRelay = isRelay;
|
|
|
|
params.defaultDataDir = std::move(defaultDataDir);
|
|
|
|
|
2020-03-19 20:07:56 +00:00
|
|
|
Configuration conf;
|
2020-03-27 21:39:40 +00:00
|
|
|
initializeConfig(conf, params);
|
2019-07-06 17:03:40 +00:00
|
|
|
|
2020-03-19 20:07:56 +00:00
|
|
|
ConfigParser parser;
|
|
|
|
if(!parser.LoadFile(fname))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2020-03-19 20:07:56 +00:00
|
|
|
parser.IterAll([&](string_view section, const SectionValues_t& values) {
|
|
|
|
for (const auto& pair : values)
|
|
|
|
{
|
|
|
|
conf.addConfigValue(section, pair.first, pair.second);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: hand parsed data to conf, pull out values
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::exception& e)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-19 20:07:56 +00:00
|
|
|
LogError("Error trying to init and parse config from file: ", e.what());
|
2019-07-02 21:28:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-06 13:46:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 20:07:56 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
Config::initializeConfig(Configuration& conf, const ConfigGenParameters& params)
|
2019-07-06 13:46:21 +00:00
|
|
|
{
|
2020-03-19 20:07:56 +00:00
|
|
|
// 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");
|
|
|
|
|
2020-03-27 21:39:40 +00:00
|
|
|
router.defineConfigOptions(conf, params);
|
|
|
|
network.defineConfigOptions(conf, params);
|
|
|
|
connect.defineConfigOptions(conf, params);
|
|
|
|
netdb.defineConfigOptions(conf, params);
|
|
|
|
dns.defineConfigOptions(conf, params);
|
|
|
|
links.defineConfigOptions(conf, params);
|
|
|
|
services.defineConfigOptions(conf, params);
|
|
|
|
system.defineConfigOptions(conf, params);
|
|
|
|
api.defineConfigOptions(conf, params);
|
|
|
|
lokid.defineConfigOptions(conf, params);
|
|
|
|
bootstrap.defineConfigOptions(conf, params);
|
|
|
|
logging.defineConfigOptions(conf, params);
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-05 16:48:05 +00:00
|
|
|
fs::path
|
2020-03-27 21:39:40 +00:00
|
|
|
GetDefaultDataDir()
|
2019-10-05 16:48:05 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2019-10-07 10:15:45 +00:00
|
|
|
const fs::path homedir = fs::path(getenv("APPDATA"));
|
2019-10-05 16:48:05 +00:00
|
|
|
#else
|
2019-10-07 10:15:45 +00:00
|
|
|
const fs::path homedir = fs::path(getenv("HOME"));
|
2019-10-05 16:48:05 +00:00
|
|
|
#endif
|
2020-03-23 20:53:42 +00:00
|
|
|
return homedir / fs::path(".lokinet/");
|
2019-10-05 16:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fs::path
|
2020-03-23 20:53:42 +00:00
|
|
|
GetDefaultConfigFilename()
|
2019-10-05 16:48:05 +00:00
|
|
|
{
|
2020-03-23 20:53:42 +00:00
|
|
|
return fs::path("lokinet.ini");
|
2019-10-05 16:48:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 20:53:42 +00:00
|
|
|
fs::path
|
|
|
|
GetDefaultConfigPath()
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
return GetDefaultDataDir() / GetDefaultConfigFilename();
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 20:53:42 +00:00
|
|
|
void
|
2020-03-27 21:39:40 +00:00
|
|
|
ensureConfig(const fs::path& defaultDataDir,
|
|
|
|
const fs::path& confFile,
|
|
|
|
bool overwrite,
|
|
|
|
bool asRouter)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-23 20:53:42 +00:00
|
|
|
std::error_code ec;
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2020-03-23 20:53:42 +00:00
|
|
|
// fail to overwrite if not instructed to do so
|
2020-03-27 21:39:40 +00:00
|
|
|
if(fs::exists(confFile, ec) && !overwrite)
|
|
|
|
throw std::invalid_argument(stringify("Config file ", confFile, " already exists"));
|
2020-03-23 20:53:42 +00:00
|
|
|
|
|
|
|
if (ec) throw std::runtime_error(stringify("filesystem error: ", ec));
|
|
|
|
|
|
|
|
// create parent dir if it doesn't exist
|
2020-03-27 21:39:40 +00:00
|
|
|
if (not fs::exists(confFile.parent_path(), ec))
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
if (not fs::create_directory(confFile.parent_path()))
|
|
|
|
throw std::runtime_error(stringify("Failed to create parent directory for ", confFile));
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
2020-03-23 20:53:42 +00:00
|
|
|
if (ec) throw std::runtime_error(stringify("filesystem error: ", ec));
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2020-03-25 22:41:22 +00:00
|
|
|
llarp::LogInfo("Attempting to create config file, asRouter: ", asRouter,
|
2020-03-27 21:39:40 +00:00
|
|
|
" path: ", confFile);
|
2020-03-23 20:53:42 +00:00
|
|
|
|
|
|
|
llarp::Config config;
|
|
|
|
std::string confStr;
|
|
|
|
if (asRouter)
|
2020-03-27 21:39:40 +00:00
|
|
|
confStr = config.generateBaseRouterConfig(std::move(defaultDataDir));
|
2020-03-25 22:41:22 +00:00
|
|
|
else
|
2020-03-27 21:39:40 +00:00
|
|
|
confStr = config.generateBaseClientConfig(std::move(defaultDataDir));
|
2020-03-23 20:53:42 +00:00
|
|
|
|
|
|
|
// open a filestream
|
2020-03-27 21:39:40 +00:00
|
|
|
auto stream = llarp::util::OpenFileStream<std::ofstream>(confFile.c_str(), std::ios::binary);
|
2020-03-23 20:53:42 +00:00
|
|
|
if (not stream.has_value() or not stream.value().is_open())
|
2020-03-27 21:39:40 +00:00
|
|
|
throw std::runtime_error(stringify("Failed to open file ", confFile, " for writing"));
|
2020-03-23 20:53:42 +00:00
|
|
|
|
2020-03-25 22:41:22 +00:00
|
|
|
llarp::LogInfo("confStr: ", confStr);
|
|
|
|
|
2020-03-23 20:53:42 +00:00
|
|
|
stream.value() << confStr;
|
|
|
|
stream.value().flush();
|
|
|
|
|
2020-03-27 21:39:40 +00:00
|
|
|
llarp::LogInfo("Generated new config ", confFile);
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
2020-03-23 20:53:42 +00:00
|
|
|
|
|
|
|
std::string
|
2020-03-27 21:39:40 +00:00
|
|
|
Config::generateBaseClientConfig(fs::path defaultDataDir)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
ConfigGenParameters params;
|
|
|
|
params.isRelay = false;
|
|
|
|
params.defaultDataDir = std::move(defaultDataDir);
|
2020-03-25 22:41:22 +00:00
|
|
|
|
2020-03-27 21:39:40 +00:00
|
|
|
llarp::Configuration def;
|
|
|
|
initializeConfig(def, params);
|
2020-03-25 22:41:22 +00:00
|
|
|
|
|
|
|
// router
|
|
|
|
def.addSectionComment("router", "Configuration for routing activity.");
|
|
|
|
|
|
|
|
def.addOptionComment("router", "threads",
|
|
|
|
"The number of threads available for performing cryptographic functions.");
|
|
|
|
def.addOptionComment("router", "threads",
|
|
|
|
"The minimum is one thread, but network performance may increase with more.");
|
|
|
|
def.addOptionComment("router", "threads",
|
|
|
|
"threads. Should not exceed the number of logical CPU cores.");
|
|
|
|
|
2020-03-27 21:54:13 +00:00
|
|
|
def.addOptionComment("router", "data-dir",
|
|
|
|
"Optional directory for containing lokinet runtime data. This includes generated");
|
|
|
|
def.addOptionComment("router", "data-dir",
|
|
|
|
"private keys.");
|
2020-03-25 22:41:22 +00:00
|
|
|
|
|
|
|
// TODO: why did Kee want this, and/or what does it really do? Something about logs?
|
|
|
|
def.addOptionComment("router", "nickname", "Router nickname. Kee wanted it.");
|
|
|
|
|
|
|
|
def.addOptionComment("router", "min-connections",
|
|
|
|
"Minimum number of routers lokinet will attempt to maintain connections to.");
|
|
|
|
|
|
|
|
def.addOptionComment("router", "max-connections",
|
|
|
|
"Maximum number (hard limit) of routers lokinet will be connected to at any time.");
|
|
|
|
|
|
|
|
// logging
|
|
|
|
def.addSectionComment("logging", "logging settings");
|
|
|
|
|
|
|
|
def.addOptionComment("logging", "level",
|
|
|
|
"Minimum log level to print. Logging below this level will be ignored.");
|
|
|
|
def.addOptionComment("logging", "level", "Valid log levels, in ascending order, are:");
|
|
|
|
def.addOptionComment("logging", "level", " trace");
|
|
|
|
def.addOptionComment("logging", "level", " debug");
|
|
|
|
def.addOptionComment("logging", "level", " info");
|
|
|
|
def.addOptionComment("logging", "level", " warn");
|
|
|
|
def.addOptionComment("logging", "level", " error");
|
|
|
|
|
|
|
|
def.addOptionComment("logging", "type", "Log type (format). Valid options are:");
|
|
|
|
def.addOptionComment("logging", "type", " file - plaintext formatting");
|
|
|
|
def.addOptionComment("logging", "type", " json - json-formatted log statements");
|
|
|
|
def.addOptionComment("logging", "type", " syslog - logs directed to syslog");
|
|
|
|
|
|
|
|
// api
|
|
|
|
def.addSectionComment("api", "JSON API settings");
|
|
|
|
|
|
|
|
def.addOptionComment("api", "enabled", "Determines whether or not the JSON API is enabled.");
|
|
|
|
|
|
|
|
def.addOptionComment("api", "bind", "IP address and port to bind to.");
|
|
|
|
def.addOptionComment("api", "bind", "Recommend localhost-only for security purposes.");
|
|
|
|
|
|
|
|
// system
|
|
|
|
def.addSectionComment("system", "System setings for running lokinet.");
|
|
|
|
|
|
|
|
def.addOptionComment("system", "user", "The user which lokinet should run as.");
|
|
|
|
|
|
|
|
def.addOptionComment("system", "group", "The group which lokinet should run as.");
|
|
|
|
|
|
|
|
def.addOptionComment("system", "pidfile", "Location of the pidfile for lokinet.");
|
|
|
|
|
|
|
|
// dns
|
|
|
|
def.addSectionComment("dns", "DNS configuration");
|
|
|
|
|
|
|
|
def.addOptionComment("dns", "upstream",
|
|
|
|
"Upstream resolver to use as fallback for non-loki addresses.");
|
|
|
|
def.addOptionComment("dns", "upstream", "Multiple values accepted.");
|
|
|
|
|
|
|
|
def.addOptionComment("dns", "bind", "Address to bind to for handling DNS requests.");
|
|
|
|
def.addOptionComment("dns", "bind", "Multiple values accepted.");
|
|
|
|
|
|
|
|
// netdb
|
|
|
|
def.addSectionComment("netdb", "Configuration for lokinet's database of service nodes");
|
|
|
|
|
|
|
|
def.addOptionComment("netdb", "dir", "Root directory of netdb.");
|
|
|
|
|
|
|
|
// bootstrap
|
|
|
|
def.addSectionComment("bootstrap", "Configure nodes that will bootstrap us onto the network");
|
|
|
|
|
|
|
|
def.addOptionComment("bootstrap", "add-node",
|
|
|
|
"Specify a bootstrap file containing a signed RouterContact of a service node");
|
|
|
|
def.addOptionComment("bootstrap", "add-node",
|
|
|
|
"which can act as a bootstrap. Accepts multiple values.");
|
|
|
|
|
2020-03-27 00:55:52 +00:00
|
|
|
// network
|
|
|
|
def.addSectionComment("network", "Network settings");
|
|
|
|
|
|
|
|
def.addOptionComment("network", "profiles", "File to contain router profiles.");
|
|
|
|
|
|
|
|
def.addOptionComment("network", "strict-connect",
|
|
|
|
"Public key of a router which will act as sole first-hop. This may be used to");
|
|
|
|
def.addOptionComment("network", "strict-connect",
|
|
|
|
"provide a trusted router (consider that you are not fully anonymous with your");
|
|
|
|
def.addOptionComment("network", "strict-connect",
|
|
|
|
"first hop).");
|
|
|
|
|
|
|
|
def.addOptionComment("network", "exit-node", "Public key of an exit-node.");
|
|
|
|
|
|
|
|
def.addOptionComment("network", "ifname", "Interface name for lokinet traffic.");
|
|
|
|
|
|
|
|
def.addOptionComment("network", "ifaddr", "Local IP address for lokinet traffic.");
|
|
|
|
|
2020-03-25 22:41:22 +00:00
|
|
|
return def.generateINIConfig(true);
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
2020-03-23 20:53:42 +00:00
|
|
|
|
|
|
|
std::string
|
2020-03-27 21:39:40 +00:00
|
|
|
Config::generateBaseRouterConfig(fs::path defaultDataDir)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-27 21:39:40 +00:00
|
|
|
ConfigGenParameters params;
|
|
|
|
params.isRelay = true;
|
|
|
|
params.defaultDataDir = std::move(defaultDataDir);
|
|
|
|
|
2020-03-27 00:55:52 +00:00
|
|
|
llarp::Configuration def;
|
2020-03-27 21:39:40 +00:00
|
|
|
initializeConfig(def, params);
|
2020-03-23 20:53:42 +00:00
|
|
|
|
2020-03-27 00:55:52 +00:00
|
|
|
// lokid
|
|
|
|
def.addSectionComment("lokid", "Lokid configuration (settings for talking to lokid");
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2020-03-27 00:55:52 +00:00
|
|
|
def.addOptionComment("lokid", "enabled",
|
|
|
|
"Whether or not we should talk to lokid. Must be enabled for staked routers.");
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2020-03-27 00:55:52 +00:00
|
|
|
def.addOptionComment("lokid", "jsonrpc",
|
|
|
|
"Host and port of running lokid that we should talk to.");
|
2020-03-23 20:53:42 +00:00
|
|
|
|
2020-03-27 00:55:52 +00:00
|
|
|
// TODO: doesn't appear to be used in the codebase
|
|
|
|
def.addOptionComment("lokid", "service-node-seed", "File containing service node's seed.");
|
2020-03-23 20:53:42 +00:00
|
|
|
|
2020-03-27 00:55:52 +00:00
|
|
|
// extra [network] options
|
|
|
|
// TODO: probably better to create an [exit] section and only allow it for routers
|
|
|
|
def.addOptionComment("network", "exit",
|
|
|
|
"Whether or not we should act as an exit node. Beware that this increases demand");
|
|
|
|
def.addOptionComment("network", "exit",
|
|
|
|
"on the server and may pose liability concerns. Enable at your own risk.");
|
|
|
|
|
|
|
|
// TODO: define the order of precedence (e.g. is whitelist applied before blacklist?)
|
|
|
|
// additionally, what's default? What if I don't whitelist anything?
|
|
|
|
def.addOptionComment("network", "exit-whitelist",
|
|
|
|
"List of destination protocol:port pairs to whitelist, example: udp:*");
|
|
|
|
def.addOptionComment("network", "exit-whitelist",
|
|
|
|
"or tcp:80. Multiple values supported.");
|
|
|
|
|
|
|
|
def.addOptionComment("network", "exit-blacklist",
|
|
|
|
"Blacklist of destinations (same format as whitelist).");
|
|
|
|
|
|
|
|
return def.generateINIConfig(true);
|
2019-08-26 23:29:17 +00:00
|
|
|
}
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2020-03-27 00:55:52 +00:00
|
|
|
} // namespace llarp
|
2019-07-02 21:28:28 +00:00
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ensure_client_config(std::ofstream& f, std::string basepath)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-23 20:53:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
2020-03-27 00:55:52 +00:00
|
|
|
* TODO: remove this function. comments left as evidence of what a snapp config does
|
|
|
|
*
|
|
|
|
*
|
2019-07-02 21:28:28 +00:00
|
|
|
// write snapp-example.ini
|
|
|
|
const std::string snappExample_fpath = basepath + "snapp-example.ini";
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto stream = llarp::util::OpenFileStream<std::ofstream>(snappExample_fpath, std::ios::binary);
|
|
|
|
if (!stream)
|
2019-08-26 23:29:17 +00:00
|
|
|
{
|
2019-07-02 21:28:28 +00:00
|
|
|
return false;
|
2019-08-26 23:29:17 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
auto& example_f = stream.value();
|
|
|
|
if (example_f.is_open())
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
|
|
|
// pick ip
|
|
|
|
// don't revert me
|
|
|
|
const static std::string ip = "10.33.0.1/16";
|
2020-03-23 20:53:42 +00:00
|
|
|
|
|
|
|
// std::string ip = llarp::findFreePrivateRange();
|
|
|
|
// if(ip == "")
|
|
|
|
// {
|
|
|
|
// llarp::LogError(
|
|
|
|
// "Couldn't easily detect a private range to map lokinet onto");
|
|
|
|
// return false;
|
|
|
|
// }
|
2019-08-19 22:55:49 +00:00
|
|
|
example_f << "# this is an example configuration for a snapp\n";
|
|
|
|
example_f << "[example-snapp]\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
example_f << "# keyfile is the path to the private key of the snapp, "
|
2019-08-19 22:55:49 +00:00
|
|
|
"your .loki is tied to this key, DON'T LOSE IT\n";
|
|
|
|
example_f << "keyfile=" << basepath << "example-snap-keyfile.private\n";
|
|
|
|
example_f << "# ifaddr is the ip range to allocate to this snapp\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
example_f << "ifaddr=" << ip << std::endl;
|
|
|
|
// probably fine to leave this (and not-auto-detect it) I'm not worried
|
|
|
|
// about any collisions
|
|
|
|
example_f << "# ifname is the name to try and give to the network "
|
2019-08-19 22:55:49 +00:00
|
|
|
"interface this snap owns\n";
|
|
|
|
example_f << "ifname=snapp-tun0\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to write ", snappExample_fpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// now do up fname
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "\n\n";
|
|
|
|
f << "# snapps configuration section\n";
|
|
|
|
f << "[services]\n";
|
|
|
|
f << "# uncomment next line to enable a snapp\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
f << "#example-snapp=" << snappExample_fpath << std::endl;
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "\n\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
return true;
|
2020-03-23 20:53:42 +00:00
|
|
|
*/
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|