diff --git a/ClientContext.cpp b/ClientContext.cpp index 5937e8f8..f7ee20ec 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -1,5 +1,7 @@ +#include #include "util.h" #include "Log.h" +#include "Identity.h" #include "ClientContext.h" namespace i2p @@ -27,7 +29,7 @@ namespace client { if (!m_SharedLocalDestination) { - m_SharedLocalDestination = new ClientDestination (false, i2p::data::SIGNING_KEY_TYPE_DSA_SHA1); // non-public, DSA + m_SharedLocalDestination = CreateNewLocalDestination (false, i2p::data::SIGNING_KEY_TYPE_DSA_SHA1); // non-public, DSA m_Destinations[m_SharedLocalDestination->GetIdentity ().GetIdentHash ()] = m_SharedLocalDestination; m_SharedLocalDestination->Start (); } @@ -44,7 +46,7 @@ namespace client ClientDestination * localDestination = nullptr; std::string ircKeys = i2p::util::config::GetArg("-irckeys", ""); if (ircKeys.length () > 0) - localDestination = i2p::client::context.LoadLocalDestination (ircKeys, false); + localDestination = LoadLocalDestination (ircKeys, false); m_IrcTunnel = new I2PClientTunnel (m_SocksProxy->GetService (), ircDestination, i2p::util::config::GetArg("-ircport", 6668), localDestination); m_IrcTunnel->Start (); @@ -53,7 +55,7 @@ namespace client std::string eepKeys = i2p::util::config::GetArg("-eepkeys", ""); if (eepKeys.length () > 0) // eepkeys file is presented { - auto localDestination = i2p::client::context.LoadLocalDestination (eepKeys, true); + auto localDestination = LoadLocalDestination (eepKeys, true); m_ServerTunnel = new I2PServerTunnel (m_SocksProxy->GetService (), i2p::util::config::GetArg("-eephost", "127.0.0.1"), i2p::util::config::GetArg("-eepport", 80), localDestination); @@ -109,34 +111,38 @@ namespace client m_Destinations.clear (); m_SharedLocalDestination = 0; // deleted through m_Destination } - - void ClientContext::LoadLocalDestinations () - { - int numDestinations = 0; - boost::filesystem::path p (i2p::util::filesystem::GetDataDir()); - boost::filesystem::directory_iterator end; - for (boost::filesystem::directory_iterator it (p); it != end; ++it) - { - if (boost::filesystem::is_regular_file (*it) && it->path ().extension () == ".dat") - { - auto fullPath = -#if BOOST_VERSION > 10500 - it->path().string(); -#else - it->path(); -#endif - auto localDestination = new ClientDestination (fullPath, true); - m_Destinations[localDestination->GetIdentHash ()] = localDestination; - numDestinations++; - } - } - if (numDestinations > 0) - LogPrint (numDestinations, " local destinations loaded"); - } ClientDestination * ClientContext::LoadLocalDestination (const std::string& filename, bool isPublic) { - auto localDestination = new ClientDestination (i2p::util::filesystem::GetFullPath (filename), isPublic); + i2p::data::PrivateKeys keys; + std::string fullPath = i2p::util::filesystem::GetFullPath (filename); + std::ifstream s(fullPath.c_str (), std::ifstream::binary); + if (s.is_open ()) + { + s.seekg (0, std::ios::end); + size_t len = s.tellg(); + s.seekg (0, std::ios::beg); + uint8_t * buf = new uint8_t[len]; + s.read ((char *)buf, len); + keys.FromBuffer (buf, len); + delete[] buf; + LogPrint ("Local address ", keys.GetPublic ().GetIdentHash ().ToBase32 (), ".b32.i2p loaded"); + } + else + { + LogPrint ("Can't open file ", fullPath, " Creating new one"); + keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_DSA_SHA1); + std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out); + size_t len = keys.GetFullLen (); + uint8_t * buf = new uint8_t[len]; + len = keys.ToBuffer (buf, len); + f.write ((char *)buf, len); + delete[] buf; + + LogPrint ("New private keys file ", fullPath, " for ", keys.GetPublic ().GetIdentHash ().ToBase32 (), ".b32.i2p created"); + } + + auto localDestination = new ClientDestination (keys, isPublic); std::unique_lock l(m_DestinationsMutex); m_Destinations[localDestination->GetIdentHash ()] = localDestination; localDestination->Start (); @@ -145,7 +151,8 @@ namespace client ClientDestination * ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType) { - auto localDestination = new ClientDestination (isPublic, sigType); + i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType); + auto localDestination = new ClientDestination (keys, isPublic); std::unique_lock l(m_DestinationsMutex); m_Destinations[localDestination->GetIdentHash ()] = localDestination; localDestination->Start (); diff --git a/ClientContext.h b/ClientContext.h index da65f3f8..d864fdfe 100644 --- a/ClientContext.h +++ b/ClientContext.h @@ -31,10 +31,6 @@ namespace client ClientDestination * LoadLocalDestination (const std::string& filename, bool isPublic); AddressBook& GetAddressBook () { return m_AddressBook; }; - - private: - - void LoadLocalDestinations (); private: diff --git a/Destination.cpp b/Destination.cpp index 5f59667e..a18a324f 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -1,4 +1,3 @@ -#include #include #include #include "Log.h" @@ -10,57 +9,6 @@ namespace i2p { namespace client { - ClientDestination::ClientDestination (bool isPublic, i2p::data::SigningKeyType sigType): - m_IsRunning (false), m_Thread (nullptr), m_Service (nullptr), m_Work (nullptr), - m_LeaseSet (nullptr), m_IsPublic (isPublic), m_PublishReplyToken (0), - m_DatagramDestination (nullptr), m_PublishConfirmationTimer (nullptr) - { - m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType); - CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); - dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey); - m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel - if (m_IsPublic) - LogPrint ("Local address ", GetIdentHash ().ToBase32 (), ".b32.i2p created"); - m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO: - } - - ClientDestination::ClientDestination (const std::string& fullPath, bool isPublic): - m_IsRunning (false), m_Thread (nullptr), m_Service (nullptr), m_Work (nullptr), - m_LeaseSet (nullptr), m_IsPublic (isPublic), m_PublishReplyToken (0), - m_DatagramDestination (nullptr), m_PublishConfirmationTimer (nullptr) - { - std::ifstream s(fullPath.c_str (), std::ifstream::binary); - if (s.is_open ()) - { - s.seekg (0, std::ios::end); - size_t len = s.tellg(); - s.seekg (0, std::ios::beg); - uint8_t * buf = new uint8_t[len]; - s.read ((char *)buf, len); - m_Keys.FromBuffer (buf, len); - delete[] buf; - LogPrint ("Local address ", GetIdentHash ().ToBase32 (), ".b32.i2p loaded"); - } - else - { - LogPrint ("Can't open file ", fullPath, " Creating new one"); - m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_DSA_SHA1); - std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out); - size_t len = m_Keys.GetFullLen (); - uint8_t * buf = new uint8_t[len]; - len = m_Keys.ToBuffer (buf, len); - f.write ((char *)buf, len); - delete[] buf; - - LogPrint ("New private keys file ", fullPath, " for ", m_Keys.GetPublic ().GetIdentHash ().ToBase32 (), ".b32.i2p created"); - } - - CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); - dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey); - m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel - m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO: - } - ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic): m_IsRunning (false), m_Thread (nullptr), m_Service (nullptr), m_Work (nullptr), m_Keys (keys), m_LeaseSet (nullptr), m_IsPublic (isPublic), m_PublishReplyToken (0), diff --git a/Destination.h b/Destination.h index 404528e6..723ff9d2 100644 --- a/Destination.h +++ b/Destination.h @@ -25,8 +25,6 @@ namespace client { public: - ClientDestination (bool isPublic, i2p::data::SigningKeyType sigType); - ClientDestination (const std::string& fullPath, bool isPublic); ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic); ~ClientDestination ();