lokinet/llarp/config/config.cpp

757 lines
18 KiB
C++
Raw Normal View History

#include <config/config.hpp>
#include <config/ini.hpp>
#include <constants/defaults.hpp>
2019-08-12 11:20:57 +00:00
#include <constants/limits.hpp>
#include <net/net.hpp>
2019-08-26 23:29:17 +00:00
#include <router_contact.hpp>
#include <util/fs.hpp>
2019-09-01 12:10:49 +00:00
#include <util/logging/logger_syslog.hpp>
#include <util/logging/logger.hpp>
#include <util/mem.hpp>
2019-09-01 12:38:03 +00:00
#include <util/meta/memfn.hpp>
#include <util/str.hpp>
#include <absl/strings/strip.h>
2019-07-13 15:13:10 +00:00
#include <cstdlib>
#include <fstream>
#include <ios>
#include <iostream>
namespace llarp
{
2019-07-13 15:13:10 +00:00
std::string
tostr(string_view val)
{
return {val.begin(), val.end()};
}
int
svtoi(string_view val)
{
auto str = tostr(val);
return std::atoi(str.c_str());
}
2019-08-26 23:29:17 +00:00
absl::optional< bool >
setOptBool(string_view val)
{
if(IsTrueValue(val))
{
return true;
}
else if(IsFalseValue(val))
{
return false;
}
return {};
}
2019-07-02 00:20:58 +00:00
void
RouterConfig::fromSection(string_view key, string_view val)
{
2019-08-07 16:33:29 +00:00
if(key == "default-protocol")
{
m_DefaultLinkProto = tostr(val);
LogInfo("overriding default link protocol to '", val, "'");
}
if(key == "netid")
{
2019-07-06 13:46:21 +00:00
if(val.size() <= NetID::size())
{
2019-07-13 15:13:10 +00:00
m_netId = tostr(val);
2019-07-12 17:39:13 +00:00
LogInfo("setting netid to '", val, "'");
}
else
{
llarp::LogError("invalid netid '", val, "', is too long");
}
}
if(key == "max-connections")
{
2019-07-13 15:13:10 +00:00
auto ival = svtoi(val);
if(ival > 0)
{
2019-07-01 23:55:02 +00:00
m_maxConnectedRouters = ival;
LogInfo("max connections set to ", m_maxConnectedRouters);
}
}
if(key == "min-connections")
{
2019-07-13 15:13:10 +00:00
auto ival = svtoi(val);
if(ival > 0)
{
2019-07-01 23:55:02 +00:00
m_minConnectedRouters = ival;
LogInfo("min connections set to ", m_minConnectedRouters);
}
}
if(key == "nickname")
{
2019-07-13 15:13:10 +00:00
m_nickname = tostr(val);
// set logger name here
2019-07-01 23:55:02 +00:00
LogContext::Instance().nodeName = nickname();
2019-07-12 17:39:13 +00:00
LogInfo("nickname set");
}
if(key == "encryption-privkey")
{
2019-07-13 15:13:10 +00:00
m_encryptionKeyfile = tostr(val);
2019-07-12 17:39:13 +00:00
LogDebug("encryption key set to ", m_encryptionKeyfile);
}
if(key == "contact-file")
{
2019-07-13 15:13:10 +00:00
m_ourRcFile = tostr(val);
2019-07-12 17:39:13 +00:00
LogDebug("rc file set to ", m_ourRcFile);
}
if(key == "transport-privkey")
{
2019-07-13 15:13:10 +00:00
m_transportKeyfile = tostr(val);
2019-07-12 17:39:13 +00:00
LogDebug("transport key set to ", m_transportKeyfile);
}
if((key == "identity-privkey" || key == "ident-privkey"))
{
2019-07-13 15:13:10 +00:00
m_identKeyfile = tostr(val);
2019-07-12 17:39:13 +00:00
LogDebug("identity key set to ", m_identKeyfile);
}
if(key == "public-address" || key == "public-ip")
{
llarp::LogInfo("public ip ", val, " size ", val.size());
if(val.size() < 17)
{
// assume IPv4
llarp::Addr a(val);
llarp::LogInfo("setting public ipv4 ", a);
2019-07-01 23:55:02 +00:00
m_addrInfo.ip = *a.addr6();
m_publicOverride = true;
}
}
if(key == "public-port")
{
llarp::LogInfo("Setting public port ", val);
2019-07-13 15:13:10 +00:00
int p = svtoi(val);
2019-07-01 23:55:02 +00:00
// Not needed to flip upside-down - this is done in llarp::Addr(const
// AddressInfo&)
m_ip4addr.sin_port = p;
m_addrInfo.port = p;
m_publicOverride = true;
}
2019-07-13 11:54:58 +00:00
if(key == "worker-threads" || key == "threads")
{
2019-07-13 15:13:10 +00:00
m_workerThreads = svtoi(val);
2019-07-13 11:54:58 +00:00
if(m_workerThreads <= 0)
{
2019-07-12 17:39:13 +00:00
LogWarn("worker threads invalid value: '", val, "' defaulting to 1");
2019-07-13 11:54:58 +00:00
m_workerThreads = 1;
}
2019-07-12 17:39:13 +00:00
else
{
LogDebug("set to use ", m_workerThreads, " worker threads");
}
}
if(key == "net-threads")
{
2019-07-13 15:13:10 +00:00
m_numNetThreads = svtoi(val);
if(m_numNetThreads <= 0)
2019-07-01 23:55:02 +00:00
{
2019-07-12 17:39:13 +00:00
LogWarn("net threads invalid value: '", val, "' defaulting to 1");
m_numNetThreads = 1;
2019-07-01 23:55:02 +00:00
}
2019-07-12 17:39:13 +00:00
else
{
LogDebug("set to use ", m_numNetThreads, " net threads");
}
}
2019-08-26 23:29:17 +00:00
if(key == "block-bogons")
{
m_blockBogons = setOptBool(val);
}
}
2019-07-02 00:20:58 +00:00
void
NetworkConfig::fromSection(string_view key, string_view val)
{
if(key == "profiling")
{
2019-08-26 23:29:17 +00:00
m_enableProfiling = setOptBool(val);
}
else if(key == "profiles")
{
2019-07-13 15:13:10 +00:00
m_routerProfilesFile = tostr(val);
2019-07-02 00:20:58 +00:00
llarp::LogInfo("setting profiles to ", routerProfilesFile());
}
else if(key == "strict-connect")
{
2019-07-13 15:13:10 +00:00
m_strictConnect = tostr(val);
}
else
{
2019-07-13 15:13:10 +00:00
m_netConfig.emplace(tostr(key), tostr(val));
}
}
2019-07-02 00:20:58 +00:00
void
NetdbConfig::fromSection(string_view key, string_view val)
{
if(key == "dir")
{
2019-07-13 15:13:10 +00:00
m_nodedbDir = tostr(val);
}
}
2019-07-02 00:20:58 +00:00
void
DnsConfig::fromSection(string_view key, string_view val)
{
if(key == "upstream")
{
llarp::LogInfo("add upstream resolver ", val);
2019-07-13 15:13:10 +00:00
netConfig.emplace("upstream-dns", tostr(val));
}
if(key == "bind")
{
llarp::LogInfo("set local dns to ", val);
2019-07-13 15:13:10 +00:00
netConfig.emplace("local-dns", tostr(val));
}
}
2019-07-02 00:20:58 +00:00
void
2019-08-07 16:33:29 +00:00
LinksConfig::fromSection(string_view key, string_view val)
{
uint16_t proto = 0;
std::unordered_set< std::string > parsed_opts;
2019-07-13 15:13:10 +00:00
std::string v = tostr(val);
std::string::size_type idx;
2019-08-26 14:29:29 +00:00
static constexpr char delimiter = ',';
do
{
2019-08-26 14:29:29 +00:00
idx = v.find_first_of(delimiter);
if(idx != std::string::npos)
{
2019-09-22 14:32:03 +00:00
std::string data = v.substr(0, idx);
absl::StripAsciiWhitespace(&data);
parsed_opts.emplace(std::move(data));
v = v.substr(idx + 1);
}
else
{
absl::StripAsciiWhitespace(&v);
parsed_opts.insert(std::move(v));
}
} while(idx != std::string::npos);
std::unordered_set< std::string > opts;
/// for each option
for(const auto &item : parsed_opts)
{
/// see if it's a number
auto port = std::atoi(item.c_str());
if(port > 0)
{
/// set port
if(proto == 0)
{
proto = port;
}
}
2019-08-07 16:33:29 +00:00
else
{
opts.insert(item);
}
}
if(key == "*")
{
2019-08-23 12:40:56 +00:00
m_OutboundLink = std::make_tuple(
"*", AF_INET, fromEnv(proto, "OUTBOUND_PORT"), std::move(opts));
}
else
{
2019-08-07 16:33:29 +00:00
m_InboundLinks.emplace_back(tostr(key), AF_INET, proto, std::move(opts));
}
}
2019-07-02 00:20:58 +00:00
void
ConnectConfig::fromSection(ABSL_ATTRIBUTE_UNUSED string_view key,
string_view val)
{
routers.emplace_back(val.begin(), val.end());
}
2019-07-02 00:20:58 +00:00
void
ServicesConfig::fromSection(string_view key, string_view val)
{
2019-07-13 15:13:10 +00:00
services.emplace_back(tostr(key), tostr(val));
}
2019-07-02 00:20:58 +00:00
void
SystemConfig::fromSection(string_view key, string_view val)
{
if(key == "pidfile")
{
2019-07-13 15:13:10 +00:00
pidfile = tostr(val);
}
}
2019-07-02 00:20:58 +00:00
void
MetricsConfig::fromSection(string_view key, string_view val)
{
if(key == "disable-metrics")
{
disableMetrics = true;
}
else if(key == "disable-metrics-log")
{
disableMetricLogs = true;
}
else if(key == "json-metrics-path")
{
2019-07-13 15:13:10 +00:00
jsonMetricsPath = tostr(val);
}
else if(key == "metric-tank-host")
{
2019-07-13 15:13:10 +00:00
metricTankHost = tostr(val);
}
else
{
// consume everything else as a metric tag
2019-07-13 15:13:10 +00:00
metricTags[tostr(key)] = tostr(val);
}
}
2019-07-02 00:20:58 +00:00
void
ApiConfig::fromSection(string_view key, string_view val)
{
if(key == "enabled")
{
m_enableRPCServer = IsTrueValue(val);
}
if(key == "bind")
{
2019-07-13 15:13:10 +00:00
m_rpcBindAddr = tostr(val);
}
if(key == "authkey")
{
// TODO: add pubkey to whitelist
}
}
2019-07-02 00:20:58 +00:00
void
LokidConfig::fromSection(string_view key, string_view val)
{
if(key == "service-node-seed")
{
2019-07-13 15:13:10 +00:00
usingSNSeed = true;
ident_keyfile = tostr(val);
}
if(key == "enabled")
{
whitelistRouters = IsTrueValue(val);
}
if(key == "jsonrpc" || key == "addr")
{
2019-07-13 15:13:10 +00:00
lokidRPCAddr = tostr(val);
}
if(key == "username")
{
2019-07-13 15:13:10 +00:00
lokidRPCUser = tostr(val);
}
if(key == "password")
{
2019-07-13 15:13:10 +00:00
lokidRPCPassword = tostr(val);
}
}
2019-07-02 00:20:58 +00:00
void
BootstrapConfig::fromSection(string_view key, string_view val)
{
if(key == "add-node")
{
routers.emplace_back(val.begin(), val.end());
}
}
2019-07-02 00:20:58 +00:00
void
LoggingConfig::fromSection(string_view key, string_view val)
{
if(key == "type" && val == "syslog")
{
// TODO(despair): write event log syslog class
#if defined(_WIN32)
LogError("syslog not supported on win32");
#else
LogInfo("Switching to syslog");
LogContext::Instance().logStream = std::make_unique< SysLogStream >();
#endif
}
if(key == "type" && val == "json")
{
m_LogJSON = true;
}
if(key == "file")
{
LogInfo("open log file: ", val);
2019-07-13 15:13:10 +00:00
std::string fname = tostr(val);
FILE *const logfile = ::fopen(fname.c_str(), "a");
if(logfile)
{
m_LogFile = logfile;
LogInfo("will log to file ", val);
}
else if(errno)
{
LogError("could not open log file at '", val, "': ", strerror(errno));
errno = 0;
}
else
{
LogError("failed to open log file at '", val,
"' for an unknown reason, bailing tf out kbai");
::abort();
}
}
}
template < typename Section, typename Config >
Section
find_section(Config &c, const std::string &name)
{
Section ret;
auto visitor = [&ret](const ConfigParser::Section_t &section) -> bool {
for(const auto &sec : section)
{
2019-07-02 00:20:58 +00:00
ret.fromSection(sec.first, sec.second);
}
return true;
};
if(c.VisitSection(name.c_str(), visitor))
2019-08-26 23:29:17 +00:00
{
return ret;
2019-08-26 23:29:17 +00:00
}
2019-07-06 17:03:40 +00:00
return {};
}
bool
Config::Load(const char *fname)
{
ConfigParser parser;
if(!parser.LoadFile(fname))
{
return false;
}
2019-07-06 13:46:21 +00:00
return parse(parser);
}
bool
2019-07-17 00:08:43 +00:00
Config::LoadFromStr(string_view str)
2019-07-06 13:46:21 +00:00
{
ConfigParser parser;
2019-07-17 08:48:13 +00:00
if(!parser.LoadFromStr(str))
2019-07-06 13:46:21 +00:00
{
return false;
}
return parse(parser);
}
bool
Config::parse(const ConfigParser &parser)
{
router = find_section< RouterConfig >(parser, "router");
network = find_section< NetworkConfig >(parser, "network");
connect = find_section< ConnectConfig >(parser, "connect");
netdb = find_section< NetdbConfig >(parser, "netdb");
dns = find_section< DnsConfig >(parser, "dns");
2019-08-07 16:33:29 +00:00
links = find_section< LinksConfig >(parser, "bind");
services = find_section< ServicesConfig >(parser, "services");
system = find_section< SystemConfig >(parser, "system");
metrics = find_section< MetricsConfig >(parser, "metrics");
api = find_section< ApiConfig >(parser, "api");
lokid = find_section< LokidConfig >(parser, "lokid");
bootstrap = find_section< BootstrapConfig >(parser, "bootstrap");
logging = find_section< LoggingConfig >(parser, "logging");
return true;
}
} // namespace llarp
/// fname should be a relative path (from CWD) or absolute path to the config
/// file
extern "C" bool
llarp_ensure_config(const char *fname, const char *basedir, bool overwrite,
bool asRouter)
{
std::error_code ec;
if(fs::exists(fname, ec) && !overwrite)
{
return true;
}
if(ec)
{
llarp::LogError(ec);
return false;
}
2019-08-26 23:29:17 +00:00
std::string basepath;
if(basedir)
{
basepath = basedir;
#ifndef _WIN32
basepath += "/";
#else
basepath += "\\";
#endif
}
llarp::LogInfo("Attempting to create config file ", fname);
// abort if config already exists
if(!asRouter)
{
if(fs::exists(fname, ec) && !overwrite)
{
llarp::LogError(fname, " currently exists, please use -f to overwrite");
return true;
}
if(ec)
{
llarp::LogError(ec);
return false;
}
}
// write fname ini
auto optional_f =
llarp::util::OpenFileStream< std::ofstream >(fname, std::ios::binary);
if(!optional_f || !optional_f.value().is_open())
{
llarp::LogError("failed to open ", fname, " for writing");
return false;
}
auto &f = optional_f.value();
2019-08-12 11:20:57 +00:00
llarp_generic_ensure_config(f, basepath, asRouter);
if(asRouter)
{
llarp_ensure_router_config(f, basepath);
}
else
{
llarp_ensure_client_config(f, basepath);
}
llarp::LogInfo("Generated new config ", fname);
return true;
}
void
2019-08-12 11:20:57 +00:00
llarp_generic_ensure_config(std::ofstream &f, std::string basepath,
bool isRouter)
{
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";
f << std::endl;
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";
// 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";
// metrics
2019-08-19 22:55:49 +00:00
f << "[metrics]\n";
f << "json-metrics-path=" << basepath << "metrics.json\n";
f << "\n\n";
f << "# admin api (disabled by default)\n";
f << "[api]\n";
f << "enabled=false\n";
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";
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-08-19 22:55:49 +00:00
f << "# dns provider configuration section\n";
f << "[dns]\n";
f << "# resolver\n";
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";
#else
2019-08-19 22:55:49 +00:00
f << "bind=127.3.2.1:53\n";
#endif
#else
2019-08-19 22:55:49 +00:00
f << "bind=127.0.0.1:53\n";
#endif
2019-08-19 22:55:49 +00:00
f << "\n\n";
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-08-19 22:55:49 +00:00
f << "# bootstrap settings\n";
f << "[bootstrap]\n";
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";
// 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";
}
void
llarp_ensure_router_config(std::ofstream &f, std::string basepath)
{
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";
f << std::endl;
2019-08-19 22:55:49 +00:00
f << "# network settings \n";
f << "[network]\n";
f << "profiles=" << basepath << "profiles.dat\n";
// 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";
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";
// get ifname
std::string ifname;
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
}
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
}
f << std::endl;
}
bool
llarp_ensure_client_config(std::ofstream &f, std::string basepath)
{
// write snapp-example.ini
const std::string snappExample_fpath = basepath + "snapp-example.ini";
{
auto stream = llarp::util::OpenFileStream< std::ofstream >(
snappExample_fpath, std::ios::binary);
if(!stream)
2019-08-26 23:29:17 +00:00
{
return false;
2019-08-26 23:29:17 +00:00
}
auto &example_f = stream.value();
if(example_f.is_open())
{
// 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";
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";
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";
}
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";
f << "#example-snapp=" << snappExample_fpath << std::endl;
2019-08-19 22:55:49 +00:00
f << "\n\n";
2019-08-19 22:55:49 +00:00
f << "# network settings \n";
f << "[network]\n";
f << "profiles=" << basepath << "profiles.dat\n";
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";
// 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";
// 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";
return true;
}