Browse Source

use local destination function through ClientContext

pull/102/head
orignal 10 years ago
parent
commit
14f4bb8656
  1. 40
      ClientContext.cpp
  2. 17
      ClientContext.h
  3. 10
      HTTPServer.cpp
  4. 10
      I2PTunnel.cpp
  5. 20
      I2PTunnel.h
  6. 12
      SAM.cpp
  7. 6
      SAM.h
  8. 4
      SOCKS.cpp

40
ClientContext.cpp

@ -44,8 +44,8 @@ namespace client
i2p::stream::StreamingDestination * localDestination = nullptr; i2p::stream::StreamingDestination * localDestination = nullptr;
std::string ircKeys = i2p::util::config::GetArg("-irckeys", ""); std::string ircKeys = i2p::util::config::GetArg("-irckeys", "");
if (ircKeys.length () > 0) if (ircKeys.length () > 0)
localDestination = i2p::client::LoadLocalDestination (ircKeys, false); localDestination = i2p::client::context.LoadLocalDestination (ircKeys, false);
m_IrcTunnel = new i2p::stream::I2PClientTunnel (m_SocksProxy->GetService (), ircDestination, m_IrcTunnel = new I2PClientTunnel (m_SocksProxy->GetService (), ircDestination,
i2p::util::config::GetArg("-ircport", 6668), localDestination); i2p::util::config::GetArg("-ircport", 6668), localDestination);
m_IrcTunnel->Start (); m_IrcTunnel->Start ();
LogPrint("IRC tunnel started"); LogPrint("IRC tunnel started");
@ -53,8 +53,8 @@ namespace client
std::string eepKeys = i2p::util::config::GetArg("-eepkeys", ""); std::string eepKeys = i2p::util::config::GetArg("-eepkeys", "");
if (eepKeys.length () > 0) // eepkeys file is presented if (eepKeys.length () > 0) // eepkeys file is presented
{ {
auto localDestination = i2p::client::LoadLocalDestination (eepKeys, true); auto localDestination = i2p::client::context.LoadLocalDestination (eepKeys, true);
m_ServerTunnel = new i2p::stream::I2PServerTunnel (m_SocksProxy->GetService (), m_ServerTunnel = new I2PServerTunnel (m_SocksProxy->GetService (),
i2p::util::config::GetArg("-eephost", "127.0.0.1"), i2p::util::config::GetArg("-eepport", 80), i2p::util::config::GetArg("-eephost", "127.0.0.1"), i2p::util::config::GetArg("-eepport", 80),
localDestination); localDestination);
m_ServerTunnel->Start (); m_ServerTunnel->Start ();
@ -63,7 +63,7 @@ namespace client
int samPort = i2p::util::config::GetArg("-samport", 0); int samPort = i2p::util::config::GetArg("-samport", 0);
if (samPort) if (samPort)
{ {
m_SamBridge = new i2p::stream::SAMBridge (samPort); m_SamBridge = new SAMBridge (samPort);
m_SamBridge->Start (); m_SamBridge->Start ();
LogPrint("SAM bridge started"); LogPrint("SAM bridge started");
} }
@ -195,35 +195,5 @@ namespace client
return it->second; return it->second;
return nullptr; 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);
}
} }
} }

17
ClientContext.h

@ -23,8 +23,8 @@ namespace client
void Stop (); void Stop ();
i2p::stream::StreamingDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; }; i2p::stream::StreamingDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; };
i2p::stream::StreamingDestination * CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType); 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); i2p::stream::StreamingDestination * CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true);
void DeleteLocalDestination (i2p::stream::StreamingDestination * destination); void DeleteLocalDestination (i2p::stream::StreamingDestination * destination);
i2p::stream::StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const; i2p::stream::StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const;
i2p::stream::StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic); i2p::stream::StreamingDestination * LoadLocalDestination (const std::string& filename, bool isPublic);
@ -41,9 +41,9 @@ namespace client
i2p::proxy::HTTPProxy * m_HttpProxy; i2p::proxy::HTTPProxy * m_HttpProxy;
i2p::proxy::SOCKSProxy * m_SocksProxy; i2p::proxy::SOCKSProxy * m_SocksProxy;
i2p::stream::I2PClientTunnel * m_IrcTunnel; I2PClientTunnel * m_IrcTunnel;
i2p::stream::I2PServerTunnel * m_ServerTunnel; I2PServerTunnel * m_ServerTunnel;
i2p::stream::SAMBridge * m_SamBridge; SAMBridge * m_SamBridge;
public: public:
// for HTTP // for HTTP
@ -51,13 +51,6 @@ namespace client
}; };
extern ClientContext context; 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);
} }
} }

