i2pd/Daemon.cpp

240 lines
6.6 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"
2016-01-20 00:00:00 +00:00
#include "Config.h"
2014-04-20 01:54:34 +00:00
#include "Log.h"
#include "FS.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 "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"
2015-12-31 21:02:10 +00:00
#include "Crypto.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
{
2016-01-20 00:00:00 +00:00
bool service = false;
2014-10-17 13:55:41 +00:00
#ifndef _WIN32
2016-01-20 00:00:00 +00:00
i2p::config::GetOption("service", service);
2014-10-17 13:55:41 +00:00
#endif
2016-01-20 00:00:00 +00:00
return service;
2014-10-17 13:55:41 +00:00
}
bool Daemon_Singleton::init(int argc, char* argv[])
{
i2p::config::Init();
i2p::config::ParseCmdline(argc, argv);
std::string config; i2p::config::GetOption("conf", config);
std::string datadir; i2p::config::GetOption("datadir", datadir);
i2p::fs::DetectDataDir(datadir, IsService());
i2p::fs::Init();
datadir = i2p::fs::GetDataDir();
if (config == "")
2016-02-22 20:17:58 +00:00
{
config = i2p::fs::DataDirPath("i2p.conf");
// use i2p.cong only if exists
if (!i2p::fs::Exists (config)) config = ""; /* reset */
}
i2p::config::ParseConfig(config);
2016-01-20 00:00:00 +00:00
i2p::config::Finalize();
2015-12-31 21:02:10 +00:00
i2p::crypto::InitCrypto ();
2014-09-04 13:31:42 +00:00
i2p::context.Init ();
2016-01-20 00:00:00 +00:00
i2p::config::GetOption("daemon", isDaemon);
// TODO: move log init here
LogPrint(eLogInfo, "i2pd v", VERSION, " starting");
LogPrint(eLogDebug, "FS: main config file: ", config);
LogPrint(eLogDebug, "FS: data directory: ", datadir);
2014-04-20 01:54:34 +00:00
2016-01-20 00:00:00 +00:00
uint16_t port; i2p::config::GetOption("port", port);
if (!i2p::config::IsDefault("port"))
2016-02-01 23:10:45 +00:00
{
LogPrint(eLogInfo, "Daemon: accepting incoming connections at port ", port);
i2p::context.UpdatePort (port);
}
2016-01-24 12:41:08 +00:00
2016-01-20 00:00:00 +00:00
std::string host; i2p::config::GetOption("host", host);
if (!i2p::config::IsDefault("host"))
2016-02-01 23:10:45 +00:00
{
2016-02-03 14:21:22 +00:00
LogPrint(eLogInfo, "Daemon: setting address for incoming connections to ", host);
2014-10-29 17:49:21 +00:00
i2p::context.UpdateAddress (boost::asio::ip::address::from_string (host));
2016-01-24 12:41:08 +00:00
}
2014-04-20 01:54:34 +00:00
2016-01-20 00:00:00 +00: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 00:00:00 +00:00
2016-02-01 23:10:45 +00:00
if (isFloodfill)
{
LogPrint(eLogInfo, "Daemon: router will be floodfill");
i2p::context.SetFloodfill (true);
2016-02-01 23:10:45 +00:00
}
2016-02-11 12:50:29 +00:00
else
i2p::context.SetFloodfill (false);
2016-02-01 23:10:45 +00:00
if (bandwidth != '-')
{
LogPrint(eLogInfo, "Daemon: bandwidth set to ", bandwidth);
2016-02-01 23:10:45 +00:00
if (bandwidth > 'O')
i2p::context.SetExtraBandwidth ();
else if (bandwidth > 'L')
i2p::context.SetHighBandwidth ();
else
i2p::context.SetLowBandwidth ();
2016-02-21 01:20:19 +00:00
}
2016-02-01 23:10:45 +00:00
else if (isFloodfill)
{
LogPrint(eLogInfo, "Daemon: floodfill bandwidth set to 'extra'");
i2p::context.SetExtraBandwidth ();
}
else
{
LogPrint(eLogInfo, "Daemon: bandwidth set to 'low'");
2016-02-01 23:10:45 +00:00
i2p::context.SetLowBandwidth ();
}
2014-07-02 17:48:45 +00:00
2016-02-21 01:20:19 +00:00
std::string family; i2p::config::GetOption("family", family);
i2p::context.SetFamily (family);
if (family.length () > 0)
LogPrint(eLogInfo, "Daemon: family set to ", family);
2014-07-02 17:48:45 +00:00
return true;
}
bool Daemon_Singleton::start()
{
std::string logs = ""; i2p::config::GetOption("log", logs);
std::string logfile = ""; i2p::config::GetOption("logfile", logfile);
std::string loglevel = ""; i2p::config::GetOption("loglevel", loglevel);
if (isDaemon && (logs == "" || logs == "stdout"))
logs = "file";
if (logs == "file") {
if (logfile == "")
logfile = i2p::fs::DataDirPath("i2pd.log");
2016-02-01 00:00:00 +00:00
StartLog (logfile);
} else {
// use stdout
2016-02-02 17:16:29 +00:00
StartLog ("");
}
2016-02-02 17:16:29 +00:00
SetLogLevel(loglevel);
2016-01-20 00:00:00 +00: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 11:05:16 +00:00
LogPrint(eLogInfo, "Daemon: starting HTTP Server at ", httpAddr, ":", httpPort);
2016-01-20 00:00:00 +00:00
d.httpServer = std::unique_ptr<i2p::util::HTTPServer>(new i2p::util::HTTPServer(httpAddr, httpPort));
d.httpServer->Start();
}
2015-12-18 12:21:37 +00:00
LogPrint(eLogInfo, "Daemon: starting NetDB");
2014-04-20 01:54:34 +00:00
i2p::data::netdb.Start();
2015-12-18 12:21:37 +00:00
2015-11-03 14:15:49 +00:00
#ifdef USE_UPNP
2015-12-18 12:21:37 +00:00
LogPrint(eLogInfo, "Daemon: starting UPnP");
2015-11-03 14:15:49 +00:00
d.m_UPnP.Start ();
#endif
2015-12-18 12:21:37 +00:00
LogPrint(eLogInfo, "Daemon: starting Transports");
i2p::transport::transports.Start();
2015-12-18 12:21:37 +00:00
LogPrint(eLogInfo, "Daemon: starting Tunnels");
2014-04-20 01:54:34 +00:00
i2p::tunnel::tunnels.Start();
2015-12-18 12:21:37 +00:00
LogPrint(eLogInfo, "Daemon: starting Client");
2014-10-16 00:52:17 +00:00
i2p::client::context.Start ();
2015-12-18 12:21:37 +00:00
// I2P Control Protocol
2016-01-20 00:00:00 +00: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 14:15:49 +00:00
d.m_I2PControlService->Start ();
}
2014-04-20 01:54:34 +00:00
return true;
}
bool Daemon_Singleton::stop()
{
2015-12-18 12:21:37 +00:00
LogPrint(eLogInfo, "Daemon: shutting down");
LogPrint(eLogInfo, "Daemon: stopping Client");
2014-10-16 00:52:17 +00:00
i2p::client::context.Stop();
2015-12-18 12:21:37 +00:00
LogPrint(eLogInfo, "Daemon: stopping Tunnels");
2014-04-20 01:54:34 +00:00
i2p::tunnel::tunnels.Stop();
2015-11-03 14:15:49 +00:00
#ifdef USE_UPNP
2015-12-18 12:21:37 +00:00
LogPrint(eLogInfo, "Daemon: stopping UPnP");
2015-11-03 14:15:49 +00:00
d.m_UPnP.Stop ();
#endif
2015-12-18 12:21:37 +00:00
LogPrint(eLogInfo, "Daemon: stopping Transports");
i2p::transport::transports.Stop();
2015-12-18 12:21:37 +00:00
LogPrint(eLogInfo, "Daemon: stopping NetDB");
2014-04-20 01:54:34 +00:00
i2p::data::netdb.Stop();
if (d.httpServer) {
LogPrint(eLogInfo, "Daemon: stopping HTTP Server");
d.httpServer->Stop();
d.httpServer = nullptr;
}
2015-11-03 14:15:49 +00:00
if (d.m_I2PControlService)
{
2015-12-18 12:21:37 +00:00
LogPrint(eLogInfo, "Daemon: stopping I2PControl");
2015-11-03 14:15:49 +00:00
d.m_I2PControlService->Stop ();
d.m_I2PControlService = nullptr;
}
2015-12-31 21:02:10 +00:00
i2p::crypto::TerminateCrypto ();
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
}