lokinet/llarp/exit/context.cpp

126 lines
2.6 KiB
C++
Raw Normal View History

#include "context.hpp"
2019-07-30 23:42:13 +00:00
#include <memory>
#include <stdexcept>
namespace llarp::exit
{
Context::Context(Router* r) : router(r)
{}
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())
2019-02-08 19:43:25 +00:00
{
itr->second->Stop();
m_Closed.emplace_back(std::move(itr->second));
itr = m_Exits.erase(itr);
2019-02-08 19:43:25 +00:00
}
}
2019-02-08 19:43:25 +00:00
util::StatusObject
Context::ExtractStatus() const
{
util::StatusObject obj{};
auto itr = m_Exits.begin();
while (itr != m_Exits.end())
2018-11-14 18:02:27 +00:00
{
obj[itr->first] = itr->second->ExtractStatus();
++itr;
2018-11-14 18:02:27 +00:00
}
return obj;
}
2018-11-14 18:02:27 +00:00
void
Context::CalculateExitTraffic(TrafficStats& stats)
{
auto itr = m_Exits.begin();
while (itr != m_Exits.end())
2018-11-14 12:23:08 +00:00
{
itr->second->CalculateTrafficStats(stats);
++itr;
2018-11-14 12:23:08 +00:00
}
}
2018-11-14 12:23:08 +00:00
exit::Endpoint*
Context::FindEndpointForPath(const PathID_t& path) const
{
auto itr = m_Exits.begin();
while (itr != m_Exits.end())
2018-11-14 12:23:08 +00:00
{
auto ep = itr->second->FindEndpointByPath(path);
if (ep)
return ep;
++itr;
2018-11-14 12:23:08 +00:00
}
return nullptr;
}
2018-11-14 12:23:08 +00:00
bool
Context::ObtainNewExit(const PubKey& pk, const PathID_t& path, bool permitInternet)
{
auto itr = m_Exits.begin();
while (itr != m_Exits.end())
{
if (itr->second->AllocateNewExit(pk, path, permitInternet))
return true;
++itr;
}
return false;
}
std::shared_ptr<handlers::ExitEndpoint>
Context::GetExitEndpoint(std::string name) const
{
if (auto itr = m_Exits.find(name); itr != m_Exits.end())
{
return itr->second;
}
return nullptr;
}
void
Context::AddExitEndpoint(
const std::string& name, const NetworkConfig& networkConfig, const DnsConfig& dnsConfig)
{
if (m_Exits.find(name) != m_Exits.end())
throw std::invalid_argument{fmt::format("An exit with name {} already exists", name)};
auto endpoint = std::make_unique<handlers::ExitEndpoint>(name, router);
endpoint->Configure(networkConfig, dnsConfig);
// add endpoint
if (!endpoint->Start())
throw std::runtime_error{fmt::format("Failed to start endpoint {}", name)};
m_Exits.emplace(name, std::move(endpoint));
}
} // namespace llarp::exit