Browse Source

[windows] add binding exceptions messagebox notifications, update exceptions handling code

Signed-off-by: R4SAS <r4sas@i2pmail.org>
pull/1515/head
R4SAS 5 years ago
parent
commit
42d4781a96
Signed by: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 3
      Win32/Win32App.cpp
  2. 25
      daemon/Daemon.cpp
  3. 6
      daemon/HTTPServer.cpp
  4. 43
      libi2pd/Log.h
  5. 10
      libi2pd/NTCP2.cpp
  6. 10
      libi2pd/NTCPSession.cpp
  7. 6
      libi2pd/SSU.cpp
  8. 68
      libi2pd_client/ClientContext.cpp
  9. 6
      libi2pd_client/I2PService.cpp

3
Win32/Win32App.cpp

@ -432,7 +432,7 @@ namespace win32
HWND hWnd = FindWindow (I2PD_WIN32_CLASSNAME, TEXT("i2pd")); HWND hWnd = FindWindow (I2PD_WIN32_CLASSNAME, TEXT("i2pd"));
if (hWnd) if (hWnd)
PostMessage (hWnd, WM_COMMAND, MAKEWPARAM(ID_EXIT, 0), 0); PostMessage (hWnd, WM_COMMAND, MAKEWPARAM(ID_EXIT, 0), 0);
UnSubscribeFromEvents(); // UnSubscribeFromEvents(); // TODO: understand why unsubscribing crashes app
UnregisterClass (I2PD_WIN32_CLASSNAME, GetModuleHandle(NULL)); UnregisterClass (I2PD_WIN32_CLASSNAME, GetModuleHandle(NULL));
} }
@ -451,6 +451,5 @@ namespace win32
PostMessage (hWnd, WM_COMMAND, MAKEWPARAM(ID_STOP_GRACEFUL_SHUTDOWN, 0), 0); PostMessage (hWnd, WM_COMMAND, MAKEWPARAM(ID_STOP_GRACEFUL_SHUTDOWN, 0), 0);
return hWnd; return hWnd;
} }
} }
} }

25
daemon/Daemon.cpp