10
HTTPServer.cpp

@ -517,7 +517,7 @@ namespace util
if (m_Stream) if (m_Stream)
{ {
m_Stream->Close (); m_Stream->Close ();
i2p::client::GetSharedLocalDestination ()->DeleteStream (m_Stream); i2p::client::context.GetSharedLocalDestination ()->DeleteStream (m_Stream);
m_Stream = nullptr; m_Stream = nullptr;
} }
m_Socket->close (); m_Socket->close ();
@ -793,7 +793,7 @@ namespace util
{ {
i2p::data::IdentHash ident; i2p::data::IdentHash ident;
i2p::data::Base32ToByteStream (b32.c_str (), b32.length (), ident, 32); i2p::data::Base32ToByteStream (b32.c_str (), b32.length (), ident, 32);
auto dest = i2p::client::FindLocalDestination (ident); auto dest = i2p::client::context.FindLocalDestination (ident);
if (dest) if (dest)
{ {
s << "<b>LeaseSets:</b> <i>" << dest->GetNumRemoteLeaseSets () << "</i><br>"; s << "<b>LeaseSets:</b> <i>" << dest->GetNumRemoteLeaseSets () << "</i><br>";
@ -859,9 +859,9 @@ namespace util
auto leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (destination); auto leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (destination);
if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) if (!leaseSet || !leaseSet->HasNonExpiredLeases ())
{ {
i2p::data::netdb.RequestDestination (destination, true, i2p::client::GetSharedLocalDestination ()->GetTunnelPool ()); i2p::data::netdb.RequestDestination (destination, true, i2p::client::context.GetSharedLocalDestination ()->GetTunnelPool ());
std::this_thread::sleep_for (std::chrono::seconds(10)); // wait for 10 seconds std::this_thread::sleep_for (std::chrono::seconds(10)); // wait for 10 seconds
leaseSet = i2p::client::GetSharedLocalDestination ()->FindLeaseSet (destination); leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (destination);
if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) // still no LeaseSet if (!leaseSet || !leaseSet->HasNonExpiredLeases ()) // still no LeaseSet
{ {
SendReply (leaseSet ? "<html>" + itoopieImage + "<br>Leases expired</html>" : "<html>" + itoopieImage + "LeaseSet not found</html>", 504); SendReply (leaseSet ? "<html>" + itoopieImage + "<br>Leases expired</html>" : "<html>" + itoopieImage + "LeaseSet not found</html>", 504);
@ -869,7 +869,7 @@ namespace util
} }
} }
if (!m_Stream) if (!m_Stream)
m_Stream = i2p::client::GetSharedLocalDestination ()->CreateNewOutgoingStream (*leaseSet); m_Stream = i2p::client::context.GetSharedLocalDestination ()->CreateNewOutgoingStream (*leaseSet);
if (m_Stream) if (m_Stream)
{ {
m_Stream->Send ((uint8_t *)buf, len); m_Stream->Send ((uint8_t *)buf, len);

10
I2PTunnel.cpp

@ -8,7 +8,7 @@
namespace i2p namespace i2p
{ {
namespace stream namespace client
{ {
I2PTunnelConnection::I2PTunnelConnection (I2PTunnel * owner, I2PTunnelConnection::I2PTunnelConnection (I2PTunnel * owner,
boost::asio::ip::tcp::socket * socket, const i2p::data::LeaseSet * leaseSet): boost::asio::ip::tcp::socket * socket, const i2p::data::LeaseSet * leaseSet):
@ -20,7 +20,7 @@ namespace stream
Receive (); Receive ();
} }
I2PTunnelConnection::I2PTunnelConnection (I2PTunnel * owner, Stream * stream, I2PTunnelConnection::I2PTunnelConnection (I2PTunnel * owner, i2p::stream::Stream * stream,
boost::asio::ip::tcp::socket * socket, const boost::asio::ip::tcp::endpoint& target): boost::asio::ip::tcp::socket * socket, const boost::asio::ip::tcp::endpoint& target):
m_Socket (socket), m_Stream (stream), m_Owner (owner) m_Socket (socket), m_Stream (stream), m_Owner (owner)
{ {
@ -145,9 +145,9 @@ namespace stream
} }
I2PClientTunnel::I2PClientTunnel (boost::asio::io_service& service, const std::string& destination, I2PClientTunnel::I2PClientTunnel (boost::asio::io_service& service, const std::string& destination,
int port, StreamingDestination * localDestination): int port, i2p::stream::StreamingDestination * localDestination):
I2PTunnel (service, localDestination ? localDestination : I2PTunnel (service, localDestination ? localDestination :
i2p::client::CreateNewLocalDestination (false, i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256)), i2p::client::context.CreateNewLocalDestination (false, i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256)),
m_Acceptor (service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Acceptor (service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
m_Timer (service), m_Destination (destination), m_DestinationIdentHash (nullptr), m_Timer (service), m_Destination (destination), m_DestinationIdentHash (nullptr),
m_RemoteLeaseSet (nullptr) m_RemoteLeaseSet (nullptr)
@ -251,7 +251,7 @@ namespace stream
} }
I2PServerTunnel::I2PServerTunnel (boost::asio::io_service& service, const std::string& address, int port, I2PServerTunnel::I2PServerTunnel (boost::asio::io_service& service, const std::string& address, int port,
StreamingDestination * localDestination): I2PTunnel (service, localDestination), i2p::stream::StreamingDestination * localDestination): I2PTunnel (service, localDestination),
m_Endpoint (boost::asio::ip::address::from_string (address), port) m_Endpoint (boost::asio::ip::address::from_string (address), port)
{ {
} }

20
I2PTunnel.h

@ -10,7 +10,7 @@
namespace i2p namespace i2p
{ {
namespace stream namespace client
{ {
const size_t I2P_TUNNEL_CONNECTION_BUFFER_SIZE = 8192; const size_t I2P_TUNNEL_CONNECTION_BUFFER_SIZE = 8192;
const int I2P_TUNNEL_CONNECTION_MAX_IDLE = 3600; // in seconds const int I2P_TUNNEL_CONNECTION_MAX_IDLE = 3600; // in seconds
@ -23,7 +23,7 @@ namespace stream
I2PTunnelConnection (I2PTunnel * owner, boost::asio::ip::tcp::socket * socket, I2PTunnelConnection (I2PTunnel * owner, boost::asio::ip::tcp::socket * socket,
const i2p::data::LeaseSet * leaseSet); const i2p::data::LeaseSet * leaseSet);
I2PTunnelConnection (I2PTunnel * owner, Stream * stream, boost::asio::ip::tcp::socket * socket, I2PTunnelConnection (I2PTunnel * owner, i2p::stream::Stream * stream, boost::asio::ip::tcp::socket * socket,
const boost::asio::ip::tcp::endpoint& target); const boost::asio::ip::tcp::endpoint& target);
~I2PTunnelConnection (); ~I2PTunnelConnection ();
@ -43,7 +43,7 @@ namespace stream
uint8_t m_Buffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE], m_StreamBuffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE]; uint8_t m_Buffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE], m_StreamBuffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE];
boost::asio::ip::tcp::socket * m_Socket; boost::asio::ip::tcp::socket * m_Socket;
Stream * m_Stream; i2p::stream::Stream * m_Stream;
I2PTunnel * m_Owner; I2PTunnel * m_Owner;
}; };
@ -51,22 +51,22 @@ namespace stream
{ {
public: public:
I2PTunnel (boost::asio::io_service& service, StreamingDestination * localDestination): I2PTunnel (boost::asio::io_service& service, i2p::stream::StreamingDestination * localDestination):
m_Service (service), m_LocalDestination (localDestination) {}; m_Service (service), m_LocalDestination (localDestination) {};
virtual ~I2PTunnel () { ClearConnections (); }; virtual ~I2PTunnel () { ClearConnections (); };
void AddConnection (I2PTunnelConnection * conn); void AddConnection (I2PTunnelConnection * conn);
void RemoveConnection (I2PTunnelConnection * conn); void RemoveConnection (I2PTunnelConnection * conn);
void ClearConnections (); void ClearConnections ();
StreamingDestination * GetLocalDestination () { return m_LocalDestination; }; i2p::stream::StreamingDestination * GetLocalDestination () { return m_LocalDestination; };
void SetLocalDestination (StreamingDestination * dest) { m_LocalDestination = dest; }; void SetLocalDestination (i2p::stream::StreamingDestination * dest) { m_LocalDestination = dest; };
boost::asio::io_service& GetService () { return m_Service; }; boost::asio::io_service& GetService () { return m_Service; };
private: private:
boost::asio::io_service& m_Service; boost::asio::io_service& m_Service;
StreamingDestination * m_LocalDestination; i2p::stream::StreamingDestination * m_LocalDestination;
std::set<I2PTunnelConnection *> m_Connections; std::set<I2PTunnelConnection *> m_Connections;
}; };
@ -75,7 +75,7 @@ namespace stream
public: public:
I2PClientTunnel (boost::asio::io_service& service, const std::string& destination, int port, I2PClientTunnel (boost::asio::io_service& service, const std::string& destination, int port,
StreamingDestination * localDestination = nullptr); i2p::stream::StreamingDestination * localDestination = nullptr);
~I2PClientTunnel (); ~I2PClientTunnel ();
void Start (); void Start ();
@ -102,7 +102,7 @@ namespace stream
public: public:
I2PServerTunnel (boost::asio::io_service& service, const std::string& address, int port, I2PServerTunnel (boost::asio::io_service& service, const std::string& address, int port,
StreamingDestination * localDestination); i2p::stream::StreamingDestination * localDestination);
void Start (); void Start ();
void Stop (); void Stop ();
@ -110,7 +110,7 @@ namespace stream
private: private:
void Accept (); void Accept ();
void HandleAccept (Stream * stream); void HandleAccept (i2p::stream::Stream * stream);
private: private:

12
SAM.cpp

@ -11,7 +11,7 @@
namespace i2p namespace i2p
{ {
namespace stream namespace client
{ {
SAMSocket::SAMSocket (SAMBridge& owner): SAMSocket::SAMSocket (SAMBridge& owner):
m_Owner (owner), m_Socket (m_Owner.GetService ()), m_Timer (m_Owner.GetService ()), m_Owner (owner), m_Socket (m_Owner.GetService ()), m_Timer (m_Owner.GetService ()),
@ -25,6 +25,7 @@ namespace stream
if (m_Stream) if (m_Stream)
{ {
m_Stream->Close (); m_Stream->Close ();
if (m_Session && m_Session->localDestination)
m_Session->localDestination->DeleteStream (m_Stream); m_Session->localDestination->DeleteStream (m_Stream);
} }
} }
@ -34,6 +35,7 @@ namespace stream
if (m_Stream) if (m_Stream)
{ {
m_Stream->Close (); m_Stream->Close ();
if (m_Session && m_Session->localDestination)
m_Session->localDestination->DeleteStream (m_Stream); m_Session->localDestination->DeleteStream (m_Stream);
m_Stream = nullptr; m_Stream = nullptr;
} }
@ -332,7 +334,7 @@ namespace stream
void SAMSocket::ProcessDestGenerate () void SAMSocket::ProcessDestGenerate ()
{ {
LogPrint ("SAM dest generate"); LogPrint ("SAM dest generate");
auto localDestination = i2p::client::CreateNewLocalDestination (); auto localDestination = i2p::client::context.CreateNewLocalDestination ();
if (localDestination) if (localDestination)
{ {
uint8_t buf[1024]; uint8_t buf[1024];
@ -566,7 +568,7 @@ namespace stream
SAMSession * SAMBridge::CreateSession (const std::string& id, const std::string& destination) SAMSession * SAMBridge::CreateSession (const std::string& id, const std::string& destination)
{ {
StreamingDestination * localDestination = nullptr; i2p::stream::StreamingDestination * localDestination = nullptr;
if (destination != "") if (destination != "")
{ {
uint8_t * buf = new uint8_t[destination.length ()]; uint8_t * buf = new uint8_t[destination.length ()];
@ -574,10 +576,10 @@ namespace stream
i2p::data::PrivateKeys keys; i2p::data::PrivateKeys keys;
keys.FromBuffer (buf, l); keys.FromBuffer (buf, l);
delete[] buf; delete[] buf;
localDestination = i2p::client::CreateNewLocalDestination (keys); localDestination = i2p::client::context.CreateNewLocalDestination (keys);
} }
else // transient else // transient
localDestination = i2p::client::CreateNewLocalDestination (); localDestination = i2p::client::context.CreateNewLocalDestination ();
if (localDestination) if (localDestination)
{ {
SAMSession session; SAMSession session;

6
SAM.h

@ -14,7 +14,7 @@
namespace i2p namespace i2p
{ {
namespace stream namespace client
{ {
const size_t SAM_SOCKET_BUFFER_SIZE = 4096; const size_t SAM_SOCKET_BUFFER_SIZE = 4096;
const int SAM_SOCKET_CONNECTION_MAX_IDLE = 3600; // in seconds const int SAM_SOCKET_CONNECTION_MAX_IDLE = 3600; // in seconds
@ -106,13 +106,13 @@ namespace stream
SAMSocketType m_SocketType; SAMSocketType m_SocketType;
std::string m_ID; // nickname std::string m_ID; // nickname
bool m_IsSilent; bool m_IsSilent;
Stream * m_Stream; i2p::stream::Stream * m_Stream;
SAMSession * m_Session; SAMSession * m_Session;
}; };
struct SAMSession struct SAMSession
{ {
StreamingDestination * localDestination; i2p::stream::StreamingDestination * localDestination;
std::list<SAMSocket *> sockets; std::list<SAMSocket *> sockets;
}; };

4
SOCKS.cpp

@ -160,7 +160,7 @@ namespace proxy
LogPrint("--- sock4a find lease set"); LogPrint("--- sock4a find lease set");
m_ls = i2p::data::netdb.FindLeaseSet(m_dest); m_ls = i2p::data::netdb.FindLeaseSet(m_dest);
if (!m_ls || m_ls->HasNonExpiredLeases()) { if (!m_ls || m_ls->HasNonExpiredLeases()) {
i2p::data::netdb.RequestDestination (m_dest, true, i2p::client::GetSharedLocalDestination ()->GetTunnelPool ()); i2p::data::netdb.RequestDestination (m_dest, true, i2p::client::context.GetSharedLocalDestination ()->GetTunnelPool ());
m_ls_timer.expires_from_now(boost::posix_time::seconds(socks_leaseset_timeout)); m_ls_timer.expires_from_now(boost::posix_time::seconds(socks_leaseset_timeout));
m_ls_timer.async_wait(boost::bind(&SOCKS4AHandler::LeaseSetTimeout, this, boost::asio::placeholders::error)); m_ls_timer.async_wait(boost::bind(&SOCKS4AHandler::LeaseSetTimeout, this, boost::asio::placeholders::error));
} else { } else {
@ -224,7 +224,7 @@ namespace proxy
void SOCKS4AHandler::SentConnectionSuccess(const boost::system::error_code & ecode) void SOCKS4AHandler::SentConnectionSuccess(const boost::system::error_code & ecode)
{ {
LogPrint("--- socks4a making connection"); LogPrint("--- socks4a making connection");
m_stream = i2p::client::GetSharedLocalDestination ()->CreateNewOutgoingStream(*m_ls); m_stream = i2p::client::context.GetSharedLocalDestination ()->CreateNewOutgoingStream(*m_ls);
m_state = OKAY; m_state = OKAY;
LogPrint("--- socks4a state is ", m_state); LogPrint("--- socks4a state is ", m_state);
AsyncSockRead(); AsyncSockRead();

Loading…
Cancel
Save