ClientContext.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #ifndef CLIENT_CONTEXT_H__
  2. #define CLIENT_CONTEXT_H__
  3. #include <map>
  4. #include <mutex>
  5. #include <memory>
  6. #include <boost/asio.hpp>
  7. #include "Destination.h"
  8. #include "I2PService.h"
  9. #include "HTTPProxy.h"
  10. #include "SOCKS.h"
  11. #include "I2PTunnel.h"
  12. #include "SAM.h"
  13. #include "BOB.h"
  14. #include "I2CP.h"
  15. #include "AddressBook.h"
  16. namespace i2p
  17. {
  18. namespace client
  19. {
  20. const char I2P_TUNNELS_SECTION_TYPE[] = "type";
  21. const char I2P_TUNNELS_SECTION_TYPE_CLIENT[] = "client";
  22. const char I2P_TUNNELS_SECTION_TYPE_SERVER[] = "server";
  23. const char I2P_TUNNELS_SECTION_TYPE_HTTP[] = "http";
  24. const char I2P_TUNNELS_SECTION_TYPE_IRC[] = "irc";
  25. const char I2P_TUNNELS_SECTION_TYPE_UDPCLIENT[] = "udpclient";
  26. const char I2P_TUNNELS_SECTION_TYPE_UDPSERVER[] = "udpserver";
  27. const char I2P_TUNNELS_SECTION_TYPE_SOCKS[] = "socks";
  28. const char I2P_TUNNELS_SECTION_TYPE_WEBSOCKS[] = "websocks";
  29. const char I2P_TUNNELS_SECTION_TYPE_HTTPPROXY[] = "httpproxy";
  30. const char I2P_CLIENT_TUNNEL_PORT[] = "port";
  31. const char I2P_CLIENT_TUNNEL_ADDRESS[] = "address";
  32. const char I2P_CLIENT_TUNNEL_DESTINATION[] = "destination";
  33. const char I2P_CLIENT_TUNNEL_KEYS[] = "keys";
  34. const char I2P_CLIENT_TUNNEL_SIGNATURE_TYPE[] = "signaturetype";
  35. const char I2P_CLIENT_TUNNEL_CRYPTO_TYPE[] = "cryptotype";
  36. const char I2P_CLIENT_TUNNEL_DESTINATION_PORT[] = "destinationport";
  37. const char I2P_CLIENT_TUNNEL_MATCH_TUNNELS[] = "matchtunnels";
  38. const char I2P_CLIENT_TUNNEL_CONNECT_TIMEOUT[] = "connecttimeout";
  39. const char I2P_SERVER_TUNNEL_HOST[] = "host";
  40. const char I2P_SERVER_TUNNEL_HOST_OVERRIDE[] = "hostoverride";
  41. const char I2P_SERVER_TUNNEL_PORT[] = "port";
  42. const char I2P_SERVER_TUNNEL_KEYS[] = "keys";
  43. const char I2P_SERVER_TUNNEL_SIGNATURE_TYPE[] = "signaturetype";
  44. const char I2P_SERVER_TUNNEL_INPORT[] = "inport";
  45. const char I2P_SERVER_TUNNEL_ACCESS_LIST[] = "accesslist";
  46. const char I2P_SERVER_TUNNEL_GZIP[] = "gzip";
  47. const char I2P_SERVER_TUNNEL_WEBIRC_PASSWORD[] = "webircpassword";
  48. const char I2P_SERVER_TUNNEL_ADDRESS[] = "address";
  49. const char I2P_SERVER_TUNNEL_ENABLE_UNIQUE_LOCAL[] = "enableuniquelocal";
  50. class ClientContext
  51. {
  52. public:
  53. ClientContext ();
  54. ~ClientContext ();
  55. void Start ();
  56. void Stop ();
  57. void ReloadConfig ();
  58. std::shared_ptr<ClientDestination> GetSharedLocalDestination () const { return m_SharedLocalDestination; };
  59. std::shared_ptr<ClientDestination> CreateNewLocalDestination (bool isPublic = false, // transient
  60. i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
  61. i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL,
  62. const std::map<std::string, std::string> * params = nullptr); // used by SAM only
  63. std::shared_ptr<ClientDestination> CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true,
  64. const std::map<std::string, std::string> * params = nullptr);
  65. std::shared_ptr<ClientDestination> CreateNewMatchedTunnelDestination(const i2p::data::PrivateKeys &keys, const std::string & name, const std::map<std::string, std::string> * params = nullptr);
  66. void DeleteLocalDestination (std::shared_ptr<ClientDestination> destination);
  67. std::shared_ptr<ClientDestination> FindLocalDestination (const i2p::data::IdentHash& destination) const;
  68. bool LoadPrivateKeys (i2p::data::PrivateKeys& keys, const std::string& filename,
  69. i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
  70. i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL);
  71. AddressBook& GetAddressBook () { return m_AddressBook; };
  72. const BOBCommandChannel * GetBOBCommandChannel () const { return m_BOBCommandChannel; };
  73. const SAMBridge * GetSAMBridge () const { return m_SamBridge; };
  74. const I2CPServer * GetI2CPServer () const { return m_I2CPServer; };
  75. std::vector<std::shared_ptr<DatagramSessionInfo> > GetForwardInfosFor(const i2p::data::IdentHash & destination);
  76. private:
  77. void ReadTunnels ();
  78. void ReadTunnels (const std::string& tunConf, int& numClientTunnels, int& numServerTunnels);
  79. void ReadHttpProxy ();
  80. void ReadSocksProxy ();
  81. template<typename Section, typename Type>
  82. std::string GetI2CPOption (const Section& section, const std::string& name, const Type& value) const;
  83. template<typename Section>
  84. std::string GetI2CPStringOption (const Section& section, const std::string& name, const std::string& value) const; // GetI2CPOption with string default value
  85. template<typename Section>
  86. void ReadI2CPOptionsGroup (const Section& section, const std::string& group, std::map<std::string, std::string>& options) const;
  87. template<typename Section>
  88. void ReadI2CPOptions (const Section& section, std::map<std::string, std::string>& options) const; // for tunnels
  89. void ReadI2CPOptionsFromConfig (const std::string& prefix, std::map<std::string, std::string>& options) const; // for HTTP and SOCKS proxy
  90. void CleanupUDP(const boost::system::error_code & ecode);
  91. void ScheduleCleanupUDP();
  92. template<typename Visitor>
  93. void VisitTunnels (Visitor v); // Visitor: (I2PService *) -> bool, true means retain
  94. void CreateNewSharedLocalDestination ();
  95. private:
  96. std::mutex m_DestinationsMutex;
  97. std::map<i2p::data::IdentHash, std::shared_ptr<ClientDestination> > m_Destinations;
  98. std::shared_ptr<ClientDestination> m_SharedLocalDestination;
  99. AddressBook m_AddressBook;
  100. i2p::proxy::HTTPProxy * m_HttpProxy;
  101. i2p::proxy::SOCKSProxy * m_SocksProxy;
  102. std::map<boost::asio::ip::tcp::endpoint, std::shared_ptr<I2PService> > m_ClientTunnels; // local endpoint->tunnel
  103. std::map<std::pair<i2p::data::IdentHash, int>, std::shared_ptr<I2PServerTunnel> > m_ServerTunnels; // <destination,port>->tunnel
  104. std::mutex m_ForwardsMutex;
  105. std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<I2PUDPClientTunnel> > m_ClientForwards; // local endpoint -> udp tunnel
  106. std::map<std::pair<i2p::data::IdentHash, int>, std::shared_ptr<I2PUDPServerTunnel> > m_ServerForwards; // <destination,port> -> udp tunnel
  107. SAMBridge * m_SamBridge;
  108. BOBCommandChannel * m_BOBCommandChannel;
  109. I2CPServer * m_I2CPServer;
  110. std::unique_ptr<boost::asio::deadline_timer> m_CleanupUDPTimer;
  111. public:
  112. // for HTTP
  113. const decltype(m_Destinations)& GetDestinations () const { return m_Destinations; };
  114. const decltype(m_ClientTunnels)& GetClientTunnels () const { return m_ClientTunnels; };
  115. const decltype(m_ServerTunnels)& GetServerTunnels () const { return m_ServerTunnels; };
  116. const decltype(m_ClientForwards)& GetClientForwards () const { return m_ClientForwards; }
  117. const decltype(m_ServerForwards)& GetServerForwards () const { return m_ServerForwards; }
  118. const i2p::proxy::HTTPProxy * GetHttpProxy () const { return m_HttpProxy; }
  119. const i2p::proxy::SOCKSProxy * GetSocksProxy () const { return m_SocksProxy; }
  120. };
  121. extern ClientContext context;
  122. }
  123. }
  124. #endif