2018-12-12 01:06:46 +00:00
|
|
|
#include <exit/context.hpp>
|
2018-11-12 16:43:40 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace exit
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
Context::Context(AbstractRouter* r) : m_Router(r)
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
Context::~Context()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Tick(llarp_time_t now)
|
2018-12-24 16:09:05 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
auto itr = m_Exits.begin();
|
|
|
|
while(itr != m_Exits.end())
|
|
|
|
{
|
|
|
|
itr->second->Tick(now);
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto itr = m_Closed.begin();
|
|
|
|
while(itr != m_Closed.end())
|
|
|
|
{
|
|
|
|
if((*itr)->ShouldRemove())
|
|
|
|
itr = m_Closed.erase(itr);
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Stop()
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
|
|
|
auto itr = m_Exits.begin();
|
|
|
|
while(itr != m_Exits.end())
|
|
|
|
{
|
2018-12-24 16:09:05 +00:00
|
|
|
itr->second->Stop();
|
|
|
|
m_Closed.emplace_back(std::move(itr->second));
|
|
|
|
itr = m_Exits.erase(itr);
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
|
|
|
Context::ExtractStatus() const
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject obj{};
|
2019-02-08 19:43:25 +00:00
|
|
|
auto itr = m_Exits.begin();
|
|
|
|
while(itr != m_Exits.end())
|
|
|
|
{
|
2019-02-11 17:14:43 +00:00
|
|
|
obj.Put(itr->first, itr->second->ExtractStatus());
|
2019-02-08 19:43:25 +00:00
|
|
|
++itr;
|
|
|
|
}
|
2019-02-11 17:14:43 +00:00
|
|
|
return obj;
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
void
|
|
|
|
Context::CalculateExitTraffic(TrafficStats& stats)
|
|
|
|
{
|
|
|
|
auto itr = m_Exits.begin();
|
|
|
|
while(itr != m_Exits.end())
|
|
|
|
{
|
|
|
|
itr->second->CalculateTrafficStats(stats);
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 21:28:28 +00:00
|
|
|
exit::Endpoint*
|
|
|
|
Context::FindEndpointForPath(const PathID_t& path) const
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
auto itr = m_Exits.begin();
|
|
|
|
while(itr != m_Exits.end())
|
|
|
|
{
|
|
|
|
auto ep = itr->second->FindEndpointByPath(path);
|
|
|
|
if(ep)
|
|
|
|
return ep;
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-07-02 21:28:28 +00:00
|
|
|
Context::ObtainNewExit(const PubKey& pk, const PathID_t& path,
|
2018-11-14 12:23:08 +00:00
|
|
|
bool permitInternet)
|
|
|
|
{
|
|
|
|
auto itr = m_Exits.begin();
|
|
|
|
while(itr != m_Exits.end())
|
|
|
|
{
|
|
|
|
if(itr->second->AllocateNewExit(pk, path, permitInternet))
|
|
|
|
return true;
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
bool
|
|
|
|
Context::AddExitEndpoint(const std::string& name, const Config_t& conf)
|
|
|
|
{
|
|
|
|
// check for duplicate exit by name
|
|
|
|
{
|
|
|
|
auto itr = m_Exits.find(name);
|
|
|
|
if(itr != m_Exits.end())
|
|
|
|
{
|
2019-07-02 21:28:28 +00:00
|
|
|
LogError("duplicate exit with name ", name);
|
2018-11-12 16:43:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-07-02 21:28:28 +00:00
|
|
|
std::unique_ptr< handlers::ExitEndpoint > endpoint;
|
2018-11-12 16:43:40 +00:00
|
|
|
// make new endpoint
|
2019-07-02 21:28:28 +00:00
|
|
|
endpoint.reset(new handlers::ExitEndpoint(name, m_Router));
|
2018-11-12 16:43:40 +00:00
|
|
|
// configure
|
|
|
|
{
|
|
|
|
auto itr = conf.begin();
|
|
|
|
while(itr != conf.end())
|
|
|
|
{
|
|
|
|
if(!endpoint->SetOption(itr->first, itr->second))
|
2019-01-16 21:06:35 +00:00
|
|
|
{
|
2019-07-02 21:28:28 +00:00
|
|
|
LogWarn("Couldn't set option ", itr->first, " to ", itr->second);
|
2018-11-12 16:43:40 +00:00
|
|
|
return false;
|
2019-01-16 21:06:35 +00:00
|
|
|
}
|
2018-11-12 16:43:40 +00:00
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add endpoint
|
2018-11-14 19:50:16 +00:00
|
|
|
if(!endpoint->Start())
|
2019-01-16 21:06:35 +00:00
|
|
|
{
|
2019-07-02 21:28:28 +00:00
|
|
|
LogWarn("Couldn't start exit endpoint");
|
2018-11-14 19:50:16 +00:00
|
|
|
return false;
|
2019-01-16 21:06:35 +00:00
|
|
|
}
|
2018-11-12 16:43:40 +00:00
|
|
|
m_Exits.emplace(name, std::move(endpoint));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace exit
|
2018-12-10 16:26:46 +00:00
|
|
|
} // namespace llarp
|