lokinet/llarp/exit/context.cpp

140 lines
3.0 KiB
C++
Raw Normal View History

2018-12-12 01:06:46 +00:00
#include <exit/context.hpp>
2019-07-30 23:42:13 +00:00
#include <memory>
namespace llarp
{
namespace exit
{
Context::Context(AbstractRouter* r) : m_Router(r)
{
}
2019-07-30 23:42:13 +00:00
Context::~Context() = default;
void
Context::Tick(llarp_time_t now)
{
{
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()
{
auto itr = m_Exits.begin();
while(itr != m_Exits.end())
{
itr->second->Stop();
m_Closed.emplace_back(std::move(itr->second));
itr = m_Exits.erase(itr);
}
}
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())
{
obj[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;
}
}
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
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;
}
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())
{
LogError("duplicate exit with name ", name);
return false;
}
}
std::unique_ptr< handlers::ExitEndpoint > endpoint;
// make new endpoint
2019-07-30 23:42:13 +00:00
endpoint = std::make_unique< handlers::ExitEndpoint >(name, m_Router);
// configure
{
auto itr = conf.begin();
while(itr != conf.end())
{
if(!endpoint->SetOption(itr->first, itr->second))
2019-01-16 21:06:35 +00:00
{
LogWarn("Couldn't set option ", itr->first, " to ", itr->second);
return false;
2019-01-16 21:06:35 +00:00
}
++itr;
}
}
// add endpoint
2018-11-14 19:50:16 +00:00
if(!endpoint->Start())
2019-01-16 21:06:35 +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
}
m_Exits.emplace(name, std::move(endpoint));
return true;
}
} // namespace exit
} // namespace llarp