i2pd/Daemon.cpp

214 lines
6.2 KiB
C++
Raw Normal View History

#include <thread>
2015-11-03 17:15:49 +03:00
#include <memory>
2014-04-20 05:54:34 +04:00
#include "Daemon.h"
2016-01-20 03:00:00 +03:00
#include "Config.h"
2014-04-20 05:54:34 +04:00
#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"
#include "Destination.h"
#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
{
class Daemon_Singleton::Daemon_Singleton_Private
2014-04-20 05:54:34 +04:00
{
public:
2015-11-03 17:15:49 +03:00
Daemon_Singleton_Private() {};
~Daemon_Singleton_Private() {};
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
i2p::transport::UPnP m_UPnP;
2015-11-03 17:15:49 +03:00
#endif
};
2014-04-23 04:43:58 +04:00
Daemon_Singleton::Daemon_Singleton() : running(1), d(*new Daemon_Singleton_Private()) {};
Daemon_Singleton::~Daemon_Singleton() {
delete &d;
};
2014-10-17 17:55:41 +04:00
bool Daemon_Singleton::IsService () const
{
2016-01-20 03:00:00 +03:00
bool service = false;
2014-10-17 17:55:41 +04:00
#ifndef _WIN32
2016-01-20 03:00:00 +03:00
i2p::config::GetOption("service", service);
2014-10-17 17:55:41 +04:00
#endif
2016-01-20 03:00:00 +03:00
return service;
2014-10-17 17:55:41 +04:00
}
bool Daemon_Singleton::init(int argc, char* argv[])
{
std::string config = i2p::util::filesystem::GetConfigFile().string();
std::string tunconf = i2p::util::filesystem::GetTunnelsConfigFile().string();
std::string datadir = i2p::util::filesystem::GetDataDir().string();
LogPrint(eLogInfo, "i2pd v", VERSION, " starting");
LogPrint(eLogDebug, "FS: main config file: ", config);
LogPrint(eLogDebug, "FS: tunnels config: ", tunconf);
LogPrint(eLogDebug, "FS: data directory: ", datadir);
2016-01-20 03:00:00 +03:00
i2p::config::Init();
i2p::config::ParseCmdline(argc, argv);
i2p::config::ParseConfig(config);
2016-01-20 03:00:00 +03:00
i2p::config::Finalize();
2016-01-01 00:02:10 +03:00
i2p::crypto::InitCrypto ();
2014-09-04 17:31:42 +04:00
i2p::context.Init ();
2016-01-20 03:00:00 +03:00
i2p::config::GetOption("daemon", isDaemon);
// temporary hack
std::string logs = "";
i2p::config::GetOption("log", logs);
if (logs != "")
isLogging = true;
2014-04-20 05:54:34 +04:00
2016-01-20 03:00:00 +03:00
uint16_t port; i2p::config::GetOption("port", port);
2016-01-24 15:41:08 +03:00
LogPrint(eLogInfo, "Daemon: accepting incoming connections at port ", port);
i2p::context.UpdatePort (port);
2016-01-20 03:00:00 +03:00
std::string host; i2p::config::GetOption("host", host);
2016-01-24 15:41:08 +03:00
if (host != "") {
LogPrint(eLogInfo, "Daemon: address for incoming connections is ", host);
2014-10-29 20:49:21 +03:00
i2p::context.UpdateAddress (boost::asio::ip::address::from_string (host));
2016-01-24 15:41:08 +03:00
}
2014-04-20 05:54:34 +04:00
2016-01-20 03:00:00 +03:00
bool ipv6; i2p::config::GetOption("ipv6", ipv6);
bool transit; i2p::config::GetOption("notransit", transit);
i2p::context.SetSupportsV6 (ipv6);
i2p::context.SetAcceptsTunnels (!transit);
bool isFloodfill; i2p::config::GetOption("floodfill", isFloodfill);
char bandwidth; i2p::config::GetOption("bandwidth", bandwidth);
2016-01-20 03:00:00 +03:00
if (isFloodfill) {
LogPrint(eLogInfo, "Daemon: router will be floodfill, bandwidth set to 'extra'");
i2p::context.SetFloodfill (true);
2016-01-03 06:17:04 +03:00
i2p::context.SetExtraBandwidth ();
} else if (bandwidth != '-') {
LogPrint(eLogInfo, "Daemon: bandwidth set to ", bandwidth);
2016-01-20 03:00:00 +03:00
switch (bandwidth) {
case 'P' : i2p::context.SetExtraBandwidth (); break;
case 'L' : i2p::context.SetHighBandwidth (); break;
default : i2p::context.SetLowBandwidth (); break;
}
2015-03-19 18:14:21 +03:00
}
2014-07-02 21:48:45 +04:00
return true;
}
bool Daemon_Singleton::start()
{
if (isLogging)
2014-04-20 05:54:34 +04:00
{
2016-02-01 03:00:00 +03:00
// set default to stdout
std::string logfile = ""; i2p::config::GetOption("logfile", logfile);
std::string loglevel = ""; i2p::config::GetOption("loglevel", loglevel);
if (isDaemon && logfile == "") {
// can't log to stdout, use autodetect of logfile
if (IsService ()) {
logfile = "/var/log";
} else {
logfile = i2p::util::filesystem::GetDataDir().string();
}
#ifndef _WIN32
2016-02-01 03:00:00 +03:00
logfile.append("/i2pd.log");
#else
2016-02-01 03:00:00 +03:00
logfile.append("\\i2pd.log");
#endif
2015-12-28 03:00:00 +03:00
}
2016-02-01 03:00:00 +03:00
StartLog (logfile);
2016-01-20 03:00:00 +03:00
g_Log->SetLogLevel(loglevel);
}
2014-07-02 21:48:45 +04:00
2016-01-20 03:00:00 +03:00
bool http; i2p::config::GetOption("http.enabled", http);
if (http) {
std::string httpAddr; i2p::config::GetOption("http.address", httpAddr);
uint16_t httpPort; i2p::config::GetOption("http.port", httpPort);
2016-01-24 14:05:16 +03:00
LogPrint(eLogInfo, "Daemon: starting HTTP Server at ", httpAddr, ":", httpPort);
2016-01-20 03:00:00 +03:00
d.httpServer = std::unique_ptr<i2p::util::HTTPServer>(new i2p::util::HTTPServer(httpAddr, httpPort));
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");
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
// I2P Control Protocol
2016-01-20 03:00:00 +03:00
bool i2pcontrol; i2p::config::GetOption("i2pcontrol.enabled", i2pcontrol);
if (i2pcontrol) {
std::string i2pcpAddr; i2p::config::GetOption("i2pcontrol.address", i2pcpAddr);
uint16_t i2pcpPort; i2p::config::GetOption("i2pcontrol.port", i2pcpPort);
LogPrint(eLogInfo, "Daemon: starting I2PControl at ", i2pcpAddr, ":", i2pcpPort);
d.m_I2PControlService = std::unique_ptr<i2p::client::I2PControlService>(new i2p::client::I2PControlService (i2pcpAddr, i2pcpPort));
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");
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");
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
}