i2pd/libi2pd_client/ClientContext.h

145 lines
6.3 KiB
C
Raw Normal View History

2014-10-16 00:52:17 +00:00
#ifndef CLIENT_CONTEXT_H__
#define CLIENT_CONTEXT_H__
#include <map>
2014-10-16 00:52:17 +00:00
#include <mutex>
2015-02-12 21:11:56 +00:00
#include <memory>
#include <boost/asio.hpp>
2014-10-16 00:52:17 +00:00
#include "Destination.h"
#include "I2PService.h"
#include "HTTPProxy.h"
#include "SOCKS.h"
#include "I2PTunnel.h"
#include "SAM.h"
2014-12-02 15:34:02 +00:00
#include "BOB.h"
2016-05-12 19:37:46 +00:00
#include "I2CP.h"
2014-10-24 19:22:36 +00:00
#include "AddressBook.h"
2014-10-16 00:52:17 +00:00
namespace i2p
{
namespace client
{
2015-03-13 23:51:31 +00:00
const char I2P_TUNNELS_SECTION_TYPE[] = "type";
const char I2P_TUNNELS_SECTION_TYPE_CLIENT[] = "client";
const char I2P_TUNNELS_SECTION_TYPE_SERVER[] = "server";
2015-05-20 20:00:09 +00:00
const char I2P_TUNNELS_SECTION_TYPE_HTTP[] = "http";
2016-02-22 19:33:21 +00:00
const char I2P_TUNNELS_SECTION_TYPE_IRC[] = "irc";
const char I2P_TUNNELS_SECTION_TYPE_UDPCLIENT[] = "udpclient";
const char I2P_TUNNELS_SECTION_TYPE_UDPSERVER[] = "udpserver";
const char I2P_TUNNELS_SECTION_TYPE_SOCKS[] = "socks";
const char I2P_TUNNELS_SECTION_TYPE_WEBSOCKS[] = "websocks";
const char I2P_TUNNELS_SECTION_TYPE_HTTPPROXY[] = "httpproxy";
2015-03-13 23:51:31 +00:00
const char I2P_CLIENT_TUNNEL_PORT[] = "port";
2015-11-30 14:44:32 +00:00
const char I2P_CLIENT_TUNNEL_ADDRESS[] = "address";
2015-03-13 23:51:31 +00:00
const char I2P_CLIENT_TUNNEL_DESTINATION[] = "destination";
const char I2P_CLIENT_TUNNEL_KEYS[] = "keys";
2016-01-04 02:43:43 +00:00
const char I2P_CLIENT_TUNNEL_SIGNATURE_TYPE[] = "signaturetype";
const char I2P_CLIENT_TUNNEL_CRYPTO_TYPE[] = "cryptotype";
const char I2P_CLIENT_TUNNEL_DESTINATION_PORT[] = "destinationport";
2017-04-08 19:14:47 +00:00
const char I2P_CLIENT_TUNNEL_MATCH_TUNNELS[] = "matchtunnels";
const char I2P_CLIENT_TUNNEL_CONNECT_TIMEOUT[] = "connecttimeout";
const char I2P_SERVER_TUNNEL_HOST[] = "host";
const char I2P_SERVER_TUNNEL_HOST_OVERRIDE[] = "hostoverride";
2015-03-13 23:51:31 +00:00
const char I2P_SERVER_TUNNEL_PORT[] = "port";
const char I2P_SERVER_TUNNEL_KEYS[] = "keys";
2016-01-04 02:43:43 +00:00
const char I2P_SERVER_TUNNEL_SIGNATURE_TYPE[] = "signaturetype";
const char I2P_SERVER_TUNNEL_INPORT[] = "inport";
const char I2P_SERVER_TUNNEL_ACCESS_LIST[] = "accesslist";
2016-03-04 06:37:38 +00:00
const char I2P_SERVER_TUNNEL_GZIP[] = "gzip";
const char I2P_SERVER_TUNNEL_WEBIRC_PASSWORD[] = "webircpassword";
2016-09-03 13:38:53 +00:00
const char I2P_SERVER_TUNNEL_ADDRESS[] = "address";
const char I2P_SERVER_TUNNEL_ENABLE_UNIQUE_LOCAL[] = "enableuniquelocal";
2014-10-16 00:52:17 +00:00
class ClientContext
{
public:
ClientContext ();
~ClientContext ();
2014-10-16 00:52:17 +00:00
void Start ();
void Stop ();
void ReloadConfig ();
2015-02-24 20:40:50 +00:00
std::shared_ptr<ClientDestination> GetSharedLocalDestination () const { return m_SharedLocalDestination; };
2017-11-14 20:05:07 +00:00
std::shared_ptr<ClientDestination> CreateNewLocalDestination (bool isPublic = false, // transient
2017-11-14 18:31:13 +00:00
i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1,
2017-11-14 20:05:07 +00:00
i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL,
2017-11-14 18:31:13 +00:00
const std::map<std::string, std::string> * params = nullptr); // used by SAM only
std::shared_ptr<ClientDestination> CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true,
2017-11-14 18:31:13 +00:00
const std::map<std::string, std::string> * params = nullptr);
2017-04-08 19:16:51 +00:00
std::shared_ptr<ClientDestination> CreateNewMatchedTunnelDestination(const i2p::data::PrivateKeys &keys, const std::string & name, const std::map<std::string, std::string> * params = nullptr);
2015-02-24 20:40:50 +00:00
void DeleteLocalDestination (std::shared_ptr<ClientDestination> destination);
std::shared_ptr<ClientDestination> FindLocalDestination (const i2p::data::IdentHash& destination) const;
2018-01-06 03:48:51 +00:00
bool LoadPrivateKeys (i2p::data::PrivateKeys& keys, const std::string& filename,
i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256,
i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL);
2014-10-16 00:52:17 +00:00
2014-10-24 19:22:36 +00:00
AddressBook& GetAddressBook () { return m_AddressBook; };
2017-11-14 18:31:13 +00:00
const BOBCommandChannel * GetBOBCommandChannel () const { return m_BOBCommandChannel; };
const SAMBridge * GetSAMBridge () const { return m_SamBridge; };
2016-11-17 18:10:29 +00:00
const I2CPServer * GetI2CPServer () const { return m_I2CPServer; };
2016-09-03 17:58:34 +00:00
2016-09-03 21:53:46 +00:00
std::vector<std::shared_ptr<DatagramSessionInfo> > GetForwardInfosFor(const i2p::data::IdentHash & destination);
2016-10-20 13:12:15 +00:00
2015-02-12 21:11:56 +00:00
private:
void ReadTunnels ();
void ReadHttpProxy ();
void ReadSocksProxy ();
2016-01-15 17:24:40 +00:00
template<typename Section, typename Type>
std::string GetI2CPOption (const Section& section, const std::string& name, const Type& value) const;
template<typename Section>
void ReadI2CPOptions (const Section& section, std::map<std::string, std::string>& options) const; // for tunnels
void ReadI2CPOptionsFromConfig (const std::string& prefix, std::map<std::string, std::string>& options) const; // for HTTP and SOCKS proxy
2016-01-15 17:24:40 +00:00
void CleanupUDP(const boost::system::error_code & ecode);
void ScheduleCleanupUDP();
template<typename Visitor>
void VisitTunnels (Visitor v); // Visitor: (I2PService *) -> bool, true means retain
void CreateNewSharedLocalDestination ();
private:
2014-10-16 00:52:17 +00:00
std::mutex m_DestinationsMutex;
2015-02-24 20:40:50 +00:00
std::map<i2p::data::IdentHash, std::shared_ptr<ClientDestination> > m_Destinations;
std::shared_ptr<ClientDestination> m_SharedLocalDestination;
2014-10-16 00:52:17 +00:00
2014-10-24 19:22:36 +00:00
AddressBook m_AddressBook;
i2p::proxy::HTTPProxy * m_HttpProxy;
i2p::proxy::SOCKSProxy * m_SocksProxy;
2018-04-25 20:18:07 +00:00
std::map<boost::asio::ip::tcp::endpoint, std::shared_ptr<I2PService> > m_ClientTunnels; // local endpoint->tunnel
std::map<std::pair<i2p::data::IdentHash, int>, std::shared_ptr<I2PServerTunnel> > m_ServerTunnels; // <destination,port>->tunnel
2016-08-21 19:02:17 +00:00
std::mutex m_ForwardsMutex;
2018-04-25 20:18:07 +00:00
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<I2PUDPClientTunnel> > m_ClientForwards; // local endpoint -> udp tunnel
std::map<std::pair<i2p::data::IdentHash, int>, std::shared_ptr<I2PUDPServerTunnel> > m_ServerForwards; // <destination,port> -> udp tunnel
SAMBridge * m_SamBridge;
2014-12-02 15:34:02 +00:00
BOBCommandChannel * m_BOBCommandChannel;
2016-05-12 19:37:46 +00:00
I2CPServer * m_I2CPServer;
std::unique_ptr<boost::asio::deadline_timer> m_CleanupUDPTimer;
2014-10-16 00:52:17 +00:00
public:
// for HTTP
const decltype(m_Destinations)& GetDestinations () const { return m_Destinations; };
2016-01-14 01:21:53 +00:00
const decltype(m_ClientTunnels)& GetClientTunnels () const { return m_ClientTunnels; };
const decltype(m_ServerTunnels)& GetServerTunnels () const { return m_ServerTunnels; };
const decltype(m_ClientForwards)& GetClientForwards () const { return m_ClientForwards; }
const decltype(m_ServerForwards)& GetServerForwards () const { return m_ServerForwards; }
2016-10-31 19:42:50 +00:00
const i2p::proxy::HTTPProxy * GetHttpProxy () const { return m_HttpProxy; }
2016-12-28 03:45:51 +00:00
const i2p::proxy::SOCKSProxy * GetSocksProxy () const { return m_SocksProxy; }
2014-10-16 00:52:17 +00:00
};
extern ClientContext context;
}
}
2014-10-16 00:52:17 +00:00
#endif