2014-04-22 22:07:21 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2014-04-20 01:54:34 +00:00
|
|
|
#include "Daemon.h"
|
|
|
|
|
|
|
|
#include "Log.h"
|
|
|
|
#include "base64.h"
|
|
|
|
#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"
|
2014-04-22 22:07:21 +00:00
|
|
|
#include "HTTPServer.h"
|
|
|
|
#include "HTTPProxy.h"
|
2014-07-14 16:40:06 +00:00
|
|
|
#include "SOCKS.h"
|
2014-08-14 14:20:22 +00:00
|
|
|
#include "I2PTunnel.h"
|
2014-04-20 01:54:34 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
2014-04-22 22:07:21 +00:00
|
|
|
class Daemon_Singleton::Daemon_Singleton_Private
|
2014-04-20 01:54:34 +00:00
|
|
|
{
|
2014-04-22 22:07:21 +00:00
|
|
|
public:
|
2014-08-14 14:20:22 +00:00
|
|
|
Daemon_Singleton_Private() : httpServer(nullptr), httpProxy(nullptr),
|
|
|
|
socksProxy(nullptr), ircTunnel(nullptr) { };
|
2014-04-22 22:07:21 +00:00
|
|
|
~Daemon_Singleton_Private() {
|
|
|
|
delete httpServer;
|
|
|
|
delete httpProxy;
|
2014-07-14 16:40:06 +00:00
|
|
|
delete socksProxy;
|
2014-08-14 14:20:22 +00:00
|
|
|
delete ircTunnel;
|
2014-04-22 22:07:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
i2p::util::HTTPServer *httpServer;
|
|
|
|
i2p::proxy::HTTPProxy *httpProxy;
|
2014-07-14 16:40:06 +00:00
|
|
|
i2p::proxy::SOCKSProxy *socksProxy;
|
2014-08-14 14:20:22 +00:00
|
|
|
i2p::stream::I2PClientTunnel * ircTunnel;
|
2014-04-22 22:07:21 +00:00
|
|
|
};
|
|
|
|
|
2014-04-23 00:43:58 +00:00
|
|
|
Daemon_Singleton::Daemon_Singleton() : running(1), d(*new Daemon_Singleton_Private()) {};
|
2014-04-22 22:07:21 +00:00
|
|
|
Daemon_Singleton::~Daemon_Singleton() {
|
|
|
|
delete &d;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool Daemon_Singleton::init(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
i2p::util::config::OptionParser(argc, argv);
|
|
|
|
|
|
|
|
LogPrint("\n\n\n\ni2pd starting\n");
|
|
|
|
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);
|
2014-04-22 22:07:21 +00:00
|
|
|
isLogging = i2p::util::config::GetArg("-log", 1);
|
2014-04-20 01:54:34 +00:00
|
|
|
|
|
|
|
//TODO: This is an ugly workaround. fix it.
|
|
|
|
//TODO: Autodetect public IP.
|
|
|
|
i2p::context.OverrideNTCPAddress(i2p::util::config::GetCharArg("-host", "127.0.0.1"),
|
2014-06-12 13:12:39 +00:00
|
|
|
i2p::util::config::GetArg("-port", 17007));
|
2014-04-20 01:54:34 +00:00
|
|
|
|
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-07-02 18:25:57 +00:00
|
|
|
if (isDaemon)
|
|
|
|
{
|
|
|
|
std::string logfile_path = i2p::util::filesystem::GetDataDir().string();
|
|
|
|
#ifndef _WIN32
|
|
|
|
logfile_path.append("/debug.log");
|
|
|
|
#else
|
|
|
|
logfile_path.append("\\debug.log");
|
|
|
|
#endif
|
|
|
|
StartLog (logfile_path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
StartLog (""); // write to stdout
|
2014-04-22 22:07:21 +00:00
|
|
|
}
|
2014-07-02 17:48:45 +00:00
|
|
|
|
2014-04-22 22:07:21 +00:00
|
|
|
d.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");
|
2014-04-20 01:54:34 +00:00
|
|
|
i2p::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-04-20 01:54:34 +00:00
|
|
|
i2p::garlic::routing.Start();
|
2014-04-22 22:37:24 +00:00
|
|
|
LogPrint("Routing started");
|
2014-04-20 01:54:34 +00:00
|
|
|
i2p::stream::StartStreaming();
|
2014-04-22 22:37:24 +00:00
|
|
|
LogPrint("Streaming started");
|
2014-04-20 01:54:34 +00:00
|
|
|
|
2014-04-22 22:07:21 +00:00
|
|
|
d.httpProxy = new i2p::proxy::HTTPProxy(i2p::util::config::GetArg("-httpproxyport", 4446));
|
|
|
|
d.httpProxy->Start();
|
2014-07-14 16:40:06 +00:00
|
|
|
LogPrint("HTTP Proxy started");
|
|
|
|
d.socksProxy = new i2p::proxy::SOCKSProxy(i2p::util::config::GetArg("-socksproxyport", 4447));
|
|
|
|
d.socksProxy->Start();
|
|
|
|
LogPrint("SOCKS Proxy Started");
|
2014-08-14 14:20:22 +00:00
|
|
|
std::string ircDestination = i2p::util::config::GetArg("-ircdest", "");
|
|
|
|
if (ircDestination.length () > 0) // ircdest is presented
|
|
|
|
{
|
|
|
|
d.ircTunnel = new i2p::stream::I2PClientTunnel (d.socksProxy->GetService (), ircDestination,
|
|
|
|
i2p::util::config::GetArg("-ircport", 6668));
|
|
|
|
d.ircTunnel->Start ();
|
|
|
|
LogPrint("IRC tunnel started");
|
|
|
|
}
|
2014-04-20 01:54:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Daemon_Singleton::stop()
|
|
|
|
{
|
|
|
|
LogPrint("Shutdown started.");
|
|
|
|
|
2014-04-22 22:07:21 +00:00
|
|
|
d.httpProxy->Stop();
|
2014-07-14 16:40:06 +00:00
|
|
|
LogPrint("HTTP Proxy stoped");
|
|
|
|
d.socksProxy->Stop();
|
|
|
|
LogPrint("SOCKS Proxy stoped");
|
2014-04-20 01:54:34 +00:00
|
|
|
i2p::stream::StopStreaming();
|
2014-04-22 22:37:24 +00:00
|
|
|
LogPrint("Streaming stoped");
|
2014-04-20 01:54:34 +00:00
|
|
|
i2p::garlic::routing.Stop();
|
2014-04-22 22:37:24 +00:00
|
|
|
LogPrint("Routing stoped");
|
2014-04-20 01:54:34 +00:00
|
|
|
i2p::tunnel::tunnels.Stop();
|
2014-04-22 22:37:24 +00:00
|
|
|
LogPrint("Tunnels stoped");
|
2014-04-20 01:54:34 +00:00
|
|
|
i2p::transports.Stop();
|
2014-04-22 22:37:24 +00:00
|
|
|
LogPrint("Transports stoped");
|
2014-04-20 01:54:34 +00:00
|
|
|
i2p::data::netdb.Stop();
|
2014-04-22 22:37:24 +00:00
|
|
|
LogPrint("NetDB stoped");
|
2014-04-22 22:07:21 +00:00
|
|
|
d.httpServer->Stop();
|
2014-07-14 16:40:06 +00:00
|
|
|
LogPrint("HTTP Server stoped");
|
2014-08-14 14:20:22 +00:00
|
|
|
if (d.ircTunnel)
|
|
|
|
{
|
|
|
|
d.ircTunnel->Stop ();
|
|
|
|
delete d.ircTunnel;
|
|
|
|
d.ircTunnel = nullptr;
|
|
|
|
LogPrint("IRC tunnel stoped");
|
|
|
|
}
|
2014-07-02 17:48:45 +00:00
|
|
|
StopLog ();
|
2014-08-14 14:20:22 +00:00
|
|
|
|
|
|
|
delete d.socksProxy; d.socksProxy = nullptr;
|
2014-04-22 22:07:21 +00:00
|
|
|
delete d.httpProxy; d.httpProxy = nullptr;
|
|
|
|
delete d.httpServer; d.httpServer = nullptr;
|
2014-04-20 01:54:34 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 22:37:24 +00:00
|
|
|
}
|