2021-03-09 22:24:35 +00:00
|
|
|
#include "context.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <memory>
|
2020-04-28 14:22:04 +00:00
|
|
|
#include <stdexcept>
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
namespace llarp::exit
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
Context::Context(Router* r) : router(r)
|
|
|
|
{}
|
|
|
|
Context::~Context() = default;
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
void
|
|
|
|
Context::Tick(llarp_time_t now)
|
|
|
|
{
|
2018-12-24 16:09:05 +00:00
|
|
|
{
|
2023-12-06 21:54:51 +00:00
|
|
|
auto itr = _exits.begin();
|
|
|
|
while (itr != _exits.end())
|
2018-12-24 16:09:05 +00:00
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
itr->second->Tick(now);
|
|
|
|
++itr;
|
2018-12-24 16:09:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2023-12-06 21:54:51 +00:00
|
|
|
auto itr = _closed.begin();
|
|
|
|
while (itr != _closed.end())
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
if ((*itr)->ShouldRemove())
|
2023-12-06 21:54:51 +00:00
|
|
|
itr = _closed.erase(itr);
|
2023-09-15 14:55:32 +00:00
|
|
|
else
|
|
|
|
++itr;
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-15 14:55:32 +00:00
|
|
|
}
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
void
|
2023-12-06 21:54:51 +00:00
|
|
|
Context::stop()
|
2023-09-15 14:55:32 +00:00
|
|
|
{
|
2023-12-06 21:54:51 +00:00
|
|
|
auto itr = _exits.begin();
|
|
|
|
while (itr != _exits.end())
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
itr->second->Stop();
|
2023-12-06 21:54:51 +00:00
|
|
|
_closed.emplace_back(std::move(itr->second));
|
|
|
|
itr = _exits.erase(itr);
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
2023-09-15 14:55:32 +00:00
|
|
|
}
|
2019-02-08 19:43:25 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
util::StatusObject
|
|
|
|
Context::ExtractStatus() const
|
|
|
|
{
|
|
|
|
util::StatusObject obj{};
|
2023-12-06 21:54:51 +00:00
|
|
|
auto itr = _exits.begin();
|
|
|
|
while (itr != _exits.end())
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
obj[itr->first] = itr->second->ExtractStatus();
|
|
|
|
++itr;
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
2023-09-15 14:55:32 +00:00
|
|
|
return obj;
|
|
|
|
}
|
2018-11-14 18:02:27 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
void
|
2023-12-06 21:54:51 +00:00
|
|
|
Context::calculate_exit_traffic(TrafficStats& stats)
|
2023-09-15 14:55:32 +00:00
|
|
|
{
|
2023-12-06 21:54:51 +00:00
|
|
|
auto itr = _exits.begin();
|
|
|
|
while (itr != _exits.end())
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
itr->second->CalculateTrafficStats(stats);
|
|
|
|
++itr;
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
2023-09-15 14:55:32 +00:00
|
|
|
}
|
2018-11-14 12:23:08 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
exit::Endpoint*
|
2023-12-06 21:54:51 +00:00
|
|
|
Context::find_endpoint_for_path(const PathID_t& path) const
|
2023-09-15 14:55:32 +00:00
|
|
|
{
|
2023-12-06 21:54:51 +00:00
|
|
|
auto itr = _exits.begin();
|
|
|
|
while (itr != _exits.end())
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
auto ep = itr->second->FindEndpointByPath(path);
|
|
|
|
if (ep)
|
|
|
|
return ep;
|
|
|
|
++itr;
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
2023-09-15 14:55:32 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-14 12:23:08 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
bool
|
2023-12-06 21:54:51 +00:00
|
|
|
Context::obtain_new_exit(const PubKey& pk, const PathID_t& path, bool permitInternet)
|
2023-09-15 14:55:32 +00:00
|
|
|
{
|
2023-12-06 21:54:51 +00:00
|
|
|
auto itr = _exits.begin();
|
|
|
|
while (itr != _exits.end())
|
2021-03-26 20:45:19 +00:00
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
if (itr->second->AllocateNewExit(pk, path, permitInternet))
|
|
|
|
return true;
|
|
|
|
++itr;
|
2021-03-26 20:45:19 +00:00
|
|
|
}
|
2023-09-15 14:55:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-26 20:45:19 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
std::shared_ptr<handlers::ExitEndpoint>
|
2023-12-06 21:54:51 +00:00
|
|
|
Context::get_exit_endpoint(std::string name) const
|
2023-09-15 14:55:32 +00:00
|
|
|
{
|
2023-12-06 21:54:51 +00:00
|
|
|
if (auto itr = _exits.find(name); itr != _exits.end())
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-04-28 14:22:04 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
void
|
2023-12-06 21:54:51 +00:00
|
|
|
Context::add_exit_endpoint(
|
2023-09-15 14:55:32 +00:00
|
|
|
const std::string& name, const NetworkConfig& networkConfig, const DnsConfig& dnsConfig)
|
|
|
|
{
|
2023-12-06 21:54:51 +00:00
|
|
|
if (_exits.find(name) != _exits.end())
|
2023-09-15 14:55:32 +00:00
|
|
|
throw std::invalid_argument{fmt::format("An exit with name {} already exists", name)};
|
2020-04-28 14:22:04 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
auto endpoint = std::make_unique<handlers::ExitEndpoint>(name, router);
|
|
|
|
endpoint->Configure(networkConfig, dnsConfig);
|
2020-04-28 14:22:04 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
// add endpoint
|
|
|
|
if (!endpoint->Start())
|
|
|
|
throw std::runtime_error{fmt::format("Failed to start endpoint {}", name)};
|
|
|
|
|
2023-12-06 21:54:51 +00:00
|
|
|
_exits.emplace(name, std::move(endpoint));
|
2023-09-15 14:55:32 +00:00
|
|
|
}
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
} // namespace llarp::exit
|