2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "message.hpp"
|
|
|
|
#include <llarp/ev/ev.hpp>
|
|
|
|
#include <llarp/net/net.hpp>
|
|
|
|
#include "unbound_resolver.hpp"
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dns
|
|
|
|
{
|
|
|
|
/// handler of dns query hooking
|
2021-03-02 02:06:20 +00:00
|
|
|
class IQueryHandler
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2021-03-02 02:06:20 +00:00
|
|
|
public:
|
2019-07-30 23:42:13 +00:00
|
|
|
virtual ~IQueryHandler() = default;
|
2018-12-13 22:20:12 +00:00
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
/// return true if we should hook this message
|
|
|
|
virtual bool
|
|
|
|
ShouldHookDNSMessage(const Message& msg) const = 0;
|
|
|
|
|
|
|
|
/// handle a hooked message
|
|
|
|
virtual bool
|
2020-04-07 18:38:56 +00:00
|
|
|
HandleHookedDNSMessage(Message query, std::function<void(Message)> sendReply) = 0;
|
2018-12-03 22:22:59 +00:00
|
|
|
};
|
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
// Base class for DNS lookups
|
|
|
|
class PacketHandler : public std::enable_shared_from_this<PacketHandler>
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2021-03-02 02:06:20 +00:00
|
|
|
public:
|
2021-03-02 07:02:59 +00:00
|
|
|
explicit PacketHandler(EventLoop_ptr loop, IQueryHandler* handler);
|
2021-03-02 18:18:22 +00:00
|
|
|
|
|
|
|
virtual ~PacketHandler() = default;
|
|
|
|
|
|
|
|
virtual bool
|
2021-06-09 10:38:11 +00:00
|
|
|
Start(
|
|
|
|
SockAddr localaddr,
|
|
|
|
std::vector<SockAddr> upstreamResolvers,
|
|
|
|
std::vector<fs::path> hostfiles);
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Stop();
|
|
|
|
|
2021-02-05 21:48:57 +00:00
|
|
|
void
|
|
|
|
Restart();
|
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
void
|
2021-03-02 02:06:20 +00:00
|
|
|
HandlePacket(const SockAddr& resolver, const SockAddr& from, llarp_buffer_t buf);
|
2019-11-01 13:40:31 +00:00
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
bool
|
2021-03-02 02:06:20 +00:00
|
|
|
ShouldHandlePacket(const SockAddr& to, const SockAddr& from, llarp_buffer_t buf) const;
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
protected:
|
|
|
|
virtual void
|
2021-09-01 15:03:39 +00:00
|
|
|
SendServerMessageBufferTo(const SockAddr& to, const SockAddr& from, llarp_buffer_t buf) = 0;
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2021-09-01 18:10:08 +00:00
|
|
|
// Returns true if this packet is something that looks like it's going to an upstream
|
|
|
|
// resolver, i.e. matches a configured resolver.
|
2021-09-01 18:31:45 +00:00
|
|
|
virtual bool
|
|
|
|
IsUpstreamResolver(const SockAddr& to, const SockAddr& from) const;
|
2021-09-01 18:10:08 +00:00
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
private:
|
2018-12-03 22:22:59 +00:00
|
|
|
void
|
2021-03-02 02:06:20 +00:00
|
|
|
HandleUpstreamFailure(const SockAddr& from, const SockAddr& to, Message msg);
|
2018-12-04 16:35:25 +00:00
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
bool
|
2021-06-09 10:38:11 +00:00
|
|
|
SetupUnboundResolver(std::vector<SockAddr> resolvers, std::vector<fs::path> hostfiles);
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
IQueryHandler* const m_QueryHandler;
|
2021-05-30 17:29:00 +00:00
|
|
|
std::set<SockAddr> m_Resolvers;
|
2021-03-02 18:18:22 +00:00
|
|
|
std::shared_ptr<UnboundResolver> m_UnboundResolver;
|
2021-03-02 07:02:59 +00:00
|
|
|
EventLoop_ptr m_Loop;
|
2021-03-02 18:18:22 +00:00
|
|
|
};
|
2019-05-22 16:20:03 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
// Proxying DNS handler that listens on a UDP port for proper DNS requests.
|
|
|
|
class Proxy : public PacketHandler
|
2021-03-02 18:18:22 +00:00
|
|
|
{
|
2021-03-02 02:06:20 +00:00
|
|
|
public:
|
2021-03-02 07:02:59 +00:00
|
|
|
explicit Proxy(EventLoop_ptr loop, IQueryHandler* handler);
|
2020-06-15 18:15:10 +00:00
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
bool
|
2021-06-09 10:38:11 +00:00
|
|
|
Start(
|
|
|
|
SockAddr localaddr,
|
|
|
|
std::vector<SockAddr> upstreamResolvers,
|
|
|
|
std::vector<fs::path> hostfiles) override;
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
protected:
|
2020-06-15 18:15:10 +00:00
|
|
|
void
|
2021-03-02 02:06:20 +00:00
|
|
|
SendServerMessageBufferTo(
|
2021-09-01 15:03:39 +00:00
|
|
|
const SockAddr& to, const SockAddr& from, llarp_buffer_t buf) override;
|
2020-06-09 16:59:49 +00:00
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
private:
|
2021-03-02 02:06:20 +00:00
|
|
|
std::shared_ptr<UDPHandle> m_Server;
|
|
|
|
EventLoop_ptr m_Loop;
|
2018-12-03 22:22:59 +00:00
|
|
|
};
|
|
|
|
} // namespace dns
|
|
|
|
} // namespace llarp
|