From f122da14858619092ecca95018aa5b4163aee460 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 8 Dec 2015 10:40:43 -0500 Subject: [PATCH] change and save I2PControl password --- I2PControl.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++- I2PControl.h | 5 +++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/I2PControl.cpp b/I2PControl.cpp index 6f2c21ba..5b262e54 100644 --- a/I2PControl.cpp +++ b/I2PControl.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #if !GCC47_BOOST149 #include #endif @@ -30,6 +31,7 @@ namespace client m_SSLContext (m_Service, boost::asio::ssl::context::sslv23), m_ShutdownTimer (m_Service) { + LoadConfig (); // certificate auto path = GetPath (); if (!boost::filesystem::exists (path)) @@ -56,6 +58,9 @@ namespace client m_MethodHandlers[I2P_CONTROL_METHOD_ROUTER_MANAGER] = &I2PControlService::RouterManagerHandler; m_MethodHandlers[I2P_CONTROL_METHOD_NETWORK_SETTING] = &I2PControlService::NetworkSettingHandler; + // I2PControl + m_I2PControlHandlers[I2P_CONTROL_I2PCONTROL_PASSWORD] = &I2PControlService::PasswordHandler; + // RouterInfo m_RouterInfoHandlers[I2P_CONTROL_ROUTER_INFO_UPTIME] = &I2PControlService::UptimeHandler; m_RouterInfoHandlers[I2P_CONTROL_ROUTER_INFO_VERSION] = &I2PControlService::VersionHandler; @@ -78,6 +83,49 @@ namespace client Stop (); } + void I2PControlService::LoadConfig () + { + auto path = GetPath (); + if (!boost::filesystem::exists (path)) + { + if (!boost::filesystem::create_directory (path)) + LogPrint (eLogError, "Failed to create i2pcontrol directory"); + } + boost::property_tree::ptree pt; + auto filename = path / I2P_CONTROL_CONFIG_FILE; + bool isNew = true; + if (boost::filesystem::exists (filename)) + { + try + { + boost::property_tree::read_ini (filename.string (), pt); + isNew = false; + } + catch (std::exception& ex) + { + LogPrint (eLogError, "Can't read ", filename, ": ", ex.what ()); + } + } + m_Password = pt.get (I2P_CONTROL_I2PCONTROL_PASSWORD, I2P_CONTROL_DEFAULT_PASSWORD); + if (isNew) SaveConfig (); + } + + void I2PControlService::SaveConfig () + { + boost::property_tree::ptree pt; + pt.put (I2P_CONTROL_I2PCONTROL_PASSWORD, m_Password); + auto filename = GetPath () / I2P_CONTROL_CONFIG_FILE; + // we take care about directory in LoadConfig + try + { + boost::property_tree::write_ini (filename.string (), pt); + } + catch (std::exception& ex) + { + LogPrint (eLogError, "Can't write ", filename, ": ", ex.what ()); + } + } + void I2PControlService::Start () { if (!m_IsRunning) @@ -326,12 +374,23 @@ namespace client LogPrint (eLogDebug, it.first); auto it1 = m_I2PControlHandlers.find (it.first); if (it1 != m_I2PControlHandlers.end ()) + { (this->*(it1->second))(it.second.data ()); + InsertParam (results, it.first, ""); + } else - LogPrint (eLogError, "I2PControl NetworkSetting unknown request ", it.first); + LogPrint (eLogError, "I2PControl I2PControl unknown request ", it.first); } } + void I2PControlService::PasswordHandler (const std::string& value) + { + LogPrint (eLogDebug, "I2PControl new password=", value); + m_Password = value; + m_Tokens.clear (); + SaveConfig (); + } + // RouterInfo void I2PControlService::RouterInfoHandler (const boost::property_tree::ptree& params, std::ostringstream& results) diff --git a/I2PControl.h b/I2PControl.h index 565281ac..10b6b651 100644 --- a/I2PControl.h +++ b/I2PControl.h @@ -25,6 +25,7 @@ namespace client const char I2P_CONTROL_PATH[] = "ipcontrol"; const char I2P_CONTROL_KEY_FILE[] = "key.pem"; const char I2P_CONTROL_CERT_FILE[] = "cert.pem"; + const char I2P_CONTROL_CONFIG_FILE[] = "i2pcontrol.conf"; const char I2P_CONTROL_DEFAULT_PASSWORD[] = "itoopie"; const char I2P_CONTROL_PROPERTY_ID[] = "id"; @@ -86,6 +87,9 @@ namespace client private: + void LoadConfig (); + void SaveConfig (); + void Run (); void Accept (); void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr socket); @@ -121,6 +125,7 @@ namespace client // I2PControl typedef void (I2PControlService::*I2PControlRequestHandler)(const std::string& value); + void PasswordHandler (const std::string& value); // RouterInfo typedef void (I2PControlService::*RouterInfoRequestHandler)(std::ostringstream& results);