Skip to content

Commit

Permalink
introduced ClientContext
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Oct 16, 2014
1 parent 18d6a2c commit 89e5b56
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 221 deletions.
146 changes: 146 additions & 0 deletions ClientContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include "util.h"
#include "ClientContext.h"

namespace i2p
{
namespace client
{
ClientContext context;
void ClientContext::Start ()
{
if (!m_SharedLocalDestination)
{
m_SharedLocalDestination = new i2p::stream::StreamingDestination (false, i2p::data::SIGNING_KEY_TYPE_DSA_SHA1); // non-public, DSA
m_Destinations[m_SharedLocalDestination->GetIdentity ().GetIdentHash ()] = m_SharedLocalDestination;
m_SharedLocalDestination->Start ();
}
}

void ClientContext::Stop ()
{
for (auto it: m_Destinations)
{
it.second->Stop ();
delete it.second;
}
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 i2p::stream::StreamingDestination (fullPath, true);
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
numDestinations++;
}
}
if (numDestinations > 0)
LogPrint (numDestinations, " local destinations loaded");
}

i2p::stream::StreamingDestination * ClientContext::LoadLocalDestination (const std::string& filename, bool isPublic)
{
auto localDestination = new i2p::stream::StreamingDestination (i2p::util::filesystem::GetFullPath (filename), isPublic);
std::unique_lock<std::mutex> l(m_DestinationsMutex);
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
localDestination->Start ();
return localDestination;
}

i2p::stream::StreamingDestination * ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType)
{
auto localDestination = new i2p::stream::StreamingDestination (isPublic, sigType);
std::unique_lock<std::mutex> l(m_DestinationsMutex);
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
localDestination->Start ();
return localDestination;
}

void ClientContext::DeleteLocalDestination (i2p::stream::StreamingDestination * destination)
{
if (!destination) return;
auto it = m_Destinations.find (destination->GetIdentHash ());
if (it != m_Destinations.end ())
{
auto d = it->second;
{
std::unique_lock<std::mutex> l(m_DestinationsMutex);
m_Destinations.erase (it);
}
d->Stop ();
delete d;
}
}

i2p::stream::StreamingDestination * ClientContext::CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic)
{
auto it = m_Destinations.find (keys.GetPublic ().GetIdentHash ());
if (it != m_Destinations.end ())
{
LogPrint ("Local destination ", keys.GetPublic ().GetIdentHash ().ToBase32 (), ".b32.i2p exists");
if (!it->second->IsRunning ())
{
it->second->Start ();
return it->second;
}
return nullptr;
}
auto localDestination = new i2p::stream::StreamingDestination (keys, isPublic);
std::unique_lock<std::mutex> l(m_DestinationsMutex);
m_Destinations[keys.GetPublic ().GetIdentHash ()] = localDestination;
localDestination->Start ();
return localDestination;
}

i2p::stream::StreamingDestination * ClientContext::FindLocalDestination (const i2p::data::IdentHash& destination) const
{
auto it = m_Destinations.find (destination);
if (it != m_Destinations.end ())
return it->second;
return nullptr;
}

i2p::stream::StreamingDestination * GetSharedLocalDestination ()
{
return context.GetSharedLocalDestination ();
}

i2p::stream::StreamingDestination * CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType)
{
return context.CreateNewLocalDestination (isPublic, sigType);
}

i2p::stream::StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic)
{
return context.CreateNewLocalDestination (keys, isPublic);
}

void DeleteLocalDestination (i2p::stream::StreamingDestination * destination)
{
context.DeleteLocalDestination (destination);
}

i2p::stream::StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination)
{
return context.FindLocalDestination (destination);
}

i2p::stream::StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic)
{
return context.LoadLocalDestination (filename, isPublic);
}
}
}
54 changes: 54 additions & 0 deletions ClientContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef CLIENT_CONTEXT_H__
#define CLIENT_CONTEXT_H__

#include <mutex>
#include "Destination.h"

namespace i2p
{
namespace client
{
class ClientContext
{
public:

ClientContext (): m_SharedLocalDestination (nullptr) {};
~ClientContext () {};

void Start ();
void Stop ();

i2p::stream::StreamingDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; };
i2p::stream::StreamingDestination * CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType);
i2p::stream::StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic);
void DeleteLocalDestination (i2p::stream::StreamingDestination * destination);
i2p::stream::StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const;
i2p::stream::StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic);

private:

void LoadLocalDestinations ();

private:

std::mutex m_DestinationsMutex;
std::map<i2p::data::IdentHash, i2p::stream::StreamingDestination *> m_Destinations;
i2p::stream::StreamingDestination * m_SharedLocalDestination;

public:
// for HTTP
const decltype(m_Destinations)& GetDestinations () const { return m_Destinations; };
};

extern ClientContext context;

i2p::stream::StreamingDestination * GetSharedLocalDestination ();
i2p::stream::StreamingDestination * CreateNewLocalDestination (bool isPublic = true, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1); // transient
i2p::stream::StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true);
void DeleteLocalDestination (i2p::stream::StreamingDestination * destination);
i2p::stream::StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination);
i2p::stream::StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic);
}
}

#endif
13 changes: 7 additions & 6 deletions Daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "HTTPServer.h"
#include "HTTPProxy.h"
#include "SOCKS.h"
#include "ClientContext.h"
#include "I2PTunnel.h"
#include "SAM.h"

Expand Down Expand Up @@ -111,8 +112,8 @@ namespace i2p
LogPrint("Transports started");
i2p::tunnel::tunnels.Start();
LogPrint("Tunnels started");
i2p::stream::StartStreaming();
LogPrint("Streaming started");
i2p::client::context.Start ();
LogPrint("Client started");

d.httpProxy = new i2p::proxy::HTTPProxy(i2p::util::config::GetArg("-httpproxyport", 4446));
d.httpProxy->Start();
Expand All @@ -126,7 +127,7 @@ namespace i2p
i2p::stream::StreamingDestination * localDestination = nullptr;
std::string ircKeys = i2p::util::config::GetArg("-irckeys", "");
if (ircKeys.length () > 0)
localDestination = i2p::stream::LoadLocalDestination (ircKeys, false);
localDestination = i2p::client::LoadLocalDestination (ircKeys, false);
d.ircTunnel = new i2p::stream::I2PClientTunnel (d.socksProxy->GetService (), ircDestination,
i2p::util::config::GetArg("-ircport", 6668), localDestination);
d.ircTunnel->Start ();
Expand All @@ -135,7 +136,7 @@ namespace i2p
std::string eepKeys = i2p::util::config::GetArg("-eepkeys", "");
if (eepKeys.length () > 0) // eepkeys file is presented
{
auto localDestination = i2p::stream::LoadLocalDestination (eepKeys, true);
auto localDestination = i2p::client::LoadLocalDestination (eepKeys, true);
d.serverTunnel = new i2p::stream::I2PServerTunnel (d.socksProxy->GetService (),
i2p::util::config::GetArg("-eephost", "127.0.0.1"), i2p::util::config::GetArg("-eepport", 80),
localDestination);
Expand All @@ -160,8 +161,8 @@ namespace i2p
LogPrint("HTTP Proxy stoped");
d.socksProxy->Stop();
LogPrint("SOCKS Proxy stoped");
i2p::stream::StopStreaming();
LogPrint("Streaming stoped");
i2p::client::context.Stop();
LogPrint("Client stoped");
i2p::tunnel::tunnels.Stop();
LogPrint("Tunnels stoped");
i2p::transports.Stop();
Expand Down
Loading

0 comments on commit 89e5b56

Please sign in to comment.