mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-24 09:56:35 +02:00
incoming connection to eepSite
This commit is contained in:
parent
6b21c816e2
commit
4cb98757ce
4 changed files with 122 additions and 21 deletions
|
@ -332,6 +332,7 @@ namespace util
|
|||
|
||||
void HTTPConnection::HandleDestinationRequest (const std::string& address, const std::string& method, const std::string& data, const std::string& uri)
|
||||
{
|
||||
const i2p::data::LeaseSet * leaseSet = nullptr;
|
||||
i2p::data::IdentHash destination;
|
||||
std::string fullAddress;
|
||||
if (address.find(".b32.i2p") != std::string::npos)
|
||||
|
@ -359,6 +360,16 @@ namespace util
|
|||
fullAddress = address;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (address == "local")
|
||||
{
|
||||
// TODO: remove later
|
||||
fullAddress = "local.i2p";
|
||||
auto destination = i2p::stream::GetSharedLocalDestination ();
|
||||
leaseSet = destination->GetLeaseSet ();
|
||||
EepAccept (destination);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (i2p::data::Base32ToByteStream(address.c_str(), address.length(), (uint8_t *)destination, 32) != 32)
|
||||
{
|
||||
|
@ -369,8 +380,10 @@ namespace util
|
|||
fullAddress = address + ".b32.i2p";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto leaseSet = i2p::data::netdb.FindLeaseSet (destination);
|
||||
if (!leaseSet)
|
||||
leaseSet = i2p::data::netdb.FindLeaseSet (destination);
|
||||
if (!leaseSet || !leaseSet->HasNonExpiredLeases ())
|
||||
{
|
||||
i2p::data::netdb.Subscribe(destination);
|
||||
|
@ -495,6 +508,47 @@ namespace util
|
|||
new HTTPConnection (m_NewSocket);
|
||||
}
|
||||
|
||||
// eepSite. TODO: move away
|
||||
|
||||
void HTTPConnection::EepAccept (i2p::stream::StreamingDestination * destination)
|
||||
{
|
||||
if (destination)
|
||||
destination->SetAcceptor (std::bind (&HTTPConnection::HandleEepAccept, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void HTTPConnection::HandleEepAccept (i2p::stream::Stream * stream)
|
||||
{
|
||||
if (stream)
|
||||
{
|
||||
auto conn = new EepSiteDummyConnection (stream);
|
||||
conn->AsyncStreamReceive ();
|
||||
}
|
||||
}
|
||||
|
||||
void EepSiteDummyConnection::AsyncStreamReceive ()
|
||||
{
|
||||
if (m_Stream)
|
||||
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, 8192),
|
||||
boost::bind (&EepSiteDummyConnection::HandleStreamReceive, this,
|
||||
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred),
|
||||
60); // 60 seconds timeout
|
||||
}
|
||||
|
||||
void EepSiteDummyConnection::HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||||
{
|
||||
if (ecode)
|
||||
{
|
||||
LogPrint ("eepSite error: ", ecode.message ());
|
||||
DeleteStream (m_Stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string response ("HTTP/1.0 200 OK\r\n");
|
||||
m_Stream->Send ((uint8_t *)response.c_str (), response.length (), 30);
|
||||
}
|
||||
delete this;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
22
HTTPServer.h
22
HTTPServer.h
|
@ -59,6 +59,10 @@ namespace util
|
|||
void FillContent (std::stringstream& s);
|
||||
std::string ExtractAddress ();
|
||||
|
||||
// for eepsite
|
||||
void EepAccept (i2p::stream::StreamingDestination * destination);
|
||||
void HandleEepAccept (i2p::stream::Stream * stream);
|
||||
|
||||
protected:
|
||||
|
||||
boost::asio::ip::tcp::socket * m_Socket;
|
||||
|
@ -106,6 +110,24 @@ namespace util
|
|||
protected:
|
||||
virtual void CreateConnection(boost::asio::ip::tcp::socket * m_NewSocket);
|
||||
};
|
||||
|
||||
// TODO: move away
|
||||
class EepSiteDummyConnection
|
||||
{
|
||||
public:
|
||||
|
||||
EepSiteDummyConnection (i2p::stream::Stream * stream): m_Stream (stream) {};
|
||||
void AsyncStreamReceive ();
|
||||
|
||||
private:
|
||||
|
||||
void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||||
|
||||
private:
|
||||
|
||||
i2p::stream::Stream * m_Stream;
|
||||
char m_StreamBuffer[8192];
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -127,6 +127,7 @@ namespace stream
|
|||
if (!m_RemoteLeaseSet)
|
||||
{
|
||||
i2p::data::Identity * identity = (i2p::data::Identity *)optionData;
|
||||
LogPrint ("Incoming stream from ", identity->Hash ().ToBase64 ());
|
||||
m_RemoteLeaseSet = i2p::data::netdb.FindLeaseSet (identity->Hash ());
|
||||
if (!m_RemoteLeaseSet)
|
||||
LogPrint ("LeaseSet ", identity->Hash ().ToBase64 (), " not found");
|
||||
|
@ -410,6 +411,8 @@ namespace stream
|
|||
else // new incoming stream
|
||||
{
|
||||
auto incomingStream = CreateNewIncomingStream ();
|
||||
if (m_Acceptor != nullptr)
|
||||
m_Acceptor (incomingStream);
|
||||
incomingStream->HandleNextPacket (packet);
|
||||
}
|
||||
}
|
||||
|
@ -438,6 +441,11 @@ namespace stream
|
|||
}
|
||||
|
||||
I2NPMessage * StreamingDestination::GetLeaseSetMsg ()
|
||||
{
|
||||
return CreateDatabaseStoreMsg (GetLeaseSet ());
|
||||
}
|
||||
|
||||
const i2p::data::LeaseSet * StreamingDestination::GetLeaseSet ()
|
||||
{
|
||||
if (!m_Pool) return nullptr;
|
||||
if (!m_LeaseSet || m_LeaseSet->HasExpiredLeases ())
|
||||
|
@ -450,7 +458,7 @@ namespace stream
|
|||
for (auto it: m_Streams)
|
||||
it.second->SetLeaseSetUpdated ();
|
||||
}
|
||||
return CreateDatabaseStoreMsg (m_LeaseSet);
|
||||
return m_LeaseSet;
|
||||
}
|
||||
|
||||
void StreamingDestination::Sign (const uint8_t * buf, int len, uint8_t * signature) const
|
||||
|
@ -525,12 +533,17 @@ namespace stream
|
|||
return m_SharedLocalDestination->CreateNewOutgoingStream (remote);
|
||||
}
|
||||
|
||||
void StreamingDestinations::DeleteClientStream (Stream * stream)
|
||||
void StreamingDestinations::DeleteStream (Stream * stream)
|
||||
{
|
||||
if (m_SharedLocalDestination)
|
||||
m_SharedLocalDestination->DeleteStream (stream);
|
||||
else
|
||||
delete stream;
|
||||
if (stream)
|
||||
{
|
||||
m_Service.post (
|
||||
[=](void)
|
||||
{
|
||||
stream->GetLocalDestination ()->DeleteStream (stream);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void StreamingDestinations::HandleNextPacket (i2p::data::IdentHash destination, Packet * packet)
|
||||
|
@ -557,7 +570,7 @@ namespace stream
|
|||
|
||||
void DeleteStream (Stream * stream)
|
||||
{
|
||||
destinations.DeleteClientStream (stream);
|
||||
destinations.DeleteStream (stream);
|
||||
}
|
||||
|
||||
void StartStreaming ()
|
||||
|
@ -570,6 +583,11 @@ namespace stream
|
|||
destinations.Stop ();
|
||||
}
|
||||
|
||||
StreamingDestination * GetSharedLocalDestination ()
|
||||
{
|
||||
return destinations.GetSharedLocalDestination ();
|
||||
}
|
||||
|
||||
void HandleDataMessage (i2p::data::IdentHash destination, const uint8_t * buf, size_t len)
|
||||
{
|
||||
uint32_t length = be32toh (*(uint32_t *)buf);
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <set>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <cryptopp/dsa.h>
|
||||
|
@ -78,6 +79,7 @@ namespace stream
|
|||
const i2p::data::LeaseSet * GetRemoteLeaseSet () const { return m_RemoteLeaseSet; };
|
||||
bool IsOpen () const { return m_IsOpen; };
|
||||
bool IsEstablished () const { return m_SendStreamID; };
|
||||
StreamingDestination * GetLocalDestination () { return m_LocalDestination; };
|
||||
|
||||
void HandleNextPacket (Packet * packet);
|
||||
size_t Send (const uint8_t * buf, size_t len, int timeout); // timeout in seconds
|
||||
|
@ -127,10 +129,12 @@ namespace stream
|
|||
|
||||
const i2p::data::PrivateKeys& GetKeys () const { return m_Keys; };
|
||||
I2NPMessage * GetLeaseSetMsg ();
|
||||
const i2p::data::LeaseSet * GetLeaseSet ();
|
||||
i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; };
|
||||
|
||||
Stream * CreateNewOutgoingStream (const i2p::data::LeaseSet& remote);
|
||||
void DeleteStream (Stream * stream);
|
||||
void SetAcceptor (const std::function<void (Stream *)>& acceptor) { m_Acceptor = acceptor; };
|
||||
void HandleNextPacket (Packet * packet);
|
||||
|
||||
// implements LocalDestination
|
||||
|
@ -156,6 +160,7 @@ namespace stream
|
|||
i2p::data::LeaseSet * m_LeaseSet;
|
||||
|
||||
CryptoPP::DSA::PrivateKey m_SigningPrivateKey;
|
||||
std::function<void (Stream *)> m_Acceptor;
|
||||
};
|
||||
|
||||
class StreamingDestinations
|
||||
|
@ -172,7 +177,8 @@ namespace stream
|
|||
void HandleNextPacket (i2p::data::IdentHash destination, Packet * packet);
|
||||
|
||||
Stream * CreateClientStream (const i2p::data::LeaseSet& remote);
|
||||
void DeleteClientStream (Stream * stream);
|
||||
void DeleteStream (Stream * stream);
|
||||
StreamingDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; };
|
||||
|
||||
private:
|
||||
|
||||
|
@ -195,6 +201,7 @@ namespace stream
|
|||
void DeleteStream (Stream * stream);
|
||||
void StartStreaming ();
|
||||
void StopStreaming ();
|
||||
StreamingDestination * GetSharedLocalDestination ();
|
||||
|
||||
// assuming data is I2CP message
|
||||
void HandleDataMessage (i2p::data::IdentHash destination, const uint8_t * buf, size_t len);
|
||||
|
|
Loading…
Add table
Reference in a new issue