@ -71,20 +71,15 @@ namespace i2p
i2p::fs::Init(); i2p::fs::Init();
datadir = i2p::fs::GetDataDir(); datadir = i2p::fs::GetDataDir();
// TODO: drop old name detection in v2.8.0
if (config == "") if (config == "")
{ {
config = i2p::fs::DataDirPath("i2p.conf");
if (i2p::fs::Exists (config)) {
LogPrint(eLogWarning, "Daemon: please rename i2p.conf to i2pd.conf here: ", config);
} else {
config = i2p::fs::DataDirPath("i2pd.conf"); config = i2p::fs::DataDirPath("i2pd.conf");
if (!i2p::fs::Exists (config)) { if (!i2p::fs::Exists (config)) {
// use i2pd.conf only if exists // use i2pd.conf only if exists
config = ""; /* reset */ config = ""; /* reset */
} }
} }
}
i2p::config::ParseConfig(config); i2p::config::ParseConfig(config);
i2p::config::Finalize(); i2p::config::Finalize();
@ -265,7 +260,7 @@ namespace i2p
restricted = idents.size() > 0; restricted = idents.size() > 0;
} }
if(!restricted) if(!restricted)
LogPrint(eLogError, "Daemon: no trusted routers of families specififed"); LogPrint(eLogError, "Daemon: no trusted routers of families specified");
} }
bool hidden; i2p::config::GetOption("trust.hidden", hidden); bool hidden; i2p::config::GetOption("trust.hidden", hidden);
@ -318,9 +313,16 @@ namespace i2p
if (http) { if (http) {
std::string httpAddr; i2p::config::GetOption("http.address", httpAddr); std::string httpAddr; i2p::config::GetOption("http.address", httpAddr);
uint16_t httpPort; i2p::config::GetOption("http.port", httpPort); uint16_t httpPort; i2p::config::GetOption("http.port", httpPort);
LogPrint(eLogInfo, "Daemon: starting HTTP Server at ", httpAddr, ":", httpPort); LogPrint(eLogInfo, "Daemon: starting webconsole at ", httpAddr, ":", httpPort);
try {
d.httpServer = std::unique_ptr<i2p::http::HTTPServer>(new i2p::http::HTTPServer(httpAddr, httpPort)); d.httpServer = std::unique_ptr<i2p::http::HTTPServer>(new i2p::http::HTTPServer(httpAddr, httpPort));
d.httpServer->Start(); d.httpServer->Start();
} catch (std::exception& ex) {
LogPrint (eLogError, "Daemon: failed to start webconsole: ", ex.what ());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start webconsole at ", httpAddr, ":", httpPort, ": ", ex.what ());
#endif
}
} }
@ -336,8 +338,15 @@ namespace i2p
std::string i2pcpAddr; i2p::config::GetOption("i2pcontrol.address", i2pcpAddr); std::string i2pcpAddr; i2p::config::GetOption("i2pcontrol.address", i2pcpAddr);
uint16_t i2pcpPort; i2p::config::GetOption("i2pcontrol.port", i2pcpPort); uint16_t i2pcpPort; i2p::config::GetOption("i2pcontrol.port", i2pcpPort);
LogPrint(eLogInfo, "Daemon: starting I2PControl at ", i2pcpAddr, ":", i2pcpPort); LogPrint(eLogInfo, "Daemon: starting I2PControl at ", i2pcpAddr, ":", i2pcpPort);
try {
d.m_I2PControlService = std::unique_ptr<i2p::client::I2PControlService>(new i2p::client::I2PControlService (i2pcpAddr, i2pcpPort)); d.m_I2PControlService = std::unique_ptr<i2p::client::I2PControlService>(new i2p::client::I2PControlService (i2pcpAddr, i2pcpPort));
d.m_I2PControlService->Start (); d.m_I2PControlService->Start ();
} catch (std::exception& ex) {
LogPrint (eLogError, "Daemon: failed to start I2PControl: ", ex.what ());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start I2PControl service at ", i2pcpAddr, ":", i2pcpPort, ": ", ex.what ());
#endif
}
} }
return true; return true;
} }

6
daemon/HTTPServer.cpp

@ -1227,14 +1227,10 @@ namespace http {
LogPrint(eLogInfo, "HTTPServer: password set to ", pass); LogPrint(eLogInfo, "HTTPServer: password set to ", pass);
} }
try {
m_IsRunning = true;
m_Thread = std::unique_ptr<std::thread>(new std::thread (std::bind (&HTTPServer::Run, this))); m_Thread = std::unique_ptr<std::thread>(new std::thread (std::bind (&HTTPServer::Run, this)));
m_Acceptor.listen (); m_Acceptor.listen ();
Accept (); Accept ();
} catch (std::exception& ex) { m_IsRunning = true;
LogPrint (eLogError, "HTTPServer: failed to start webconsole: ", ex.what ());
}
} }
void HTTPServer::Stop () void HTTPServer::Stop ()

43
libi2pd/Log.h

