i2pd/libi2pd_client/ClientContext.cpp

820 lines
28 KiB
C++
Raw Normal View History

#include <fstream>
2015-02-12 21:11:56 +00:00
#include <iostream>
2015-03-13 20:05:39 +00:00
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
2016-01-20 00:00:00 +00:00
#include "Config.h"
#include "FS.h"
#include "Log.h"
#include "Identity.h"
2016-06-14 18:37:22 +00:00
#include "util.h"
2014-10-16 00:52:17 +00:00
#include "ClientContext.h"
#include "SOCKS.h"
#include "WebSocks.h"
#include "MatchedDestination.h"
2014-10-16 00:52:17 +00:00
namespace i2p
{
namespace client
{
ClientContext context;
ClientContext::ClientContext (): m_SharedLocalDestination (nullptr),
m_HttpProxy (nullptr), m_SocksProxy (nullptr), m_SamBridge (nullptr),
m_BOBCommandChannel (nullptr), m_I2CPServer (nullptr)
{
}
ClientContext::~ClientContext ()
{
delete m_HttpProxy;
delete m_SocksProxy;
delete m_SamBridge;
2014-12-02 15:34:02 +00:00
delete m_BOBCommandChannel;
2016-05-12 19:37:46 +00:00
delete m_I2CPServer;
}
2014-10-16 00:52:17 +00:00
void ClientContext::Start ()
{
// shared local destination
2014-10-16 00:52:17 +00:00
if (!m_SharedLocalDestination)
CreateNewSharedLocalDestination ();
// addressbook
m_AddressBook.Start ();
// HTTP proxy
ReadHttpProxy ();
// SOCKS proxy
ReadSocksProxy ();
2015-02-12 21:11:56 +00:00
2016-08-22 17:04:36 +00:00
// I2P tunnels
ReadTunnels ();
2015-02-12 21:11:56 +00:00
// SAM
2016-01-20 00:00:00 +00:00
bool sam; i2p::config::GetOption("sam.enabled", sam);
if (sam) {
std::string samAddr; i2p::config::GetOption("sam.address", samAddr);
uint16_t samPort; i2p::config::GetOption("sam.port", samPort);
LogPrint(eLogInfo, "Clients: starting SAM bridge at ", samAddr, ":", samPort);
try {
m_SamBridge = new SAMBridge (samAddr, samPort);
m_SamBridge->Start ();
} catch (std::exception& e) {
LogPrint(eLogError, "Clients: Exception in SAM bridge: ", e.what());
}
}
2015-02-12 21:11:56 +00:00
// BOB
2016-01-20 00:00:00 +00:00
bool bob; i2p::config::GetOption("bob.enabled", bob);
if (bob) {
std::string bobAddr; i2p::config::GetOption("bob.address", bobAddr);
uint16_t bobPort; i2p::config::GetOption("bob.port", bobPort);
LogPrint(eLogInfo, "Clients: starting BOB command channel at ", bobAddr, ":", bobPort);
try {
m_BOBCommandChannel = new BOBCommandChannel (bobAddr, bobPort);
m_BOBCommandChannel->Start ();
} catch (std::exception& e) {
LogPrint(eLogError, "Clients: Exception in BOB bridge: ", e.what());
}
}
2016-03-26 14:31:47 +00:00
2016-05-31 15:54:45 +00:00
// I2CP
bool i2cp; i2p::config::GetOption("i2cp.enabled", i2cp);
if (i2cp)
2016-05-31 15:54:45 +00:00
{
std::string i2cpAddr; i2p::config::GetOption("i2cp.address", i2cpAddr);
uint16_t i2cpPort; i2p::config::GetOption("i2cp.port", i2cpPort);
LogPrint(eLogInfo, "Clients: starting I2CP at ", i2cpAddr, ":", i2cpPort);
try
2016-05-31 15:54:45 +00:00
{
m_I2CPServer = new I2CPServer (i2cpAddr, i2cpPort);
2018-01-06 04:01:44 +00:00
m_I2CPServer->Start ();
}
catch (std::exception& e)
2016-05-31 15:54:45 +00:00
{
LogPrint(eLogError, "Clients: Exception in I2CP: ", e.what());
}
}
2016-05-31 15:54:45 +00:00
m_AddressBook.StartResolvers ();
// start UDP cleanup
if (!m_ServerForwards.empty ())
{
m_CleanupUDPTimer.reset (new boost::asio::deadline_timer(m_SharedLocalDestination->GetService ()));
ScheduleCleanupUDP();
}
2014-10-16 00:52:17 +00:00
}
2014-10-16 00:52:17 +00:00
void ClientContext::Stop ()
{
if (m_HttpProxy)
{
LogPrint(eLogInfo, "Clients: stopping HTTP Proxy");
m_HttpProxy->Stop();
delete m_HttpProxy;
m_HttpProxy = nullptr;
}
2015-12-18 12:44:03 +00:00
if (m_SocksProxy)
{
LogPrint(eLogInfo, "Clients: stopping SOCKS Proxy");
m_SocksProxy->Stop();
delete m_SocksProxy;
m_SocksProxy = nullptr;
}
2015-12-18 12:44:03 +00:00
2015-02-12 21:11:56 +00:00
for (auto& it: m_ClientTunnels)
{
2015-12-18 12:44:03 +00:00
LogPrint(eLogInfo, "Clients: stopping I2P client tunnel on port ", it.first);
it.second->Stop ();
}
m_ClientTunnels.clear ();
2015-12-18 12:44:03 +00:00
2015-02-13 20:56:57 +00:00
for (auto& it: m_ServerTunnels)
{
2015-12-18 12:44:03 +00:00
LogPrint(eLogInfo, "Clients: stopping I2P server tunnel");
2015-02-13 20:56:57 +00:00
it.second->Stop ();
}
m_ServerTunnels.clear ();
2015-12-18 12:44:03 +00:00
if (m_SamBridge)
{
2015-12-18 12:44:03 +00:00
LogPrint(eLogInfo, "Clients: stopping SAM bridge");
m_SamBridge->Stop ();
delete m_SamBridge;
m_SamBridge = nullptr;
}
2015-12-18 12:44:03 +00:00
2014-12-02 20:47:44 +00:00
if (m_BOBCommandChannel)
{
2015-12-18 12:44:03 +00:00
LogPrint(eLogInfo, "Clients: stopping BOB command channel");
2014-12-02 20:47:44 +00:00
m_BOBCommandChannel->Stop ();
delete m_BOBCommandChannel;
2014-12-02 20:47:44 +00:00
m_BOBCommandChannel = nullptr;
2015-12-18 12:44:03 +00:00
}
2016-05-31 15:54:45 +00:00
if (m_I2CPServer)
{
LogPrint(eLogInfo, "Clients: stopping I2CP");
m_I2CPServer->Stop ();
delete m_I2CPServer;
2016-05-31 15:54:45 +00:00
m_I2CPServer = nullptr;
}
2015-12-18 12:44:03 +00:00
LogPrint(eLogInfo, "Clients: stopping AddressBook");
2016-08-21 19:02:17 +00:00
m_AddressBook.Stop ();
2018-01-06 04:01:44 +00:00
{
std::lock_guard<std::mutex> lock(m_ForwardsMutex);
m_ServerForwards.clear();
m_ClientForwards.clear();
}
if (m_CleanupUDPTimer)
{
m_CleanupUDPTimer->cancel ();
m_CleanupUDPTimer = nullptr;
}
2016-08-05 18:23:54 +00:00
for (auto& it: m_Destinations)
2014-10-16 00:52:17 +00:00
it.second->Stop ();
m_Destinations.clear ();
2016-08-21 19:02:17 +00:00
m_SharedLocalDestination = nullptr;
}
void ClientContext::ReloadConfig ()
{
// TODO: handle config changes
/*std::string config; i2p::config::GetOption("conf", config);
i2p::config::ParseConfig(config);*/
// handle tunnels
// reset isUpdated for each tunnel
VisitTunnels ([](I2PService * s)->bool { s->isUpdated = false; return true; });
// reload tunnels
ReadTunnels();
// delete not updated tunnels (not in config anymore)
VisitTunnels ([](I2PService * s)->bool { return s->isUpdated; });
2017-07-07 19:12:34 +00:00
// change shared local destination
m_SharedLocalDestination->Release ();
CreateNewSharedLocalDestination ();
// recreate HTTP proxy
if (m_HttpProxy)
{
m_HttpProxy->Stop ();
m_HttpProxy = nullptr;
}
ReadHttpProxy ();
// recreate SOCKS proxy
if (m_SocksProxy)
{
m_SocksProxy->Stop ();
m_SocksProxy = nullptr;
}
ReadSocksProxy ();
2017-07-07 19:12:34 +00:00
// delete unused destinations
std::unique_lock<std::mutex> l(m_DestinationsMutex);
for (auto it = m_Destinations.begin (); it != m_Destinations.end ();)
{
auto dest = it->second;
2017-07-07 19:12:34 +00:00
if (dest->GetRefCounter () > 0) ++it; // skip
else
{
dest->Stop ();
it = m_Destinations.erase (it);
}
}
}
2018-01-06 03:48:51 +00:00
bool ClientContext::LoadPrivateKeys (i2p::data::PrivateKeys& keys, const std::string& filename,
i2p::data::SigningKeyType sigType, i2p::data::CryptoKeyType cryptoType)
2014-10-16 00:52:17 +00:00
{
2017-11-22 19:49:45 +00:00
if (filename == "transient")
{
keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType, cryptoType);
LogPrint (eLogInfo, "Clients: New transient keys address ", m_AddressBook.ToAddress(keys.GetPublic ()->GetIdentHash ()), " created");
return true;
}
2016-07-25 15:13:54 +00:00
bool success = true;
std::string fullPath = i2p::fs::DataDirPath (filename);
std::ifstream s(fullPath, std::ifstream::binary);
if (s.is_open ())
{
s.seekg (0, std::ios::end);
size_t len = s.tellg();
s.seekg (0, std::ios::beg);
uint8_t * buf = new uint8_t[len];
s.read ((char *)buf, len);
2016-07-25 15:13:54 +00:00
if(!keys.FromBuffer (buf, len))
{
LogPrint (eLogError, "Clients: failed to load keyfile ", filename);
success = false;
}
else
LogPrint (eLogInfo, "Clients: Local address ", m_AddressBook.ToAddress(keys.GetPublic ()->GetIdentHash ()), " loaded");
delete[] buf;
}
else
{
2017-11-09 20:49:27 +00:00
LogPrint (eLogError, "Clients: can't open file ", fullPath, " Creating new one with signature type ", sigType, " crypto type ", cryptoType);
keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType, cryptoType);
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);
size_t len = keys.GetFullLen ();
uint8_t * buf = new uint8_t[len];
len = keys.ToBuffer (buf, len);
f.write ((char *)buf, len);
delete[] buf;
2015-12-18 12:44:03 +00:00
LogPrint (eLogInfo, "Clients: New private keys file ", fullPath, " for ", m_AddressBook.ToAddress(keys.GetPublic ()->GetIdentHash ()), " created");
2016-07-25 15:13:54 +00:00
}
return success;
2014-10-16 00:52:17 +00:00
}
2016-09-03 21:53:46 +00:00
std::vector<std::shared_ptr<DatagramSessionInfo> > ClientContext::GetForwardInfosFor(const i2p::data::IdentHash & destination)
2016-09-03 17:58:34 +00:00
{
2016-09-03 21:53:46 +00:00
std::vector<std::shared_ptr<DatagramSessionInfo> > infos;
2016-09-03 17:58:34 +00:00
std::lock_guard<std::mutex> lock(m_ForwardsMutex);
2016-09-03 21:53:46 +00:00
for(const auto & c : m_ClientForwards)
2016-09-03 17:58:34 +00:00
{
if (c.second->IsLocalDestination(destination))
2016-09-03 21:53:46 +00:00
{
for (auto & i : c.second->GetSessions()) infos.push_back(i);
break;
}
2016-09-03 17:58:34 +00:00
}
2016-09-03 21:53:46 +00:00
for(const auto & s : m_ServerForwards)
2016-09-03 17:58:34 +00:00
{
if(std::get<0>(s.first) == destination)
2016-09-03 21:53:46 +00:00
{
for( auto & i : s.second->GetSessions()) infos.push_back(i);
break;
}
2016-09-03 17:58:34 +00:00
}
2016-09-03 21:53:46 +00:00
return infos;
2016-09-03 17:58:34 +00:00
}
2017-11-14 20:05:07 +00:00
std::shared_ptr<ClientDestination> ClientContext::CreateNewLocalDestination (bool isPublic,
2018-01-06 04:01:44 +00:00
i2p::data::SigningKeyType sigType, i2p::data::CryptoKeyType cryptoType,
2017-11-14 20:05:07 +00:00
const std::map<std::string, std::string> * params)
2014-10-16 00:52:17 +00:00
{
2017-11-14 20:05:07 +00:00
i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType, cryptoType);
2015-02-24 20:40:50 +00:00
auto localDestination = std::make_shared<ClientDestination> (keys, isPublic, params);
2014-10-16 00:52:17 +00:00
std::unique_lock<std::mutex> l(m_DestinationsMutex);
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
localDestination->Start ();
return localDestination;
}
std::shared_ptr<ClientDestination> ClientContext::CreateNewMatchedTunnelDestination(const i2p::data::PrivateKeys &keys, const std::string & name, const std::map<std::string, std::string> * params)
{
MatchedTunnelDestination * cl = new MatchedTunnelDestination(keys, name, params);
auto localDestination = std::shared_ptr<ClientDestination>(cl);
std::unique_lock<std::mutex> l(m_DestinationsMutex);
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
localDestination->Start ();
return localDestination;
}
2015-02-24 20:40:50 +00:00
void ClientContext::DeleteLocalDestination (std::shared_ptr<ClientDestination> destination)
2014-10-16 00:52:17 +00:00
{
if (!destination) return;
auto it = m_Destinations.find (destination->GetIdentHash ());
if (it != m_Destinations.end ())
{
auto d = it->second;
{
std::unique_lock<std::mutex> l(m_DestinationsMutex);
m_Destinations.erase (it);
}
2014-10-16 00:52:17 +00:00
d->Stop ();
}
}
2015-02-24 20:40:50 +00:00
std::shared_ptr<ClientDestination> ClientContext::CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
const std::map<std::string, std::string> * params)
2014-10-16 00:52:17 +00:00
{
2015-11-03 14:15:49 +00:00
auto it = m_Destinations.find (keys.GetPublic ()->GetIdentHash ());
2014-10-16 00:52:17 +00:00
if (it != m_Destinations.end ())
{
2015-12-18 12:44:03 +00:00
LogPrint (eLogWarning, "Clients: Local destination ", m_AddressBook.ToAddress(keys.GetPublic ()->GetIdentHash ()), " exists");
2014-10-16 00:52:17 +00:00
if (!it->second->IsRunning ())
it->second->Start ();
return it->second;
}
2015-02-24 20:40:50 +00:00
auto localDestination = std::make_shared<ClientDestination> (keys, isPublic, params);
2014-10-16 00:52:17 +00:00
std::unique_lock<std::mutex> l(m_DestinationsMutex);
2015-11-03 14:15:49 +00:00
m_Destinations[keys.GetPublic ()->GetIdentHash ()] = localDestination;
2014-10-16 00:52:17 +00:00
localDestination->Start ();
return localDestination;
}
void ClientContext::CreateNewSharedLocalDestination ()
{
2018-12-05 19:58:50 +00:00
m_SharedLocalDestination = CreateNewLocalDestination (); // non-public, EDDSA
m_SharedLocalDestination->Acquire ();
}
2015-02-24 20:40:50 +00:00
std::shared_ptr<ClientDestination> ClientContext::FindLocalDestination (const i2p::data::IdentHash& destination) const
2014-10-16 00:52:17 +00:00
{
auto it = m_Destinations.find (destination);
if (it != m_Destinations.end ())
return it->second;
return nullptr;
}
2015-02-12 21:11:56 +00:00
2016-01-15 17:24:40 +00:00
template<typename Section, typename Type>
std::string ClientContext::GetI2CPOption (const Section& section, const std::string& name, const Type& value) const
{
return section.second.get (boost::property_tree::ptree::path_type (name, '/'), std::to_string (value));
}
2016-01-15 17:24:40 +00:00
template<typename Section>
void ClientContext::ReadI2CPOptions (const Section& section, std::map<std::string, std::string>& options) const
{
options[I2CP_PARAM_INBOUND_TUNNEL_LENGTH] = GetI2CPOption (section, I2CP_PARAM_INBOUND_TUNNEL_LENGTH, DEFAULT_INBOUND_TUNNEL_LENGTH);
options[I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH] = GetI2CPOption (section, I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH, DEFAULT_OUTBOUND_TUNNEL_LENGTH);
options[I2CP_PARAM_INBOUND_TUNNELS_QUANTITY] = GetI2CPOption (section, I2CP_PARAM_INBOUND_TUNNELS_QUANTITY, DEFAULT_INBOUND_TUNNELS_QUANTITY);
options[I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY] = GetI2CPOption (section, I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY, DEFAULT_OUTBOUND_TUNNELS_QUANTITY);
options[I2CP_PARAM_TAGS_TO_SEND] = GetI2CPOption (section, I2CP_PARAM_TAGS_TO_SEND, DEFAULT_TAGS_TO_SEND);
2016-11-15 15:20:09 +00:00
options[I2CP_PARAM_MIN_TUNNEL_LATENCY] = GetI2CPOption(section, I2CP_PARAM_MIN_TUNNEL_LATENCY, DEFAULT_MIN_TUNNEL_LATENCY);
options[I2CP_PARAM_MAX_TUNNEL_LATENCY] = GetI2CPOption(section, I2CP_PARAM_MAX_TUNNEL_LATENCY, DEFAULT_MAX_TUNNEL_LATENCY);
options[I2CP_PARAM_STREAMING_INITIAL_ACK_DELAY] = GetI2CPOption(section, I2CP_PARAM_STREAMING_INITIAL_ACK_DELAY, DEFAULT_INITIAL_ACK_DELAY);
}
2016-01-15 17:24:40 +00:00
2016-11-03 19:28:33 +00:00
void ClientContext::ReadI2CPOptionsFromConfig (const std::string& prefix, std::map<std::string, std::string>& options) const
{
std::string value;
if (i2p::config::GetOption(prefix + I2CP_PARAM_INBOUND_TUNNEL_LENGTH, value))
2016-11-03 19:28:33 +00:00
options[I2CP_PARAM_INBOUND_TUNNEL_LENGTH] = value;
if (i2p::config::GetOption(prefix + I2CP_PARAM_INBOUND_TUNNELS_QUANTITY, value))
options[I2CP_PARAM_INBOUND_TUNNELS_QUANTITY] = value;
if (i2p::config::GetOption(prefix + I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH, value))
2016-11-03 19:28:33 +00:00
options[I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH] = value;
if (i2p::config::GetOption(prefix + I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY, value))
2016-11-15 15:20:09 +00:00
options[I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY] = value;
if (i2p::config::GetOption(prefix + I2CP_PARAM_MIN_TUNNEL_LATENCY, value))
options[I2CP_PARAM_MIN_TUNNEL_LATENCY] = value;
if (i2p::config::GetOption(prefix + I2CP_PARAM_MAX_TUNNEL_LATENCY, value))
2018-01-06 03:48:51 +00:00
options[I2CP_PARAM_MAX_TUNNEL_LATENCY] = value;
}
2016-11-03 19:28:33 +00:00
2015-03-13 23:51:31 +00:00
void ClientContext::ReadTunnels ()
2015-03-13 20:05:39 +00:00
{
int numClientTunnels = 0, numServerTunnels = 0;
std::string tunConf; i2p::config::GetOption("tunconf", tunConf);
if (tunConf.empty ())
{
// TODO: cleanup this in 2.8.0
tunConf = i2p::fs::DataDirPath ("tunnels.cfg");
if (i2p::fs::Exists(tunConf))
LogPrint(eLogWarning, "Clients: please rename tunnels.cfg -> tunnels.conf here: ", tunConf);
else
tunConf = i2p::fs::DataDirPath ("tunnels.conf");
}
LogPrint(eLogDebug, "Clients: tunnels config file: ", tunConf);
ReadTunnels (tunConf, numClientTunnels, numServerTunnels);
std::string tunDir; i2p::config::GetOption("tunnelsdir", tunDir);
if (tunDir.empty ())
tunDir = i2p::fs::DataDirPath ("tunnels.d");
if (i2p::fs::Exists (tunDir))
{
std::vector<std::string> files;
if (i2p::fs::ReadDir (tunDir, files))
{
for (auto& it: files)
{
LogPrint(eLogDebug, "Clients: tunnels extra config file: ", it);
ReadTunnels (it, numClientTunnels, numServerTunnels);
}
}
}
LogPrint (eLogInfo, "Clients: ", numClientTunnels, " I2P client tunnels created");
LogPrint (eLogInfo, "Clients: ", numServerTunnels, " I2P server tunnels created");
}
void ClientContext::ReadTunnels (const std::string& tunConf, int& numClientTunnels, int& numServerTunnels)
{
boost::property_tree::ptree pt;
try
{
boost::property_tree::read_ini (tunConf, pt);
}
catch (std::exception& ex)
{
LogPrint (eLogWarning, "Clients: Can't read ", tunConf, ": ", ex.what ());
2015-03-13 20:05:39 +00:00
return;
}
2015-03-13 20:05:39 +00:00
for (auto& section: pt)
{
std::string name = section.first;
2015-03-13 23:51:31 +00:00
try
2015-03-13 20:05:39 +00:00
{
2015-03-13 23:51:31 +00:00
std::string type = section.second.get<std::string> (I2P_TUNNELS_SECTION_TYPE);
if (type == I2P_TUNNELS_SECTION_TYPE_CLIENT
|| type == I2P_TUNNELS_SECTION_TYPE_SOCKS
|| type == I2P_TUNNELS_SECTION_TYPE_WEBSOCKS
|| type == I2P_TUNNELS_SECTION_TYPE_HTTPPROXY
|| type == I2P_TUNNELS_SECTION_TYPE_UDPCLIENT)
2015-03-13 20:05:39 +00:00
{
// mandatory params
2017-01-07 18:55:17 +00:00
std::string dest;
if (type == I2P_TUNNELS_SECTION_TYPE_CLIENT || type == I2P_TUNNELS_SECTION_TYPE_UDPCLIENT)
dest = section.second.get<std::string> (I2P_CLIENT_TUNNEL_DESTINATION);
2015-03-13 23:51:31 +00:00
int port = section.second.get<int> (I2P_CLIENT_TUNNEL_PORT);
2015-03-13 20:05:39 +00:00
// optional params
bool matchTunnels = section.second.get(I2P_CLIENT_TUNNEL_MATCH_TUNNELS, false);
std::string keys = section.second.get (I2P_CLIENT_TUNNEL_KEYS, "transient");
2015-11-30 14:44:32 +00:00
std::string address = section.second.get (I2P_CLIENT_TUNNEL_ADDRESS, "127.0.0.1");
int destinationPort = section.second.get (I2P_CLIENT_TUNNEL_DESTINATION_PORT, 0);
2018-12-05 19:58:50 +00:00
i2p::data::SigningKeyType sigType = section.second.get (I2P_CLIENT_TUNNEL_SIGNATURE_TYPE, i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519);
i2p::data::CryptoKeyType cryptoType = section.second.get (I2P_CLIENT_TUNNEL_CRYPTO_TYPE, i2p::data::CRYPTO_KEY_TYPE_ELGAMAL);
2016-01-14 20:57:55 +00:00
// I2CP
std::map<std::string, std::string> options;
ReadI2CPOptions (section, options);
2015-03-13 20:05:39 +00:00
std::shared_ptr<ClientDestination> localDestination = nullptr;
if (keys.length () > 0)
2016-01-15 19:46:29 +00:00
{
i2p::data::PrivateKeys k;
if(LoadPrivateKeys (k, keys, sigType, cryptoType))
2016-07-25 15:13:54 +00:00
{
localDestination = FindLocalDestination (k.GetPublic ()->GetIdentHash ());
if (!localDestination)
{
if(matchTunnels)
localDestination = CreateNewMatchedTunnelDestination(k, dest, &options);
else
localDestination = CreateNewLocalDestination (k, type == I2P_TUNNELS_SECTION_TYPE_UDPCLIENT, &options);
}
2016-07-25 15:13:54 +00:00
}
2016-01-15 19:46:29 +00:00
}
if (type == I2P_TUNNELS_SECTION_TYPE_UDPCLIENT) {
// udp client
// TODO: hostnames
boost::asio::ip::udp::endpoint end(boost::asio::ip::address::from_string(address), port);
if (!localDestination)
{
localDestination = m_SharedLocalDestination;
}
2018-04-25 20:18:07 +00:00
auto clientTunnel = std::make_shared<I2PUDPClientTunnel>(name, dest, end, localDestination, destinationPort);
if(m_ClientForwards.insert(std::make_pair(end, clientTunnel)).second)
{
clientTunnel->Start();
}
else
LogPrint(eLogError, "Clients: I2P Client forward for endpoint ", end, " already exists");
2016-08-21 19:33:19 +00:00
} else {
boost::asio::ip::tcp::endpoint clientEndpoint;
2018-04-25 20:18:07 +00:00
std::shared_ptr<I2PService> clientTunnel;
if (type == I2P_TUNNELS_SECTION_TYPE_SOCKS)
{
// socks proxy
2018-08-26 13:24:11 +00:00
std::string outproxy = section.second.get("outproxy", "");
auto tun = std::make_shared<i2p::proxy::SOCKSProxy>(name, address, port, !outproxy.empty(), outproxy, destinationPort, localDestination);
2018-04-25 20:18:07 +00:00
clientTunnel = tun;
clientEndpoint = tun->GetLocalEndpoint ();
}
else if (type == I2P_TUNNELS_SECTION_TYPE_HTTPPROXY)
{
// http proxy
2017-10-27 12:42:54 +00:00
std::string outproxy = section.second.get("outproxy", "");
2018-04-25 20:18:07 +00:00
auto tun = std::make_shared<i2p::proxy::HTTPProxy>(name, address, port, outproxy, localDestination);
clientTunnel = tun;
clientEndpoint = tun->GetLocalEndpoint ();
}
else if (type == I2P_TUNNELS_SECTION_TYPE_WEBSOCKS)
{
// websocks proxy
2018-04-25 20:18:07 +00:00
auto tun = std::make_shared<WebSocks>(address, port, localDestination);
clientTunnel = tun;
clientEndpoint = tun->GetLocalEndpoint();
}
else
{
// tcp client
2018-04-25 20:18:07 +00:00
auto tun = std::make_shared<I2PClientTunnel> (name, dest, address, port, localDestination, destinationPort);
clientTunnel = tun;
clientEndpoint = tun->GetLocalEndpoint ();
}
uint32_t timeout = section.second.get<uint32_t>(I2P_CLIENT_TUNNEL_CONNECT_TIMEOUT, 0);
if(timeout)
{
clientTunnel->SetConnectTimeout(timeout);
LogPrint(eLogInfo, "Clients: I2P Client tunnel connect timeout set to ", timeout);
}
2018-04-25 20:18:07 +00:00
auto ins = m_ClientTunnels.insert (std::make_pair (clientEndpoint, clientTunnel));
if (ins.second)
{
clientTunnel->Start ();
numClientTunnels++;
}
else
{
// TODO: update
2018-04-25 20:18:07 +00:00
if (ins.first->second->GetLocalDestination () != clientTunnel->GetLocalDestination ())
2017-09-29 19:34:26 +00:00
{
LogPrint (eLogInfo, "Clients: I2P client tunnel destination updated");
2018-04-25 20:18:07 +00:00
ins.first->second->SetLocalDestination (clientTunnel->GetLocalDestination ());
2017-09-29 19:34:26 +00:00
}
ins.first->second->isUpdated = true;
2017-09-09 17:13:45 +00:00
LogPrint (eLogInfo, "Clients: I2P client tunnel for endpoint ", clientEndpoint, " already exists");
}
}
2015-03-13 20:05:39 +00:00
}
else if (type == I2P_TUNNELS_SECTION_TYPE_SERVER
|| type == I2P_TUNNELS_SECTION_TYPE_HTTP
|| type == I2P_TUNNELS_SECTION_TYPE_IRC
|| type == I2P_TUNNELS_SECTION_TYPE_UDPSERVER)
{
2015-03-13 23:51:31 +00:00
// mandatory params
std::string host = section.second.get<std::string> (I2P_SERVER_TUNNEL_HOST);
int port = section.second.get<int> (I2P_SERVER_TUNNEL_PORT);
std::string keys = section.second.get<std::string> (I2P_SERVER_TUNNEL_KEYS);
// optional params
int inPort = section.second.get (I2P_SERVER_TUNNEL_INPORT, 0);
2016-02-26 01:32:05 +00:00
std::string accessList = section.second.get (I2P_SERVER_TUNNEL_ACCESS_LIST, "");
std::string hostOverride = section.second.get (I2P_SERVER_TUNNEL_HOST_OVERRIDE, "");
2016-03-04 06:37:38 +00:00
std::string webircpass = section.second.get<std::string> (I2P_SERVER_TUNNEL_WEBIRC_PASSWORD, "");
bool gzip = section.second.get (I2P_SERVER_TUNNEL_GZIP, true);
2018-12-05 19:58:50 +00:00
i2p::data::SigningKeyType sigType = section.second.get (I2P_SERVER_TUNNEL_SIGNATURE_TYPE, i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519);
2017-11-09 21:18:59 +00:00
i2p::data::CryptoKeyType cryptoType = section.second.get (I2P_CLIENT_TUNNEL_CRYPTO_TYPE, i2p::data::CRYPTO_KEY_TYPE_ELGAMAL);
2018-04-16 13:38:32 +00:00
2016-09-03 13:38:53 +00:00
std::string address = section.second.get<std::string> (I2P_SERVER_TUNNEL_ADDRESS, "127.0.0.1");
bool isUniqueLocal = section.second.get(I2P_SERVER_TUNNEL_ENABLE_UNIQUE_LOCAL, true);
2016-01-14 20:57:55 +00:00
// I2CP
std::map<std::string, std::string> options;
ReadI2CPOptions (section, options);
2016-01-14 20:57:55 +00:00
std::shared_ptr<ClientDestination> localDestination = nullptr;
2016-01-15 19:46:29 +00:00
i2p::data::PrivateKeys k;
2017-11-09 21:18:59 +00:00
if(!LoadPrivateKeys (k, keys, sigType, cryptoType))
2016-07-25 15:13:54 +00:00
continue;
localDestination = FindLocalDestination (k.GetPublic ()->GetIdentHash ());
if (!localDestination)
localDestination = CreateNewLocalDestination (k, true, &options);
if (type == I2P_TUNNELS_SECTION_TYPE_UDPSERVER)
{
// udp server tunnel
// TODO: hostnames
auto localAddress = boost::asio::ip::address::from_string(address);
boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::address::from_string(host), port);
2018-04-25 20:18:07 +00:00
auto serverTunnel = std::make_shared<I2PUDPServerTunnel>(name, localDestination, localAddress, endpoint, port);
if(!isUniqueLocal)
{
LogPrint(eLogInfo, "Clients: disabling loopback address mapping");
serverTunnel->SetUniqueLocal(isUniqueLocal);
}
std::lock_guard<std::mutex> lock(m_ForwardsMutex);
if(m_ServerForwards.insert(
std::make_pair(
std::make_pair(
localDestination->GetIdentHash(), port),
2018-04-25 20:18:07 +00:00
serverTunnel)).second)
{
serverTunnel->Start();
LogPrint(eLogInfo, "Clients: I2P Server Forward created for UDP Endpoint ", host, ":", port, " bound on ", address, " for ",localDestination->GetIdentHash().ToBase32());
}
else
LogPrint(eLogError, "Clients: I2P Server Forward for destination/port ", m_AddressBook.ToAddress(localDestination->GetIdentHash()), "/", port, "already exists");
continue;
}
2018-04-25 20:18:07 +00:00
std::shared_ptr<I2PServerTunnel> serverTunnel;
if (type == I2P_TUNNELS_SECTION_TYPE_HTTP)
2018-04-25 20:18:07 +00:00
serverTunnel = std::make_shared<I2PServerTunnelHTTP> (name, host, port, localDestination, hostOverride, inPort, gzip);
2018-01-06 04:01:44 +00:00
else if (type == I2P_TUNNELS_SECTION_TYPE_IRC)
2018-04-25 20:18:07 +00:00
serverTunnel = std::make_shared<I2PServerTunnelIRC> (name, host, port, localDestination, webircpass, inPort, gzip);
else // regular server tunnel by default
2018-04-25 20:18:07 +00:00
serverTunnel = std::make_shared<I2PServerTunnel> (name, host, port, localDestination, inPort, gzip);
2016-02-22 19:33:21 +00:00
if(!isUniqueLocal)
{
LogPrint(eLogInfo, "Clients: disabling loopback address mapping");
serverTunnel->SetUniqueLocal(isUniqueLocal);
2018-01-06 04:01:44 +00:00
}
2015-03-16 18:52:42 +00:00
if (accessList.length () > 0)
{
std::set<i2p::data::IdentHash> idents;
size_t pos = 0, comma;
do
{
comma = accessList.find (',', pos);
i2p::data::IdentHash ident;
ident.FromBase32 (accessList.substr (pos, comma != std::string::npos ? comma - pos : std::string::npos));
2015-03-16 18:52:42 +00:00
idents.insert (ident);
pos = comma + 1;
}
while (comma != std::string::npos);
serverTunnel->SetAccessList (idents);
}
auto ins = m_ServerTunnels.insert (std::make_pair (
2018-04-25 20:18:07 +00:00
std::make_pair (localDestination->GetIdentHash (), inPort),
serverTunnel));
if (ins.second)
{
serverTunnel->Start ();
numServerTunnels++;
}
2015-03-13 23:51:31 +00:00
else
{
// TODO: update
2018-04-25 20:18:07 +00:00
if (ins.first->second->GetLocalDestination () != serverTunnel->GetLocalDestination ())
2017-09-29 19:34:26 +00:00
{
LogPrint (eLogInfo, "Clients: I2P server tunnel destination updated");
2018-04-25 20:18:07 +00:00
ins.first->second->SetLocalDestination (serverTunnel->GetLocalDestination ());
2017-09-29 19:34:26 +00:00
}
ins.first->second->isUpdated = true;
LogPrint (eLogInfo, "Clients: I2P server tunnel for destination/port ", m_AddressBook.ToAddress(localDestination->GetIdentHash ()), "/", inPort, " already exists");
}
2015-03-13 20:05:39 +00:00
}
2015-03-13 23:51:31 +00:00
else
LogPrint (eLogWarning, "Clients: Unknown section type=", type, " of ", name, " in ", tunConf);
2015-03-13 20:05:39 +00:00
}
2015-03-13 23:51:31 +00:00
catch (std::exception& ex)
2015-03-13 20:05:39 +00:00
{
2015-12-18 12:44:03 +00:00
LogPrint (eLogError, "Clients: Can't read tunnel ", name, " params: ", ex.what ());
2015-03-13 20:05:39 +00:00
}
}
2016-08-21 19:02:17 +00:00
}
void ClientContext::ReadHttpProxy ()
{
std::shared_ptr<ClientDestination> localDestination;
bool httproxy; i2p::config::GetOption("httpproxy.enabled", httproxy);
if (httproxy)
{
std::string httpProxyKeys; i2p::config::GetOption("httpproxy.keys", httpProxyKeys);
std::string httpProxyAddr; i2p::config::GetOption("httpproxy.address", httpProxyAddr);
uint16_t httpProxyPort; i2p::config::GetOption("httpproxy.port", httpProxyPort);
i2p::data::SigningKeyType sigType; i2p::config::GetOption("httpproxy.signaturetype", sigType);
std::string httpOutProxyURL; i2p::config::GetOption("httpproxy.outproxy", httpOutProxyURL);
LogPrint(eLogInfo, "Clients: starting HTTP Proxy at ", httpProxyAddr, ":", httpProxyPort);
if (httpProxyKeys.length () > 0)
{
i2p::data::PrivateKeys keys;
if(LoadPrivateKeys (keys, httpProxyKeys, sigType))
{
std::map<std::string, std::string> params;
ReadI2CPOptionsFromConfig ("httpproxy.", params);
localDestination = CreateNewLocalDestination (keys, false, &params);
if (localDestination) localDestination->Acquire ();
}
else
LogPrint(eLogError, "Clients: failed to load HTTP Proxy key");
}
try
{
m_HttpProxy = new i2p::proxy::HTTPProxy("HTTP Proxy", httpProxyAddr, httpProxyPort, httpOutProxyURL, localDestination);
m_HttpProxy->Start();
}
catch (std::exception& e)
{
LogPrint(eLogError, "Clients: Exception in HTTP Proxy: ", e.what());
}
}
}
void ClientContext::ReadSocksProxy ()
{
std::shared_ptr<ClientDestination> localDestination;
bool socksproxy; i2p::config::GetOption("socksproxy.enabled", socksproxy);
if (socksproxy)
{
std::string socksProxyKeys; i2p::config::GetOption("socksproxy.keys", socksProxyKeys);
std::string socksProxyAddr; i2p::config::GetOption("socksproxy.address", socksProxyAddr);
uint16_t socksProxyPort; i2p::config::GetOption("socksproxy.port", socksProxyPort);
bool socksOutProxy; i2p::config::GetOption("socksproxy.outproxy.enabled", socksOutProxy);
std::string socksOutProxyAddr; i2p::config::GetOption("socksproxy.outproxy", socksOutProxyAddr);
uint16_t socksOutProxyPort; i2p::config::GetOption("socksproxy.outproxyport", socksOutProxyPort);
i2p::data::SigningKeyType sigType; i2p::config::GetOption("socksproxy.signaturetype", sigType);
LogPrint(eLogInfo, "Clients: starting SOCKS Proxy at ", socksProxyAddr, ":", socksProxyPort);
if (socksProxyKeys.length () > 0)
{
i2p::data::PrivateKeys keys;
if (LoadPrivateKeys (keys, socksProxyKeys, sigType))
{
std::map<std::string, std::string> params;
ReadI2CPOptionsFromConfig ("socksproxy.", params);
localDestination = CreateNewLocalDestination (keys, false, &params);
if (localDestination) localDestination->Acquire ();
}
else
LogPrint(eLogError, "Clients: failed to load SOCKS Proxy key");
}
try
{
m_SocksProxy = new i2p::proxy::SOCKSProxy("SOCKS", socksProxyAddr, socksProxyPort,
socksOutProxy, socksOutProxyAddr, socksOutProxyPort, localDestination);
m_SocksProxy->Start();
}
catch (std::exception& e)
{
LogPrint(eLogError, "Clients: Exception in SOCKS Proxy: ", e.what());
}
}
}
void ClientContext::ScheduleCleanupUDP()
{
if (m_CleanupUDPTimer)
{
// schedule cleanup in 17 seconds
m_CleanupUDPTimer->expires_from_now (boost::posix_time::seconds (17));
m_CleanupUDPTimer->async_wait(std::bind(&ClientContext::CleanupUDP, this, std::placeholders::_1));
}
}
2016-08-21 19:02:17 +00:00
void ClientContext::CleanupUDP(const boost::system::error_code & ecode)
{
if(!ecode)
{
std::lock_guard<std::mutex> lock(m_ForwardsMutex);
for (auto & s : m_ServerForwards ) s.second->ExpireStale();
ScheduleCleanupUDP();
}
}
template<typename Container, typename Visitor>
void VisitTunnelsContainer (Container& c, Visitor v)
{
for (auto it = c.begin (); it != c.end ();)
{
if (!v (it->second.get ()))
2017-07-28 19:40:07 +00:00
{
it->second->Stop ();
it = c.erase (it);
2017-07-28 19:40:07 +00:00
}
else
it++;
}
}
template<typename Visitor>
void ClientContext::VisitTunnels (Visitor v)
{
VisitTunnelsContainer (m_ClientTunnels, v);
VisitTunnelsContainer (m_ServerTunnels, v);
// TODO: implement UDP forwards
}
}
}