-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.cpp
59 lines (54 loc) · 1.62 KB
/
Config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "Config.hpp"
#include "Logger.hpp"
static int toInt(const std::string& value, int def)
{
try
{
return std::stoi(value);
}
catch(const std::exception&)
{
return def;
}
}
template<typename T>
static void append(T& dst, const T& src)
{
dst.insert(src.begin(), src.end());
}
void Config::load(const IniFile& ini)
{
Lock lock(mutex);
_name=ini("","name");
if(_name.empty())
{
char buf[1024];
CHECK(gethostname(buf, sizeof(buf)));
_name=buf;
}
_proxyCommand=ini("","server");
Logger::global()->trace("Using proxy command '{}'",_proxyCommand);
append(_clients, ini["clients"]);
_ip=ini("","ip",_ip);
_router=toInt(ini("","router"),_router);
append(_others, ini["others"]);
_loglevel=ini("","loglevel",_loglevel);
_unprivilegedUser=ini("","user",_unprivilegedUser);
_breakLength=toInt(ini("","break",""), _breakLength);
append(_env, ini["env"]);
if(_ip.empty()==_proxyCommand.empty())
throw ConfigError("Exactly one value must be defined: `ip' or `server'");
if(_ip.empty() && _router)
throw ConfigError("Only a main node should be a router");
}
void Config::addClient(const std::string& name, const std::string& ip)
{
Logger::global()->trace("Discovered new client: {} {}", name, ip);
Lock lock(mutex);
_clients[name]=ip;
}
Config::Config()
:_breakLength(5),_router(false),_unprivilegedUser("sshtun"),_loglevel("info")
{
_env["SSH"]="ssh -o ServerAliveInterval=30 -o ServerAliveCountMax=5 -o TCPKeepAlive=yes -o EscapeChar=none -o PasswordAuthentication=no -o RequestTTY=no";
}