/ libi2pd_client / ClientContext.h
ClientContext.h
  1  /*
  2  * Copyright (c) 2013-2025, The PurpleI2P Project
  3  *
  4  * This file is part of Purple i2pd project and licensed under BSD3
  5  *
  6  * See full license text in LICENSE file at top of project tree
  7  */
  8  
  9  #ifndef CLIENT_CONTEXT_H__
 10  #define CLIENT_CONTEXT_H__
 11  
 12  #include <map>
 13  #include <mutex>
 14  #include <memory>
 15  #include <string_view>
 16  #include <boost/asio.hpp>
 17  #include "Destination.h"
 18  #include "I2PService.h"
 19  #include "I2PTunnel.h"
 20  #include "UDPTunnel.h"
 21  #include "SAM.h"
 22  #include "BOB.h"
 23  #include "I2CP.h"
 24  #include "AddressBook.h"
 25  #include "I18N_langs.h"
 26  
 27  namespace i2p
 28  {
 29  namespace client
 30  {
 31  	const char I2P_TUNNELS_SECTION_TYPE[] = "type";
 32  	const char I2P_TUNNELS_SECTION_TYPE_CLIENT[] = "client";
 33  	const char I2P_TUNNELS_SECTION_TYPE_SERVER[] = "server";
 34  	const char I2P_TUNNELS_SECTION_TYPE_HTTP[] = "http";
 35  	const char I2P_TUNNELS_SECTION_TYPE_IRC[] = "irc";
 36  	const char I2P_TUNNELS_SECTION_TYPE_UDPCLIENT[] = "udpclient";
 37  	const char I2P_TUNNELS_SECTION_TYPE_UDPSERVER[] = "udpserver";
 38  	const char I2P_TUNNELS_SECTION_TYPE_SOCKS[] = "socks";
 39  	const char I2P_TUNNELS_SECTION_TYPE_WEBSOCKS[] = "websocks";
 40  	const char I2P_TUNNELS_SECTION_TYPE_HTTPPROXY[] = "httpproxy";
 41  	const char I2P_CLIENT_TUNNEL_PORT[] = "port";
 42  	const char I2P_CLIENT_TUNNEL_ADDRESS[] = "address";
 43  	const char I2P_CLIENT_TUNNEL_DESTINATION[] = "destination";
 44  	const char I2P_CLIENT_TUNNEL_KEYS[] = "keys";
 45  	const char I2P_CLIENT_TUNNEL_GZIP[] = "gzip";
 46  	const char I2P_CLIENT_TUNNEL_SIGNATURE_TYPE[] = "signaturetype";
 47  	const char I2P_CLIENT_TUNNEL_CRYPTO_TYPE[] = "cryptotype";
 48  	const char I2P_CLIENT_TUNNEL_DESTINATION_PORT[] = "destinationport";
 49  	const char I2P_CLIENT_TUNNEL_MATCH_TUNNELS[] = "matchtunnels";
 50  	const char I2P_CLIENT_TUNNEL_CONNECT_TIMEOUT[] = "connecttimeout";
 51  	const char I2P_CLIENT_TUNNEL_KEEP_ALIVE_INTERVAL[] = "keepaliveinterval";
 52  	const char I2P_SERVER_TUNNEL_HOST[] = "host";
 53  	const char I2P_SERVER_TUNNEL_HOST_OVERRIDE[] = "hostoverride";
 54  	const char I2P_SERVER_TUNNEL_I2P_HEADERS[] = "i2pheaders";
 55  	const char I2P_SERVER_TUNNEL_PORT[] = "port";
 56  	const char I2P_SERVER_TUNNEL_KEYS[] = "keys";
 57  	const char I2P_SERVER_TUNNEL_SIGNATURE_TYPE[] = "signaturetype";
 58  	const char I2P_SERVER_TUNNEL_INPORT[] = "inport";
 59  	const char I2P_SERVER_TUNNEL_ACCESS_LIST[] = "accesslist";
 60  	const char I2P_SERVER_TUNNEL_WHITE_LIST[] = "whitelist";
 61  	const char I2P_SERVER_TUNNEL_GZIP[] = "gzip";
 62  	const char I2P_SERVER_TUNNEL_WEBIRC_PASSWORD[] = "webircpassword";
 63  	const char I2P_SERVER_TUNNEL_ADDRESS[] = "address";
 64  	const char I2P_SERVER_TUNNEL_ENABLE_UNIQUE_LOCAL[] = "enableuniquelocal";
 65  	const char I2P_SERVER_TUNNEL_SSL[] = "ssl";
 66  	const char UDP_CLIENT_TUNNEL_DATAGRAM_VERSION[] = "datagramversion";
 67  	
 68  	class ClientContext
 69  	{
 70  		public:
 71  
 72  			ClientContext ();
 73  			~ClientContext ();
 74  
 75  			void Start ();
 76  			void Stop ();
 77  
 78  			void ReloadConfig ();
 79  
 80  			std::shared_ptr<ClientDestination> GetSharedLocalDestination () const { return m_SharedLocalDestination; };
 81  			std::shared_ptr<ClientDestination> CreateNewLocalDestination (bool isPublic = false, // transient
 82  				i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
 83  				i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL,
 84  				const i2p::util::Mapping * params = nullptr); // used by SAM only
 85  			std::shared_ptr<ClientDestination> CreateNewLocalDestination (boost::asio::io_context& service,
 86  				bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
 87  				i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL,
 88  				const i2p::util::Mapping * params = nullptr); // same as previous but on external io_service
 89  			std::shared_ptr<ClientDestination> CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true,
 90  				const i2p::util::Mapping * params = nullptr);
 91  			std::shared_ptr<ClientDestination> CreateNewLocalDestination (boost::asio::io_context& service,
 92  				const i2p::data::PrivateKeys& keys, bool isPublic = true,
 93  				const i2p::util::Mapping * params = nullptr); // same as previous but on external io_service
 94  			std::shared_ptr<ClientDestination> CreateNewMatchedTunnelDestination(const i2p::data::PrivateKeys &keys,
 95  				const std::string & name, const i2p::util::Mapping * params = nullptr);
 96  			void DeleteLocalDestination (std::shared_ptr<ClientDestination> destination);
 97  			std::shared_ptr<ClientDestination> FindLocalDestination (const i2p::data::IdentHash& destination) const;
 98  			bool LoadPrivateKeys (i2p::data::PrivateKeys& keys, std::string_view filename,
 99  				i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
100  				i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL);
101  
102  			AddressBook& GetAddressBook () { return m_AddressBook; };
103  			const BOBCommandChannel * GetBOBCommandChannel () const { return m_BOBCommandChannel; };
104  			const SAMBridge * GetSAMBridge () const { return m_SamBridge; };
105  			const I2CPServer * GetI2CPServer () const { return m_I2CPServer; };
106  
107  			std::vector<std::shared_ptr<DatagramSessionInfo> > GetForwardInfosFor(const i2p::data::IdentHash & destination);
108  
109  			// i18n
110  			std::shared_ptr<const i2p::i18n::Locale> GetLanguage () { return m_Language; };
111  			void SetLanguage (const std::shared_ptr<const i2p::i18n::Locale> language) { m_Language = language; };
112  
113  		private:
114  
115  			void ReadTunnels ();
116  			void ReadTunnels (const std::string& tunConf, int& numClientTunnels, int& numServerTunnels);
117  			void ReadHttpProxy ();
118  			void ReadSocksProxy ();
119  			template<typename Section, typename Type>
120  			std::string GetI2CPOption (const Section& section, const std::string& name, const Type& value) const;
121  			template<typename Section>
122  			std::string GetI2CPStringOption (const Section& section, const std::string& name, const std::string& value) const; // GetI2CPOption with string default value
123  			template<typename Section>
124  			void ReadI2CPOptionsGroup (const Section& section, const std::string& group, i2p::util::Mapping& options) const;
125  			template<typename Section>
126  			void ReadI2CPOptions (const Section& section, bool isServer, i2p::util::Mapping& options) const; // for tunnels
127  			void ReadI2CPOptionsFromConfig (const std::string& prefix, i2p::util::Mapping& options) const; // for HTTP and SOCKS proxy
128  
129  			void CleanupUDP(const boost::system::error_code & ecode);
130  			void ScheduleCleanupUDP();
131  
132  			void VisitTunnels (bool clean);
133  
134  			void CreateNewSharedLocalDestination ();
135  			void AddLocalDestination (std::shared_ptr<ClientDestination> localDestination);
136  
137  		private:
138  
139  			std::mutex m_DestinationsMutex;
140  			std::map<i2p::data::IdentHash, std::shared_ptr<ClientDestination> > m_Destinations;
141  			std::shared_ptr<ClientDestination>  m_SharedLocalDestination;
142  
143  			AddressBook m_AddressBook;
144  
145  			I2PService * m_HttpProxy, * m_SocksProxy;
146  			std::map<boost::asio::ip::tcp::endpoint, std::shared_ptr<I2PService> > m_ClientTunnels; // local endpoint -> tunnel
147  			std::map<std::pair<i2p::data::IdentHash, int>, std::shared_ptr<I2PServerTunnel> > m_ServerTunnels; // <destination,port> -> tunnel
148  
149  			std::mutex m_ForwardsMutex;
150  			std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<I2PUDPClientTunnel> > m_ClientForwards; // local endpoint -> udp tunnel
151  			std::map<std::pair<i2p::data::IdentHash, int>, std::shared_ptr<I2PUDPServerTunnel> > m_ServerForwards; // <destination,port> -> udp tunnel
152  
153  			SAMBridge * m_SamBridge;
154  			BOBCommandChannel * m_BOBCommandChannel;
155  			I2CPServer * m_I2CPServer;
156  
157  			std::unique_ptr<boost::asio::deadline_timer> m_CleanupUDPTimer;
158  
159  			// i18n
160  			std::shared_ptr<const i2p::i18n::Locale> m_Language;
161  
162  		public:
163  
164  			// for HTTP
165  			const decltype(m_Destinations)& GetDestinations () const { return m_Destinations; };
166  			const decltype(m_ClientTunnels)& GetClientTunnels () const { return m_ClientTunnels; };
167  			const decltype(m_ServerTunnels)& GetServerTunnels () const { return m_ServerTunnels; };
168  			const decltype(m_ClientForwards)& GetClientForwards () const { return m_ClientForwards; }
169  			const decltype(m_ServerForwards)& GetServerForwards () const { return m_ServerForwards; }
170  			const I2PService * GetHttpProxy () const { return m_HttpProxy; }
171  			const I2PService * GetSocksProxy () const { return m_SocksProxy; }
172  	};
173  
174  	extern ClientContext context;
175  }
176  }
177  
178  #endif