mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-11-17 15:25:35 +00:00
4c630e0437
- Previous android java and jni code updated to work, but with much love still needed to make it work nicely, e.g. handling when the VPN is turned off. - DNS handling refactored to allow android to intercept and handle DNS requests as we can't set the system DNS to use a high port (and apparently Chrome ignores system DNS settings anyway) - add packet router structure to allow separate handling of specific intercepted traffic, e.g. UDP traffic to port 53 gets handled by our DNS handler rather than being naively forwarded as exit traffic. - For now, android lokinet is exit-only and hard-coded to use exit.loki as its exit. The exit will be configurable before release, but allowing to not use exit-only mode is more of a challenge. - some old gitignore remnants which were matching to things we don't want them to (and are no longer relevant) removed - some minor changes to CI configuration
153 lines
3.2 KiB
C++
153 lines
3.2 KiB
C++
#include <dns/unbound_resolver.hpp>
|
|
|
|
#include <dns/server.hpp>
|
|
#include <util/buffer.hpp>
|
|
|
|
namespace llarp::dns
|
|
{
|
|
struct PendingUnboundLookup
|
|
{
|
|
std::weak_ptr<UnboundResolver> resolver;
|
|
Message msg;
|
|
SockAddr source;
|
|
SockAddr replyFrom;
|
|
};
|
|
|
|
void
|
|
UnboundResolver::Stop()
|
|
{
|
|
Reset();
|
|
}
|
|
|
|
void
|
|
UnboundResolver::Reset()
|
|
{
|
|
started = false;
|
|
if (runner)
|
|
{
|
|
runner->join();
|
|
runner.reset();
|
|
}
|
|
if (unboundContext)
|
|
{
|
|
ub_ctx_delete(unboundContext);
|
|
}
|
|
unboundContext = nullptr;
|
|
}
|
|
|
|
UnboundResolver::UnboundResolver(
|
|
std::shared_ptr<Logic> logic, ReplyFunction reply, FailFunction fail)
|
|
: unboundContext(nullptr)
|
|
, started(false)
|
|
, replyFunc([logic, reply](auto res, auto source, auto buf) {
|
|
LogicCall(logic, [source, buf, reply, res]() { reply(res, source, buf); });
|
|
})
|
|
, failFunc([logic, fail](auto res, auto source, auto message) {
|
|
LogicCall(logic, [source, message, res, fail]() { fail(res, source, message); });
|
|
})
|
|
{}
|
|
|
|
// static callback
|
|
void
|
|
UnboundResolver::Callback(void* data, int err, ub_result* result)
|
|
{
|
|
std::unique_ptr<PendingUnboundLookup> lookup{static_cast<PendingUnboundLookup*>(data)};
|
|
|
|
auto this_ptr = lookup->resolver.lock();
|
|
if (not this_ptr)
|
|
return; // resolver is gone, so we don't reply.
|
|
|
|
if (err != 0)
|
|
{
|
|
Message& msg = lookup->msg;
|
|
msg.AddServFail();
|
|
this_ptr->failFunc(lookup->replyFrom, lookup->source, msg);
|
|
ub_resolve_free(result);
|
|
return;
|
|
}
|
|
std::vector<byte_t> pkt(result->answer_len);
|
|
std::copy_n(static_cast<byte_t*>(result->answer_packet), pkt.size(), pkt.data());
|
|
llarp_buffer_t buf(pkt);
|
|
|
|
MessageHeader hdr;
|
|
hdr.Decode(&buf);
|
|
hdr.id = lookup->msg.hdr_id;
|
|
|
|
buf.cur = buf.base;
|
|
hdr.Encode(&buf);
|
|
|
|
this_ptr->replyFunc(lookup->replyFrom, lookup->source, std::move(pkt));
|
|
|
|
ub_resolve_free(result);
|
|
}
|
|
|
|
bool
|
|
UnboundResolver::Init()
|
|
{
|
|
if (started)
|
|
{
|
|
Reset();
|
|
}
|
|
|
|
unboundContext = ub_ctx_create();
|
|
|
|
if (not unboundContext)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
ub_ctx_async(unboundContext, 1);
|
|
runner = std::make_unique<std::thread>([&]() {
|
|
while (started)
|
|
{
|
|
if (unboundContext)
|
|
ub_wait(unboundContext);
|
|
std::this_thread::sleep_for(25ms);
|
|
}
|
|
});
|
|
started = true;
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
UnboundResolver::AddUpstreamResolver(const std::string& upstreamResolverIP)
|
|
{
|
|
if (ub_ctx_set_fwd(unboundContext, upstreamResolverIP.c_str()) != 0)
|
|
{
|
|
Reset();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void
|
|
UnboundResolver::Lookup(SockAddr to, SockAddr from, Message msg)
|
|
{
|
|
if (not unboundContext)
|
|
{
|
|
msg.AddServFail();
|
|
failFunc(to, from, std::move(msg));
|
|
return;
|
|
}
|
|
|
|
const auto& q = msg.questions[0];
|
|
auto* lookup = new PendingUnboundLookup{weak_from_this(), msg, from, to};
|
|
int err = ub_resolve_async(
|
|
unboundContext,
|
|
q.Name().c_str(),
|
|
q.qtype,
|
|
q.qclass,
|
|
(void*)lookup,
|
|
&UnboundResolver::Callback,
|
|
nullptr);
|
|
|
|
if (err != 0)
|
|
{
|
|
msg.AddServFail();
|
|
failFunc(to, from, std::move(msg));
|
|
return;
|
|
}
|
|
}
|
|
|
|
} // namespace llarp::dns
|