2020-05-22 15:18:41 +02:00
|
|
|
/*
|
2024-02-21 00:51:37 +01:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 15:18:41 +02:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2014-10-16 02:52:17 +02:00
|
|
|
#ifndef CLIENT_CONTEXT_H__
|
|
|
|
#define CLIENT_CONTEXT_H__
|
|
|
|
|
2015-02-13 16:18:42 +01:00
|
|
|
#include <map>
|
2014-10-16 02:52:17 +02:00
|
|
|
#include <mutex>
|
2015-02-12 22:11:56 +01:00
|
|
|
#include <memory>
|
2016-04-24 23:32:24 +02:00
|
|
|
#include <boost/asio.hpp>
|
2014-10-16 02:52:17 +02:00
|
|
|
#include "Destination.h"
|
2016-05-26 02:00:00 +02:00
|
|
|
#include "I2PService.h"
|
2014-10-16 03:39:39 +02:00
|
|
|
#include "I2PTunnel.h"
|
2022-10-25 21:30:12 +02:00
|
|
|
#include "UDPTunnel.h"
|
2014-10-16 03:39:39 +02:00
|
|
|
#include "SAM.h"
|
2014-12-02 16:34:02 +01:00
|
|
|
#include "BOB.h"
|
2016-05-12 21:37:46 +02:00
|
|
|
#include "I2CP.h"
|
2014-10-24 21:22:36 +02:00
|
|
|
#include "AddressBook.h"
|
2021-09-04 20:01:57 +02:00
|
|
|
#include "I18N_langs.h"
|
2014-10-16 02:52:17 +02:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace client
|
|
|
|
{
|
2015-03-14 00:51:31 +01: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 22:00:09 +02:00
|
|
|
const char I2P_TUNNELS_SECTION_TYPE_HTTP[] = "http";
|
2016-02-22 20:33:21 +01:00
|
|
|
const char I2P_TUNNELS_SECTION_TYPE_IRC[] = "irc";
|
2016-09-03 17:46:47 +02:00
|
|
|
const char I2P_TUNNELS_SECTION_TYPE_UDPCLIENT[] = "udpclient";
|
|
|
|
const char I2P_TUNNELS_SECTION_TYPE_UDPSERVER[] = "udpserver";
|
2017-01-07 14:32:50 +01:00
|
|
|
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-14 00:51:31 +01:00
|
|
|
const char I2P_CLIENT_TUNNEL_PORT[] = "port";
|
2015-11-30 15:44:32 +01:00
|
|
|
const char I2P_CLIENT_TUNNEL_ADDRESS[] = "address";
|
2015-03-14 00:51:31 +01:00
|
|
|
const char I2P_CLIENT_TUNNEL_DESTINATION[] = "destination";
|
|
|
|
const char I2P_CLIENT_TUNNEL_KEYS[] = "keys";
|
2020-05-18 18:01:13 +02:00
|
|
|
const char I2P_CLIENT_TUNNEL_GZIP[] = "gzip";
|
2016-01-04 03:43:43 +01:00
|
|
|
const char I2P_CLIENT_TUNNEL_SIGNATURE_TYPE[] = "signaturetype";
|
2017-11-08 21:59:41 +01:00
|
|
|
const char I2P_CLIENT_TUNNEL_CRYPTO_TYPE[] = "cryptotype";
|
2017-04-08 18:51:35 +02:00
|
|
|
const char I2P_CLIENT_TUNNEL_DESTINATION_PORT[] = "destinationport";
|
2017-04-08 21:14:47 +02:00
|
|
|
const char I2P_CLIENT_TUNNEL_MATCH_TUNNELS[] = "matchtunnels";
|
2020-02-27 12:58:06 +01:00
|
|
|
const char I2P_CLIENT_TUNNEL_CONNECT_TIMEOUT[] = "connecttimeout";
|
2021-09-26 22:25:12 +02:00
|
|
|
const char I2P_CLIENT_TUNNEL_KEEP_ALIVE_INTERVAL[] = "keepaliveinterval";
|
2017-04-08 18:51:35 +02:00
|
|
|
const char I2P_SERVER_TUNNEL_HOST[] = "host";
|
|
|
|
const char I2P_SERVER_TUNNEL_HOST_OVERRIDE[] = "hostoverride";
|
2015-03-14 00:51:31 +01:00
|
|
|
const char I2P_SERVER_TUNNEL_PORT[] = "port";
|
|
|
|
const char I2P_SERVER_TUNNEL_KEYS[] = "keys";
|
2016-01-04 03:43:43 +01:00
|
|
|
const char I2P_SERVER_TUNNEL_SIGNATURE_TYPE[] = "signaturetype";
|
2015-03-15 16:28:30 +01:00
|
|
|
const char I2P_SERVER_TUNNEL_INPORT[] = "inport";
|
2017-04-08 18:51:35 +02:00
|
|
|
const char I2P_SERVER_TUNNEL_ACCESS_LIST[] = "accesslist";
|
2020-07-07 01:01:56 +02:00
|
|
|
const char I2P_SERVER_TUNNEL_WHITE_LIST[] = "whitelist";
|
2016-03-04 07:37:38 +01:00
|
|
|
const char I2P_SERVER_TUNNEL_GZIP[] = "gzip";
|
|
|
|
const char I2P_SERVER_TUNNEL_WEBIRC_PASSWORD[] = "webircpassword";
|
2016-09-03 15:38:53 +02:00
|
|
|
const char I2P_SERVER_TUNNEL_ADDRESS[] = "address";
|
2017-01-12 22:17:11 +01:00
|
|
|
const char I2P_SERVER_TUNNEL_ENABLE_UNIQUE_LOCAL[] = "enableuniquelocal";
|
2022-10-09 03:41:28 +02:00
|
|
|
const char I2P_SERVER_TUNNEL_SSL[] = "ssl";
|
2017-04-08 18:51:35 +02:00
|
|
|
|
2014-10-16 02:52:17 +02:00
|
|
|
class ClientContext
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-10-16 03:39:39 +02:00
|
|
|
ClientContext ();
|
|
|
|
~ClientContext ();
|
2014-10-16 02:52:17 +02:00
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
2016-05-12 17:38:18 +02:00
|
|
|
void ReloadConfig ();
|
|
|
|
|
2015-02-24 21:40:50 +01:00
|
|
|
std::shared_ptr<ClientDestination> GetSharedLocalDestination () const { return m_SharedLocalDestination; };
|
2017-11-14 21:05:07 +01:00
|
|
|
std::shared_ptr<ClientDestination> CreateNewLocalDestination (bool isPublic = false, // transient
|
2018-12-05 20:58:50 +01:00
|
|
|
i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
|
2017-11-14 21:05:07 +01:00
|
|
|
i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL,
|
2017-11-14 19:31:13 +01:00
|
|
|
const std::map<std::string, std::string> * params = nullptr); // used by SAM only
|
2024-11-25 16:08:27 +01:00
|
|
|
std::shared_ptr<ClientDestination> CreateNewLocalDestination (boost::asio::io_context& service,
|
2020-02-04 21:31:04 +01:00
|
|
|
bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
|
|
|
|
i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL,
|
|
|
|
const std::map<std::string, std::string> * params = nullptr); // same as previous but on external io_service
|
2017-04-08 18:51:35 +02:00
|
|
|
std::shared_ptr<ClientDestination> CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true,
|
2017-11-14 19:31:13 +01:00
|
|
|
const std::map<std::string, std::string> * params = nullptr);
|
2024-11-25 16:08:27 +01:00
|
|
|
std::shared_ptr<ClientDestination> CreateNewLocalDestination (boost::asio::io_context& service,
|
2020-03-01 11:25:50 +01:00
|
|
|
const i2p::data::PrivateKeys& keys, bool isPublic = true,
|
2020-02-04 21:31:04 +01:00
|
|
|
const std::map<std::string, std::string> * params = nullptr); // same as previous but on external io_service
|
2020-03-01 11:25:50 +01: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 21:40:50 +01:00
|
|
|
void DeleteLocalDestination (std::shared_ptr<ClientDestination> destination);
|
2017-04-08 18:51:35 +02:00
|
|
|
std::shared_ptr<ClientDestination> FindLocalDestination (const i2p::data::IdentHash& destination) const;
|
2018-01-06 04:48:51 +01:00
|
|
|
bool LoadPrivateKeys (i2p::data::PrivateKeys& keys, const std::string& filename,
|
2018-12-05 20:58:50 +01:00
|
|
|
i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
|
2017-11-08 21:59:41 +01:00
|
|
|
i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL);
|
2014-10-16 02:52:17 +02:00
|
|
|
|
2014-10-24 21:22:36 +02:00
|
|
|
AddressBook& GetAddressBook () { return m_AddressBook; };
|
2017-11-14 19:31:13 +01:00
|
|
|
const BOBCommandChannel * GetBOBCommandChannel () const { return m_BOBCommandChannel; };
|
2015-02-21 04:47:36 +01:00
|
|
|
const SAMBridge * GetSAMBridge () const { return m_SamBridge; };
|
2016-11-17 19:10:29 +01:00
|
|
|
const I2CPServer * GetI2CPServer () const { return m_I2CPServer; };
|
2016-09-03 19:58:34 +02:00
|
|
|
|
2016-09-03 23:53:46 +02:00
|
|
|
std::vector<std::shared_ptr<DatagramSessionInfo> > GetForwardInfosFor(const i2p::data::IdentHash & destination);
|
2016-10-20 15:12:15 +02:00
|
|
|
|
2021-09-04 20:01:57 +02:00
|
|
|
// i18n
|
|
|
|
std::shared_ptr<const i2p::i18n::Locale> GetLanguage () { return m_Language; };
|
|
|
|
void SetLanguage (const std::shared_ptr<const i2p::i18n::Locale> language) { m_Language = language; };
|
2021-11-27 21:30:35 +01:00
|
|
|
|
2015-02-12 22:11:56 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
void ReadTunnels ();
|
2018-10-19 21:23:46 +02:00
|
|
|
void ReadTunnels (const std::string& tunConf, int& numClientTunnels, int& numServerTunnels);
|
2018-01-23 21:13:43 +01:00
|
|
|
void ReadHttpProxy ();
|
|
|
|
void ReadSocksProxy ();
|
2016-01-15 18:24:40 +01:00
|
|
|
template<typename Section, typename Type>
|
|
|
|
std::string GetI2CPOption (const Section& section, const std::string& name, const Type& value) const;
|
|
|
|
template<typename Section>
|
2019-01-15 21:43:21 +01:00
|
|
|
std::string GetI2CPStringOption (const Section& section, const std::string& name, const std::string& value) const; // GetI2CPOption with string default value
|
|
|
|
template<typename Section>
|
2019-07-13 02:58:17 +02:00
|
|
|
void ReadI2CPOptionsGroup (const Section& section, const std::string& group, std::map<std::string, std::string>& options) const;
|
|
|
|
template<typename Section>
|
2020-10-05 01:52:12 +02:00
|
|
|
void ReadI2CPOptions (const Section& section, bool isServer, std::map<std::string, std::string>& options) const; // for tunnels
|
2017-11-24 21:37:17 +01:00
|
|
|
void ReadI2CPOptionsFromConfig (const std::string& prefix, std::map<std::string, std::string>& options) const; // for HTTP and SOCKS proxy
|
2016-01-15 18:24:40 +01:00
|
|
|
|
2016-09-03 17:46:47 +02:00
|
|
|
void CleanupUDP(const boost::system::error_code & ecode);
|
|
|
|
void ScheduleCleanupUDP();
|
2017-04-08 18:51:35 +02:00
|
|
|
|
2021-09-03 22:25:47 +02:00
|
|
|
void VisitTunnels (bool clean);
|
2017-07-28 21:12:15 +02:00
|
|
|
|
2020-03-01 11:25:50 +01:00
|
|
|
void CreateNewSharedLocalDestination ();
|
2020-02-04 21:31:04 +01:00
|
|
|
void AddLocalDestination (std::shared_ptr<ClientDestination> localDestination);
|
2018-01-23 20:40:05 +01:00
|
|
|
|
2016-09-03 17:46:47 +02:00
|
|
|
private:
|
2014-10-16 02:52:17 +02:00
|
|
|
|
|
|
|
std::mutex m_DestinationsMutex;
|
2015-02-24 21:40:50 +01:00
|
|
|
std::map<i2p::data::IdentHash, std::shared_ptr<ClientDestination> > m_Destinations;
|
2017-04-08 18:51:35 +02:00
|
|
|
std::shared_ptr<ClientDestination> m_SharedLocalDestination;
|
2014-10-16 02:52:17 +02:00
|
|
|
|
2014-10-24 21:22:36 +02:00
|
|
|
AddressBook m_AddressBook;
|
|
|
|
|
2024-02-21 00:51:37 +01:00
|
|
|
I2PService * m_HttpProxy, * m_SocksProxy;
|
2022-05-09 09:59:25 +02: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 21:02:17 +02:00
|
|
|
|
2017-04-08 18:51:35 +02:00
|
|
|
std::mutex m_ForwardsMutex;
|
2018-04-25 22:18:07 +02: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
|
2017-04-08 18:51:35 +02:00
|
|
|
|
2016-09-03 17:46:47 +02:00
|
|
|
SAMBridge * m_SamBridge;
|
2014-12-02 16:34:02 +01:00
|
|
|
BOBCommandChannel * m_BOBCommandChannel;
|
2016-05-12 21:37:46 +02:00
|
|
|
I2CPServer * m_I2CPServer;
|
2014-10-16 03:39:39 +02:00
|
|
|
|
2016-09-16 16:31:11 +02:00
|
|
|
std::unique_ptr<boost::asio::deadline_timer> m_CleanupUDPTimer;
|
2017-04-08 18:51:35 +02:00
|
|
|
|
2021-09-04 20:01:57 +02:00
|
|
|
// i18n
|
|
|
|
std::shared_ptr<const i2p::i18n::Locale> m_Language;
|
2021-11-27 21:30:35 +01:00
|
|
|
|
2014-10-16 02:52:17 +02:00
|
|
|
public:
|
2020-03-01 11:25:50 +01:00
|
|
|
|
2014-10-16 02:52:17 +02:00
|
|
|
// for HTTP
|
|
|
|
const decltype(m_Destinations)& GetDestinations () const { return m_Destinations; };
|
2016-01-14 02:21:53 +01:00
|
|
|
const decltype(m_ClientTunnels)& GetClientTunnels () const { return m_ClientTunnels; };
|
|
|
|
const decltype(m_ServerTunnels)& GetServerTunnels () const { return m_ServerTunnels; };
|
2016-09-03 17:46:47 +02:00
|
|
|
const decltype(m_ClientForwards)& GetClientForwards () const { return m_ClientForwards; }
|
|
|
|
const decltype(m_ServerForwards)& GetServerForwards () const { return m_ServerForwards; }
|
2024-02-21 00:51:37 +01:00
|
|
|
const I2PService * GetHttpProxy () const { return m_HttpProxy; }
|
|
|
|
const I2PService * GetSocksProxy () const { return m_SocksProxy; }
|
2014-10-16 02:52:17 +02:00
|
|
|
};
|
2017-04-08 18:51:35 +02:00
|
|
|
|
|
|
|
extern ClientContext context;
|
|
|
|
}
|
|
|
|
}
|
2014-10-16 02:52:17 +02:00
|
|
|
|
|
|
|
#endif
|