2018-08-17 19:49:58 +00:00
|
|
|
#include <llarp/handlers/tun.hpp>
|
2018-07-11 16:11:19 +00:00
|
|
|
#include <llarp/service/context.hpp>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
|
|
|
Context::Context(llarp_router *r) : m_Router(r)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Context::~Context()
|
|
|
|
{
|
|
|
|
auto itr = m_Endpoints.begin();
|
|
|
|
while(itr != m_Endpoints.end())
|
|
|
|
{
|
|
|
|
itr = m_Endpoints.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Tick()
|
|
|
|
{
|
2018-07-18 22:50:05 +00:00
|
|
|
auto now = llarp_time_now_ms();
|
2018-07-11 16:11:19 +00:00
|
|
|
auto itr = m_Endpoints.begin();
|
|
|
|
while(itr != m_Endpoints.end())
|
|
|
|
{
|
2018-07-18 22:50:05 +00:00
|
|
|
itr->second->Tick(now);
|
2018-07-11 16:11:19 +00:00
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Context::AddEndpoint(const Config::section_t &conf)
|
|
|
|
{
|
|
|
|
{
|
2018-08-23 14:07:53 +00:00
|
|
|
auto itr = m_Endpoints.find(conf.first);
|
|
|
|
if(itr != m_Endpoints.end())
|
|
|
|
{
|
|
|
|
llarp::LogError("cannot add hidden service with duplicate name: ",
|
|
|
|
conf.first);
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-11 16:11:19 +00:00
|
|
|
}
|
2018-08-23 14:07:53 +00:00
|
|
|
// extract type
|
|
|
|
std::string endpointType = "tun";
|
|
|
|
for(const auto &option : conf.second)
|
|
|
|
{
|
|
|
|
if(option.first == "type")
|
|
|
|
endpointType = option.second;
|
|
|
|
}
|
|
|
|
std::unique_ptr< llarp::service::Endpoint > service;
|
|
|
|
|
|
|
|
static std::map< std::string,
|
|
|
|
std::function< llarp::service::Endpoint *(
|
|
|
|
const std::string &, llarp_router *) > >
|
|
|
|
endpointConstructors = {
|
|
|
|
{"tun",
|
|
|
|
[](const std::string &nick,
|
|
|
|
llarp_router *r) -> llarp::service::Endpoint * {
|
|
|
|
return new llarp::handlers::TunEndpoint(nick, r);
|
|
|
|
}},
|
|
|
|
{"null",
|
|
|
|
[](const std::string &nick,
|
|
|
|
llarp_router *r) -> llarp::service::Endpoint * {
|
|
|
|
return new llarp::service::Endpoint(nick, r);
|
|
|
|
}}};
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2018-08-23 14:07:53 +00:00
|
|
|
{
|
|
|
|
// detect type
|
|
|
|
auto itr = endpointConstructors.find(endpointType);
|
|
|
|
if(itr == endpointConstructors.end())
|
|
|
|
{
|
|
|
|
llarp::LogError("no such endpoint type: ", endpointType);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct
|
|
|
|
service = std::unique_ptr< llarp::service::Endpoint >(
|
|
|
|
itr->second(conf.first, m_Router));
|
|
|
|
}
|
|
|
|
// configure
|
2018-07-11 16:11:19 +00:00
|
|
|
for(const auto &option : conf.second)
|
|
|
|
{
|
|
|
|
auto &k = option.first;
|
2018-08-23 14:07:53 +00:00
|
|
|
if(k == "type")
|
|
|
|
continue;
|
2018-07-11 16:11:19 +00:00
|
|
|
auto &v = option.second;
|
|
|
|
if(!service->SetOption(k, v))
|
|
|
|
{
|
2018-07-12 13:43:37 +00:00
|
|
|
llarp::LogError("failed to set ", k, "=", v,
|
|
|
|
" for hidden service endpoint ", conf.first);
|
2018-07-11 16:11:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 14:07:53 +00:00
|
|
|
// start
|
2018-07-11 16:11:19 +00:00
|
|
|
if(service->Start())
|
|
|
|
{
|
2018-07-29 23:29:36 +00:00
|
|
|
llarp::LogInfo("added hidden service endpoint ", service->Name());
|
2018-08-17 19:49:58 +00:00
|
|
|
m_Endpoints.insert(std::make_pair(conf.first, std::move(service)));
|
2018-07-11 16:11:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
llarp::LogError("failed to start hidden service endpoint ", conf.first);
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-17 04:37:50 +00:00
|
|
|
} // namespace service
|
2018-08-17 19:49:58 +00:00
|
|
|
} // namespace llarp
|