@ -23,6 +23,11 @@
#include <syslog.h> #include <syslog.h>
#endif #endif
#ifdef WIN32_APP
#include <windows.h>
#include "Win32/Win32App.h"
#endif
enum LogLevel enum LogLevel
{ {
eLogNone = 0, eLogNone = 0,
@ -197,4 +202,42 @@ void LogPrint (LogLevel level, TArgs&&... args) noexcept
log.Append(msg); log.Append(msg);
} }
#ifdef WIN32_APP
/**
* @brief Show message box for user with message in it
* @param level Message level (eLogError, eLogInfo, ...)
* @param args Array of message parts
*/
template<typename... TArgs>
void ShowMessageBox (LogLevel level, TArgs&&... args) noexcept
{
// fold message to single string
std::stringstream ss("");
#if (__cplusplus >= 201703L) // C++ 17 or higher
(LogPrint (ss, std::forward<TArgs>(args)), ...);
#else
LogPrint (ss, std::forward<TArgs>(args)...);
#endif
HWND hWnd = FindWindow (I2PD_WIN32_CLASSNAME, TEXT("i2pd"));
if (!hWnd) hWnd = NULL;
UINT uType;
switch (level) {
case eLogError :
case eLogWarning :
uType = MB_ICONWARNING;
break;
case eLogNone :
case eLogInfo :
case eLogDebug :
default :
uType = MB_ICONINFORMATION;
break;
}
MessageBox( hWnd, TEXT(ss.str ().c_str ()), TEXT("i2pd"), uType | MB_TASKMODAL | MB_OK );
}
#endif // WIN32_APP
#endif // LOG_H__ #endif // LOG_H__

10
libi2pd/NTCP2.cpp

@ -1195,7 +1195,10 @@ namespace transport
} }
catch ( std::exception & ex ) catch ( std::exception & ex )
{ {
LogPrint(eLogError, "NTCP2: Failed to bind to v4 port ",address->port, ex.what()); LogPrint(eLogError, "NTCP2: Failed to bind to v4 port ", address->port, ex.what());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start IPv4 NTCP2 transport at port ", address->port, ": ", ex.what ());
#endif
continue; continue;
} }
@ -1218,7 +1221,10 @@ namespace transport
auto conn = std::make_shared<NTCP2Session> (*this); auto conn = std::make_shared<NTCP2Session> (*this);
m_NTCP2V6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAcceptV6, this, conn, std::placeholders::_1)); m_NTCP2V6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCP2Server::HandleAcceptV6, this, conn, std::placeholders::_1));
} catch ( std::exception & ex ) { } catch ( std::exception & ex ) {
LogPrint(eLogError, "NTCP2: failed to bind to v6 port ", address->port); LogPrint(eLogError, "NTCP2: failed to bind to v6 port ", address->port, ": ", ex.what());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start IPv6 NTCP2 transport at port ", address->port, ": ", ex.what ());
#endif
continue; continue;
} }
} }

10
libi2pd/NTCPSession.cpp

@ -823,7 +823,10 @@ namespace transport
LogPrint (eLogInfo, "NTCP: Start listening v6 TCP port ", address->port); LogPrint (eLogInfo, "NTCP: Start listening v6 TCP port ", address->port);
} catch ( std::exception & ex ) { } catch ( std::exception & ex ) {
/** fail to bind ip4 */ /** fail to bind ip4 */
LogPrint(eLogError, "NTCP: Failed to bind to v4 port ",address->port, ex.what()); LogPrint(eLogError, "NTCP: Failed to bind to v4 port ", address->port, ": ", ex.what());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start IPv4 NTCP transport at port ", address->port, ": ", ex.what ());
#endif
continue; continue;
} }
@ -846,7 +849,10 @@ namespace transport
auto conn = std::make_shared<NTCPSession> (*this); auto conn = std::make_shared<NTCPSession> (*this);
m_NTCPV6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAcceptV6, this, conn, std::placeholders::_1)); m_NTCPV6Acceptor->async_accept(conn->GetSocket (), std::bind (&NTCPServer::HandleAcceptV6, this, conn, std::placeholders::_1));
} catch ( std::exception & ex ) { } catch ( std::exception & ex ) {
LogPrint(eLogError, "NTCP: failed to bind to v6 port ", address->port); LogPrint(eLogError, "NTCP: failed to bind to v6 port ", address->port, ": ", ex.what());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start IPv6 NTCP transport at port ", address->port, ": ", ex.what ());
#endif
continue; continue;
} }
} }

6
libi2pd/SSU.cpp

