lokinet/llarp/handlers/exit.hpp

221 lines
5.3 KiB
C++
Raw Normal View History

#pragma once
#include <llarp/exit/endpoint.hpp>
#include "tun.hpp"
#include <llarp/dns/server.hpp>
#include <unordered_map>
namespace llarp
{
struct AbstractRouter;
namespace handlers
{
2021-03-26 13:16:43 +00:00
struct ExitEndpoint : public dns::IQueryHandler, public EndpointBase
{
ExitEndpoint(std::string name, AbstractRouter* r);
2019-07-30 23:42:13 +00:00
~ExitEndpoint() override;
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
LookupNameAsync(
std::string name,
std::function<void(std::optional<AddressVariant_t>)> resultHandler) override;
const EventLoop_ptr&
Loop() override;
bool
SendToOrQueue(
service::ConvoTag tag, const llarp_buffer_t& payload, service::ProtocolType t) override;
void
Tick(llarp_time_t now);
void
Configure(const NetworkConfig& networkConfig, const DnsConfig& dnsConfig);
std::string
Name() const;
2018-11-14 19:53:03 +00:00
bool
2021-03-26 13:57:28 +00:00
VisitEndpointsFor(const PubKey& pk, std::function<bool(exit::Endpoint* const)> visit) const;
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;
bool
ShouldHookDNSMessage(const dns::Message& msg) const override;
bool
HandleHookedDNSMessage(dns::Message msg, std::function<void(dns::Message)>) override;
2018-11-14 12:23:08 +00:00
bool
AllocateNewExit(const PubKey pk, const PathID_t& path, bool permitInternet);
2018-11-14 12:23:08 +00:00
exit::Endpoint*
FindEndpointByPath(const PathID_t& path);
2018-11-14 12:23:08 +00:00
exit::Endpoint*
FindEndpointByIP(huint32_t ip);
2018-11-14 12:23:08 +00:00
bool
UpdateEndpointPath(const PubKey& remote, const PathID_t& next);
2018-11-14 12:23:08 +00:00
/// handle ip packet from outside
void
OnInetPacket(net::IPPacket buf);
AbstractRouter*
GetRouter();
2018-11-28 12:32:38 +00:00
llarp_time_t
Now() const;
template <typename Stats>
2018-11-14 18:02:27 +00:00
void
CalculateTrafficStats(Stats& stats)
{
auto itr = m_ActiveExits.begin();
while (itr != m_ActiveExits.end())
2018-11-14 18:02:27 +00:00
{
2018-11-15 21:47:05 +00:00
stats[itr->first].first += itr->second->TxRate();
stats[itr->first].second += itr->second->RxRate();
2018-11-14 18:02:27 +00:00
++itr;
}
}
/// DO NOT CALL ME
2018-11-14 12:23:08 +00:00
void
DelEndpointInfo(const PathID_t& path);
2018-11-14 12:23:08 +00:00
/// DO NOT CALL ME
2018-11-14 18:02:27 +00:00
void
RemoveExit(const exit::Endpoint* ep);
2018-11-14 18:02:27 +00:00
bool
QueueOutboundTraffic(net::IPPacket pkt);
AddressVariant_t
LocalAddress() const override;
std::unordered_map<AddressVariant_t, SendStat>
GetStatistics() const override;
/// sets up networking and starts traffic
bool
Start();
bool
Stop();
bool
ShouldRemove() const;
2018-11-15 16:19:24 +00:00
bool
HasLocalMappedAddrFor(const PubKey& pk) const;
2018-11-15 16:19:24 +00:00
2019-06-11 16:44:05 +00:00
huint128_t
GetIfAddr() const;
void
2018-11-28 16:38:20 +00:00
Flush();
2021-03-26 13:16:43 +00:00
quic::TunnelManager*
GetQUICTunnel() override;
2021-03-26 13:16:43 +00:00
2019-06-11 16:44:05 +00:00
huint128_t
GetIPForIdent(const PubKey pk);
/// async obtain snode session and call callback when it's ready to send
void
ObtainSNodeSession(const RouterID& router, exit::SessionReadyFunc obtainCb);
private:
2019-06-11 16:44:05 +00:00
huint128_t
AllocateNewAddress();
/// 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
ObtainServiceNodeIP(const RouterID& router);
bool
2019-06-11 16:44:05 +00:00
QueueSNodePacket(const llarp_buffer_t& buf, huint128_t from);
void
2019-06-11 16:44:05 +00:00
MarkIPActive(huint128_t ip);
void
KickIdentOffExit(const PubKey& pk);
AbstractRouter* m_Router;
std::shared_ptr<dns::Proxy> m_Resolver;
2018-11-15 21:47:05 +00:00
bool m_ShouldInitTun;
std::string m_Name;
2018-11-14 12:23:08 +00:00
bool m_PermitExit;
std::unordered_map<PathID_t, PubKey> m_Paths;
2018-12-23 13:29:11 +00:00
std::unordered_map<PubKey, exit::Endpoint*> m_ChosenExits;
2018-12-23 13:29:11 +00:00
std::unordered_multimap<PubKey, std::unique_ptr<exit::Endpoint>> m_ActiveExits;
using KeyMap_t = std::unordered_map<PubKey, huint128_t>;
2018-11-15 16:05:31 +00:00
KeyMap_t m_KeyToIP;
using SNodes_t = std::set<PubKey>;
/// set of pubkeys we treat as snodes
SNodes_t m_SNodeKeys;
using SNodeSessions_t = std::unordered_map<RouterID, std::shared_ptr<exit::SNodeSession>>;
/// snode sessions we are talking to directly
SNodeSessions_t m_SNodeSessions;
std::unordered_map<huint128_t, PubKey> m_IPToKey;
2019-06-11 16:44:05 +00:00
huint128_t m_IfAddr;
huint128_t m_HigestAddr;
huint128_t m_NextAddr;
IPRange m_OurRange;
std::string m_ifname;
std::unordered_map<huint128_t, llarp_time_t> m_IPActivity;
std::shared_ptr<vpn::NetworkInterface> m_NetIf;
2020-05-06 20:38:44 +00:00
IpAddress m_LocalResolverAddr;
std::vector<IpAddress> m_UpstreamResolvers;
2021-03-26 13:16:43 +00:00
std::shared_ptr<quic::TunnelManager> m_QUIC;
2019-06-11 16:44:05 +00:00
using Pkt_t = net::IPPacket;
using PacketQueue_t = util::CoDelQueue<
Pkt_t,
Pkt_t::GetTime,
Pkt_t::PutTime,
Pkt_t::CompareOrder,
Pkt_t::GetNow,
util::NullMutex,
util::NullLock>;
/// internet to llarp packet queue
PacketQueue_t m_InetToNetwork;
2019-06-11 19:42:11 +00:00
bool m_UseV6;
};
} // namespace handlers
} // namespace llarp