2021-08-30 17:55:33 +00:00
|
|
|
#include "route_manager.hpp"
|
2021-09-01 18:10:08 +00:00
|
|
|
#include <llarp/handlers/tun.hpp>
|
|
|
|
#include <llarp/service/context.hpp>
|
|
|
|
#include <llarp.hpp>
|
|
|
|
#include <memory>
|
2021-08-30 17:55:33 +00:00
|
|
|
|
2021-09-01 18:31:45 +00:00
|
|
|
namespace llarp::apple
|
|
|
|
{
|
|
|
|
void
|
|
|
|
RouteManager::check_trampoline(bool enable)
|
|
|
|
{
|
|
|
|
if (trampoline_active == enable)
|
|
|
|
return;
|
|
|
|
auto router = context.router;
|
|
|
|
if (!router)
|
|
|
|
{
|
|
|
|
LogError("Cannot reconfigure to use DNS trampoline: no router");
|
|
|
|
return;
|
|
|
|
}
|
2021-09-01 18:10:08 +00:00
|
|
|
|
2021-09-01 18:31:45 +00:00
|
|
|
std::shared_ptr<llarp::handlers::TunEndpoint> tun;
|
2022-08-30 22:01:52 +00:00
|
|
|
router->hiddenServiceContext().ForEachService([&tun](const auto& /*name*/, const auto ep) {
|
2021-09-01 18:10:08 +00:00
|
|
|
tun = std::dynamic_pointer_cast<llarp::handlers::TunEndpoint>(ep);
|
|
|
|
return !tun;
|
2021-09-01 18:31:45 +00:00
|
|
|
});
|
2021-09-01 18:10:08 +00:00
|
|
|
|
2021-09-01 18:31:45 +00:00
|
|
|
if (!tun)
|
|
|
|
{
|
|
|
|
LogError("Cannot reconfigure to use DNS trampoline: no tun endpoint found (!?)");
|
|
|
|
return;
|
|
|
|
}
|
2021-09-01 18:10:08 +00:00
|
|
|
|
2021-09-01 18:31:45 +00:00
|
|
|
if (enable)
|
2022-09-16 23:55:59 +00:00
|
|
|
tun->ReconfigureDNS({SockAddr{127, 0, 0, 1, {dns_trampoline_port}}});
|
2021-09-01 18:31:45 +00:00
|
|
|
else
|
2022-09-16 23:55:59 +00:00
|
|
|
tun->ReconfigureDNS(router->GetConfig()->dns.m_upstreamDNS);
|
|
|
|
|
2021-09-01 18:31:45 +00:00
|
|
|
trampoline_active = enable;
|
|
|
|
}
|
2021-09-01 18:10:08 +00:00
|
|
|
|
2022-09-19 15:22:28 +00:00
|
|
|
void
|
|
|
|
RouteManager::AddDefaultRouteViaInterface(vpn::NetworkInterface&)
|
2021-09-01 18:31:45 +00:00
|
|
|
{
|
|
|
|
check_trampoline(true);
|
2021-08-30 17:55:33 +00:00
|
|
|
if (callback_context and route_callbacks.add_default_route)
|
2021-09-01 18:31:45 +00:00
|
|
|
route_callbacks.add_default_route(callback_context);
|
|
|
|
}
|
2021-08-30 17:55:33 +00:00
|
|
|
|
2022-09-19 15:22:28 +00:00
|
|
|
void
|
|
|
|
RouteManager::DelDefaultRouteViaInterface(vpn::NetworkInterface&)
|
2021-09-01 18:31:45 +00:00
|
|
|
{
|
|
|
|
check_trampoline(false);
|
2021-08-30 17:55:33 +00:00
|
|
|
if (callback_context and route_callbacks.del_default_route)
|
2021-09-01 18:31:45 +00:00
|
|
|
route_callbacks.del_default_route(callback_context);
|
|
|
|
}
|
2021-08-30 17:55:33 +00:00
|
|
|
|
2021-09-01 18:31:45 +00:00
|
|
|
void
|
|
|
|
RouteManager::AddRouteViaInterface(vpn::NetworkInterface&, IPRange range)
|
|
|
|
{
|
|
|
|
check_trampoline(true);
|
2021-08-30 17:55:33 +00:00
|
|
|
if (callback_context)
|
|
|
|
{
|
2021-09-01 18:31:45 +00:00
|
|
|
if (range.IsV4())
|
|
|
|
{
|
|
|
|
if (route_callbacks.add_ipv4_route)
|
|
|
|
route_callbacks.add_ipv4_route(
|
|
|
|
range.BaseAddressString().c_str(),
|
|
|
|
net::TruncateV6(range.netmask_bits).ToString().c_str(),
|
|
|
|
callback_context);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (route_callbacks.add_ipv6_route)
|
|
|
|
route_callbacks.add_ipv6_route(
|
|
|
|
range.BaseAddressString().c_str(), range.HostmaskBits(), callback_context);
|
|
|
|
}
|
2021-08-30 17:55:33 +00:00
|
|
|
}
|
2021-09-01 18:31:45 +00:00
|
|
|
}
|
2021-08-30 17:55:33 +00:00
|
|
|
|
2021-09-01 18:31:45 +00:00
|
|
|
void
|
|
|
|
RouteManager::DelRouteViaInterface(vpn::NetworkInterface&, IPRange range)
|
|
|
|
{
|
|
|
|
check_trampoline(false);
|
2021-08-30 17:55:33 +00:00
|
|
|
if (callback_context)
|
|
|
|
{
|
2021-09-01 18:31:45 +00:00
|
|
|
if (range.IsV4())
|
|
|
|
{
|
|
|
|
if (route_callbacks.del_ipv4_route)
|
|
|
|
route_callbacks.del_ipv4_route(
|
|
|
|
range.BaseAddressString().c_str(),
|
|
|
|
net::TruncateV6(range.netmask_bits).ToString().c_str(),
|
|
|
|
callback_context);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (route_callbacks.del_ipv6_route)
|
|
|
|
route_callbacks.del_ipv6_route(
|
|
|
|
range.BaseAddressString().c_str(), range.HostmaskBits(), callback_context);
|
|
|
|
}
|
2021-08-30 17:55:33 +00:00
|
|
|
}
|
2021-09-01 18:31:45 +00:00
|
|
|
}
|
2021-08-30 17:55:33 +00:00
|
|
|
|
2021-09-01 18:31:45 +00:00
|
|
|
} // namespace llarp::apple
|