mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
667 lines
24 KiB
667 lines
24 KiB
10 years ago
|
#include <fstream>
|
||
10 years ago
|
#include <iostream>
|
||
10 years ago
|
#include <boost/property_tree/ptree.hpp>
|
||
|
#include <boost/property_tree/ini_parser.hpp>
|
||
9 years ago
|
#include "Config.h"
|
||
9 years ago
|
#include "FS.h"
|
||
10 years ago
|
#include "Log.h"
|
||
10 years ago
|
#include "Identity.h"
|
||
9 years ago
|
#include "util.h"
|
||
10 years ago
|
#include "ClientContext.h"
|
||
8 years ago
|
#include "SOCKS.h"
|
||
|
#include "WebSocks.h"
|
||
8 years ago
|
#include "MatchedDestination.h"
|
||
10 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace client
|
||
|
{
|
||
8 years ago
|
ClientContext context;
|
||
10 years ago
|
|
||
|
ClientContext::ClientContext (): m_SharedLocalDestination (nullptr),
|
||
8 years ago
|
m_HttpProxy (nullptr), m_SocksProxy (nullptr), m_SamBridge (nullptr),
|
||
8 years ago
|
m_BOBCommandChannel (nullptr), m_I2CPServer (nullptr)
|
||
10 years ago
|
{
|
||
|
}
|
||
8 years ago
|
|
||
|
ClientContext::~ClientContext ()
|
||
10 years ago
|
{
|
||
|
delete m_HttpProxy;
|
||
|
delete m_SocksProxy;
|
||
|
delete m_SamBridge;
|
||
10 years ago
|
delete m_BOBCommandChannel;
|
||
9 years ago
|
delete m_I2CPServer;
|
||
10 years ago
|
}
|
||
8 years ago
|
|
||
10 years ago
|
void ClientContext::Start ()
|
||
|
{
|
||
|
if (!m_SharedLocalDestination)
|
||
8 years ago
|
{
|
||
10 years ago
|
m_SharedLocalDestination = CreateNewLocalDestination (); // non-public, DSA
|
||
9 years ago
|
m_Destinations[m_SharedLocalDestination->GetIdentity ()->GetIdentHash ()] = m_SharedLocalDestination;
|
||
10 years ago
|
m_SharedLocalDestination->Start ();
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
|
||
|
m_AddressBook.Start ();
|
||
|
|
||
|
std::shared_ptr<ClientDestination> localDestination;
|
||
9 years ago
|
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);
|
||
8 years ago
|
i2p::data::SigningKeyType sigType; i2p::config::GetOption("httpproxy.signaturetype", sigType);
|
||
9 years ago
|
LogPrint(eLogInfo, "Clients: starting HTTP Proxy at ", httpProxyAddr, ":", httpProxyPort);
|
||
|
if (httpProxyKeys.length () > 0)
|
||
|
{
|
||
|
i2p::data::PrivateKeys keys;
|
||
8 years ago
|
if(LoadPrivateKeys (keys, httpProxyKeys, sigType))
|
||
8 years ago
|
{
|
||
|
std::map<std::string, std::string> params;
|
||
8 years ago
|
ReadI2CPOptionsFromConfig ("httpproxy.", params);
|
||
8 years ago
|
localDestination = CreateNewLocalDestination (keys, false, ¶ms);
|
||
8 years ago
|
}
|
||
8 years ago
|
else
|
||
|
LogPrint(eLogError, "Clients: failed to load HTTP Proxy key");
|
||
9 years ago
|
}
|
||
9 years ago
|
try {
|
||
|
m_HttpProxy = new i2p::proxy::HTTPProxy(httpProxyAddr, httpProxyPort, localDestination);
|
||
|
m_HttpProxy->Start();
|
||
|
} catch (std::exception& e) {
|
||
9 years ago
|
LogPrint(eLogError, "Clients: Exception in HTTP Proxy: ", e.what());
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
localDestination = nullptr;
|
||
9 years ago
|
bool socksproxy; i2p::config::GetOption("socksproxy.enabled", socksproxy);
|
||
8 years ago
|
if (socksproxy)
|
||
8 years ago
|
{
|
||
9 years ago
|
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);
|
||
9 years ago
|
std::string socksOutProxyAddr; i2p::config::GetOption("socksproxy.outproxy", socksOutProxyAddr);
|
||
|
uint16_t socksOutProxyPort; i2p::config::GetOption("socksproxy.outproxyport", socksOutProxyPort);
|
||
8 years ago
|
i2p::data::SigningKeyType sigType; i2p::config::GetOption("socksproxy.signaturetype", sigType);
|
||
9 years ago
|
LogPrint(eLogInfo, "Clients: starting SOCKS Proxy at ", socksProxyAddr, ":", socksProxyPort);
|
||
|
if (socksProxyKeys.length () > 0)
|
||
|
{
|
||
|
i2p::data::PrivateKeys keys;
|
||
8 years ago
|
if (LoadPrivateKeys (keys, socksProxyKeys, sigType))
|
||
8 years ago
|
{
|
||
|
std::map<std::string, std::string> params;
|
||
|
ReadI2CPOptionsFromConfig ("socksproxy.", params);
|
||
|
localDestination = CreateNewLocalDestination (keys, false, ¶ms);
|
||
|
}
|
||
|
else
|
||
|
LogPrint(eLogError, "Clients: failed to load SOCKS Proxy key");
|
||
9 years ago
|
}
|
||
9 years ago
|
try {
|
||
|
m_SocksProxy = new i2p::proxy::SOCKSProxy(socksProxyAddr, socksProxyPort, socksOutProxyAddr, socksOutProxyPort, localDestination);
|
||
|
m_SocksProxy->Start();
|
||
|
} catch (std::exception& e) {
|
||
|
LogPrint(eLogError, "Clients: Exception in SOCKS Proxy: ", e.what());
|
||
|
}
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
// I2P tunnels
|
||
8 years ago
|
ReadTunnels ();
|
||
|
|
||
10 years ago
|
// SAM
|
||
9 years ago
|
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);
|
||
9 years ago
|
try {
|
||
|
m_SamBridge = new SAMBridge (samAddr, samPort);
|
||
|
m_SamBridge->Start ();
|
||
|
} catch (std::exception& e) {
|
||
|
LogPrint(eLogError, "Clients: Exception in SAM bridge: ", e.what());
|
||
|
}
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
|
// BOB
|
||
9 years ago
|
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);
|
||
9 years ago
|
LogPrint(eLogInfo, "Clients: starting BOB command channel at ", bobAddr, ":", bobPort);
|
||
9 years ago
|
try {
|
||
|
m_BOBCommandChannel = new BOBCommandChannel (bobAddr, bobPort);
|
||
|
m_BOBCommandChannel->Start ();
|
||
|
} catch (std::exception& e) {
|
||
|
LogPrint(eLogError, "Clients: Exception in BOB bridge: ", e.what());
|
||
|
}
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// I2CP
|
||
|
bool i2cp; i2p::config::GetOption("i2cp.enabled", i2cp);
|
||
8 years ago
|
if (i2cp)
|
||
9 years ago
|
{
|
||
|
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);
|
||
8 years ago
|
try
|
||
9 years ago
|
{
|
||
|
m_I2CPServer = new I2CPServer (i2cpAddr, i2cpPort);
|
||
|
m_I2CPServer->Start ();
|
||
8 years ago
|
}
|
||
|
catch (std::exception& e)
|
||
9 years ago
|
{
|
||
|
LogPrint(eLogError, "Clients: Exception in I2CP: ", e.what());
|
||
|
}
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
m_AddressBook.StartResolvers ();
|
||
8 years ago
|
|
||
|
// start UDP cleanup
|
||
|
if (!m_ServerForwards.empty ())
|
||
|
{
|
||
|
m_CleanupUDPTimer.reset (new boost::asio::deadline_timer(m_SharedLocalDestination->GetService ()));
|
||
|
ScheduleCleanupUDP();
|
||
|
}
|
||
10 years ago
|
}
|
||
8 years ago
|
|
||
10 years ago
|
void ClientContext::Stop ()
|
||
|
{
|
||
9 years ago
|
if (m_HttpProxy)
|
||
|
{
|
||
|
LogPrint(eLogInfo, "Clients: stopping HTTP Proxy");
|
||
|
m_HttpProxy->Stop();
|
||
|
delete m_HttpProxy;
|
||
|
m_HttpProxy = nullptr;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (m_SocksProxy)
|
||
|
{
|
||
|
LogPrint(eLogInfo, "Clients: stopping SOCKS Proxy");
|
||
|
m_SocksProxy->Stop();
|
||
|
delete m_SocksProxy;
|
||
|
m_SocksProxy = nullptr;
|
||
|
}
|
||
9 years ago
|
|
||
10 years ago
|
for (auto& it: m_ClientTunnels)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint(eLogInfo, "Clients: stopping I2P client tunnel on port ", it.first);
|
||
10 years ago
|
it.second->Stop ();
|
||
10 years ago
|
}
|
||
8 years ago
|
m_ClientTunnels.clear ();
|
||
9 years ago
|
|
||
10 years ago
|
for (auto& it: m_ServerTunnels)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint(eLogInfo, "Clients: stopping I2P server tunnel");
|
||
10 years ago
|
it.second->Stop ();
|
||
|
}
|
||
8 years ago
|
m_ServerTunnels.clear ();
|
||
9 years ago
|
|
||
10 years ago
|
if (m_SamBridge)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogInfo, "Clients: stopping SAM bridge");
|
||
10 years ago
|
m_SamBridge->Stop ();
|
||
8 years ago
|
delete m_SamBridge;
|
||
10 years ago
|
m_SamBridge = nullptr;
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
10 years ago
|
if (m_BOBCommandChannel)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogInfo, "Clients: stopping BOB command channel");
|
||
10 years ago
|
m_BOBCommandChannel->Stop ();
|
||
8 years ago
|
delete m_BOBCommandChannel;
|
||
10 years ago
|
m_BOBCommandChannel = nullptr;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (m_I2CPServer)
|
||
|
{
|
||
|
LogPrint(eLogInfo, "Clients: stopping I2CP");
|
||
|
m_I2CPServer->Stop ();
|
||
8 years ago
|
delete m_I2CPServer;
|
||
9 years ago
|
m_I2CPServer = nullptr;
|
||
|
}
|
||
|
|
||
9 years ago
|
LogPrint(eLogInfo, "Clients: stopping AddressBook");
|
||
8 years ago
|
m_AddressBook.Stop ();
|
||
|
|
||
8 years ago
|
{
|
||
8 years ago
|
std::lock_guard<std::mutex> lock(m_ForwardsMutex);
|
||
|
m_ServerForwards.clear();
|
||
|
m_ClientForwards.clear();
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
if (m_CleanupUDPTimer)
|
||
|
{
|
||
8 years ago
|
m_CleanupUDPTimer->cancel ();
|
||
|
m_CleanupUDPTimer = nullptr;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
for (auto& it: m_Destinations)
|
||
10 years ago
|
it.second->Stop ();
|
||
|
m_Destinations.clear ();
|
||
8 years ago
|
m_SharedLocalDestination = nullptr;
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
|
void ClientContext::ReloadConfig ()
|
||
|
{
|
||
8 years ago
|
std::string config; i2p::config::GetOption("conf", config);
|
||
|
i2p::config::ParseConfig(config);
|
||
8 years ago
|
Stop();
|
||
|
Start();
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
bool ClientContext::LoadPrivateKeys (i2p::data::PrivateKeys& keys, const std::string& filename, i2p::data::SigningKeyType sigType)
|
||
10 years ago
|
{
|
||
8 years ago
|
bool success = true;
|
||
9 years ago
|
std::string fullPath = i2p::fs::DataDirPath (filename);
|
||
|
std::ifstream s(fullPath, std::ifstream::binary);
|
||
8 years ago
|
if (s.is_open ())
|
||
|
{
|
||
10 years ago
|
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);
|
||
8 years ago
|
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");
|
||
10 years ago
|
delete[] buf;
|
||
8 years ago
|
}
|
||
10 years ago
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "Clients: can't open file ", fullPath, " Creating new one with signature type ", sigType);
|
||
8 years ago
|
keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType);
|
||
10 years ago
|
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;
|
||
8 years ago
|
|
||
9 years ago
|
LogPrint (eLogInfo, "Clients: New private keys file ", fullPath, " for ", m_AddressBook.ToAddress(keys.GetPublic ()->GetIdentHash ()), " created");
|
||
8 years ago
|
}
|
||
|
return success;
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
std::vector<std::shared_ptr<DatagramSessionInfo> > ClientContext::GetForwardInfosFor(const i2p::data::IdentHash & destination)
|
||
8 years ago
|
{
|
||
8 years ago
|
std::vector<std::shared_ptr<DatagramSessionInfo> > infos;
|
||
8 years ago
|
std::lock_guard<std::mutex> lock(m_ForwardsMutex);
|
||
8 years ago
|
for(const auto & c : m_ClientForwards)
|
||
8 years ago
|
{
|
||
|
if (c.second->IsLocalDestination(destination))
|
||
8 years ago
|
{
|
||
|
for (auto & i : c.second->GetSessions()) infos.push_back(i);
|
||
|
break;
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
for(const auto & s : m_ServerForwards)
|
||
8 years ago
|
{
|
||
|
if(std::get<0>(s.first) == destination)
|
||
8 years ago
|
{
|
||
|
for( auto & i : s.second->GetSessions()) infos.push_back(i);
|
||
|
break;
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
return infos;
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
10 years ago
|
std::shared_ptr<ClientDestination> ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType,
|
||
8 years ago
|
const std::map<std::string, std::string> * params)
|
||
10 years ago
|
{
|
||
10 years ago
|
i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType);
|
||
10 years ago
|
auto localDestination = std::make_shared<ClientDestination> (keys, isPublic, params);
|
||
10 years ago
|
std::unique_lock<std::mutex> l(m_DestinationsMutex);
|
||
|
m_Destinations[localDestination->GetIdentHash ()] = localDestination;
|
||
|
localDestination->Start ();
|
||
|
return localDestination;
|
||
|
}
|
||
|
|
||
8 years ago
|
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;
|
||
|
}
|
||
|
|
||
10 years ago
|
void ClientContext::DeleteLocalDestination (std::shared_ptr<ClientDestination> destination)
|
||
10 years ago
|
{
|
||
|
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);
|
||
8 years ago
|
}
|
||
10 years ago
|
d->Stop ();
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
std::shared_ptr<ClientDestination> ClientContext::CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
|
||
10 years ago
|
const std::map<std::string, std::string> * params)
|
||
10 years ago
|
{
|
||
9 years ago
|
auto it = m_Destinations.find (keys.GetPublic ()->GetIdentHash ());
|
||
10 years ago
|
if (it != m_Destinations.end ())
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "Clients: Local destination ", m_AddressBook.ToAddress(keys.GetPublic ()->GetIdentHash ()), " exists");
|
||
10 years ago
|
if (!it->second->IsRunning ())
|
||
8 years ago
|
{
|
||
10 years ago
|
it->second->Start ();
|
||
|
return it->second;
|
||
8 years ago
|
}
|
||
10 years ago
|
return nullptr;
|
||
8 years ago
|
}
|
||
10 years ago
|
auto localDestination = std::make_shared<ClientDestination> (keys, isPublic, params);
|
||
10 years ago
|
std::unique_lock<std::mutex> l(m_DestinationsMutex);
|
||
9 years ago
|
m_Destinations[keys.GetPublic ()->GetIdentHash ()] = localDestination;
|
||
10 years ago
|
localDestination->Start ();
|
||
|
return localDestination;
|
||
|
}
|
||
8 years ago
|
|
||
10 years ago
|
std::shared_ptr<ClientDestination> ClientContext::FindLocalDestination (const i2p::data::IdentHash& destination) const
|
||
10 years ago
|
{
|
||
|
auto it = m_Destinations.find (destination);
|
||
|
if (it != m_Destinations.end ())
|
||
|
return it->second;
|
||
|
return nullptr;
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
template<typename Section, typename Type>
|
||
|
std::string ClientContext::GetI2CPOption (const Section& section, const std::string& name, const Type& value) const
|
||
|
{
|
||
9 years ago
|
return section.second.get (boost::property_tree::ptree::path_type (name, '/'), std::to_string (value));
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
|
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);
|
||
9 years ago
|
options[I2CP_PARAM_TAGS_TO_SEND] = GetI2CPOption (section, I2CP_PARAM_TAGS_TO_SEND, DEFAULT_TAGS_TO_SEND);
|
||
8 years ago
|
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);
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
void ClientContext::ReadI2CPOptionsFromConfig (const std::string& prefix, std::map<std::string, std::string>& options) const
|
||
|
{
|
||
8 years ago
|
std::string value;
|
||
|
if (i2p::config::GetOption(prefix + I2CP_PARAM_INBOUND_TUNNEL_LENGTH, value))
|
||
8 years ago
|
options[I2CP_PARAM_INBOUND_TUNNEL_LENGTH] = value;
|
||
8 years ago
|
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))
|
||
8 years ago
|
options[I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH] = value;
|
||
8 years ago
|
if (i2p::config::GetOption(prefix + I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY, value))
|
||
8 years ago
|
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))
|
||
|
options[I2CP_PARAM_MAX_TUNNEL_LATENCY] = value;
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
10 years ago
|
void ClientContext::ReadTunnels ()
|
||
10 years ago
|
{
|
||
|
boost::property_tree::ptree pt;
|
||
9 years ago
|
std::string tunConf; i2p::config::GetOption("tunconf", tunConf);
|
||
9 years ago
|
if (tunConf == "") {
|
||
|
// TODO: cleanup this in 2.8.0
|
||
9 years ago
|
tunConf = i2p::fs::DataDirPath ("tunnels.cfg");
|
||
9 years ago
|
if (i2p::fs::Exists(tunConf)) {
|
||
|
LogPrint(eLogWarning, "FS: please rename tunnels.cfg -> tunnels.conf here: ", tunConf);
|
||
|
} else {
|
||
|
tunConf = i2p::fs::DataDirPath ("tunnels.conf");
|
||
|
}
|
||
|
}
|
||
9 years ago
|
LogPrint(eLogDebug, "FS: tunnels config file: ", tunConf);
|
||
8 years ago
|
try
|
||
9 years ago
|
{
|
||
|
boost::property_tree::read_ini (tunConf, pt);
|
||
8 years ago
|
}
|
||
|
catch (std::exception& ex)
|
||
9 years ago
|
{
|
||
|
LogPrint (eLogWarning, "Clients: Can't read ", tunConf, ": ", ex.what ());
|
||
10 years ago
|
return;
|
||
|
}
|
||
8 years ago
|
|
||
10 years ago
|
int numClientTunnels = 0, numServerTunnels = 0;
|
||
|
for (auto& section: pt)
|
||
|
{
|
||
8 years ago
|
std::string name = section.first;
|
||
10 years ago
|
try
|
||
10 years ago
|
{
|
||
10 years ago
|
std::string type = section.second.get<std::string> (I2P_TUNNELS_SECTION_TYPE);
|
||
8 years ago
|
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)
|
||
10 years ago
|
{
|
||
|
// mandatory params
|
||
8 years ago
|
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);
|
||
10 years ago
|
int port = section.second.get<int> (I2P_CLIENT_TUNNEL_PORT);
|
||
10 years ago
|
// optional params
|
||
8 years ago
|
bool matchTunnels = section.second.get(I2P_CLIENT_TUNNEL_MATCH_TUNNELS, false);
|
||
10 years ago
|
std::string keys = section.second.get (I2P_CLIENT_TUNNEL_KEYS, "");
|
||
9 years ago
|
std::string address = section.second.get (I2P_CLIENT_TUNNEL_ADDRESS, "127.0.0.1");
|
||
10 years ago
|
int destinationPort = section.second.get (I2P_CLIENT_TUNNEL_DESTINATION_PORT, 0);
|
||
9 years ago
|
i2p::data::SigningKeyType sigType = section.second.get (I2P_CLIENT_TUNNEL_SIGNATURE_TYPE, i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256);
|
||
9 years ago
|
// I2CP
|
||
8 years ago
|
std::map<std::string, std::string> options;
|
||
|
ReadI2CPOptions (section, options);
|
||
10 years ago
|
|
||
|
std::shared_ptr<ClientDestination> localDestination = nullptr;
|
||
|
if (keys.length () > 0)
|
||
9 years ago
|
{
|
||
|
i2p::data::PrivateKeys k;
|
||
8 years ago
|
if(LoadPrivateKeys (k, keys, sigType))
|
||
|
{
|
||
|
localDestination = FindLocalDestination (k.GetPublic ()->GetIdentHash ());
|
||
|
if (!localDestination)
|
||
8 years ago
|
{
|
||
|
if(matchTunnels)
|
||
|
localDestination = CreateNewMatchedTunnelDestination(k, dest, &options);
|
||
|
else
|
||
|
localDestination = CreateNewLocalDestination (k, type == I2P_TUNNELS_SECTION_TYPE_UDPCLIENT, &options);
|
||
|
}
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
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;
|
||
|
}
|
||
|
auto clientTunnel = new I2PUDPClientTunnel(name, dest, end, localDestination, destinationPort);
|
||
|
if(m_ClientForwards.insert(std::make_pair(end, std::unique_ptr<I2PUDPClientTunnel>(clientTunnel))).second)
|
||
|
{
|
||
|
clientTunnel->Start();
|
||
|
}
|
||
|
else
|
||
|
LogPrint(eLogError, "Clients: I2P Client forward for endpoint ", end, " already exists");
|
||
8 years ago
|
|
||
8 years ago
|
} else {
|
||
8 years ago
|
boost::asio::ip::tcp::endpoint clientEndpoint;
|
||
|
I2PService * clientTunnel = nullptr;
|
||
|
if (type == I2P_TUNNELS_SECTION_TYPE_SOCKS)
|
||
|
{
|
||
|
// socks proxy
|
||
|
clientTunnel = new i2p::proxy::SOCKSProxy(address, port, "", destinationPort, localDestination);
|
||
|
clientEndpoint = ((i2p::proxy::SOCKSProxy*)clientTunnel)->GetAcceptor().local_endpoint();
|
||
|
}
|
||
|
else if (type == I2P_TUNNELS_SECTION_TYPE_HTTPPROXY)
|
||
|
{
|
||
|
// http proxy
|
||
|
clientTunnel = new i2p::proxy::HTTPProxy(address, port, localDestination);
|
||
|
clientEndpoint = ((i2p::proxy::HTTPProxy*)clientTunnel)->GetAcceptor().local_endpoint();
|
||
|
}
|
||
|
else if (type == I2P_TUNNELS_SECTION_TYPE_WEBSOCKS)
|
||
|
{
|
||
|
// websocks proxy
|
||
|
clientTunnel = new WebSocks(address, port, localDestination);;
|
||
|
clientEndpoint = ((WebSocks*)clientTunnel)->GetLocalEndpoint();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// tcp client
|
||
|
clientTunnel = new I2PClientTunnel (name, dest, address, port, localDestination, destinationPort);
|
||
|
clientEndpoint = ((I2PClientTunnel*)clientTunnel)->GetAcceptor().local_endpoint();
|
||
|
}
|
||
|
if (m_ClientTunnels.insert (std::make_pair (clientEndpoint, std::unique_ptr<I2PService>(clientTunnel))).second)
|
||
8 years ago
|
{
|
||
|
clientTunnel->Start ();
|
||
|
numClientTunnels++;
|
||
|
}
|
||
|
else
|
||
8 years ago
|
LogPrint (eLogError, "Clients: I2P client tunnel for endpoint ", clientEndpoint, "already exists");
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|
||
8 years ago
|
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)
|
||
8 years ago
|
{
|
||
10 years ago
|
// 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
|
||
10 years ago
|
int inPort = section.second.get (I2P_SERVER_TUNNEL_INPORT, 0);
|
||
9 years ago
|
std::string accessList = section.second.get (I2P_SERVER_TUNNEL_ACCESS_LIST, "");
|
||
|
std::string hostOverride = section.second.get (I2P_SERVER_TUNNEL_HOST_OVERRIDE, "");
|
||
9 years ago
|
std::string webircpass = section.second.get<std::string> (I2P_SERVER_TUNNEL_WEBIRC_PASSWORD, "");
|
||
9 years ago
|
bool gzip = section.second.get (I2P_SERVER_TUNNEL_GZIP, true);
|
||
9 years ago
|
i2p::data::SigningKeyType sigType = section.second.get (I2P_SERVER_TUNNEL_SIGNATURE_TYPE, i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256);
|
||
8 years ago
|
uint32_t maxConns = section.second.get(i2p::stream::I2CP_PARAM_STREAMING_MAX_CONNS_PER_MIN, i2p::stream::DEFAULT_MAX_CONNS_PER_MIN);
|
||
8 years ago
|
std::string address = section.second.get<std::string> (I2P_SERVER_TUNNEL_ADDRESS, "127.0.0.1");
|
||
8 years ago
|
bool isUniqueLocal = section.second.get(I2P_SERVER_TUNNEL_ENABLE_UNIQUE_LOCAL, true);
|
||
8 years ago
|
|
||
9 years ago
|
// I2CP
|
||
8 years ago
|
std::map<std::string, std::string> options;
|
||
|
ReadI2CPOptions (section, options);
|
||
9 years ago
|
|
||
9 years ago
|
std::shared_ptr<ClientDestination> localDestination = nullptr;
|
||
9 years ago
|
i2p::data::PrivateKeys k;
|
||
8 years ago
|
if(!LoadPrivateKeys (k, keys, sigType))
|
||
|
continue;
|
||
9 years ago
|
localDestination = FindLocalDestination (k.GetPublic ()->GetIdentHash ());
|
||
8 years ago
|
if (!localDestination)
|
||
9 years ago
|
localDestination = CreateNewLocalDestination (k, true, &options);
|
||
8 years ago
|
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);
|
||
|
I2PUDPServerTunnel * serverTunnel = new I2PUDPServerTunnel(name, localDestination, localAddress, endpoint, port);
|
||
8 years ago
|
if(!isUniqueLocal)
|
||
8 years ago
|
{
|
||
8 years ago
|
LogPrint(eLogInfo, "Clients: disabling loopback address mapping");
|
||
8 years ago
|
serverTunnel->SetUniqueLocal(isUniqueLocal);
|
||
8 years ago
|
}
|
||
8 years ago
|
std::lock_guard<std::mutex> lock(m_ForwardsMutex);
|
||
|
if(m_ServerForwards.insert(
|
||
|
std::make_pair(
|
||
|
std::make_pair(
|
||
|
localDestination->GetIdentHash(), port),
|
||
|
std::unique_ptr<I2PUDPServerTunnel>(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");
|
||
8 years ago
|
|
||
8 years ago
|
continue;
|
||
|
}
|
||
8 years ago
|
|
||
9 years ago
|
I2PServerTunnel * serverTunnel;
|
||
9 years ago
|
if (type == I2P_TUNNELS_SECTION_TYPE_HTTP)
|
||
|
serverTunnel = new I2PServerTunnelHTTP (name, host, port, localDestination, hostOverride, inPort, gzip);
|
||
|
else if (type == I2P_TUNNELS_SECTION_TYPE_IRC)
|
||
9 years ago
|
serverTunnel = new I2PServerTunnelIRC (name, host, port, localDestination, webircpass, inPort, gzip);
|
||
9 years ago
|
else // regular server tunnel by default
|
||
|
serverTunnel = new I2PServerTunnel (name, host, port, localDestination, inPort, gzip);
|
||
9 years ago
|
|
||
8 years ago
|
LogPrint(eLogInfo, "Clients: Set Max Conns To ", maxConns);
|
||
|
serverTunnel->SetMaxConnsPerMinute(maxConns);
|
||
8 years ago
|
if(!isUniqueLocal)
|
||
|
{
|
||
8 years ago
|
LogPrint(eLogInfo, "Clients: disabling loopback address mapping");
|
||
8 years ago
|
serverTunnel->SetUniqueLocal(isUniqueLocal);
|
||
|
}
|
||
|
|
||
10 years ago
|
if (accessList.length () > 0)
|
||
|
{
|
||
|
std::set<i2p::data::IdentHash> idents;
|
||
|
size_t pos = 0, comma;
|
||
|
do
|
||
|
{
|
||
|
comma = accessList.find (',', pos);
|
||
|
i2p::data::IdentHash ident;
|
||
8 years ago
|
ident.FromBase32 (accessList.substr (pos, comma != std::string::npos ? comma - pos : std::string::npos));
|
||
10 years ago
|
idents.insert (ident);
|
||
|
pos = comma + 1;
|
||
|
}
|
||
|
while (comma != std::string::npos);
|
||
|
serverTunnel->SetAccessList (idents);
|
||
|
}
|
||
9 years ago
|
if (m_ServerTunnels.insert (std::make_pair (
|
||
8 years ago
|
std::make_pair (localDestination->GetIdentHash (), inPort),
|
||
9 years ago
|
std::unique_ptr<I2PServerTunnel>(serverTunnel))).second)
|
||
9 years ago
|
{
|
||
8 years ago
|
serverTunnel->Start ();
|
||
9 years ago
|
numServerTunnels++;
|
||
|
}
|
||
10 years ago
|
else
|
||
9 years ago
|
LogPrint (eLogError, "Clients: I2P server tunnel for destination/port ", m_AddressBook.ToAddress(localDestination->GetIdentHash ()), "/", inPort, " already exists");
|
||
8 years ago
|
|
||
10 years ago
|
}
|
||
10 years ago
|
else
|
||
9 years ago
|
LogPrint (eLogWarning, "Clients: Unknown section type=", type, " of ", name, " in ", tunConf);
|
||
8 years ago
|
|
||
10 years ago
|
}
|
||
10 years ago
|
catch (std::exception& ex)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogError, "Clients: Can't read tunnel ", name, " params: ", ex.what ());
|
||
10 years ago
|
}
|
||
8 years ago
|
}
|
||
9 years ago
|
LogPrint (eLogInfo, "Clients: ", numClientTunnels, " I2P client tunnels created");
|
||
|
LogPrint (eLogInfo, "Clients: ", numServerTunnels, " I2P server tunnels created");
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
void ClientContext::ScheduleCleanupUDP()
|
||
|
{
|
||
8 years ago
|
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));
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
void ClientContext::CleanupUDP(const boost::system::error_code & ecode)
|
||
|
{
|
||
|
if(!ecode)
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lock(m_ForwardsMutex);
|
||
8 years ago
|
for (auto & s : m_ServerForwards ) s.second->ExpireStale();
|
||
8 years ago
|
ScheduleCleanupUDP();
|
||
|
}
|
||
|
}
|
||
8 years ago
|
}
|
||
|
}
|