2017-11-28 14:05:31 +00:00
|
|
|
#include "config.hpp"
|
2018-02-01 13:21:00 +00:00
|
|
|
#include <llarp/config.h>
|
2018-08-09 13:55:51 +00:00
|
|
|
#include <llarp/defaults.h>
|
2018-07-27 00:21:57 +00:00
|
|
|
#include <llarp/net.hpp>
|
|
|
|
#include "fs.hpp"
|
2017-10-03 19:14:46 +00:00
|
|
|
#include "ini.hpp"
|
2018-07-27 00:21:57 +00:00
|
|
|
#include "logger.hpp"
|
2018-01-26 14:17:51 +00:00
|
|
|
#include "mem.hpp"
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
template < typename Config, typename Section >
|
|
|
|
static const Section &
|
|
|
|
find_section(Config &c, const std::string &name, const Section &fallback)
|
|
|
|
{
|
|
|
|
if(c.sections.find(name) == c.sections.end())
|
|
|
|
return fallback;
|
|
|
|
return c.sections[name].values;
|
2018-01-29 14:27:24 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
Config::Load(const char *fname)
|
|
|
|
{
|
|
|
|
std::ifstream f;
|
|
|
|
f.open(fname);
|
|
|
|
if(f.is_open())
|
|
|
|
{
|
|
|
|
ini::Parser parser(f);
|
|
|
|
auto &top = parser.top();
|
|
|
|
router = find_section(top, "router", section_t{});
|
|
|
|
network = find_section(top, "network", section_t{});
|
2018-06-07 16:22:49 +00:00
|
|
|
connect = find_section(top, "connect", section_t{});
|
2018-05-22 15:54:19 +00:00
|
|
|
netdb = find_section(top, "netdb", section_t{});
|
2018-07-26 10:53:27 +00:00
|
|
|
dns = find_section(top, "dns", section_t{});
|
2018-06-07 16:22:49 +00:00
|
|
|
iwp_links = find_section(top, "bind", section_t{});
|
2018-07-09 17:32:11 +00:00
|
|
|
services = find_section(top, "services", section_t{});
|
2018-08-09 13:55:51 +00:00
|
|
|
system = find_section(top, "system", section_t{});
|
2018-10-09 12:06:30 +00:00
|
|
|
api = find_section(top, "api", section_t{});
|
2018-05-22 15:54:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-02-01 13:21:00 +00:00
|
|
|
} // namespace llarp
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-11-06 14:06:09 +00:00
|
|
|
extern "C" bool
|
2018-10-01 09:57:57 +00:00
|
|
|
llarp_ensure_config(const char *fname, const char *basedir, bool overwrite,
|
|
|
|
bool asRouter)
|
2018-10-01 09:56:14 +00:00
|
|
|
{
|
|
|
|
std::error_code ec;
|
|
|
|
if(fs::exists(fname, ec) && !overwrite)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
llarp::LogError(ec);
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-01 09:57:57 +00:00
|
|
|
|
2018-10-01 09:56:14 +00:00
|
|
|
std::string basepath = "";
|
|
|
|
if(basedir)
|
|
|
|
{
|
|
|
|
basepath = basedir;
|
2018-10-02 23:11:44 +00:00
|
|
|
#ifndef _WIN32
|
2018-10-01 09:56:14 +00:00
|
|
|
basepath += "/";
|
2018-10-02 23:11:44 +00:00
|
|
|
#else
|
2018-10-03 10:42:44 +00:00
|
|
|
basepath += "\\";
|
2018-10-02 23:11:44 +00:00
|
|
|
#endif
|
2018-10-01 09:56:14 +00:00
|
|
|
}
|
2018-10-01 09:57:57 +00:00
|
|
|
|
2018-10-01 09:56:14 +00:00
|
|
|
// abort if client.ini already exists
|
2018-10-01 09:57:57 +00:00
|
|
|
if(!asRouter)
|
2018-10-01 09:56:14 +00:00
|
|
|
{
|
2018-10-01 09:57:57 +00:00
|
|
|
if(fs::exists(basepath + "client.ini", ec) && !overwrite)
|
2018-10-01 09:56:14 +00:00
|
|
|
{
|
2018-10-01 09:57:57 +00:00
|
|
|
llarp::LogError(
|
|
|
|
basepath, "client.ini currently exists, please use -f to overwrite");
|
2018-10-01 09:56:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
llarp::LogError(ec);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-10-01 09:57:57 +00:00
|
|
|
|
2018-10-01 09:56:14 +00:00
|
|
|
// write fname ini
|
|
|
|
std::ofstream f(fname);
|
|
|
|
if(!f.is_open())
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to open ", fname, " for writing");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llarp_generic_ensure_config(f, basepath);
|
2018-10-01 09:57:57 +00:00
|
|
|
if(asRouter)
|
2018-10-01 09:56:14 +00:00
|
|
|
{
|
2018-10-02 23:11:44 +00:00
|
|
|
llarp_ensure_router_config(f, basepath);
|
2018-10-01 09:56:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp_ensure_client_config(f, basepath);
|
|
|
|
}
|
|
|
|
llarp::LogInfo("Generated new config ", fname);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_generic_ensure_config(std::ofstream &f, std::string basepath)
|
|
|
|
{
|
|
|
|
f << "# this configuration was auto generated with 'sane' defaults"
|
2018-10-01 09:57:57 +00:00
|
|
|
<< std::endl;
|
2018-10-01 09:56:14 +00:00
|
|
|
f << "# change these values as desired" << std::endl;
|
|
|
|
f << std::endl << std::endl;
|
2018-10-04 13:55:29 +00:00
|
|
|
f << "[router]" << std::endl;
|
2018-10-01 09:56:14 +00:00
|
|
|
f << "# number of crypto worker threads " << std::endl;
|
|
|
|
f << "threads=4" << std::endl;
|
|
|
|
f << "# path to store signed RC" << std::endl;
|
|
|
|
f << "contact-file=" << basepath << "self.signed" << std::endl;
|
|
|
|
f << "# path to store transport private key" << std::endl;
|
|
|
|
f << "transport-privkey=" << basepath << "transport.private" << std::endl;
|
|
|
|
f << "# path to store identity signing key" << std::endl;
|
|
|
|
f << "ident-privkey=" << basepath << "identity.private" << std::endl;
|
|
|
|
f << "# encryption key for onion routing" << std::endl;
|
|
|
|
f << "encryption-privkey=" << basepath << "encryption.private" << std::endl;
|
|
|
|
f << std::endl;
|
|
|
|
f << "# uncomment following line to set router nickname to 'lokinet'"
|
2018-10-01 09:57:57 +00:00
|
|
|
<< std::endl;
|
2018-10-01 09:56:14 +00:00
|
|
|
f << "# nickname=lokinet" << std::endl;
|
|
|
|
f << std::endl << std::endl;
|
2018-10-01 09:57:57 +00:00
|
|
|
|
2018-10-09 12:06:30 +00:00
|
|
|
f << "# admin api (disabled by default)" << std::endl;
|
|
|
|
f << "[api]" << std::endl;
|
|
|
|
f << "enabled=false" << std::endl;
|
|
|
|
f << "# authkey=insertpubkey1here" << std::endl;
|
|
|
|
f << "# authkey=insertpubkey2here" << std::endl;
|
|
|
|
f << "# authkey=insertpubkey3here" << std::endl;
|
|
|
|
#ifdef _WIN32
|
|
|
|
f << "bind=127.0.0.1:1190" << std::endl;
|
|
|
|
#else
|
|
|
|
f << "bind=unix:" << basepath << "api.socket" << std::endl;
|
|
|
|
#endif
|
|
|
|
f << std::endl << std::endl;
|
|
|
|
|
2018-10-01 09:56:14 +00:00
|
|
|
f << "# system settings for priviledges and such" << std::endl;
|
|
|
|
f << "[system]" << std::endl;
|
|
|
|
#ifdef _WIN32
|
|
|
|
f << "# ";
|
|
|
|
#endif
|
|
|
|
f << "user=" << DEFAULT_LOKINET_USER << std::endl;
|
|
|
|
#ifdef _WIN32
|
|
|
|
f << "# ";
|
|
|
|
#endif
|
|
|
|
f << "group=" << DEFAULT_LOKINET_GROUP << std::endl;
|
|
|
|
f << std::endl << std::endl;
|
2018-10-01 09:57:57 +00:00
|
|
|
|
2018-10-01 09:56:14 +00:00
|
|
|
f << "# dns provider configuration section" << std::endl;
|
|
|
|
f << "[dns]" << std::endl;
|
|
|
|
f << "# opennic us resolver" << std::endl;
|
|
|
|
f << "upstream=" << DEFAULT_RESOLVER_US << std::endl;
|
|
|
|
f << "# opennic eu resolver" << std::endl;
|
|
|
|
f << "upstream=" << DEFAULT_RESOLVER_EU << std::endl;
|
|
|
|
f << "# opennic au resolver" << std::endl;
|
|
|
|
f << "upstream=" << DEFAULT_RESOLVER_AU << std::endl;
|
2018-11-13 01:42:18 +00:00
|
|
|
f << "bind=127.0.0.1:53" << std::endl;
|
2018-10-01 09:56:14 +00:00
|
|
|
f << std::endl << std::endl;
|
2018-10-01 09:57:57 +00:00
|
|
|
|
2018-10-01 09:56:14 +00:00
|
|
|
f << "# network database settings block " << std::endl;
|
|
|
|
f << "[netdb]" << std::endl;
|
|
|
|
f << "# directory for network database skiplist storage" << std::endl;
|
|
|
|
f << "dir=" << basepath << "netdb" << std::endl;
|
|
|
|
f << std::endl << std::endl;
|
2018-10-01 09:57:57 +00:00
|
|
|
|
2018-10-01 09:56:14 +00:00
|
|
|
f << "# bootstrap settings " << std::endl;
|
|
|
|
f << "[connect]" << std::endl;
|
|
|
|
f << "bootstrap=" << basepath << "bootstrap.signed" << std::endl;
|
|
|
|
f << std::endl << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-10-02 23:11:44 +00:00
|
|
|
llarp_ensure_router_config(std::ofstream &f, std::string basepath)
|
2018-10-01 09:56:14 +00:00
|
|
|
{
|
2018-10-04 16:48:26 +00:00
|
|
|
f << "# network settings " << std::endl;
|
|
|
|
f << "[network]" << std::endl;
|
|
|
|
f << "profiles=" << basepath << "profiles.dat" << std::endl;
|
2018-11-12 16:43:40 +00:00
|
|
|
f << "ifaddr=10.105.0.1/16" << std::endl;
|
|
|
|
f << "ifname=lokitun0" << std::endl;
|
|
|
|
f << "enabled=true" << std::endl;
|
|
|
|
f << "exit=false" << std::endl;
|
|
|
|
f << "# exit-blacklist=tcp:25" << std::endl;
|
|
|
|
f << "# exit-whitelist=tcp:*" << std::endl;
|
|
|
|
f << "# exit-whitelist=udp:*" << std::endl;
|
2018-10-04 16:48:26 +00:00
|
|
|
f << std::endl;
|
2018-10-01 09:56:14 +00:00
|
|
|
f << "# ROUTERS ONLY: publish network interfaces for handling inbound traffic"
|
2018-10-01 09:57:57 +00:00
|
|
|
<< std::endl;
|
2018-10-01 09:56:14 +00:00
|
|
|
f << "[bind]" << std::endl;
|
2018-10-02 23:11:44 +00:00
|
|
|
// get ifname
|
2018-10-01 09:56:14 +00:00
|
|
|
std::string ifname;
|
|
|
|
if(llarp::GetBestNetIF(ifname, AF_INET))
|
|
|
|
f << ifname << "=1090" << std::endl;
|
|
|
|
else
|
|
|
|
f << "# could not autodetect network interface" << std::endl
|
2018-10-01 09:57:57 +00:00
|
|
|
<< "# eth0=1090" << std::endl;
|
|
|
|
|
2018-10-01 09:56:14 +00:00
|
|
|
f << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
llarp_ensure_client_config(std::ofstream &f, std::string basepath)
|
|
|
|
{
|
2018-11-04 13:16:27 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
const std::string snappExample_fpath = basepath + "snapp-example.ini";
|
|
|
|
// done with fname.ini
|
|
|
|
// start client.ini
|
|
|
|
// write fname ini
|
|
|
|
{
|
|
|
|
std::ofstream clientini_f(snappExample_fpath);
|
|
|
|
if(f.is_open())
|
|
|
|
{
|
|
|
|
clientini_f << "# this is an example configuration for a snapp";
|
|
|
|
clientini_f << "[example-snapp]" << std::endl;
|
|
|
|
clientini_f << "# keyfile is the path to the private key of the snapp"
|
|
|
|
<< std::endl;
|
|
|
|
clientini_f << "keyfile=" << basepath << "example-snap-keyfile.private"
|
|
|
|
<< std::endl;
|
|
|
|
clientini_f << "# ifaddr is the ip range to allocate to this snapp"
|
|
|
|
<< std::endl;
|
|
|
|
clientini_f << "ifaddr=10.55.0.0/16" << std::endl;
|
|
|
|
clientini_f << "# ifname is the name to try and give to the network "
|
|
|
|
"interface this snap owns"
|
|
|
|
<< std::endl;
|
|
|
|
clientini_f << "ifname=snapp-tun0" << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to write ", snappExample_fpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f << std::endl << std::endl;
|
|
|
|
f << "# snapps configuration section" << std::endl;
|
|
|
|
f << "[services]";
|
|
|
|
f << "# uncomment next line to enable persistant snapp" << std::endl;
|
|
|
|
f << "#example-snapp=" << snappExample_fpath << std::endl;
|
|
|
|
f << std::endl << std::endl;
|
|
|
|
#endif
|
2018-10-02 23:11:44 +00:00
|
|
|
f << "# network settings " << std::endl;
|
|
|
|
f << "[network]" << std::endl;
|
2018-10-04 16:48:26 +00:00
|
|
|
f << "profiles=" << basepath << "profiles.dat" << std::endl;
|
2018-10-02 23:11:44 +00:00
|
|
|
#ifndef __linux__
|
|
|
|
f << "# ";
|
|
|
|
#endif
|
2018-10-01 09:56:14 +00:00
|
|
|
// pick ip
|
2018-10-03 10:42:44 +00:00
|
|
|
std::string ip = llarp::findFreePrivateRange();
|
|
|
|
/*
|
2018-10-01 09:56:14 +00:00
|
|
|
struct privatesInUse ifsInUse = llarp_getPrivateIfs();
|
2018-10-01 09:57:57 +00:00
|
|
|
std::string ip = "";
|
|
|
|
if(!ifsInUse.ten)
|
2018-10-01 09:56:14 +00:00
|
|
|
{
|
|
|
|
ip = "10.10.0.1/24";
|
|
|
|
}
|
2018-10-01 09:57:57 +00:00
|
|
|
else if(!ifsInUse.oneSeven)
|
|
|
|
{
|
|
|
|
ip = "172.16.10.1/24";
|
|
|
|
}
|
|
|
|
else if(!ifsInUse.oneNine)
|
|
|
|
{
|
|
|
|
ip = "192.168.10.1/24";
|
|
|
|
}
|
2018-10-01 09:56:14 +00:00
|
|
|
else
|
2018-10-01 09:57:57 +00:00
|
|
|
{
|
|
|
|
llarp::LogError(
|
|
|
|
"Couldn't easily detect a private range to map lokinet onto");
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-03 10:42:44 +00:00
|
|
|
*/
|
|
|
|
if(ip == "")
|
|
|
|
{
|
|
|
|
llarp::LogError(
|
|
|
|
"Couldn't easily detect a private range to map lokinet onto");
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-01 09:57:57 +00:00
|
|
|
llarp::LogDebug("Detected " + ip
|
|
|
|
+ " is available for use, configuring as such");
|
2018-10-03 10:42:44 +00:00
|
|
|
// clientini_f << "ifaddr=" << ip << std::endl;
|
2018-10-01 09:56:14 +00:00
|
|
|
// pick interface name
|
2018-10-03 10:42:44 +00:00
|
|
|
std::string ifName = llarp::findFreeLokiTunIfName();
|
|
|
|
if(ifName == "")
|
2018-10-01 09:56:14 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("Could not find any free lokitun interface names");
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-02 23:11:44 +00:00
|
|
|
/*
|
2018-10-01 09:56:14 +00:00
|
|
|
clientini_f << "ifname=lokinum" << std::to_string(num) << std::endl;
|
|
|
|
// prefetch-tags=test
|
|
|
|
// enable netns?
|
2018-10-01 09:57:57 +00:00
|
|
|
|
|
|
|
llarp::LogInfo("Generated hidden service client as " + basepath
|
|
|
|
+ "client.ini");
|
2018-10-02 23:11:44 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-04 13:56:06 +00:00
|
|
|
f << "ifname=" << ifName << std::endl;
|
2018-10-02 23:11:44 +00:00
|
|
|
f << "ifaddr=" << ip << std::endl;
|
2018-10-03 11:46:24 +00:00
|
|
|
f << "enabled=true" << std::endl;
|
2018-10-02 23:11:44 +00:00
|
|
|
|
2018-10-01 09:56:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
extern "C"
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-07-27 04:25:34 +00:00
|
|
|
void
|
|
|
|
llarp_new_config(struct llarp_config **conf)
|
|
|
|
{
|
2018-08-09 13:55:51 +00:00
|
|
|
llarp_config *c = new llarp_config();
|
2018-07-27 04:25:34 +00:00
|
|
|
*conf = c;
|
|
|
|
}
|
2017-10-03 19:14:46 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
void
|
|
|
|
llarp_free_config(struct llarp_config **conf)
|
|
|
|
{
|
|
|
|
if(*conf)
|
|
|
|
delete *conf;
|
|
|
|
*conf = nullptr;
|
|
|
|
}
|
2018-01-08 13:49:05 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
int
|
|
|
|
llarp_load_config(struct llarp_config *conf, const char *fname)
|
|
|
|
{
|
|
|
|
if(!conf->impl.Load(fname))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-06 17:02:57 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
void
|
|
|
|
llarp_config_iter(struct llarp_config *conf,
|
|
|
|
struct llarp_config_iterator *iter)
|
|
|
|
{
|
|
|
|
iter->conf = conf;
|
|
|
|
std::map< std::string, llarp::Config::section_t & > sections = {
|
2018-11-11 13:14:19 +00:00
|
|
|
{"network", conf->impl.network}, {"connect", conf->impl.connect},
|
|
|
|
{"system", conf->impl.system}, {"bind", conf->impl.iwp_links},
|
|
|
|
{"netdb", conf->impl.netdb}, {"api", conf->impl.api},
|
|
|
|
{"services", conf->impl.services}};
|
2018-06-06 17:02:57 +00:00
|
|
|
|
2018-07-27 04:25:34 +00:00
|
|
|
for(const auto item : conf->impl.router)
|
|
|
|
iter->visit(iter, "router", item.first.c_str(), item.second.c_str());
|
2018-07-27 00:21:57 +00:00
|
|
|
|
2018-11-11 13:14:19 +00:00
|
|
|
for(const auto item : conf->impl.dns)
|
|
|
|
iter->visit(iter, "dns", item.first.c_str(), item.second.c_str());
|
|
|
|
|
2018-10-04 17:34:26 +00:00
|
|
|
for(const auto §ion : sections)
|
|
|
|
for(const auto &item : section.second)
|
2018-07-27 04:25:34 +00:00
|
|
|
iter->visit(iter, section.first.c_str(), item.first.c_str(),
|
|
|
|
item.second.c_str());
|
2018-07-27 00:21:57 +00:00
|
|
|
}
|
2018-08-22 15:51:50 +00:00
|
|
|
}
|