2014-04-23 02:07:21 +04:00
|
|
|
#include <thread>
|
2015-11-03 17:15:49 +03:00
|
|
|
#include <memory>
|
2014-04-23 02:07:21 +04:00
|
|
|
|
2014-04-20 05:54:34 +04:00
|
|
|
#include "Daemon.h"
|
|
|
|
|
|
|
|
#include "Log.h"
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "Base.h"
|
2014-10-17 19:42:05 +04:00
|
|
|
#include "version.h"
|
2014-04-20 05:54:34 +04:00
|
|
|
#include "Transports.h"
|
|
|
|
#include "NTCPSession.h"
|
|
|
|
#include "RouterInfo.h"
|
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "Tunnel.h"
|
|
|
|
#include "NetDb.h"
|
|
|
|
#include "Garlic.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "Streaming.h"
|
2014-10-05 16:54:59 +04:00
|
|
|
#include "Destination.h"
|
2014-04-23 02:07:21 +04:00
|
|
|
#include "HTTPServer.h"
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "I2PControl.h"
|
2014-10-16 04:52:17 +04:00
|
|
|
#include "ClientContext.h"
|
2016-01-01 00:02:10 +03:00
|
|
|
#include "Crypto.h"
|
2014-04-20 05:54:34 +04:00
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
#ifdef USE_UPNP
|
|
|
|
#include "UPnP.h"
|
|
|
|
#endif
|
|
|
|
|
2014-04-20 05:54:34 +04:00
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
2014-04-23 02:07:21 +04:00
|
|
|
class Daemon_Singleton::Daemon_Singleton_Private
|
2014-04-20 05:54:34 +04:00
|
|
|
{
|
2014-04-23 02:07:21 +04:00
|
|
|
public:
|
2015-11-03 17:15:49 +03:00
|
|
|
Daemon_Singleton_Private() {};
|
|
|
|
~Daemon_Singleton_Private() {};
|
2014-04-23 02:07:21 +04:00
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
std::unique_ptr<i2p::util::HTTPServer> httpServer;
|
|
|
|
std::unique_ptr<i2p::client::I2PControlService> m_I2PControlService;
|
|
|
|
|
|
|
|
#ifdef USE_UPNP
|
2015-11-11 22:21:52 +03:00
|
|
|
i2p::transport::UPnP m_UPnP;
|
2015-11-03 17:15:49 +03:00
|
|
|
#endif
|
2014-04-23 02:07:21 +04:00
|
|
|
};
|
|
|
|
|
2014-04-23 04:43:58 +04:00
|
|
|
Daemon_Singleton::Daemon_Singleton() : running(1), d(*new Daemon_Singleton_Private()) {};
|
2014-04-23 02:07:21 +04:00
|
|
|
Daemon_Singleton::~Daemon_Singleton() {
|
|
|
|
delete &d;
|
|
|
|
};
|
|
|
|
|
2014-10-17 17:55:41 +04:00
|
|
|
bool Daemon_Singleton::IsService () const
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
return i2p::util::config::GetArg("-service", 0);
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2014-04-23 02:07:21 +04:00
|
|
|
|
|
|
|
bool Daemon_Singleton::init(int argc, char* argv[])
|
|
|
|
{
|
2016-01-01 00:02:10 +03:00
|
|
|
i2p::crypto::InitCrypto ();
|
2014-04-23 02:07:21 +04:00
|
|
|
i2p::util::config::OptionParser(argc, argv);
|
2014-09-04 17:31:42 +04:00
|
|
|
i2p::context.Init ();
|
2014-04-23 02:07:21 +04:00
|
|
|
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "\n\n\n\ni2pd v", VERSION, " starting\n");
|
|
|
|
LogPrint(eLogDebug, "data directory: ", i2p::util::filesystem::GetDataDir().string());
|
2014-04-23 02:07:21 +04:00
|
|
|
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
|
|
|
|
|
2014-04-20 05:54:34 +04:00
|
|
|
isDaemon = i2p::util::config::GetArg("-daemon", 0);
|
2014-04-23 02:07:21 +04:00
|
|
|
isLogging = i2p::util::config::GetArg("-log", 1);
|
2014-04-20 05:54:34 +04:00
|
|
|
|
2014-09-11 17:32:34 +04:00
|
|
|
int port = i2p::util::config::GetArg("-port", 0);
|
|
|
|
if (port)
|
|
|
|
i2p::context.UpdatePort (port);
|
2015-12-28 03:00:00 +03:00
|
|
|
std::string host = i2p::util::config::GetArg("-host", "");
|
|
|
|
if (host != "")
|
2014-10-29 20:49:21 +03:00
|
|
|
i2p::context.UpdateAddress (boost::asio::ip::address::from_string (host));
|
2014-04-20 05:54:34 +04:00
|
|
|
|
2014-10-27 04:32:06 +03:00
|
|
|
i2p::context.SetSupportsV6 (i2p::util::config::GetArg("-v6", 0));
|
2016-01-03 06:17:04 +03:00
|
|
|
bool isFloodfill = i2p::util::config::GetArg("-floodfill", 0);
|
|
|
|
i2p::context.SetFloodfill (isFloodfill);
|
2015-03-21 04:13:06 +03:00
|
|
|
auto bandwidth = i2p::util::config::GetArg("-bandwidth", "");
|
2015-03-19 18:14:21 +03:00
|
|
|
if (bandwidth.length () > 0)
|
|
|
|
{
|
2016-01-03 06:17:04 +03:00
|
|
|
if (bandwidth[0] > 'O')
|
|
|
|
i2p::context.SetExtraBandwidth ();
|
|
|
|
else if (bandwidth[0] > 'L')
|
2015-03-19 18:14:21 +03:00
|
|
|
i2p::context.SetHighBandwidth ();
|
|
|
|
else
|
|
|
|
i2p::context.SetLowBandwidth ();
|
|
|
|
}
|
2016-01-03 06:17:04 +03:00
|
|
|
else if (isFloodfill)
|
|
|
|
i2p::context.SetExtraBandwidth ();
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogDebug, "Daemon: CMD parameters:");
|
2014-07-02 21:48:45 +04:00
|
|
|
for (int i = 0; i < argc; ++i)
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogDebug, i, ": ", argv[i]);
|
2014-07-02 21:48:45 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Daemon_Singleton::start()
|
|
|
|
{
|
|
|
|
// initialize log
|
|
|
|
if (isLogging)
|
2014-04-20 05:54:34 +04:00
|
|
|
{
|
2014-10-26 05:11:31 +03:00
|
|
|
if (isDaemon)
|
|
|
|
{
|
|
|
|
std::string logfile_path = IsService () ? "/var/log" : i2p::util::filesystem::GetDataDir().string();
|
2015-01-03 23:38:48 +03:00
|
|
|
#ifndef _WIN32
|
2014-10-26 05:11:31 +03:00
|
|
|
logfile_path.append("/i2pd.log");
|
2015-01-03 23:38:48 +03:00
|
|
|
#else
|
2014-10-26 05:11:31 +03:00
|
|
|
logfile_path.append("\\i2pd.log");
|
2015-01-03 23:38:48 +03:00
|
|
|
#endif
|
2014-10-26 05:11:31 +03:00
|
|
|
StartLog (logfile_path);
|
2015-12-28 03:00:00 +03:00
|
|
|
} else {
|
2014-10-26 05:11:31 +03:00
|
|
|
StartLog (""); // write to stdout
|
2015-12-28 03:00:00 +03:00
|
|
|
}
|
|
|
|
g_Log->SetLogLevel(i2p::util::config::GetArg("-loglevel", "info"));
|
2014-04-23 02:07:21 +04:00
|
|
|
}
|
2014-07-02 21:48:45 +04:00
|
|
|
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "Daemon: staring HTTP Server");
|
2015-11-30 17:44:32 +03:00
|
|
|
d.httpServer = std::unique_ptr<i2p::util::HTTPServer>(new i2p::util::HTTPServer(i2p::util::config::GetArg("-httpaddress", "127.0.0.1"), i2p::util::config::GetArg("-httpport", 7070)));
|
2014-04-23 02:07:21 +04:00
|
|
|
d.httpServer->Start();
|
2015-12-18 15:21:37 +03:00
|
|
|
|
|
|
|
LogPrint(eLogInfo, "Daemon: starting NetDB");
|
2014-04-20 05:54:34 +04:00
|
|
|
i2p::data::netdb.Start();
|
2015-12-18 15:21:37 +03:00
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
#ifdef USE_UPNP
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "Daemon: starting UPnP");
|
2015-11-03 17:15:49 +03:00
|
|
|
d.m_UPnP.Start ();
|
|
|
|
#endif
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "Daemon: starting Transports");
|
2014-10-21 20:25:53 +04:00
|
|
|
i2p::transport::transports.Start();
|
2015-12-18 15:21:37 +03:00
|
|
|
|
|
|
|
LogPrint(eLogInfo, "Daemon: starting Tunnels");
|
2014-04-20 05:54:34 +04:00
|
|
|
i2p::tunnel::tunnels.Start();
|
2015-12-18 15:21:37 +03:00
|
|
|
|
|
|
|
LogPrint(eLogInfo, "Daemon: starting Client");
|
2014-10-16 04:52:17 +04:00
|
|
|
i2p::client::context.Start ();
|
2015-12-18 15:21:37 +03:00
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
// I2P Control
|
|
|
|
int i2pcontrolPort = i2p::util::config::GetArg("-i2pcontrolport", 0);
|
|
|
|
if (i2pcontrolPort)
|
|
|
|
{
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "Daemon: starting I2PControl");
|
2015-11-30 17:44:32 +03:00
|
|
|
d.m_I2PControlService = std::unique_ptr<i2p::client::I2PControlService>(new i2p::client::I2PControlService (i2p::util::config::GetArg("-i2pcontroladdress", "127.0.0.1"), i2pcontrolPort));
|
2015-11-03 17:15:49 +03:00
|
|
|
d.m_I2PControlService->Start ();
|
|
|
|
}
|
2014-04-20 05:54:34 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Daemon_Singleton::stop()
|
|
|
|
{
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "Daemon: shutting down");
|
|
|
|
LogPrint(eLogInfo, "Daemon: stopping Client");
|
2014-10-16 04:52:17 +04:00
|
|
|
i2p::client::context.Stop();
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "Daemon: stopping Tunnels");
|
2014-04-20 05:54:34 +04:00
|
|
|
i2p::tunnel::tunnels.Stop();
|
2015-11-03 17:15:49 +03:00
|
|
|
#ifdef USE_UPNP
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "Daemon: stopping UPnP");
|
2015-11-03 17:15:49 +03:00
|
|
|
d.m_UPnP.Stop ();
|
|
|
|
#endif
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "Daemon: stopping Transports");
|
2014-10-21 20:25:53 +04:00
|
|
|
i2p::transport::transports.Stop();
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "Daemon: stopping NetDB");
|
2014-04-20 05:54:34 +04:00
|
|
|
i2p::data::netdb.Stop();
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "Daemon: stopping HTTP Server");
|
2014-04-23 02:07:21 +04:00
|
|
|
d.httpServer->Stop();
|
2015-11-03 17:15:49 +03:00
|
|
|
d.httpServer = nullptr;
|
|
|
|
if (d.m_I2PControlService)
|
|
|
|
{
|
2015-12-18 15:21:37 +03:00
|
|
|
LogPrint(eLogInfo, "Daemon: stopping I2PControl");
|
2015-11-03 17:15:49 +03:00
|
|
|
d.m_I2PControlService->Stop ();
|
|
|
|
d.m_I2PControlService = nullptr;
|
|
|
|
}
|
2016-01-01 00:02:10 +03:00
|
|
|
i2p::crypto::TerminateCrypto ();
|
2014-07-02 21:48:45 +04:00
|
|
|
StopLog ();
|
2014-08-14 18:20:22 +04:00
|
|
|
|
2014-04-20 05:54:34 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 02:37:24 +04:00
|
|
|
}
|