@ -53,6 +53,9 @@ namespace transport
LogPrint (eLogInfo, "SSU: Start listening v4 port ", m_Endpoint.port()); LogPrint (eLogInfo, "SSU: Start listening v4 port ", m_Endpoint.port());
} catch ( std::exception & ex ) { } catch ( std::exception & ex ) {
LogPrint (eLogError, "SSU: failed to bind to v4 port ", m_Endpoint.port(), ": ", ex.what()); LogPrint (eLogError, "SSU: failed to bind to v4 port ", m_Endpoint.port(), ": ", ex.what());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start IPv4 SSU transport at port ", m_Endpoint.port(), ": ", ex.what ());
#endif
} }
} }
@ -67,6 +70,9 @@ namespace transport
LogPrint (eLogInfo, "SSU: Start listening v6 port ", m_EndpointV6.port()); LogPrint (eLogInfo, "SSU: Start listening v6 port ", m_EndpointV6.port());
} catch ( std::exception & ex ) { } catch ( std::exception & ex ) {
LogPrint (eLogError, "SSU: failed to bind to v6 port ", m_EndpointV6.port(), ": ", ex.what()); LogPrint (eLogError, "SSU: failed to bind to v6 port ", m_EndpointV6.port(), ": ", ex.what());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start IPv6 SSU transport at port ", m_Endpoint.port(), ": ", ex.what ());
#endif
} }
} }

68
libi2pd_client/ClientContext.cpp

