2020-05-22 13:18:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2020, The PurpleI2P Project
|
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2016-06-27 21:00:54 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <clocale>
|
|
|
|
#include "Config.h"
|
|
|
|
#include "Daemon.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2020-11-17 14:59:40 +00:00
|
|
|
#include "Win32Service.h"
|
2016-06-27 21:00:54 +00:00
|
|
|
#ifdef WIN32_APP
|
2020-05-05 15:13:59 +00:00
|
|
|
#include <windows.h>
|
2020-11-17 14:59:40 +00:00
|
|
|
#include "Win32App.h"
|
2016-06-27 21:00:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2017-09-27 20:28:58 +00:00
|
|
|
namespace util
|
|
|
|
{
|
|
|
|
bool DaemonWin32::init(int argc, char* argv[])
|
2016-06-27 21:00:54 +00:00
|
|
|
{
|
2017-09-27 20:28:58 +00:00
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
SetConsoleCP(1251);
|
|
|
|
SetConsoleOutputCP(1251);
|
|
|
|
setlocale(LC_ALL, "Russian");
|
2018-02-10 00:10:32 +00:00
|
|
|
setlocale(LC_TIME, "C");
|
2016-06-27 21:00:54 +00:00
|
|
|
|
2020-05-05 15:13:59 +00:00
|
|
|
i2p::log::SetThrowFunction ([](const std::string& s)
|
|
|
|
{
|
|
|
|
MessageBox(0, TEXT(s.c_str ()), TEXT("i2pd"), MB_ICONERROR | MB_TASKMODAL | MB_OK );
|
2020-03-01 10:25:50 +00:00
|
|
|
});
|
|
|
|
|
2017-09-27 20:28:58 +00:00
|
|
|
if (!Daemon_Singleton::init(argc, argv))
|
|
|
|
return false;
|
2016-06-27 21:00:54 +00:00
|
|
|
|
2017-09-27 20:28:58 +00:00
|
|
|
std::string serviceControl; i2p::config::GetOption("svcctl", serviceControl);
|
|
|
|
if (serviceControl == "install")
|
|
|
|
{
|
|
|
|
LogPrint(eLogInfo, "WinSVC: installing ", SERVICE_NAME, " as service");
|
|
|
|
InstallService(
|
|
|
|
SERVICE_NAME, // Name of service
|
|
|
|
SERVICE_DISPLAY_NAME, // Name to display
|
|
|
|
SERVICE_START_TYPE, // Service start type
|
|
|
|
SERVICE_DEPENDENCIES, // Dependencies
|
|
|
|
SERVICE_ACCOUNT, // Service running account
|
|
|
|
SERVICE_PASSWORD // Password of the account
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (serviceControl == "remove")
|
|
|
|
{
|
|
|
|
LogPrint(eLogInfo, "WinSVC: uninstalling ", SERVICE_NAME, " service");
|
|
|
|
UninstallService(SERVICE_NAME);
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-27 21:00:54 +00:00
|
|
|
|
2017-09-27 20:28:58 +00:00
|
|
|
if (isDaemon)
|
|
|
|
{
|
|
|
|
LogPrint(eLogDebug, "Daemon: running as service");
|
2019-02-05 21:24:01 +00:00
|
|
|
I2PService service((PSTR)SERVICE_NAME);
|
2017-09-27 20:28:58 +00:00
|
|
|
if (!I2PService::Run(service))
|
2016-06-27 21:00:54 +00:00
|
|
|
{
|
2017-09-27 20:28:58 +00:00
|
|
|
LogPrint(eLogError, "Daemon: Service failed to run w/err 0x%08lx\n", GetLastError());
|
2016-06-27 21:00:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-27 20:28:58 +00:00
|
|
|
return false;
|
2016-06-27 21:00:54 +00:00
|
|
|
}
|
2017-09-27 20:28:58 +00:00
|
|
|
else
|
|
|
|
LogPrint(eLogDebug, "Daemon: running as user");
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-27 21:00:54 +00:00
|
|
|
|
2017-09-27 20:28:58 +00:00
|
|
|
bool DaemonWin32::start()
|
|
|
|
{
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
SetConsoleCP(1251);
|
|
|
|
SetConsoleOutputCP(1251);
|
|
|
|
setlocale(LC_ALL, "Russian");
|
2018-02-10 00:10:32 +00:00
|
|
|
setlocale(LC_TIME, "C");
|
2016-06-27 21:00:54 +00:00
|
|
|
#ifdef WIN32_APP
|
2017-09-27 20:28:58 +00:00
|
|
|
if (!i2p::win32::StartWin32App ()) return false;
|
2016-06-27 21:00:54 +00:00
|
|
|
|
2017-09-27 20:28:58 +00:00
|
|
|
// override log
|
|
|
|
i2p::config::SetOption("log", std::string ("file"));
|
2016-06-27 21:00:54 +00:00
|
|
|
#endif
|
2017-09-27 20:28:58 +00:00
|
|
|
bool ret = Daemon_Singleton::start();
|
|
|
|
if (ret && i2p::log::Logger().GetLogType() == eLogFile)
|
|
|
|
{
|
|
|
|
// TODO: find out where this garbage to console comes from
|
|
|
|
SetStdHandle(STD_OUTPUT_HANDLE, INVALID_HANDLE_VALUE);
|
|
|
|
SetStdHandle(STD_ERROR_HANDLE, INVALID_HANDLE_VALUE);
|
2016-06-27 21:00:54 +00:00
|
|
|
}
|
2017-09-27 20:28:58 +00:00
|
|
|
bool insomnia; i2p::config::GetOption("insomnia", insomnia);
|
|
|
|
if (insomnia)
|
|
|
|
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-06-27 21:00:54 +00:00
|
|
|
|
2017-09-27 20:28:58 +00:00
|
|
|
bool DaemonWin32::stop()
|
|
|
|
{
|
2016-06-27 21:00:54 +00:00
|
|
|
#ifdef WIN32_APP
|
2017-09-27 20:28:58 +00:00
|
|
|
i2p::win32::StopWin32App ();
|
2016-06-27 21:00:54 +00:00
|
|
|
#endif
|
2017-09-27 20:28:58 +00:00
|
|
|
return Daemon_Singleton::stop();
|
|
|
|
}
|
2016-06-27 21:00:54 +00:00
|
|
|
|
2017-09-27 20:28:58 +00:00
|
|
|
void DaemonWin32::run ()
|
|
|
|
{
|
2016-06-27 21:00:54 +00:00
|
|
|
#ifdef WIN32_APP
|
2017-09-27 20:28:58 +00:00
|
|
|
i2p::win32::RunWin32App ();
|
2016-06-27 21:00:54 +00:00
|
|
|
#else
|
2017-09-27 20:28:58 +00:00
|
|
|
while (running)
|
2016-06-27 21:00:54 +00:00
|
|
|
{
|
|
|
|
std::this_thread::sleep_for (std::chrono::seconds(1));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2017-09-27 20:28:58 +00:00
|
|
|
}
|
2017-10-04 17:15:29 +00:00
|
|
|
#endif //_WIN32
|