mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
239 lines
9.5 KiB
239 lines
9.5 KiB
10 years ago
|
#ifndef DESTINATION_H__
|
||
|
#define DESTINATION_H__
|
||
|
|
||
|
#include <thread>
|
||
|
#include <mutex>
|
||
10 years ago
|
#include <memory>
|
||
10 years ago
|
#include <map>
|
||
10 years ago
|
#include <set>
|
||
10 years ago
|
#include <string>
|
||
10 years ago
|
#include <functional>
|
||
8 years ago
|
#ifdef I2LUA
|
||
8 years ago
|
#include <future>
|
||
8 years ago
|
#endif
|
||
10 years ago
|
#include <boost/asio.hpp>
|
||
10 years ago
|
#include "Identity.h"
|
||
|
#include "TunnelPool.h"
|
||
9 years ago
|
#include "Crypto.h"
|
||
10 years ago
|
#include "LeaseSet.h"
|
||
10 years ago
|
#include "Garlic.h"
|
||
10 years ago
|
#include "NetDb.h"
|
||
10 years ago
|
#include "Streaming.h"
|
||
10 years ago
|
#include "Datagram.h"
|
||
10 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
10 years ago
|
namespace client
|
||
10 years ago
|
{
|
||
10 years ago
|
const uint8_t PROTOCOL_TYPE_STREAMING = 6;
|
||
|
const uint8_t PROTOCOL_TYPE_DATAGRAM = 17;
|
||
8 years ago
|
const uint8_t PROTOCOL_TYPE_RAW = 18;
|
||
10 years ago
|
const int PUBLISH_CONFIRMATION_TIMEOUT = 5; // in seconds
|
||
9 years ago
|
const int PUBLISH_VERIFICATION_TIMEOUT = 10; // in seconds after successfull publish
|
||
8 years ago
|
const int PUBLISH_MIN_INTERVAL = 20; // in seconds
|
||
|
const int PUBLISH_REGULAR_VERIFICATION_INTERNAL = 100; // in seconds periodically
|
||
10 years ago
|
const int LEASESET_REQUEST_TIMEOUT = 5; // in seconds
|
||
|
const int MAX_LEASESET_REQUEST_TIMEOUT = 40; // in seconds
|
||
8 years ago
|
const int DESTINATION_CLEANUP_TIMEOUT = 3; // in minutes
|
||
9 years ago
|
const unsigned int MAX_NUM_FLOODFILLS_PER_REQUEST = 7;
|
||
8 years ago
|
|
||
10 years ago
|
// I2CP
|
||
|
const char I2CP_PARAM_INBOUND_TUNNEL_LENGTH[] = "inbound.length";
|
||
|
const int DEFAULT_INBOUND_TUNNEL_LENGTH = 3;
|
||
|
const char I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH[] = "outbound.length";
|
||
|
const int DEFAULT_OUTBOUND_TUNNEL_LENGTH = 3;
|
||
10 years ago
|
const char I2CP_PARAM_INBOUND_TUNNELS_QUANTITY[] = "inbound.quantity";
|
||
|
const int DEFAULT_INBOUND_TUNNELS_QUANTITY = 5;
|
||
|
const char I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY[] = "outbound.quantity";
|
||
|
const int DEFAULT_OUTBOUND_TUNNELS_QUANTITY = 5;
|
||
10 years ago
|
const char I2CP_PARAM_EXPLICIT_PEERS[] = "explicitPeers";
|
||
10 years ago
|
const int STREAM_REQUEST_TIMEOUT = 60; //in seconds
|
||
9 years ago
|
const char I2CP_PARAM_TAGS_TO_SEND[] = "crypto.tagsToSend";
|
||
|
const int DEFAULT_TAGS_TO_SEND = 40;
|
||
9 years ago
|
|
||
8 years ago
|
// latency
|
||
|
const char I2CP_PARAM_MIN_TUNNEL_LATENCY[] = "latency.min";
|
||
|
const int DEFAULT_MIN_TUNNEL_LATENCY = 0;
|
||
|
const char I2CP_PARAM_MAX_TUNNEL_LATENCY[] = "latency.max";
|
||
|
const int DEFAULT_MAX_TUNNEL_LATENCY = 0;
|
||
8 years ago
|
|
||
10 years ago
|
typedef std::function<void (std::shared_ptr<i2p::stream::Stream> stream)> StreamRequestComplete;
|
||
|
|
||
9 years ago
|
class LeaseSetDestination: public i2p::garlic::GarlicDestination,
|
||
|
public std::enable_shared_from_this<LeaseSetDestination>
|
||
10 years ago
|
{
|
||
10 years ago
|
typedef std::function<void (std::shared_ptr<i2p::data::LeaseSet> leaseSet)> RequestComplete;
|
||
|
// leaseSet = nullptr means not found
|
||
10 years ago
|
struct LeaseSetRequest
|
||
|
{
|
||
|
LeaseSetRequest (boost::asio::io_service& service): requestTime (0), requestTimeoutTimer (service) {};
|
||
|
std::set<i2p::data::IdentHash> excluded;
|
||
|
uint64_t requestTime;
|
||
|
boost::asio::deadline_timer requestTimeoutTimer;
|
||
8 years ago
|
std::list<RequestComplete> requestComplete;
|
||
9 years ago
|
std::shared_ptr<i2p::tunnel::OutboundTunnel> outboundTunnel;
|
||
|
std::shared_ptr<i2p::tunnel::InboundTunnel> replyTunnel;
|
||
8 years ago
|
|
||
|
void Complete (std::shared_ptr<i2p::data::LeaseSet> ls)
|
||
|
{
|
||
|
for (auto& it: requestComplete) it (ls);
|
||
|
requestComplete.clear ();
|
||
8 years ago
|
}
|
||
|
};
|
||
|
|
||
|
|
||
10 years ago
|
public:
|
||
|
|
||
9 years ago
|
LeaseSetDestination (bool isPublic, const std::map<std::string, std::string> * params = nullptr);
|
||
8 years ago
|
~LeaseSetDestination ();
|
||
10 years ago
|
|
||
9 years ago
|
virtual bool Start ();
|
||
|
virtual bool Stop ();
|
||
10 years ago
|
bool IsRunning () const { return m_IsRunning; };
|
||
10 years ago
|
boost::asio::io_service& GetService () { return m_Service; };
|
||
8 years ago
|
std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () { return m_Pool; };
|
||
9 years ago
|
bool IsReady () const { return m_LeaseSet && !m_LeaseSet->IsExpired () && m_Pool->GetOutboundTunnels ().size () > 0; };
|
||
10 years ago
|
std::shared_ptr<const i2p::data::LeaseSet> FindLeaseSet (const i2p::data::IdentHash& ident);
|
||
10 years ago
|
bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr);
|
||
8 years ago
|
void CancelDestinationRequest (const i2p::data::IdentHash& dest, bool notify = true);
|
||
9 years ago
|
|
||
10 years ago
|
// implements GarlicDestination
|
||
9 years ago
|
std::shared_ptr<const i2p::data::LocalLeaseSet> GetLeaseSet ();
|
||
10 years ago
|
std::shared_ptr<i2p::tunnel::TunnelPool> GetTunnelPool () const { return m_Pool; }
|
||
10 years ago
|
void HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from);
|
||
10 years ago
|
|
||
10 years ago
|
// override GarlicDestination
|
||
10 years ago
|
bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag);
|
||
10 years ago
|
void ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg);
|
||
8 years ago
|
void ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
|
||
10 years ago
|
void SetLeaseSetUpdated ();
|
||
|
|
||
9 years ago
|
protected:
|
||
10 years ago
|
|
||
9 years ago
|
void SetLeaseSet (i2p::data::LocalLeaseSet * newLeaseSet);
|
||
8 years ago
|
virtual void CleanupDestination () {}; // additional clean up in derived classes
|
||
9 years ago
|
// I2CP
|
||
|
virtual void HandleDataMessage (const uint8_t * buf, size_t len) = 0;
|
||
9 years ago
|
virtual void CreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels) = 0;
|
||
8 years ago
|
|
||
10 years ago
|
private:
|
||
8 years ago
|
|
||
|
void Run ();
|
||
10 years ago
|
void UpdateLeaseSet ();
|
||
10 years ago
|
void Publish ();
|
||
|
void HandlePublishConfirmationTimer (const boost::system::error_code& ecode);
|
||
9 years ago
|
void HandlePublishVerificationTimer (const boost::system::error_code& ecode);
|
||
8 years ago
|
void HandlePublishDelayTimer (const boost::system::error_code& ecode);
|
||
10 years ago
|
void HandleDatabaseStoreMessage (const uint8_t * buf, size_t len);
|
||
|
void HandleDatabaseSearchReplyMessage (const uint8_t * buf, size_t len);
|
||
8 years ago
|
void HandleDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg);
|
||
10 years ago
|
|
||
10 years ago
|
void RequestLeaseSet (const i2p::data::IdentHash& dest, RequestComplete requestComplete);
|
||
8 years ago
|
bool SendLeaseSetRequest (const i2p::data::IdentHash& dest, std::shared_ptr<const i2p::data::RouterInfo> nextFloodfill, std::shared_ptr<LeaseSetRequest> request);
|
||
10 years ago
|
void HandleRequestTimoutTimer (const boost::system::error_code& ecode, const i2p::data::IdentHash& dest);
|
||
10 years ago
|
void HandleCleanupTimer (const boost::system::error_code& ecode);
|
||
8 years ago
|
void CleanupRemoteLeaseSets ();
|
||
|
|
||
10 years ago
|
private:
|
||
|
|
||
10 years ago
|
volatile bool m_IsRunning;
|
||
8 years ago
|
std::thread * m_Thread;
|
||
10 years ago
|
boost::asio::io_service m_Service;
|
||
9 years ago
|
mutable std::mutex m_RemoteLeaseSetsMutex;
|
||
10 years ago
|
std::map<i2p::data::IdentHash, std::shared_ptr<i2p::data::LeaseSet> > m_RemoteLeaseSets;
|
||
9 years ago
|
std::map<i2p::data::IdentHash, std::shared_ptr<LeaseSetRequest> > m_LeaseSetRequests;
|
||
10 years ago
|
|
||
10 years ago
|
std::shared_ptr<i2p::tunnel::TunnelPool> m_Pool;
|
||
8 years ago
|
std::mutex m_LeaseSetMutex;
|
||
9 years ago
|
std::shared_ptr<i2p::data::LocalLeaseSet> m_LeaseSet;
|
||
10 years ago
|
bool m_IsPublic;
|
||
10 years ago
|
uint32_t m_PublishReplyToken;
|
||
8 years ago
|
uint64_t m_LastSubmissionTime; // in seconds
|
||
10 years ago
|
std::set<i2p::data::IdentHash> m_ExcludedFloodfills; // for publishing
|
||
8 years ago
|
|
||
|
boost::asio::deadline_timer m_PublishConfirmationTimer, m_PublishVerificationTimer,
|
||
8 years ago
|
m_PublishDelayTimer, m_CleanupTimer;
|
||
10 years ago
|
|
||
10 years ago
|
public:
|
||
8 years ago
|
|
||
10 years ago
|
// for HTTP only
|
||
|
int GetNumRemoteLeaseSets () const { return m_RemoteLeaseSets.size (); };
|
||
8 years ago
|
const decltype(m_RemoteLeaseSets)& GetLeaseSets () const { return m_RemoteLeaseSets; };
|
||
8 years ago
|
};
|
||
9 years ago
|
|
||
|
class ClientDestination: public LeaseSetDestination
|
||
|
{
|
||
|
public:
|
||
8 years ago
|
#ifdef I2LUA
|
||
8 years ago
|
// type for informing that a client destination is ready
|
||
|
typedef std::promise<std::shared_ptr<ClientDestination> > ReadyPromise;
|
||
8 years ago
|
// informs promise with shared_from_this() when this destination is ready to use
|
||
|
// if cancelled before ready, informs promise with nullptr
|
||
|
void Ready(ReadyPromise & p);
|
||
|
#endif
|
||
8 years ago
|
|
||
9 years ago
|
ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map<std::string, std::string> * params = nullptr);
|
||
9 years ago
|
~ClientDestination ();
|
||
8 years ago
|
|
||
|
virtual bool Start ();
|
||
|
virtual bool Stop ();
|
||
|
|
||
9 years ago
|
const i2p::data::PrivateKeys& GetPrivateKeys () const { return m_Keys; };
|
||
8 years ago
|
void Sign (const uint8_t * buf, int len, uint8_t * signature) const { m_Keys.Sign (buf, len, signature); };
|
||
|
|
||
9 years ago
|
// streaming
|
||
|
std::shared_ptr<i2p::stream::StreamingDestination> CreateStreamingDestination (int port, bool gzip = true); // additional
|
||
|
std::shared_ptr<i2p::stream::StreamingDestination> GetStreamingDestination (int port = 0) const;
|
||
|
// following methods operate with default streaming destination
|
||
|
void CreateStream (StreamRequestComplete streamRequestComplete, const i2p::data::IdentHash& dest, int port = 0);
|
||
|
std::shared_ptr<i2p::stream::Stream> CreateStream (std::shared_ptr<const i2p::data::LeaseSet> remote, int port = 0);
|
||
|
void AcceptStreams (const i2p::stream::StreamingDestination::Acceptor& acceptor);
|
||
|
void StopAcceptingStreams ();
|
||
|
bool IsAcceptingStreams () const;
|
||
8 years ago
|
void AcceptOnce (const i2p::stream::StreamingDestination::Acceptor& acceptor);
|
||
8 years ago
|
|
||
9 years ago
|
// datagram
|
||
8 years ago
|
i2p::datagram::DatagramDestination * GetDatagramDestination () const { return m_DatagramDestination; };
|
||
|
i2p::datagram::DatagramDestination * CreateDatagramDestination ();
|
||
8 years ago
|
|
||
|
// implements LocalDestination
|
||
9 years ago
|
const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; };
|
||
8 years ago
|
std::shared_ptr<const i2p::data::IdentityEx> GetIdentity () const { return m_Keys.GetPublic (); };
|
||
9 years ago
|
|
||
9 years ago
|
protected:
|
||
8 years ago
|
|
||
8 years ago
|
void CleanupDestination ();
|
||
9 years ago
|
// I2CP
|
||
|
void HandleDataMessage (const uint8_t * buf, size_t len);
|
||
9 years ago
|
void CreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels);
|
||
8 years ago
|
|
||
9 years ago
|
private:
|
||
|
|
||
9 years ago
|
std::shared_ptr<ClientDestination> GetSharedFromThis ()
|
||
8 years ago
|
{ return std::static_pointer_cast<ClientDestination>(shared_from_this ()); }
|
||
9 years ago
|
void PersistTemporaryKeys ();
|
||
8 years ago
|
#ifdef I2LUA
|
||
8 years ago
|
void ScheduleCheckForReady(ReadyPromise * p);
|
||
|
void HandleCheckForReady(const boost::system::error_code & ecode, ReadyPromise * p);
|
||
8 years ago
|
#endif
|
||
9 years ago
|
private:
|
||
|
|
||
|
i2p::data::PrivateKeys m_Keys;
|
||
9 years ago
|
uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256];
|
||
9 years ago
|
|
||
|
std::shared_ptr<i2p::stream::StreamingDestination> m_StreamingDestination; // default
|
||
|
std::map<uint16_t, std::shared_ptr<i2p::stream::StreamingDestination> > m_StreamingDestinationsByPorts;
|
||
8 years ago
|
i2p::datagram::DatagramDestination * m_DatagramDestination;
|
||
9 years ago
|
|
||
8 years ago
|
boost::asio::deadline_timer m_ReadyChecker;
|
||
8 years ago
|
|
||
9 years ago
|
public:
|
||
|
|
||
|
// for HTTP only
|
||
|
std::vector<std::shared_ptr<const i2p::stream::Stream> > GetAllStreams () const;
|
||
8 years ago
|
};
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
|
#endif
|