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.
361 lines
12 KiB
361 lines
12 KiB
11 years ago
|
#ifndef I2PTUNNEL_H__
|
||
|
#define I2PTUNNEL_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
|
#include <string>
|
||
11 years ago
|
#include <set>
|
||
8 years ago
|
#include <tuple>
|
||
10 years ago
|
#include <memory>
|
||
10 years ago
|
#include <sstream>
|
||
11 years ago
|
#include <boost/asio.hpp>
|
||
|
#include "Identity.h"
|
||
10 years ago
|
#include "Destination.h"
|
||
8 years ago
|
#include "Datagram.h"
|
||
11 years ago
|
#include "Streaming.h"
|
||
10 years ago
|
#include "I2PService.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
10 years ago
|
namespace client
|
||
11 years ago
|
{
|
||
8 years ago
|
const size_t I2P_TUNNEL_CONNECTION_BUFFER_SIZE = 65536;
|
||
8 years ago
|
const int I2P_TUNNEL_CONNECTION_MAX_IDLE = 3600; // in seconds
|
||
10 years ago
|
const int I2P_TUNNEL_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds
|
||
8 years ago
|
// for HTTP tunnels
|
||
9 years ago
|
const char X_I2P_DEST_HASH[] = "X-I2P-DestHash"; // hash in base64
|
||
9 years ago
|
const char X_I2P_DEST_B64[] = "X-I2P-DestB64"; // full address in base64
|
||
|
const char X_I2P_DEST_B32[] = "X-I2P-DestB32"; // .b32.i2p address
|
||
10 years ago
|
|
||
|
class I2PTunnelConnection: public I2PServiceHandler, public std::enable_shared_from_this<I2PTunnelConnection>
|
||
11 years ago
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||
10 years ago
|
std::shared_ptr<const i2p::data::LeaseSet> leaseSet, int port = 0); // to I2P
|
||
10 years ago
|
I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||
8 years ago
|
std::shared_ptr<i2p::stream::Stream> stream); // to I2P using simplified API
|
||
|
I2PTunnelConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||
10 years ago
|
const boost::asio::ip::tcp::endpoint& target, bool quiet = true); // from I2P
|
||
11 years ago
|
~I2PTunnelConnection ();
|
||
10 years ago
|
void I2PConnect (const uint8_t * msg = nullptr, size_t len = 0);
|
||
8 years ago
|
void Connect (bool isUniqueLocal = true);
|
||
8 years ago
|
|
||
10 years ago
|
protected:
|
||
11 years ago
|
|
||
8 years ago
|
void Terminate ();
|
||
11 years ago
|
|
||
|
void Receive ();
|
||
8 years ago
|
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||
10 years ago
|
virtual void Write (const uint8_t * buf, size_t len); // can be overloaded
|
||
8 years ago
|
void HandleWrite (const boost::system::error_code& ecode);
|
||
11 years ago
|
|
||
|
void StreamReceive ();
|
||
|
void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
||
10 years ago
|
void HandleConnect (const boost::system::error_code& ecode);
|
||
11 years ago
|
|
||
8 years ago
|
std::shared_ptr<const boost::asio::ip::tcp::socket> GetSocket () const { return m_Socket; };
|
||
|
|
||
11 years ago
|
private:
|
||
|
|
||
|
uint8_t m_Buffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE], m_StreamBuffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE];
|
||
10 years ago
|
std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
|
||
10 years ago
|
std::shared_ptr<i2p::stream::Stream> m_Stream;
|
||
10 years ago
|
boost::asio::ip::tcp::endpoint m_RemoteEndpoint;
|
||
10 years ago
|
bool m_IsQuiet; // don't send destination
|
||
10 years ago
|
};
|
||
10 years ago
|
|
||
8 years ago
|
class I2PClientTunnelConnectionHTTP: public I2PTunnelConnection
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
I2PClientTunnelConnectionHTTP (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||
|
std::shared_ptr<i2p::stream::Stream> stream):
|
||
|
I2PTunnelConnection (owner, socket, stream), m_HeaderSent (false),
|
||
|
m_ConnectionSent (false), m_ProxyConnectionSent (false) {};
|
||
|
|
||
|
protected:
|
||
|
|
||
8 years ago
|
void Write (const uint8_t * buf, size_t len);
|
||
|
|
||
|
private:
|
||
8 years ago
|
|
||
|
std::stringstream m_InHeader, m_OutHeader;
|
||
|
bool m_HeaderSent, m_ConnectionSent, m_ProxyConnectionSent;
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
class I2PServerTunnelConnectionHTTP: public I2PTunnelConnection
|
||
10 years ago
|
{
|
||
|
public:
|
||
8 years ago
|
|
||
8 years ago
|
I2PServerTunnelConnectionHTTP (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
||
8 years ago
|
std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||
|
const boost::asio::ip::tcp::endpoint& target, const std::string& host);
|
||
10 years ago
|
|
||
|
protected:
|
||
|
|
||
|
void Write (const uint8_t * buf, size_t len);
|
||
|
|
||
|
private:
|
||
8 years ago
|
|
||
10 years ago
|
std::string m_Host;
|
||
|
std::stringstream m_InHeader, m_OutHeader;
|
||
10 years ago
|
bool m_HeaderSent;
|
||
9 years ago
|
std::shared_ptr<const i2p::data::IdentityEx> m_From;
|
||
10 years ago
|
};
|
||
|
|
||
9 years ago
|
class I2PTunnelConnectionIRC: public I2PTunnelConnection
|
||
|
{
|
||
|
public:
|
||
8 years ago
|
|
||
9 years ago
|
I2PTunnelConnectionIRC (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
||
8 years ago
|
std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||
|
const boost::asio::ip::tcp::endpoint& target, const std::string& m_WebircPass);
|
||
9 years ago
|
|
||
|
protected:
|
||
|
|
||
|
void Write (const uint8_t * buf, size_t len);
|
||
|
|
||
|
private:
|
||
8 years ago
|
|
||
9 years ago
|
std::shared_ptr<const i2p::data::IdentityEx> m_From;
|
||
|
std::stringstream m_OutPacket, m_InPacket;
|
||
9 years ago
|
bool m_NeedsWebIrc;
|
||
8 years ago
|
std::string m_WebircPass;
|
||
9 years ago
|
};
|
||
|
|
||
|
|
||
10 years ago
|
class I2PClientTunnel: public TCPIPAcceptor
|
||
11 years ago
|
{
|
||
10 years ago
|
protected:
|
||
|
|
||
|
// Implements TCPIPAcceptor
|
||
10 years ago
|
std::shared_ptr<I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket);
|
||
10 years ago
|
|
||
11 years ago
|
public:
|
||
|
|
||
8 years ago
|
I2PClientTunnel (const std::string& name, const std::string& destination,
|
||
9 years ago
|
const std::string& address, int port, std::shared_ptr<ClientDestination> localDestination, int destinationPort = 0);
|
||
10 years ago
|
~I2PClientTunnel () {}
|
||
8 years ago
|
|
||
11 years ago
|
void Start ();
|
||
|
void Stop ();
|
||
|
|
||
8 years ago
|
const char* GetName() { return m_Name.c_str (); }
|
||
|
|
||
11 years ago
|
private:
|
||
|
|
||
10 years ago
|
const i2p::data::IdentHash * GetIdentHash ();
|
||
11 years ago
|
|
||
9 years ago
|
private:
|
||
8 years ago
|
|
||
9 years ago
|
std::string m_Name, m_Destination;
|
||
11 years ago
|
const i2p::data::IdentHash * m_DestinationIdentHash;
|
||
8 years ago
|
int m_DestinationPort;
|
||
8 years ago
|
};
|
||
|
|
||
10 years ago
|
|
||
8 years ago
|
/** 2 minute timeout for udp sessions */
|
||
|
const uint64_t I2P_UDP_SESSION_TIMEOUT = 1000 * 60 * 2;
|
||
|
|
||
|
/** max size for i2p udp */
|
||
|
const size_t I2P_UDP_MAX_MTU = i2p::datagram::MAX_DATAGRAM_SIZE;
|
||
8 years ago
|
|
||
8 years ago
|
struct UDPSession
|
||
|
{
|
||
|
i2p::datagram::DatagramDestination * m_Destination;
|
||
|
boost::asio::ip::udp::socket IPSocket;
|
||
|
i2p::data::IdentHash Identity;
|
||
|
boost::asio::ip::udp::endpoint FromEndpoint;
|
||
|
boost::asio::ip::udp::endpoint SendEndpoint;
|
||
|
uint64_t LastActivity;
|
||
|
|
||
|
uint16_t LocalPort;
|
||
|
uint16_t RemotePort;
|
||
|
|
||
|
uint8_t m_Buffer[I2P_UDP_MAX_MTU];
|
||
8 years ago
|
|
||
8 years ago
|
UDPSession(boost::asio::ip::udp::endpoint localEndpoint,
|
||
|
const std::shared_ptr<i2p::client::ClientDestination> & localDestination,
|
||
8 years ago
|
boost::asio::ip::udp::endpoint remote, const i2p::data::IdentHash * ident,
|
||
8 years ago
|
uint16_t ourPort, uint16_t theirPort);
|
||
|
void HandleReceived(const boost::system::error_code & ecode, std::size_t len);
|
||
|
void Receive();
|
||
|
};
|
||
|
|
||
8 years ago
|
|
||
8 years ago
|
/** read only info about a datagram session */
|
||
|
struct DatagramSessionInfo
|
||
|
{
|
||
|
/** the name of this forward */
|
||
|
std::string Name;
|
||
|
/** ident hash of local destination */
|
||
|
std::shared_ptr<const i2p::data::IdentHash> LocalIdent;
|
||
|
/** ident hash of remote destination */
|
||
|
std::shared_ptr<const i2p::data::IdentHash> RemoteIdent;
|
||
|
/** ident hash of IBGW in use currently in this session or nullptr if none is set */
|
||
|
std::shared_ptr<const i2p::data::IdentHash> CurrentIBGW;
|
||
|
/** ident hash of OBEP in use for this session or nullptr if none is set */
|
||
|
std::shared_ptr<const i2p::data::IdentHash> CurrentOBEP;
|
||
|
/** i2p router's udp endpoint */
|
||
|
boost::asio::ip::udp::endpoint LocalEndpoint;
|
||
|
/** client's udp endpoint */
|
||
|
boost::asio::ip::udp::endpoint RemoteEndpoint;
|
||
|
/** how long has this converstation been idle in ms */
|
||
|
uint64_t idle;
|
||
|
};
|
||
8 years ago
|
|
||
|
typedef std::shared_ptr<UDPSession> UDPSessionPtr;
|
||
8 years ago
|
|
||
8 years ago
|
/** server side udp tunnel, many i2p inbound to 1 ip outbound */
|
||
|
class I2PUDPServerTunnel
|
||
8 years ago
|
{
|
||
8 years ago
|
public:
|
||
|
I2PUDPServerTunnel(const std::string & name,
|
||
|
std::shared_ptr<i2p::client::ClientDestination> localDestination,
|
||
8 years ago
|
boost::asio::ip::address localAddress,
|
||
8 years ago
|
boost::asio::ip::udp::endpoint forwardTo, uint16_t port);
|
||
|
~I2PUDPServerTunnel();
|
||
|
/** expire stale udp conversations */
|
||
|
void ExpireStale(const uint64_t delta=I2P_UDP_SESSION_TIMEOUT);
|
||
|
void Start();
|
||
|
const char * GetName() const { return m_Name.c_str(); }
|
||
8 years ago
|
std::vector<std::shared_ptr<DatagramSessionInfo> > GetSessions();
|
||
8 years ago
|
std::shared_ptr<ClientDestination> GetLocalDestination () const { return m_LocalDest; }
|
||
|
|
||
8 years ago
|
void SetUniqueLocal(bool isUniqueLocal = true) { m_IsUniqueLocal = isUniqueLocal; }
|
||
8 years ago
|
|
||
8 years ago
|
private:
|
||
8 years ago
|
|
||
8 years ago
|
void HandleRecvFromI2P(const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
||
8 years ago
|
UDPSessionPtr ObtainUDPSession(const i2p::data::IdentityEx& from, uint16_t localPort, uint16_t remotePort);
|
||
8 years ago
|
|
||
8 years ago
|
private:
|
||
8 years ago
|
bool m_IsUniqueLocal;
|
||
8 years ago
|
const std::string m_Name;
|
||
|
boost::asio::ip::address m_LocalAddress;
|
||
|
boost::asio::ip::udp::endpoint m_RemoteEndpoint;
|
||
|
std::mutex m_SessionsMutex;
|
||
8 years ago
|
std::vector<UDPSessionPtr> m_Sessions;
|
||
8 years ago
|
std::shared_ptr<i2p::client::ClientDestination> m_LocalDest;
|
||
|
};
|
||
|
|
||
8 years ago
|
class I2PUDPClientTunnel
|
||
8 years ago
|
{
|
||
|
public:
|
||
|
I2PUDPClientTunnel(const std::string & name, const std::string &remoteDest,
|
||
|
boost::asio::ip::udp::endpoint localEndpoint, std::shared_ptr<i2p::client::ClientDestination> localDestination,
|
||
|
uint16_t remotePort);
|
||
|
~I2PUDPClientTunnel();
|
||
|
void Start();
|
||
|
const char * GetName() const { return m_Name.c_str(); }
|
||
8 years ago
|
std::vector<std::shared_ptr<DatagramSessionInfo> > GetSessions();
|
||
8 years ago
|
|
||
8 years ago
|
bool IsLocalDestination(const i2p::data::IdentHash & destination) const { return destination == m_LocalDest->GetIdentHash(); }
|
||
8 years ago
|
|
||
|
std::shared_ptr<ClientDestination> GetLocalDestination () const { return m_LocalDest; }
|
||
8 years ago
|
void ExpireStale(const uint64_t delta=I2P_UDP_SESSION_TIMEOUT);
|
||
8 years ago
|
|
||
8 years ago
|
private:
|
||
8 years ago
|
typedef std::pair<boost::asio::ip::udp::endpoint, uint64_t> UDPConvo;
|
||
8 years ago
|
void RecvFromLocal();
|
||
|
void HandleRecvFromLocal(const boost::system::error_code & e, std::size_t transferred);
|
||
8 years ago
|
void HandleRecvFromI2P(const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
||
|
void TryResolving();
|
||
|
const std::string m_Name;
|
||
8 years ago
|
std::mutex m_SessionsMutex;
|
||
|
std::map<uint16_t, UDPConvo > m_Sessions; // maps i2p port -> local udp convo
|
||
8 years ago
|
const std::string m_RemoteDest;
|
||
|
std::shared_ptr<i2p::client::ClientDestination> m_LocalDest;
|
||
|
const boost::asio::ip::udp::endpoint m_LocalEndpoint;
|
||
|
i2p::data::IdentHash * m_RemoteIdent;
|
||
|
std::thread * m_ResolveThread;
|
||
8 years ago
|
boost::asio::ip::udp::socket m_LocalSocket;
|
||
|
boost::asio::ip::udp::endpoint m_RecvEndpoint;
|
||
|
uint8_t m_RecvBuff[I2P_UDP_MAX_MTU];
|
||
8 years ago
|
uint16_t RemotePort;
|
||
|
bool m_cancel_resolve;
|
||
|
};
|
||
8 years ago
|
|
||
10 years ago
|
class I2PServerTunnel: public I2PService
|
||
10 years ago
|
{
|
||
|
public:
|
||
|
|
||
8 years ago
|
I2PServerTunnel (const std::string& name, const std::string& address, int port,
|
||
|
std::shared_ptr<ClientDestination> localDestination, int inport = 0, bool gzip = true);
|
||
10 years ago
|
|
||
|
void Start ();
|
||
|
void Stop ();
|
||
|
|
||
8 years ago
|
void SetAccessList (const std::set<i2p::data::IdentHash>& accessList);
|
||
10 years ago
|
|
||
8 years ago
|
void SetUniqueLocal (bool isUniqueLocal) { m_IsUniqueLocal = isUniqueLocal; }
|
||
8 years ago
|
bool IsUniqueLocal () const { return m_IsUniqueLocal; }
|
||
8 years ago
|
|
||
10 years ago
|
const std::string& GetAddress() const { return m_Address; }
|
||
|
int GetPort () const { return m_Port; };
|
||
9 years ago
|
uint16_t GetLocalPort () const { return m_PortDestination->GetLocalPort (); };
|
||
10 years ago
|
const boost::asio::ip::tcp::endpoint& GetEndpoint () const { return m_Endpoint; }
|
||
|
|
||
8 years ago
|
const char* GetName() { return m_Name.c_str (); }
|
||
9 years ago
|
|
||
|
void SetMaxConnsPerMinute(const uint32_t conns) { m_PortDestination->SetMaxConnsPerMinute(conns); }
|
||
8 years ago
|
|
||
9 years ago
|
private:
|
||
10 years ago
|
|
||
8 years ago
|
void HandleResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::iterator it,
|
||
10 years ago
|
std::shared_ptr<boost::asio::ip::tcp::resolver> resolver);
|
||
|
|
||
10 years ago
|
void Accept ();
|
||
10 years ago
|
void HandleAccept (std::shared_ptr<i2p::stream::Stream> stream);
|
||
8 years ago
|
virtual std::shared_ptr<I2PTunnelConnection> CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream);
|
||
10 years ago
|
|
||
|
private:
|
||
8 years ago
|
|
||
8 years ago
|
bool m_IsUniqueLocal;
|
||
9 years ago
|
std::string m_Name, m_Address;
|
||
10 years ago
|
int m_Port;
|
||
8 years ago
|
boost::asio::ip::tcp::endpoint m_Endpoint;
|
||
10 years ago
|
std::shared_ptr<i2p::stream::StreamingDestination> m_PortDestination;
|
||
|
std::set<i2p::data::IdentHash> m_AccessList;
|
||
8 years ago
|
bool m_IsAccessList;
|
||
10 years ago
|
};
|
||
10 years ago
|
|
||
|
class I2PServerTunnelHTTP: public I2PServerTunnel
|
||
|
{
|
||
|
public:
|
||
|
|
||
8 years ago
|
I2PServerTunnelHTTP (const std::string& name, const std::string& address, int port,
|
||
9 years ago
|
std::shared_ptr<ClientDestination> localDestination, const std::string& host,
|
||
8 years ago
|
int inport = 0, bool gzip = true);
|
||
10 years ago
|
|
||
|
private:
|
||
|
|
||
8 years ago
|
std::shared_ptr<I2PTunnelConnection> CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream);
|
||
9 years ago
|
|
||
|
private:
|
||
|
|
||
|
std::string m_Host;
|
||
10 years ago
|
};
|
||
8 years ago
|
|
||
9 years ago
|
class I2PServerTunnelIRC: public I2PServerTunnel
|
||
|
{
|
||
|
public:
|
||
|
|
||
8 years ago
|
I2PServerTunnelIRC (const std::string& name, const std::string& address, int port,
|
||
9 years ago
|
std::shared_ptr<ClientDestination> localDestination, const std::string& webircpass,
|
||
8 years ago
|
int inport = 0, bool gzip = true);
|
||
9 years ago
|
|
||
|
private:
|
||
|
|
||
8 years ago
|
std::shared_ptr<I2PTunnelConnection> CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream);
|
||
9 years ago
|
|
||
|
private:
|
||
|
|
||
9 years ago
|
std::string m_WebircPass;
|
||
9 years ago
|
};
|
||
|
|
||
10 years ago
|
}
|
||
8 years ago
|
}
|
||
11 years ago
|
|
||
|
#endif
|