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 = _exits.begin();
while (itr != _exits.end())
{
itr->second->Tick(now);
++itr;
}
}
{
auto itr = _closed.begin();
while (itr != _closed.end())
{
if ((*itr)->ShouldRemove())
itr = _closed.erase(itr);
else
++itr;
}
}
}
void
Context::stop()
{
auto itr = _exits.begin();
while (itr != _exits.end())
2019-02-08 19:43:25 +00:00
{
itr->second->Stop();
_closed.emplace_back(std::move(itr->second));
itr = _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 = _exits.begin();
while (itr != _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::calculate_exit_traffic(TrafficStats& stats)
{
auto itr = _exits.begin();
while (itr != _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::find_endpoint_for_path(const PathID_t& path) const
{
auto itr = _exits.begin();
while (itr != _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::obtain_new_exit(const PubKey& pk, const PathID_t& path, bool permitInternet)
{
auto itr = _exits.begin();
while (itr != _exits.end())
{
if (itr->second->AllocateNewExit(pk, path, permitInternet))
return true;
++itr;
}
return false;
}
std::shared_ptr<handlers::ExitEndpoint>
Context::get_exit_endpoint(std::string name) const
{
if (auto itr = _exits.find(name); itr != _exits.end())
{
return itr->second;
}
return nullptr;
}
void
Context::add_exit_endpoint(
const std::string& name, const NetworkConfig& networkConfig, const DnsConfig& dnsConfig)
{
if (_exits.find(name) != _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)};
_exits.emplace(name, std::move(endpoint));
}
} // namespace llarp::exit