2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "tun.hpp"
|
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/dns/server.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <llarp/exit/endpoint.hpp>
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <unordered_map>
|
2023-10-19 21:59:57 +00:00
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
struct Router;
|
2018-11-12 16:43:40 +00:00
|
|
|
namespace handlers
|
|
|
|
{
|
2022-04-07 20:44:23 +00:00
|
|
|
struct ExitEndpoint : public dns::Resolver_Base, public EndpointBase
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2022-04-07 20:44:23 +00:00
|
|
|
int
|
|
|
|
Rank() const override
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string_view
|
|
|
|
ResolverName() const override
|
|
|
|
{
|
|
|
|
return "snode";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MaybeHookDNS(
|
2022-07-28 16:07:38 +00:00
|
|
|
std::shared_ptr<dns::PacketSource_Base> source,
|
2022-04-07 20:44:23 +00:00
|
|
|
const dns::Message& query,
|
|
|
|
const SockAddr& to,
|
|
|
|
const SockAddr& from) override;
|
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
ExitEndpoint(std::string name, Router* r);
|
2019-07-30 23:42:13 +00:00
|
|
|
~ExitEndpoint() override;
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2021-03-26 13:16:43 +00:00
|
|
|
std::optional<AddressVariant_t>
|
|
|
|
GetEndpointWithConvoTag(service::ConvoTag tag) const override;
|
|
|
|
|
|
|
|
std::optional<service::ConvoTag>
|
|
|
|
GetBestConvoTagFor(AddressVariant_t addr) const override;
|
|
|
|
|
|
|
|
bool
|
|
|
|
EnsurePathTo(
|
|
|
|
AddressVariant_t addr,
|
|
|
|
std::function<void(std::optional<service::ConvoTag>)> hook,
|
|
|
|
llarp_time_t timeout) override;
|
|
|
|
|
|
|
|
void
|
2023-10-10 20:49:53 +00:00
|
|
|
lookup_name(std::string name, std::function<void(oxen::quic::message)> func) override;
|
2021-03-26 13:16:43 +00:00
|
|
|
|
|
|
|
const EventLoop_ptr&
|
|
|
|
Loop() override;
|
|
|
|
|
2021-04-06 12:25:46 +00:00
|
|
|
std::unordered_set<EndpointBase::AddressVariant_t>
|
2023-10-10 20:49:53 +00:00
|
|
|
AllRemoteEndpoints() const override;
|
2021-04-06 12:25:46 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
SRVRecordsChanged() override;
|
|
|
|
|
2023-10-10 20:49:53 +00:00
|
|
|
void MarkAddressOutbound(service::Address) override{};
|
2021-06-05 13:06:17 +00:00
|
|
|
|
2021-03-26 13:16:43 +00:00
|
|
|
bool
|
2023-10-12 20:37:45 +00:00
|
|
|
send_to(service::ConvoTag tag, std::string payload) override;
|
2021-03-26 13:16:43 +00:00
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
void
|
2018-11-15 13:13:19 +00:00
|
|
|
Tick(llarp_time_t now);
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2020-04-28 14:22:04 +00:00
|
|
|
void
|
|
|
|
Configure(const NetworkConfig& networkConfig, const DnsConfig& dnsConfig);
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2018-11-19 13:39:35 +00:00
|
|
|
std::string
|
2018-11-15 13:13:19 +00:00
|
|
|
Name() const;
|
2018-11-14 19:53:03 +00:00
|
|
|
|
2019-05-07 16:07:44 +00:00
|
|
|
bool
|
2021-03-26 13:57:28 +00:00
|
|
|
VisitEndpointsFor(const PubKey& pk, std::function<bool(exit::Endpoint* const)> visit) const;
|
2019-05-07 16:07:44 +00:00
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
2019-04-19 15:10:26 +00:00
|
|
|
ExtractStatus() const;
|
2019-02-08 19:43:25 +00:00
|
|
|
|
2019-06-11 19:42:11 +00:00
|
|
|
bool
|
|
|
|
SupportsV6() const;
|
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
bool
|
2022-04-07 20:44:23 +00:00
|
|
|
ShouldHookDNSMessage(const dns::Message& msg) const;
|
2018-12-13 16:14:44 +00:00
|
|
|
|
|
|
|
bool
|
2022-04-07 20:44:23 +00:00
|
|
|
HandleHookedDNSMessage(dns::Message msg, std::function<void(dns::Message)>);
|
2018-12-13 16:14:44 +00:00
|
|
|
|
2021-04-12 11:39:07 +00:00
|
|
|
void
|
|
|
|
LookupServiceAsync(
|
|
|
|
std::string name,
|
|
|
|
std::string service,
|
|
|
|
std::function<void(std::vector<dns::SRVData>)> handler) override;
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
AllocateNewExit(const PubKey pk, const PathID_t& path, bool permitInternet);
|
2018-11-14 12:23:08 +00:00
|
|
|
|
2019-01-07 22:15:31 +00:00
|
|
|
exit::Endpoint*
|
|
|
|
FindEndpointByPath(const PathID_t& path);
|
2018-11-14 12:23:08 +00:00
|
|
|
|
2019-01-07 22:15:31 +00:00
|
|
|
exit::Endpoint*
|
2018-11-15 13:13:19 +00:00
|
|
|
FindEndpointByIP(huint32_t ip);
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
bool
|
2019-01-07 22:15:31 +00:00
|
|
|
UpdateEndpointPath(const PubKey& remote, const PathID_t& next);
|
2018-11-14 12:23:08 +00:00
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
/// handle ip packet from outside
|
|
|
|
void
|
2021-01-11 23:13:22 +00:00
|
|
|
OnInetPacket(net::IPPacket buf);
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
Router*
|
2019-01-07 22:15:31 +00:00
|
|
|
GetRouter();
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2018-11-28 12:32:38 +00:00
|
|
|
llarp_time_t
|
|
|
|
Now() const;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Stats>
|
2018-11-14 18:02:27 +00:00
|
|
|
void
|
|
|
|
CalculateTrafficStats(Stats& stats)
|
|
|
|
{
|
2023-09-15 14:55:32 +00:00
|
|
|
for (auto& [pubkey, endpoint] : active_exits)
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2021-11-10 01:03:36 +00:00
|
|
|
stats[pubkey].first += endpoint->TxRate();
|
|
|
|
stats[pubkey].second += endpoint->RxRate();
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
/// DO NOT CALL ME
|
2018-11-14 12:23:08 +00:00
|
|
|
void
|
2019-01-07 22:15:31 +00:00
|
|
|
DelEndpointInfo(const PathID_t& path);
|
2018-11-14 12:23:08 +00:00
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
/// DO NOT CALL ME
|
2018-11-14 18:02:27 +00:00
|
|
|
void
|
2019-01-07 22:15:31 +00:00
|
|
|
RemoveExit(const exit::Endpoint* ep);
|
2018-11-14 18:02:27 +00:00
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
bool
|
2021-01-11 23:13:22 +00:00
|
|
|
QueueOutboundTraffic(net::IPPacket pkt);
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2021-04-02 15:10:37 +00:00
|
|
|
AddressVariant_t
|
2021-03-27 18:54:09 +00:00
|
|
|
LocalAddress() const override;
|
|
|
|
|
2021-04-06 12:25:46 +00:00
|
|
|
std::optional<SendStat>
|
|
|
|
GetStatFor(AddressVariant_t remote) const override;
|
2021-04-02 15:10:37 +00:00
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
/// sets up networking and starts traffic
|
|
|
|
bool
|
|
|
|
Start();
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
bool
|
|
|
|
Stop();
|
|
|
|
|
|
|
|
bool
|
|
|
|
ShouldRemove() const;
|
|
|
|
|
2018-11-15 16:19:24 +00:00
|
|
|
bool
|
2019-01-07 22:15:31 +00:00
|
|
|
HasLocalMappedAddrFor(const PubKey& pk) const;
|
2018-11-15 16:19:24 +00:00
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2018-11-15 13:13:19 +00:00
|
|
|
GetIfAddr() const;
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
void
|
2018-11-28 16:38:20 +00:00
|
|
|
Flush();
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2023-10-19 11:49:46 +00:00
|
|
|
link::TunnelManager*
|
2021-03-26 20:45:19 +00:00
|
|
|
GetQUICTunnel() override;
|
2021-03-26 13:16:43 +00:00
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2019-01-07 22:15:31 +00:00
|
|
|
GetIPForIdent(const PubKey pk);
|
2021-03-26 20:45:19 +00:00
|
|
|
/// async obtain snode session and call callback when it's ready to send
|
|
|
|
void
|
2023-09-15 14:55:32 +00:00
|
|
|
ObtainSNodeSession(const RouterID& rid, exit::SessionReadyFunc obtain_cb);
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2021-03-26 20:45:19 +00:00
|
|
|
private:
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2018-11-15 13:13:19 +00:00
|
|
|
AllocateNewAddress();
|
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
/// obtain ip for service node session, creates a new session if one does
|
|
|
|
/// not existing already
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2019-01-07 22:15:31 +00:00
|
|
|
ObtainServiceNodeIP(const RouterID& router);
|
2018-12-13 16:14:44 +00:00
|
|
|
|
|
|
|
bool
|
2019-06-11 16:44:05 +00:00
|
|
|
QueueSNodePacket(const llarp_buffer_t& buf, huint128_t from);
|
2018-12-13 16:14:44 +00:00
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
void
|
2019-06-11 16:44:05 +00:00
|
|
|
MarkIPActive(huint128_t ip);
|
2018-11-15 13:13:19 +00:00
|
|
|
|
|
|
|
void
|
2019-01-07 22:15:31 +00:00
|
|
|
KickIdentOffExit(const PubKey& pk);
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
Router* router;
|
|
|
|
std::shared_ptr<dns::Server> resolver;
|
|
|
|
bool should_init_tun;
|
|
|
|
std::string name;
|
|
|
|
bool permit_exit;
|
|
|
|
std::unordered_map<PathID_t, PubKey> paths;
|
2018-12-23 13:29:11 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
std::unordered_map<PubKey, exit::Endpoint*> chosen_exits;
|
2018-12-23 13:29:11 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
std::unordered_multimap<PubKey, std::unique_ptr<exit::Endpoint>> active_exits;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
std::unordered_map<PubKey, huint128_t> key_to_IP;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
using SNodes_t = std::set<PubKey>;
|
2018-12-13 16:14:44 +00:00
|
|
|
/// set of pubkeys we treat as snodes
|
2023-09-15 14:55:32 +00:00
|
|
|
SNodes_t snode_keys;
|
2018-12-13 16:14:44 +00:00
|
|
|
|
2021-03-09 18:39:40 +00:00
|
|
|
using SNodeSessions_t = std::unordered_map<RouterID, std::shared_ptr<exit::SNodeSession>>;
|
2018-12-13 16:14:44 +00:00
|
|
|
/// snode sessions we are talking to directly
|
2023-09-15 14:55:32 +00:00
|
|
|
SNodeSessions_t snode_sessions;
|
2018-12-13 16:14:44 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
std::unordered_map<huint128_t, PubKey> ip_to_key;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
huint128_t if_addr;
|
|
|
|
huint128_t highest_addr;
|
2019-06-11 16:44:05 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
huint128_t next_addr;
|
|
|
|
IPRange ip_range;
|
|
|
|
std::string if_name;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
std::unordered_map<huint128_t, llarp_time_t> ip_activity;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
std::shared_ptr<vpn::NetworkInterface> if_net;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
SockAddr resolver_addr;
|
|
|
|
std::vector<SockAddr> upstream_resolvers;
|
2018-12-13 16:14:44 +00:00
|
|
|
|
2023-10-19 21:59:57 +00:00
|
|
|
// std::shared_ptr<link::TunnelManager> tunnel_manager;
|
2021-03-26 13:16:43 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
using PacketQueue_t = std::
|
|
|
|
priority_queue<net::IPPacket, std::vector<net::IPPacket>, net::IPPacket::CompareOrder>;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
|
|
|
/// internet to llarp packet queue
|
2023-09-15 14:55:32 +00:00
|
|
|
PacketQueue_t inet_to_network;
|
|
|
|
bool use_ipv6;
|
|
|
|
DnsConfig dns_conf;
|
2018-11-12 16:43:40 +00:00
|
|
|
};
|
|
|
|
} // namespace handlers
|
|
|
|
} // namespace llarp
|