@ -58,14 +58,14 @@ namespace client
uint16_t samPort; i2p::config::GetOption("sam.port", samPort); uint16_t samPort; i2p::config::GetOption("sam.port", samPort);
bool singleThread; i2p::config::GetOption("sam.singlethread", singleThread); bool singleThread; i2p::config::GetOption("sam.singlethread", singleThread);
LogPrint(eLogInfo, "Clients: starting SAM bridge at ", samAddr, ":", samPort); LogPrint(eLogInfo, "Clients: starting SAM bridge at ", samAddr, ":", samPort);
try try {
{
m_SamBridge = new SAMBridge (samAddr, samPort, singleThread); m_SamBridge = new SAMBridge (samAddr, samPort, singleThread);
m_SamBridge->Start (); m_SamBridge->Start ();
} } catch (std::exception& e) {
catch (std::exception& e)
{
LogPrint(eLogError, "Clients: Exception in SAM bridge: ", e.what()); LogPrint(eLogError, "Clients: Exception in SAM bridge: ", e.what());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start SAM bridge at ", samAddr, ":", samPort, ": ", e.what ());
#endif
} }
} }
@ -80,6 +80,9 @@ namespace client
m_BOBCommandChannel->Start (); m_BOBCommandChannel->Start ();
} catch (std::exception& e) { } catch (std::exception& e) {
LogPrint(eLogError, "Clients: Exception in BOB bridge: ", e.what()); LogPrint(eLogError, "Clients: Exception in BOB bridge: ", e.what());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start BOB bridge at ", bobAddr, ":", bobPort, ": ", e.what ());
#endif
} }
} }
@ -90,14 +93,14 @@ namespace client
std::string i2cpAddr; i2p::config::GetOption("i2cp.address", i2cpAddr); std::string i2cpAddr; i2p::config::GetOption("i2cp.address", i2cpAddr);
uint16_t i2cpPort; i2p::config::GetOption("i2cp.port", i2cpPort); uint16_t i2cpPort; i2p::config::GetOption("i2cp.port", i2cpPort);
LogPrint(eLogInfo, "Clients: starting I2CP at ", i2cpAddr, ":", i2cpPort); LogPrint(eLogInfo, "Clients: starting I2CP at ", i2cpAddr, ":", i2cpPort);
try try {
{
m_I2CPServer = new I2CPServer (i2cpAddr, i2cpPort); m_I2CPServer = new I2CPServer (i2cpAddr, i2cpPort);
m_I2CPServer->Start (); m_I2CPServer->Start ();
} } catch (std::exception& e) {
catch (std::exception& e)
{
LogPrint(eLogError, "Clients: Exception in I2CP: ", e.what()); LogPrint(eLogError, "Clients: Exception in I2CP: ", e.what());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start I2CP at ", i2cpAddr, ":", i2cpPort, ": ", e.what ());
#endif
} }
} }
@ -501,16 +504,12 @@ namespace client
LogPrint (eLogInfo, "Clients: ", numServerTunnels, " I2P server tunnels created"); LogPrint (eLogInfo, "Clients: ", numServerTunnels, " I2P server tunnels created");
} }
void ClientContext::ReadTunnels (const std::string& tunConf, int& numClientTunnels, int& numServerTunnels) void ClientContext::ReadTunnels (const std::string& tunConf, int& numClientTunnels, int& numServerTunnels)
{ {
boost::property_tree::ptree pt; boost::property_tree::ptree pt;
try try {
{
boost::property_tree::read_ini (tunConf, pt); boost::property_tree::read_ini (tunConf, pt);
} } catch (std::exception& ex) {
catch (std::exception& ex)
{
LogPrint (eLogWarning, "Clients: Can't read ", tunConf, ": ", ex.what ()); LogPrint (eLogWarning, "Clients: Can't read ", tunConf, ": ", ex.what ());
return; return;
} }
@ -565,14 +564,11 @@ namespace client
// TODO: hostnames // TODO: hostnames
boost::asio::ip::udp::endpoint end(boost::asio::ip::address::from_string(address), port); boost::asio::ip::udp::endpoint end(boost::asio::ip::address::from_string(address), port);
if (!localDestination) if (!localDestination)
{
localDestination = m_SharedLocalDestination; localDestination = m_SharedLocalDestination;
}
auto clientTunnel = std::make_shared<I2PUDPClientTunnel>(name, dest, end, localDestination, destinationPort); auto clientTunnel = std::make_shared<I2PUDPClientTunnel>(name, dest, end, localDestination, destinationPort);
if(m_ClientForwards.insert(std::make_pair(end, clientTunnel)).second) if(m_ClientForwards.insert(std::make_pair(end, clientTunnel)).second)
{
clientTunnel->Start(); clientTunnel->Start();
}
else else
LogPrint(eLogError, "Clients: I2P Client forward for endpoint ", end, " already exists"); LogPrint(eLogError, "Clients: I2P Client forward for endpoint ", end, " already exists");
@ -598,7 +594,10 @@ namespace client
} }
else if (type == I2P_TUNNELS_SECTION_TYPE_WEBSOCKS) else if (type == I2P_TUNNELS_SECTION_TYPE_WEBSOCKS)
{ {
LogPrint(eLogError, "Clients: I2P Client tunnel websocks is deprecated"); LogPrint(eLogWarning, "Clients: I2P Client tunnel websocks is deprecated, not starting ", name, " tunnel");
#ifdef WIN32_APP
ShowMessageBox (eLogWarning, "Skipped websocks tunnel ", name, " because it's support is discontinued.");
#endif
continue; continue;
} }
else else
@ -608,6 +607,7 @@ namespace client
clientTunnel = tun; clientTunnel = tun;
clientEndpoint = tun->GetLocalEndpoint (); clientEndpoint = tun->GetLocalEndpoint ();
} }
uint32_t timeout = section.second.get<uint32_t>(I2P_CLIENT_TUNNEL_CONNECT_TIMEOUT, 0); uint32_t timeout = section.second.get<uint32_t>(I2P_CLIENT_TUNNEL_CONNECT_TIMEOUT, 0);
if(timeout) if(timeout)
{ {
@ -745,12 +745,14 @@ namespace client
} }
else else
LogPrint (eLogWarning, "Clients: Unknown section type=", type, " of ", name, " in ", tunConf); LogPrint (eLogWarning, "Clients: Unknown section type = ", type, " of ", name, " in ", tunConf);
} }
catch (std::exception& ex) catch (std::exception& ex)
{ {
LogPrint (eLogError, "Clients: Can't read tunnel ", name, " params: ", ex.what ()); LogPrint (eLogError, "Clients: Can't read tunnel ", name, " params: ", ex.what ());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start tunnel ", name, ": ", ex.what ());
#endif
} }
} }
} }
@ -781,14 +783,14 @@ namespace client
else else
LogPrint(eLogError, "Clients: failed to load HTTP Proxy key"); LogPrint(eLogError, "Clients: failed to load HTTP Proxy key");
} }
try try {
{
m_HttpProxy = new i2p::proxy::HTTPProxy("HTTP Proxy", httpProxyAddr, httpProxyPort, httpOutProxyURL, httpAddresshelper, localDestination); m_HttpProxy = new i2p::proxy::HTTPProxy("HTTP Proxy", httpProxyAddr, httpProxyPort, httpOutProxyURL, httpAddresshelper, localDestination);
m_HttpProxy->Start(); m_HttpProxy->Start();
} } catch (std::exception& e) {
catch (std::exception& e)
{
LogPrint(eLogError, "Clients: Exception in HTTP Proxy: ", e.what()); LogPrint(eLogError, "Clients: Exception in HTTP Proxy: ", e.what());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start HTTP Proxy at ", httpProxyAddr, ":", httpProxyPort, ": ", e.what ());
#endif
} }
} }
} }
@ -820,15 +822,15 @@ namespace client
else else
LogPrint(eLogError, "Clients: failed to load SOCKS Proxy key"); LogPrint(eLogError, "Clients: failed to load SOCKS Proxy key");
} }
try try {
{
m_SocksProxy = new i2p::proxy::SOCKSProxy("SOCKS", socksProxyAddr, socksProxyPort, m_SocksProxy = new i2p::proxy::SOCKSProxy("SOCKS", socksProxyAddr, socksProxyPort,
socksOutProxy, socksOutProxyAddr, socksOutProxyPort, localDestination); socksOutProxy, socksOutProxyAddr, socksOutProxyPort, localDestination);
m_SocksProxy->Start(); m_SocksProxy->Start();
} } catch (std::exception& e) {
catch (std::exception& e)
{
LogPrint(eLogError, "Clients: Exception in SOCKS Proxy: ", e.what()); LogPrint(eLogError, "Clients: Exception in SOCKS Proxy: ", e.what());
#ifdef WIN32_APP
ShowMessageBox (eLogError, "Unable to start SOCKS Proxy at ", socksProxyAddr, ":", socksProxyPort, ": ", e.what ());
#endif
} }
} }
} }

