2019-07-02 21:28:28 +00:00
|
|
|
#include <config/config.hpp>
|
|
|
|
|
|
|
|
#include <config/ini.hpp>
|
|
|
|
#include <constants/defaults.hpp>
|
2019-08-12 11:20:57 +00:00
|
|
|
#include <constants/limits.hpp>
|
2019-07-02 21:28:28 +00:00
|
|
|
#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>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
const char*
|
2020-02-22 21:40:12 +00:00
|
|
|
lokinetEnv(string_view suffix)
|
|
|
|
{
|
|
|
|
std::string env;
|
|
|
|
env.reserve(8 + suffix.size());
|
|
|
|
env.append("LOKINET_"s);
|
|
|
|
env.append(suffix.begin(), suffix.end());
|
|
|
|
return std::getenv(env.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
fromEnv(string_view val, string_view envNameSuffix)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (const char* ptr = lokinetEnv(envNameSuffix))
|
2020-02-22 21:40:12 +00:00
|
|
|
return ptr;
|
|
|
|
return {val.begin(), val.end()};
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-04-07 18:38:56 +00:00
|
|
|
fromEnv(const int& val, string_view envNameSuffix)
|
2020-02-22 21:40:12 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (const char* ptr = lokinetEnv(envNameSuffix))
|
2020-02-22 21:40:12 +00:00
|
|
|
return std::atoi(ptr);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t
|
2020-04-07 18:38:56 +00:00
|
|
|
fromEnv(const uint16_t& val, string_view envNameSuffix)
|
2020-02-22 21:40:12 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (const char* ptr = lokinetEnv(envNameSuffix))
|
2020-02-22 21:40:12 +00:00
|
|
|
return std::atoi(ptr);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2020-04-07 18:38:56 +00:00
|
|
|
fromEnv(const size_t& val, string_view envNameSuffix)
|
2020-02-22 21:40:12 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (const char* ptr = lokinetEnv(envNameSuffix))
|
2020-02-22 21:40:12 +00:00
|
|
|
return std::atoll(ptr);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
nonstd::optional<bool>
|
|
|
|
fromEnv(const nonstd::optional<bool>& val, string_view envNameSuffix)
|
2020-02-22 21:40:12 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (const char* ptr = lokinetEnv(envNameSuffix))
|
2020-02-22 21:40:12 +00:00
|
|
|
return IsTrueValue(ptr);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2020-03-19 21:51:22 +00:00
|
|
|
LoggingConfig::LogType LoggingConfig::LogTypeFromString(const std::string& str)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
RouterConfig::defineConfigOptions(Configuration& conf)
|
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-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-18 18:24:23 +00:00
|
|
|
conf.defineOption<int>("router", "max-connections", false, m_maxConnectedRouters,
|
|
|
|
[this](int arg) {
|
|
|
|
if (arg < 1)
|
|
|
|
throw std::invalid_argument("max-connections must be >= 1");
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
m_maxConnectedRouters = arg;
|
|
|
|
});
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
conf.defineOption<int>("router", "min-connections", false, m_minConnectedRouters,
|
|
|
|
[this](int arg) {
|
|
|
|
if (arg < 1)
|
|
|
|
throw std::invalid_argument("min-connections must be >= 1");
|
2020-03-13 20:45:33 +00:00
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
m_minConnectedRouters = arg;
|
|
|
|
});
|
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);
|
|
|
|
// TODO: side effect here, no side effects in config parsing!!
|
|
|
|
LogContext::Instance().nodeName = nickname();
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<std::string>("router", "encryption-privkey", false, m_encryptionKeyfile,
|
|
|
|
[this](std::string arg) {
|
|
|
|
m_encryptionKeyfile = std::move(arg);
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<std::string>("router", "contact-file", false, m_ourRcFile,
|
|
|
|
[this](std::string arg) {
|
|
|
|
m_ourRcFile = std::move(arg);
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<std::string>("router", "transport-privkey", false, m_transportKeyfile,
|
|
|
|
[this](std::string arg) {
|
|
|
|
m_transportKeyfile = std::move(arg);
|
|
|
|
});
|
|
|
|
|
|
|
|
conf.defineOption<std::string>("router", "identity-privkey", false, m_identKeyfile,
|
|
|
|
[this](std::string arg) {
|
|
|
|
m_identKeyfile = std::move(arg);
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
NetworkConfig::defineConfigOptions(Configuration& conf)
|
2019-07-02 21:28:28 +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
|
|
|
|
NetdbConfig::defineConfigOptions(Configuration& conf)
|
2019-07-02 21:28:28 +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
|
|
|
|
DnsConfig::defineConfigOptions(Configuration& conf)
|
2019-07-02 21:28:28 +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
|
|
|
|
LinksConfig::defineConfigOptions(Configuration& conf)
|
2019-07-02 21:28:28 +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 == "*")
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-23 16:15:00 +00:00
|
|
|
info.port = fromEnv(info.port, "OUTBOUND_PORT");
|
|
|
|
m_OutboundLink = std::move(info);
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
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-08-07 16:33:29 +00:00
|
|
|
}
|
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
|
|
|
|
ConnectConfig::defineConfigOptions(Configuration& conf)
|
2019-07-02 21:28:28 +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
|
|
|
|
ServicesConfig::defineConfigOptions(Configuration& conf)
|
2019-07-02 21:28:28 +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
|
|
|
|
SystemConfig::defineConfigOptions(Configuration& conf)
|
2019-07-02 21:28:28 +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
|
|
|
|
ApiConfig::defineConfigOptions(Configuration& conf)
|
2019-07-02 21:28:28 +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
|
|
|
|
LokidConfig::defineConfigOptions(Configuration& conf)
|
2019-07-02 21:28:28 +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
|
|
|
|
BootstrapConfig::defineConfigOptions(Configuration& conf)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-18 18:24:23 +00:00
|
|
|
// TODO: multi-value
|
2020-03-13 20:45:33 +00:00
|
|
|
/*
|
|
|
|
if(key == "add-node")
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
|
|
|
routers.emplace_back(val.begin(), val.end());
|
|
|
|
}
|
2020-03-13 20:45:33 +00:00
|
|
|
*/
|
2020-03-18 18:24:23 +00:00
|
|
|
(void)conf;
|
2020-03-19 20:07:56 +00:00
|
|
|
// throw std::runtime_error("FIXME");
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 18:24:23 +00:00
|
|
|
void
|
|
|
|
LoggingConfig::defineConfigOptions(Configuration& conf)
|
2019-07-02 21:28:28 +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
|
|
|
|
Config::Load(const char *fname)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-03-19 20:07:56 +00:00
|
|
|
// TODO: DRY
|
|
|
|
try
|
2019-08-26 23:29:17 +00:00
|
|
|
{
|
2020-03-19 20:07:56 +00:00
|
|
|
Configuration conf;
|
|
|
|
initializeConfig(conf);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-07-17 00:08:43 +00:00
|
|
|
Config::LoadFromStr(string_view str)
|
2019-07-06 13:46:21 +00:00
|
|
|
{
|
2020-03-19 20:07:56 +00:00
|
|
|
// TODO: DRY
|
|
|
|
try
|
2019-07-06 13:46:21 +00:00
|
|
|
{
|
2020-03-19 20:07:56 +00:00
|
|
|
Configuration conf;
|
|
|
|
initializeConfig(conf);
|
|
|
|
|
|
|
|
ConfigParser parser;
|
|
|
|
if(!parser.LoadFromStr(str))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: hand parsed data to conf, pull out values
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::exception& e)
|
|
|
|
{
|
|
|
|
LogError("Error trying to init and parse config from string: ", e.what());
|
2019-07-06 13:46:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 20:07:56 +00:00
|
|
|
void
|
|
|
|
Config::initializeConfig(Configuration& conf)
|
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");
|
|
|
|
|
|
|
|
router.defineConfigOptions(conf);
|
|
|
|
network.defineConfigOptions(conf);
|
|
|
|
connect.defineConfigOptions(conf);
|
|
|
|
netdb.defineConfigOptions(conf);
|
|
|
|
dns.defineConfigOptions(conf);
|
|
|
|
links.defineConfigOptions(conf);
|
|
|
|
services.defineConfigOptions(conf);
|
|
|
|
system.defineConfigOptions(conf);
|
|
|
|
api.defineConfigOptions(conf);
|
|
|
|
lokid.defineConfigOptions(conf);
|
|
|
|
bootstrap.defineConfigOptions(conf);
|
|
|
|
logging.defineConfigOptions(conf);
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
2019-10-05 16:48:05 +00:00
|
|
|
fs::path
|
|
|
|
GetDefaultConfigDir()
|
|
|
|
{
|
|
|
|
#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
|
|
|
|
return homedir / fs::path(".lokinet");
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::path
|
|
|
|
GetDefaultConfigPath()
|
|
|
|
{
|
|
|
|
return GetDefaultConfigDir() / "lokinet.ini";
|
|
|
|
}
|
|
|
|
|
2019-07-02 21:28:28 +00:00
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
/// fname should be a relative path (from CWD) or absolute path to the config
|
|
|
|
/// file
|
|
|
|
extern "C" bool
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ensure_config(const char* fname, const char* basedir, bool overwrite, bool asRouter)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (Lokinet_INIT())
|
2019-11-15 19:40:43 +00:00
|
|
|
return false;
|
2019-07-02 21:28:28 +00:00
|
|
|
std::error_code ec;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fs::exists(fname, ec) && !overwrite)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ec)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
|
|
|
llarp::LogError(ec);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-26 23:29:17 +00:00
|
|
|
std::string basepath;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (basedir)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
|
|
|
basepath = basedir;
|
|
|
|
#ifndef _WIN32
|
|
|
|
basepath += "/";
|
|
|
|
#else
|
|
|
|
basepath += "\\";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp::LogInfo("Attempting to create config file ", fname);
|
|
|
|
|
|
|
|
// abort if config already exists
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!asRouter)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fs::exists(fname, ec) && !overwrite)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
|
|
|
llarp::LogError(fname, " currently exists, please use -f to overwrite");
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ec)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
|
|
|
llarp::LogError(ec);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// write fname ini
|
2020-04-07 18:38:56 +00:00
|
|
|
auto optional_f = llarp::util::OpenFileStream<std::ofstream>(fname, std::ios::binary);
|
|
|
|
if (!optional_f || !optional_f.value().is_open())
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to open ", fname, " for writing");
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
auto& f = optional_f.value();
|
2019-08-12 11:20:57 +00:00
|
|
|
llarp_generic_ensure_config(f, basepath, asRouter);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (asRouter)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
|
|
|
llarp_ensure_router_config(f, basepath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp_ensure_client_config(f, basepath);
|
|
|
|
}
|
|
|
|
llarp::LogInfo("Generated new config ", fname);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_generic_ensure_config(std::ofstream& f, std::string basepath, bool isRouter)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "# this configuration was auto generated with 'sane' defaults\n";
|
|
|
|
f << "# change these values as desired\n";
|
|
|
|
f << "\n\n";
|
|
|
|
f << "[router]\n";
|
|
|
|
f << "# number of crypto worker threads \n";
|
|
|
|
f << "threads=4\n";
|
|
|
|
f << "# path to store signed RC\n";
|
|
|
|
f << "contact-file=" << basepath << "self.signed\n";
|
|
|
|
f << "# path to store transport private key\n";
|
|
|
|
f << "transport-privkey=" << basepath << "transport.private\n";
|
|
|
|
f << "# path to store identity signing key\n";
|
|
|
|
f << "ident-privkey=" << basepath << "identity.private\n";
|
|
|
|
f << "# encryption key for onion routing\n";
|
|
|
|
f << "encryption-privkey=" << basepath << "encryption.private\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
f << std::endl;
|
2020-04-07 18:38:56 +00:00
|
|
|
f << "# uncomment following line to set router nickname to 'lokinet'" << std::endl;
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "#nickname=lokinet\n";
|
2019-08-12 11:20:57 +00:00
|
|
|
const auto limits = isRouter ? llarp::limits::snode : llarp::limits::client;
|
|
|
|
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "# maintain min connections to other routers\n";
|
2019-08-12 11:20:57 +00:00
|
|
|
f << "min-routers=" << std::to_string(limits.DefaultMinRouters) << std::endl;
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "# hard limit of routers globally we are connected to at any given "
|
|
|
|
"time\n";
|
2019-08-12 11:20:57 +00:00
|
|
|
f << "max-routers=" << std::to_string(limits.DefaultMaxRouters) << std::endl;
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "\n\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
|
|
|
|
// logging
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "[logging]\n";
|
|
|
|
f << "level=info\n";
|
|
|
|
f << "# uncomment for logging to file\n";
|
|
|
|
f << "#type=file\n";
|
|
|
|
f << "#file=/path/to/logfile\n";
|
|
|
|
f << "# uncomment for syslog logging\n";
|
|
|
|
f << "#type=syslog\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "\n\n";
|
|
|
|
|
2019-12-19 21:54:09 +00:00
|
|
|
f << "# admin api\n";
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "[api]\n";
|
2019-12-19 21:54:09 +00:00
|
|
|
f << "enabled=true\n";
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "#authkey=insertpubkey1here\n";
|
|
|
|
f << "#authkey=insertpubkey2here\n";
|
|
|
|
f << "#authkey=insertpubkey3here\n";
|
|
|
|
f << "bind=127.0.0.1:1190\n";
|
|
|
|
f << "\n\n";
|
|
|
|
|
|
|
|
f << "# system settings for privileges and such\n";
|
|
|
|
f << "[system]\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
f << "user=" << DEFAULT_LOKINET_USER << std::endl;
|
|
|
|
f << "group=" << DEFAULT_LOKINET_GROUP << std::endl;
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "pidfile=" << basepath << "lokinet.pid\n";
|
|
|
|
f << "\n\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "# dns provider configuration section\n";
|
|
|
|
f << "[dns]\n";
|
|
|
|
f << "# resolver\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
f << "upstream=" << DEFAULT_RESOLVER_US << std::endl;
|
|
|
|
|
|
|
|
// Make auto-config smarter
|
|
|
|
// will this break reproducibility rules?
|
|
|
|
// (probably)
|
|
|
|
#ifdef __linux__
|
|
|
|
#ifdef ANDROID
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "bind=127.0.0.1:1153\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
#else
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "bind=127.3.2.1:53\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
#endif
|
|
|
|
#else
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "bind=127.0.0.1:53\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
#endif
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "\n\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "# network database settings block \n";
|
|
|
|
f << "[netdb]\n";
|
|
|
|
f << "# directory for network database skiplist storage\n";
|
|
|
|
f << "dir=" << basepath << "netdb\n";
|
|
|
|
f << "\n\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "# bootstrap settings\n";
|
|
|
|
f << "[bootstrap]\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
f << "# add a bootstrap node's signed identity to the list of nodes we want "
|
2019-08-19 22:55:49 +00:00
|
|
|
"to bootstrap from\n";
|
|
|
|
f << "# if we don't have any peers we connect to this router\n";
|
|
|
|
f << "add-node=" << basepath << "bootstrap.signed\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
// we only process one of these...
|
2019-08-19 22:55:49 +00:00
|
|
|
// f << "# add another bootstrap node\n";
|
|
|
|
// f << "#add-node=/path/to/alternative/self.signed\n";
|
|
|
|
f << "\n\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_ensure_router_config(std::ofstream& f, std::string basepath)
|
2019-07-02 21:28:28 +00:00
|
|
|
{
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "# lokid settings (disabled by default)\n";
|
|
|
|
f << "[lokid]\n";
|
|
|
|
f << "enabled=false\n";
|
|
|
|
f << "jsonrpc=127.0.0.1:22023\n";
|
|
|
|
f << "#service-node-seed=/path/to/servicenode/seed\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
f << std::endl;
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "# network settings \n";
|
|
|
|
f << "[network]\n";
|
|
|
|
f << "profiles=" << basepath << "profiles.dat\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
// better to let the routers auto-configure
|
2019-08-19 22:55:49 +00:00
|
|
|
// f << "ifaddr=auto\n";
|
|
|
|
// f << "ifname=auto\n";
|
|
|
|
f << "enabled=true\n";
|
|
|
|
f << "exit=false\n";
|
|
|
|
f << "#exit-blacklist=tcp:25\n";
|
|
|
|
f << "#exit-whitelist=tcp:*\n";
|
|
|
|
f << "#exit-whitelist=udp:*\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
f << std::endl;
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "# ROUTERS ONLY: publish network interfaces for handling inbound "
|
|
|
|
"traffic\n";
|
|
|
|
f << "[bind]\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
// get ifname
|
|
|
|
std::string ifname;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (llarp::GetBestNetIF(ifname, AF_INET))
|
2019-08-26 23:29:17 +00:00
|
|
|
{
|
2019-08-19 22:55:49 +00:00
|
|
|
f << ifname << "=1090\n";
|
2019-08-26 23:29:17 +00:00
|
|
|
}
|
2019-07-02 21:28:28 +00:00
|
|
|
else
|
2019-08-26 23:29:17 +00:00
|
|
|
{
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "# could not autodetect network interface\n"
|
|
|
|
<< "#eth0=1090\n";
|
2019-08-26 23:29:17 +00:00
|
|
|
}
|
2019-07-02 21:28:28 +00:00
|
|
|
|
|
|
|
f << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
// 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";
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "# network settings \n";
|
|
|
|
f << "[network]\n";
|
|
|
|
f << "profiles=" << basepath << "profiles.dat\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
f << "# uncomment next line to add router with pubkey to list of routers we "
|
2019-08-19 22:55:49 +00:00
|
|
|
"connect directly to\n";
|
|
|
|
f << "#strict-connect=pubkey\n";
|
|
|
|
f << "# uncomment next line to use router with pubkey as an exit node\n";
|
|
|
|
f << "#exit-node=pubkey\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
|
|
|
|
// better to set them to auto then to hard code them now
|
|
|
|
// operating environment may change over time and this will help adapt
|
2019-08-19 22:55:49 +00:00
|
|
|
// f << "ifname=auto\n";
|
|
|
|
// f << "ifaddr=auto\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
|
|
|
|
// should this also be auto? or not declared?
|
|
|
|
// probably auto in case they want to set up a hidden service
|
2019-08-19 22:55:49 +00:00
|
|
|
f << "enabled=true\n";
|
2019-07-02 21:28:28 +00:00
|
|
|
return true;
|
|
|
|
}
|