2014-04-23 02:07:21 +04:00
|
|
|
#include <thread>
|
|
|
|
|
2014-04-20 05:54:34 +04:00
|
|
|
#include "Daemon.h"
|
|
|
|
|
2015-07-30 16:34:56 +03:00
|
|
|
#include "util/Log.h"
|
2014-10-17 19:42:05 +04:00
|
|
|
#include "version.h"
|
2015-07-27 23:17:31 +03:00
|
|
|
#include "transport/Transports.h"
|
|
|
|
#include "transport/NTCPSession.h"
|
2014-04-20 05:54:34 +04:00
|
|
|
#include "RouterInfo.h"
|
|
|
|
#include "RouterContext.h"
|
2015-07-30 17:25:43 +03:00
|
|
|
#include "tunnel/Tunnel.h"
|
2014-04-20 05:54:34 +04:00
|
|
|
#include "NetDb.h"
|
|
|
|
#include "Garlic.h"
|
2015-07-28 19:37:06 +03:00
|
|
|
#include "util/util.h"
|
2014-04-20 05:54:34 +04:00
|
|
|
#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"
|
2014-10-16 04:52:17 +04:00
|
|
|
#include "ClientContext.h"
|
2014-04-20 05:54:34 +04:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2015-07-17 00:29:52 +03:00
|
|
|
namespace util
|
|
|
|
{
|
|
|
|
class Daemon_Singleton::Daemon_Singleton_Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Daemon_Singleton_Private() : httpServer(nullptr)
|
|
|
|
{};
|
|
|
|
~Daemon_Singleton_Private()
|
|
|
|
{
|
|
|
|
delete httpServer;
|
|
|
|
};
|
|
|
|
|
|
|
|
i2p::util::HTTPServer *httpServer;
|
|
|
|
};
|
|
|
|
|
|
|
|
Daemon_Singleton::Daemon_Singleton() : running(1), d(*new Daemon_Singleton_Private()) {};
|
|
|
|
Daemon_Singleton::~Daemon_Singleton() {
|
|
|
|
delete &d;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool Daemon_Singleton::IsService () const
|
|
|
|
{
|
2014-10-17 17:55:41 +04:00
|
|
|
#ifndef _WIN32
|
2015-07-17 00:29:52 +03:00
|
|
|
return i2p::util::config::GetArg("-service", 0);
|
2014-10-17 17:55:41 +04:00
|
|
|
#else
|
2015-07-17 00:29:52 +03:00
|
|
|
return false;
|
2014-10-17 17:55:41 +04:00
|
|
|
#endif
|
2015-07-17 00:29:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Daemon_Singleton::init(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
i2p::util::config::OptionParser(argc, argv);
|
|
|
|
i2p::context.Init ();
|
|
|
|
|
|
|
|
LogPrint("\n\n\n\ni2pd starting\n");
|
|
|
|
LogPrint("Version ", VERSION);
|
|
|
|
LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string());
|
|
|
|
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
|
|
|
|
|
|
|
|
isDaemon = i2p::util::config::GetArg("-daemon", 0);
|
|
|
|
isLogging = i2p::util::config::GetArg("-log", 1);
|
|
|
|
|
|
|
|
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])
|
|
|
|
i2p::context.UpdateAddress (boost::asio::ip::address::from_string (host));
|
|
|
|
|
|
|
|
i2p::context.SetSupportsV6 (i2p::util::config::GetArg("-v6", 0));
|
|
|
|
i2p::context.SetFloodfill (i2p::util::config::GetArg("-floodfill", 0));
|
|
|
|
auto bandwidth = i2p::util::config::GetArg("-bandwidth", "");
|
|
|
|
if (bandwidth.length () > 0)
|
|
|
|
{
|
|
|
|
if (bandwidth[0] > 'L')
|
|
|
|
i2p::context.SetHighBandwidth ();
|
|
|
|
else
|
|
|
|
i2p::context.SetLowBandwidth ();
|
|
|
|
}
|
|
|
|
|
|
|
|
LogPrint("CMD parameters:");
|
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
LogPrint(i, " ", argv[i]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Daemon_Singleton::start()
|
|
|
|
{
|
|
|
|
// initialize log
|
|
|
|
if (isLogging)
|
|
|
|
{
|
|
|
|
if (isDaemon)
|
|
|
|
{
|
|
|
|
std::string logfile_path = IsService () ? "/var/log" : i2p::util::filesystem::GetDataDir().string();
|
2015-01-03 23:38:48 +03:00
|
|
|
#ifndef _WIN32
|
2015-07-17 00:29:52 +03:00
|
|
|
logfile_path.append("/i2pd.log");
|
2015-01-03 23:38:48 +03:00
|
|
|
#else
|
2015-07-17 00:29:52 +03:00
|
|
|
logfile_path.append("\\i2pd.log");
|
2015-01-03 23:38:48 +03:00
|
|
|
#endif
|
2015-07-17 00:29:52 +03:00
|
|
|
StartLog (logfile_path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
StartLog (""); // write to stdout
|
|
|
|
}
|
|
|
|
|
2015-07-27 13:13:05 +03:00
|
|
|
d.httpServer = new i2p::util::HTTPServer(
|
|
|
|
i2p::util::config::GetArg("-httpaddress", "127.0.0.1"),
|
|
|
|
i2p::util::config::GetArg("-httpport", 7070)
|
|
|
|
);
|
2015-07-17 00:29:52 +03:00
|
|
|
d.httpServer->Start();
|
|
|
|
LogPrint("HTTP Server started");
|
|
|
|
i2p::data::netdb.Start();
|
|
|
|
LogPrint("NetDB started");
|
|
|
|
i2p::transport::transports.Start();
|
|
|
|
LogPrint("Transports started");
|
|
|
|
i2p::tunnel::tunnels.Start();
|
|
|
|
LogPrint("Tunnels started");
|
|
|
|
i2p::client::context.Start ();
|
|
|
|
LogPrint("Client started");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Daemon_Singleton::stop()
|
|
|
|
{
|
|
|
|
LogPrint("Shutdown started.");
|
|
|
|
i2p::client::context.Stop();
|
|
|
|
LogPrint("Client stopped");
|
|
|
|
i2p::tunnel::tunnels.Stop();
|
|
|
|
LogPrint("Tunnels stopped");
|
|
|
|
i2p::transport::transports.Stop();
|
|
|
|
LogPrint("Transports stopped");
|
|
|
|
i2p::data::netdb.Stop();
|
|
|
|
LogPrint("NetDB stopped");
|
|
|
|
d.httpServer->Stop();
|
|
|
|
LogPrint("HTTP Server stopped");
|
|
|
|
|
|
|
|
StopLog ();
|
|
|
|
|
|
|
|
delete d.httpServer; d.httpServer = nullptr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 02:37:24 +04:00
|
|
|
}
|