i2pd/Daemon.cpp

175 lines
4.4 KiB
C++
Raw Normal View History

#include <thread>
2015-11-03 14:15:49 +00:00
#include <memory>
2014-04-20 01:54:34 +00:00
#include "Daemon.h"
#include "Log.h"
2015-11-03 14:15:49 +00:00
#include "Base.h"
2014-10-17 15:42:05 +00:00
#include "version.h"
2014-04-20 01:54:34 +00: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"
#include "Destination.h"
#include "HTTPServer.h"
2015-11-03 14:15:49 +00:00
#include "I2PControl.h"
2014-10-16 00:52:17 +00:00
#include "ClientContext.h"
// ssl.h somehow pulls Windows.h stuff that has to go after asio
#include <openssl/ssl.h>
2014-04-20 01:54:34 +00:00
2015-11-03 14:15:49 +00:00
#ifdef USE_UPNP
#include "UPnP.h"
#endif
2014-04-20 01:54:34 +00:00
namespace i2p
{
namespace util
{
class Daemon_Singleton::Daemon_Singleton_Private
2014-04-20 01:54:34 +00:00
{
public:
2015-11-03 14:15:49 +00:00
Daemon_Singleton_Private() {};
~Daemon_Singleton_Private() {};
2015-11-03 14:15:49 +00:00
std::unique_ptr<i2p::util::HTTPServer> httpServer;
std::unique_ptr<i2p::client::I2PControlService> m_I2PControlService;
#ifdef USE_UPNP
i2p::transport::UPnP m_UPnP;
2015-11-03 14:15:49 +00:00
#endif
};
2014-04-23 00:43:58 +00:00
Daemon_Singleton::Daemon_Singleton() : running(1), d(*new Daemon_Singleton_Private()) {};
Daemon_Singleton::~Daemon_Singleton() {
delete &d;
};
2014-10-17 13:55:41 +00:00
bool Daemon_Singleton::IsService () const
{
#ifndef _WIN32
return i2p::util::config::GetArg("-service", 0);
#else
return false;
#endif
}
bool Daemon_Singleton::init(int argc, char* argv[])
{
2015-11-03 14:15:49 +00:00
SSL_library_init ();
i2p::util::config::OptionParser(argc, argv);
2014-09-04 13:31:42 +00:00
i2p::context.Init ();
LogPrint("\n\n\n\ni2pd starting\n");
2014-10-17 15:42:05 +00:00
LogPrint("Version ", VERSION);
LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string());
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
2014-04-20 01:54:34 +00:00
isDaemon = i2p::util::config::GetArg("-daemon", 0);
isLogging = i2p::util::config::GetArg("-log", 1);
2014-04-20 01:54:34 +00:00
2014-09-11 13:32:34 +00:00
int port = i2p::util::config::GetArg("-port", 0);
if (port)
i2p::context.UpdatePort (port);
const char * host = i2p::util::config::GetCharArg("-host", "");
if (host && host[0])
2014-10-29 17:49:21 +00:00
i2p::context.UpdateAddress (boost::asio::ip::address::from_string (host));
2014-04-20 01:54:34 +00:00
i2p::context.SetSupportsV6 (i2p::util::config::GetArg("-v6", 0));
2015-02-02 16:14:09 +00:00
i2p::context.SetFloodfill (i2p::util::config::GetArg("-floodfill", 0));
2015-03-21 01:13:06 +00:00
auto bandwidth = i2p::util::config::GetArg("-bandwidth", "");
2015-03-19 15:14:21 +00:00
if (bandwidth.length () > 0)
{
if (bandwidth[0] > 'L')
i2p::context.SetHighBandwidth ();
else
i2p::context.SetLowBandwidth ();
}
2014-07-02 17:48:45 +00:00
LogPrint("CMD parameters:");
for (int i = 0; i < argc; ++i)
LogPrint(i, " ", argv[i]);
return true;
}
bool Daemon_Singleton::start()
{
// initialize log
if (isLogging)
2014-04-20 01:54:34 +00:00
{
2014-10-26 02:11:31 +00:00
if (isDaemon)
{
std::string logfile_path = IsService () ? "/var/log" : i2p::util::filesystem::GetDataDir().string();
#ifndef _WIN32
2014-10-26 02:11:31 +00:00
logfile_path.append("/i2pd.log");
#else
2014-10-26 02:11:31 +00:00
logfile_path.append("\\i2pd.log");
#endif
2014-10-26 02:11:31 +00:00
StartLog (logfile_path);
}
else
StartLog (""); // write to stdout
}
2014-07-02 17:48:45 +00:00
2015-11-03 14:15:49 +00:00
d.httpServer = std::unique_ptr<i2p::util::HTTPServer>(new i2p::util::HTTPServer(i2p::util::config::GetArg("-httpport", 7070)));
d.httpServer->Start();
2014-07-14 16:40:06 +00:00
LogPrint("HTTP Server started");
2014-04-20 01:54:34 +00:00
i2p::data::netdb.Start();
2014-04-22 22:37:24 +00:00
LogPrint("NetDB started");
2015-11-03 14:15:49 +00:00
#ifdef USE_UPNP
d.m_UPnP.Start ();
LogPrint(eLogInfo, "UPnP started");
#endif
i2p::transport::transports.Start();
2014-04-22 22:37:24 +00:00
LogPrint("Transports started");
2014-04-20 01:54:34 +00:00
i2p::tunnel::tunnels.Start();
2014-04-22 22:37:24 +00:00
LogPrint("Tunnels started");
2014-10-16 00:52:17 +00:00
i2p::client::context.Start ();
LogPrint("Client started");
2015-11-03 14:15:49 +00:00
// I2P Control
int i2pcontrolPort = i2p::util::config::GetArg("-i2pcontrolport", 0);
if (i2pcontrolPort)
{
d.m_I2PControlService = std::unique_ptr<i2p::client::I2PControlService>(new i2p::client::I2PControlService (i2pcontrolPort));
d.m_I2PControlService->Start ();
LogPrint("I2PControl started");
}
2014-04-20 01:54:34 +00:00
return true;
}
bool Daemon_Singleton::stop()
{
LogPrint("Shutdown started.");
2014-10-16 00:52:17 +00:00
i2p::client::context.Stop();
2015-02-12 02:06:52 +00:00
LogPrint("Client stopped");
2014-04-20 01:54:34 +00:00
i2p::tunnel::tunnels.Stop();
2015-02-12 02:06:52 +00:00
LogPrint("Tunnels stopped");
2015-11-03 14:15:49 +00:00
#ifdef USE_UPNP
d.m_UPnP.Stop ();
LogPrint(eLogInfo, "UPnP stopped");
#endif
i2p::transport::transports.Stop();
2015-02-12 02:06:52 +00:00
LogPrint("Transports stopped");
2014-04-20 01:54:34 +00:00
i2p::data::netdb.Stop();
2015-02-12 02:06:52 +00:00
LogPrint("NetDB stopped");
d.httpServer->Stop();
2015-11-03 14:15:49 +00:00
d.httpServer = nullptr;
2015-02-12 02:06:52 +00:00
LogPrint("HTTP Server stopped");
2015-11-03 14:15:49 +00:00
if (d.m_I2PControlService)
{
d.m_I2PControlService->Stop ();
d.m_I2PControlService = nullptr;
LogPrint("I2PControl stopped");
}
2014-07-02 17:48:45 +00:00
StopLog ();
2014-08-14 14:20:22 +00:00
2014-04-20 01:54:34 +00:00
return true;
}
}
2014-04-22 22:37:24 +00:00
}