6
libi2pd_client/I2PService.cpp

@ -118,7 +118,7 @@ namespace client
AddReadyCallback([this, streamRequestComplete, address, port] (const boost::system::error_code & ec) { AddReadyCallback([this, streamRequestComplete, address, port] (const boost::system::error_code & ec) {
if(ec) if(ec)
{ {
LogPrint(eLogWarning, "I2PService::CeateStream() ", ec.message()); LogPrint(eLogWarning, "I2PService::CreateStream() ", ec.message());
streamRequestComplete(nullptr); streamRequestComplete(nullptr);
} }
else else
@ -285,12 +285,8 @@ namespace client
m_Acceptor.reset (new boost::asio::ip::tcp::acceptor (GetService (), m_LocalEndpoint)); m_Acceptor.reset (new boost::asio::ip::tcp::acceptor (GetService (), m_LocalEndpoint));
// update the local end point in case port has been set zero and got updated now // update the local end point in case port has been set zero and got updated now
m_LocalEndpoint = m_Acceptor->local_endpoint(); m_LocalEndpoint = m_Acceptor->local_endpoint();
try {
m_Acceptor->listen (); m_Acceptor->listen ();
Accept (); Accept ();
} catch (std::exception& ex) {
LogPrint (eLogError, "I2PService: failed to start ", GetName(), " acceptor: ", ex.what ());
}
} }
void TCPIPAcceptor::Stop () void TCPIPAcceptor::Stop ()

Loading…
Cancel
Save