2018-12-12 01:06:46 +00:00
|
|
|
#include <exit/context.hpp>
|
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
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
2019-07-30 23:42:13 +00:00
|
|
|
Context::~Context() = default;
|
2018-11-12 16:43:40 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Context::Tick(llarp_time_t now)
|
2018-12-24 16:09:05 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
auto itr = m_Exits.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_Exits.end())
|
2018-12-24 16:09:05 +00:00
|
|
|
{
|
|
|
|
itr->second->Tick(now);
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto itr = m_Closed.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_Closed.end())
|
2018-12-24 16:09:05 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if ((*itr)->ShouldRemove())
|
2018-12-24 16:09:05 +00:00
|
|
|
itr = m_Closed.erase(itr);
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Stop()
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
|
|
|
auto itr = m_Exits.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_Exits.end())
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
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();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_Exits.end())
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2019-08-19 09:33:26 +00:00
|
|
|
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();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_Exits.end())
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
|
|
|
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();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_Exits.end())
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
auto ep = itr->second->FindEndpointByPath(path);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ep)
|
2018-11-14 12:23:08 +00:00
|
|
|
return ep;
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Context::ObtainNewExit(const PubKey& pk, const PathID_t& path, bool permitInternet)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
auto itr = m_Exits.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_Exits.end())
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second->AllocateNewExit(pk, path, permitInternet))
|
2018-11-14 12:23:08 +00:00
|
|
|
return true;
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:22:04 +00:00
|
|
|
void
|
2020-04-27 15:24:05 +00:00
|
|
|
Context::AddExitEndpoint(
|
|
|
|
const std::string& name, const NetworkConfig& networkConfig, const DnsConfig& dnsConfig)
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2020-04-28 14:22:04 +00:00
|
|
|
if (m_Exits.find(name) != m_Exits.end())
|
|
|
|
throw std::invalid_argument(stringify("An exit with name ", name, " already exists"));
|
|
|
|
|
|
|
|
auto endpoint = std::make_unique<handlers::ExitEndpoint>(name, m_Router);
|
|
|
|
endpoint->Configure(networkConfig, dnsConfig);
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
// add endpoint
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!endpoint->Start())
|
2020-04-28 14:22:04 +00:00
|
|
|
throw std::runtime_error(stringify("Failed to start endpoint ", name));
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
m_Exits.emplace(name, std::move(endpoint));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace exit
|
2018-12-10 16:26:46 +00:00
|
|
|
} // namespace llarp
|