This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Daemon.cpp
392 lines (349 loc) · 13.7 KB
/
Daemon.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* Copyright (c) 2013-2016, The Kovri I2P Router Project
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Parts of the project are originally copyright (c) 2013-2015 The PurpleI2P Project
*/
#include "Daemon.h"
#include <string>
#include <vector>
#include <thread>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include "client/ClientContext.h"
#include "Destination.h"
#include "Garlic.h"
#include "NetworkDatabase.h"
#include "RouterContext.h"
#include "RouterInfo.h"
#include "Version.h"
#include "Streaming.h"
#include "core/util/Log.h"
#include "transport/NTCPSession.h"
#include "transport/Transports.h"
#include "tunnel/Tunnel.h"
#include "util/Config.h"
namespace i2p {
namespace util {
Daemon_Singleton::Daemon_Singleton()
: m_IsRunning(true),
m_log(i2p::util::log::Log::Get()) {}
Daemon_Singleton::~Daemon_Singleton() {}
bool Daemon_Singleton::IsService() const {
#ifndef _WIN32
return i2p::util::config::var_map["service"].as<bool>();
#else
return false;
#endif
}
// TODO(anonimal): find a better way to initialize
bool Daemon_Singleton::Init() {
i2p::context.Init(
i2p::util::config::var_map["host"].as<std::string>(),
i2p::util::config::var_map["port"].as<int>(),
i2p::util::filesystem::GetDataPath());
m_IsDaemon = i2p::util::config::var_map["daemon"].as<bool>();
m_IsLogging = i2p::util::config::var_map["log"].as<bool>();
int port = i2p::util::config::var_map["port"].as<int>();
i2p::context.UpdatePort(port);
i2p::context.UpdateAddress(
boost::asio::ip::address::from_string(
i2p::util::config::var_map["host"].as<std::string>()));
i2p::context.SetSupportsV6(
i2p::util::config::var_map["v6"].as<bool>());
i2p::context.SetFloodfill(
i2p::util::config::var_map["floodfill"].as<bool>());
auto bandwidth = i2p::util::config::var_map["bandwidth"].as<std::string>();
if (bandwidth.length() > 0) {
if (bandwidth[0] > 'L')
i2p::context.SetHighBandwidth();
else
i2p::context.SetLowBandwidth();
}
// Set reseed options
i2p::context.ReseedFrom(
i2p::util::config::var_map["reseed-from"].as<std::string>());
i2p::context.ReseedSkipSSLCheck(
i2p::util::config::var_map["reseed-skip-ssl-check"].as<bool>());
// Initialize the ClientContext
InitClientContext();
return true;
}
bool Daemon_Singleton::Start() {
LogPrint("The Kovri I2P Router Project");
LogPrint("Version ", KOVRI_VERSION);
LogPrint("Listening on port ", i2p::util::config::var_map["port"].as<int>());
if (m_IsLogging) {
if (m_IsDaemon) {
std::string logfile_path = IsService() ? "/var/log" :
i2p::util::filesystem::GetDataPath().string();
#ifndef _WIN32
logfile_path.append("/kovri.log");
#else
logfile_path.append("\\kovri.log");
#endif
StartLog(logfile_path);
} else {
StartLog(""); // write to stdout
}
} else {
m_log->Stop();
}
try {
LogPrint("Starting NetDB...");
if (i2p::data::netdb.Start()) {
LogPrint("NetDB started");
} else {
LogPrint("NetDB failed to start");
return false;
}
LogPrint("Starting transports...");
i2p::transport::transports.Start();
LogPrint("Transports started");
LogPrint("Starting tunnels...");
i2p::tunnel::tunnels.Start();
LogPrint("Tunnels started");
LogPrint("Starting client...");
i2p::client::context.Start();
LogPrint("Client started");
} catch (std::runtime_error& e) {
LogPrint(eLogError, e.what());
return false;
}
return true;
}
bool Daemon_Singleton::Stop() {
LogPrint("Stopping client...");
i2p::client::context.Stop();
LogPrint("Client stopped");
LogPrint("Stopping tunnels...");
i2p::tunnel::tunnels.Stop();
LogPrint("Tunnels stopped");
LogPrint("Stopping transports...");
i2p::transport::transports.Stop();
LogPrint("Transports stopped");
LogPrint("Stopping NetDB...");
i2p::data::netdb.Stop();
LogPrint("NetDB stopped");
LogPrint("Goodbye!");
StopLog();
return true;
}
void Daemon_Singleton::Reload() {
// TODO(unassigned): do we want to add locking?
LogPrint("Reloading configuration");
// reload tunnels.conf
ReloadTunnels();
// TODO(anonimal): reload kovri.conf
}
void Daemon_Singleton::InitClientContext() {
i2p::client::context.RegisterShutdownHandler([this]() {
m_IsRunning = false;
});
std::shared_ptr<i2p::client::ClientDestination> localDestination;
// Setup proxies and services
std::string proxyKeys =
i2p::util::config::var_map["proxykeys"].as<std::string>();
if (proxyKeys.length() > 0)
localDestination = i2p::client::context.LoadLocalDestination(
proxyKeys, false);
i2p::client::context.SetHTTPProxy(new i2p::proxy::HTTPProxy(
"HTTP Proxy", // TODO(unassigned): what if we want to change the name?
i2p::util::config::var_map["httpproxyaddress"].as<std::string>(),
i2p::util::config::var_map["httpproxyport"].as<int>(),
localDestination));
i2p::client::context.SetSOCKSProxy(new i2p::proxy::SOCKSProxy(
i2p::util::config::var_map["socksproxyaddress"].as<std::string>(),
i2p::util::config::var_map["socksproxyport"].as<int>(),
localDestination));
int i2pcontrolPort = i2p::util::config::var_map["i2pcontrolport"].as<int>();
if (i2pcontrolPort) {
i2p::client::context.SetI2PControlService(
new i2p::client::i2pcontrol::I2PControlService(
i2p::client::context.GetIoService(),
i2p::util::config::var_map["i2pcontroladdress"].as<std::string>(),
i2pcontrolPort,
i2p::util::config::var_map["i2pcontrolpassword"].as<std::string>()));
}
// Setup client and server tunnels
SetupTunnels();
}
void Daemon_Singleton::SetupTunnels() {
boost::property_tree::ptree pt;
std::string pathTunnelsConfigFile =
i2p::util::filesystem::GetTunnelsConfigFile().string();
try {
boost::property_tree::read_ini(pathTunnelsConfigFile, pt);
} catch(const std::exception& ex) {
LogPrint(eLogWarning, "Can't read ",
pathTunnelsConfigFile, ": ", ex.what());
return;
}
int numClientTunnels = 0, numServerTunnels = 0;
for (auto& section : pt) {
const std::string name = section.first;
const auto& value = section.second;
try {
std::string type = value.get<std::string>(I2P_TUNNELS_SECTION_TYPE);
if (type == I2P_TUNNELS_SECTION_TYPE_CLIENT) {
// Mandatory parameters
std::string dest = value.get<std::string>(
I2P_CLIENT_TUNNEL_DESTINATION);
int port = value.get<int>(I2P_CLIENT_TUNNEL_PORT);
// Optional parameters
std::string address = value.get(I2P_CLIENT_TUNNEL_ADDRESS, "127.0.0.1");
std::string keys = value.get(I2P_CLIENT_TUNNEL_KEYS, "");
int destinationPort = value.get(I2P_CLIENT_TUNNEL_DESTINATION_PORT, 0);
std::shared_ptr<i2p::client::ClientDestination> localDestination;
if (keys.length() > 0)
localDestination = i2p::client::context.LoadLocalDestination(
keys, false);
bool result = i2p::client::context.InsertClientTunnel(port,
new i2p::client::I2PClientTunnel(
name,
dest,
address,
port,
localDestination,
destinationPort));
if (result)
++numClientTunnels;
else
LogPrint(eLogError, "I2P client tunnel with port ",
port, " already exists");
} else if (type == I2P_TUNNELS_SECTION_TYPE_SERVER ||
type == I2P_TUNNELS_SECTION_TYPE_HTTP) {
// Mandatory parameters
std::string host = value.get<std::string>(I2P_SERVER_TUNNEL_HOST);
int port = value.get<int>(I2P_SERVER_TUNNEL_PORT);
std::string keys = value.get<std::string>(I2P_SERVER_TUNNEL_KEYS);
// Optional parameters
int inPort = value.get(I2P_SERVER_TUNNEL_INPORT, 0);
std::string accessList = value.get(I2P_SERVER_TUNNEL_ACCESS_LIST, "");
auto localDestination = i2p::client::context.LoadLocalDestination(
keys, true);
i2p::client::I2PServerTunnel* serverTunnel =
(type == I2P_TUNNELS_SECTION_TYPE_HTTP) ?
new i2p::client::I2PServerTunnelHTTP(
name, host, port, localDestination, inPort) :
new i2p::client::I2PServerTunnel(
name, host, port, localDestination, inPort);
serverTunnel->SetAccessListString(accessList);
bool result = i2p::client::context.InsertServerTunnel(
localDestination->GetIdentHash(), serverTunnel);
if (result)
++numServerTunnels;
else
LogPrint(eLogError, "I2P server tunnel for destination ",
i2p::client::context.GetAddressBook().ToAddress(
localDestination->GetIdentHash()), " already exists");
} else {
LogPrint(eLogWarning, "Unknown section type=", type,
" of ", name, " in ", pathTunnelsConfigFile);
}
} catch(const std::exception& ex) {
LogPrint(eLogError, "Can't read tunnel ", name, " params: ", ex.what());
}
}
LogPrint(eLogInfo, numClientTunnels, " I2P client tunnels created");
LogPrint(eLogInfo, numServerTunnels, " I2P server tunnels created");
}
void Daemon_Singleton::ReloadTunnels() {
boost::property_tree::ptree pt;
std::string tunnelsConfigFile =
i2p::util::filesystem::GetTunnelsConfigFile().string();
try {
boost::property_tree::read_ini(tunnelsConfigFile, pt);
} catch (const std::exception& ex) {
LogPrint(eLogWarning, "Can't read ", tunnelsConfigFile,
": ", ex.what());
return;
}
// List of tunnels that still exist after config update
// Make sure the default IRC and eepsite tunnels do not get removed
std::vector<std::string> updatedTunnels;
// Iterate over tunnels' ident hashes for what's in tunnels.conf now
for (auto& section : pt) {
// TODO(unassigned): what if we switch a server from client to tunnel
// or vice versa?
const std::string tunnelName = section.first;
const auto value = section.second;
const std::string type = value.get<std::string>(
I2P_TUNNELS_SECTION_TYPE, "");
if (type == I2P_TUNNELS_SECTION_TYPE_SERVER ||
type == I2P_TUNNELS_SECTION_TYPE_HTTP) {
// Obtain server options
std::string keyfile = value.get<std::string>(I2P_SERVER_TUNNEL_KEYS, "");
std::string hostStr = value.get<std::string>(I2P_SERVER_TUNNEL_HOST, "");
int port = value.get<int>(I2P_SERVER_TUNNEL_PORT, 0);
int inPort = value.get(I2P_SERVER_TUNNEL_INPORT, 0);
std::string accessList = value.get(I2P_SERVER_TUNNEL_ACCESS_LIST, "");
i2p::client::context.UpdateServerTunnel(
tunnelName, keyfile, hostStr, accessList, port, inPort,
(type == I2P_TUNNELS_SECTION_TYPE_HTTP));
} else if (type == I2P_TUNNELS_SECTION_TYPE_CLIENT) {
// Get client tunnel parameters
std::string keyfile = value.get(I2P_CLIENT_TUNNEL_KEYS, "");
std::string destination = value.get<std::string>(
I2P_CLIENT_TUNNEL_DESTINATION, "");
std::string hostStr = value.get(I2P_CLIENT_TUNNEL_ADDRESS, "127.0.0.1");
int port = value.get<int>(I2P_CLIENT_TUNNEL_PORT, 0);
int destPort = value.get(I2P_CLIENT_TUNNEL_DESTINATION_PORT, 0);
i2p::client::I2PClientTunnel* tunnel =
i2p::client::context.GetClientTunnel(port);
if (tunnel && tunnel->GetName() != tunnelName) {
// Conflicting port
// TODO(unassigned): what if we interchange two client tunnels' ports?
// TODO(EinMByte): the addresses could differ
LogPrint(eLogError,
tunnelName, " will not be updated, Conflicting Port");
continue;
}
i2p::client::context.UpdateClientTunnel(
tunnelName, keyfile, destination, hostStr, port, destPort);
}
}
i2p::client::context.RemoveServerTunnels(
[&updatedTunnels](i2p::client::I2PServerTunnel* tunnel) {
return std::find(
updatedTunnels.begin(),
updatedTunnels.end(),
tunnel->GetName())
== updatedTunnels.end();
});
i2p::client::context.RemoveClientTunnels(
[&updatedTunnels](i2p::client::I2PClientTunnel* tunnel) {
return std::find(
updatedTunnels.begin(),
updatedTunnels.end(),
tunnel->GetName())
== updatedTunnels.end();
});
}
} // namespace util
} // namespace i2p