2014-04-22 22:07:21 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2014-04-20 01:54:34 +00:00
|
|
|
#include "Daemon.h"
|
|
|
|
|
2015-07-30 13:34:56 +00:00
|
|
|
#include "util/Log.h"
|
2014-10-17 15:42:05 +00:00
|
|
|
#include "version.h"
|
2015-07-27 20:17:31 +00:00
|
|
|
#include "transport/Transports.h"
|
|
|
|
#include "transport/NTCPSession.h"
|
2014-04-20 01:54:34 +00:00
|
|
|
#include "RouterInfo.h"
|
|
|
|
#include "RouterContext.h"
|
2015-07-30 14:25:43 +00:00
|
|
|
#include "tunnel/Tunnel.h"
|
2015-08-26 20:17:10 +00:00
|
|
|
#include "NetworkDatabase.h"
|
2014-04-20 01:54:34 +00:00
|
|
|
#include "Garlic.h"
|
2015-07-28 16:37:06 +00:00
|
|
|
#include "util/util.h"
|
2014-04-20 01:54:34 +00:00
|
|
|
#include "Streaming.h"
|
2014-10-05 12:54:59 +00:00
|
|
|
#include "Destination.h"
|
2014-04-22 22:07:21 +00:00
|
|
|
#include "HTTPServer.h"
|
2014-10-16 00:52:17 +00:00
|
|
|
#include "ClientContext.h"
|
2014-04-20 01:54:34 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2015-07-16 21:29:52 +00: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 13:55:41 +00:00
|
|
|
#ifndef _WIN32
|
2015-07-16 21:29:52 +00:00
|
|
|
return i2p::util::config::GetArg("-service", 0);
|
2014-10-17 13:55:41 +00:00
|
|
|
#else
|
2015-07-16 21:29:52 +00:00
|
|
|
return false;
|
2014-10-17 13:55:41 +00:00
|
|
|
#endif
|
2015-07-16 21:29:52 +00: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());
|
2015-09-18 12:19:06 +00:00
|
|
|
i2p::util::filesystem::ReadConfigFile(
|
|
|
|
i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs
|
|
|
|
);
|
|
|
|
|
|
|
|
if(i2p::util::config::HasArg("-install")) {
|
|
|
|
try {
|
|
|
|
i2p::util::filesystem::InstallFiles();
|
|
|
|
LogPrint("Succesfully installed all files.");
|
|
|
|
} catch(const std::runtime_error& e) {
|
|
|
|
LogPrint(eLogError, "Failed to install: ", e.what());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-07-16 21:29:52 +00:00
|
|
|
|
|
|
|
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 20:38:48 +00:00
|
|
|
#ifndef _WIN32
|
2015-07-16 21:29:52 +00:00
|
|
|
logfile_path.append("/i2pd.log");
|
2015-01-03 20:38:48 +00:00
|
|
|
#else
|
2015-07-16 21:29:52 +00:00
|
|
|
logfile_path.append("\\i2pd.log");
|
2015-01-03 20:38:48 +00:00
|
|
|
#endif
|
2015-07-16 21:29:52 +00:00
|
|
|
StartLog (logfile_path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
StartLog (""); // write to stdout
|
|
|
|
}
|
2015-07-27 10:13:05 +00:00
|
|
|
d.httpServer = new i2p::util::HTTPServer(
|
|
|
|
i2p::util::config::GetArg("-httpaddress", "127.0.0.1"),
|
|
|
|
i2p::util::config::GetArg("-httpport", 7070)
|
|
|
|
);
|
2015-07-16 21:29:52 +00: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-22 22:37:24 +00:00
|
|
|
}
|