2018-12-12 01:12:59 +00:00
|
|
|
#include <handlers/exit.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <net.hpp>
|
2018-12-12 01:12:59 +00:00
|
|
|
#include <router.hpp>
|
|
|
|
#include <str.hpp>
|
2018-12-13 16:14:44 +00:00
|
|
|
#include <dns/dns.hpp>
|
2018-11-15 21:47:05 +00:00
|
|
|
#include <cassert>
|
2018-11-12 16:43:40 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace handlers
|
|
|
|
{
|
2018-11-15 13:13:19 +00:00
|
|
|
static void
|
2018-11-23 14:37:26 +00:00
|
|
|
ExitHandlerRecvPkt(llarp_tun_io *tun, llarp_buffer_t buf)
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2018-11-23 14:37:26 +00:00
|
|
|
static_cast< ExitEndpoint * >(tun->user)->OnInetPacket(buf);
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
2018-12-13 16:14:44 +00:00
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
static void
|
2018-11-28 16:38:20 +00:00
|
|
|
ExitHandlerFlush(llarp_tun_io *tun)
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2018-11-28 16:38:20 +00:00
|
|
|
static_cast< ExitEndpoint * >(tun->user)->Flush();
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
ExitEndpoint::ExitEndpoint(const std::string &name, llarp::Router *r)
|
2018-11-15 13:13:19 +00:00
|
|
|
: m_Router(r)
|
2018-12-13 16:14:44 +00:00
|
|
|
, m_Resolver(r->netloop, this)
|
2018-11-15 13:13:19 +00:00
|
|
|
, m_Name(name)
|
2018-11-26 22:46:22 +00:00
|
|
|
, m_Tun{{0}, 0, {0}, 0, 0, 0, 0, 0, 0, 0}
|
2018-12-13 16:14:44 +00:00
|
|
|
, m_LocalResolverAddr("127.0.0.1", 53)
|
2018-11-15 13:13:19 +00:00
|
|
|
, m_InetToNetwork(name + "_exit_rx", r->netloop, r->netloop)
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2018-11-15 21:47:05 +00:00
|
|
|
m_Tun.user = this;
|
|
|
|
m_Tun.recvpkt = &ExitHandlerRecvPkt;
|
2018-11-28 16:38:20 +00:00
|
|
|
m_Tun.tick = &ExitHandlerFlush;
|
2018-11-15 21:47:05 +00:00
|
|
|
m_ShouldInitTun = true;
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ExitEndpoint::~ExitEndpoint()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
bool
|
|
|
|
ExitEndpoint::ShouldHookDNSMessage(const llarp::dns::Message &msg) const
|
|
|
|
{
|
|
|
|
if(msg.questions.size() == 0)
|
|
|
|
return false;
|
|
|
|
if(msg.questions[0].qtype == llarp::dns::qTypePTR)
|
|
|
|
{
|
|
|
|
llarp::huint32_t ip;
|
|
|
|
if(!llarp::dns::DecodePTR(msg.questions[0].qname, ip))
|
|
|
|
return false;
|
|
|
|
return m_OurRange.Contains(ip);
|
|
|
|
}
|
|
|
|
else if(msg.questions[0].qtype == llarp::dns::qTypeA)
|
|
|
|
{
|
|
|
|
return msg.questions[0].qname.find(".snode.")
|
|
|
|
== (msg.questions[0].qname.size() - 7);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ExitEndpoint::HandleHookedDNSMessage(
|
|
|
|
llarp::dns::Message msg,
|
|
|
|
std::function< void(llarp::dns::Message) > reply)
|
|
|
|
{
|
|
|
|
if(msg.questions[0].qtype == llarp::dns::qTypePTR)
|
|
|
|
{
|
|
|
|
llarp::huint32_t ip;
|
|
|
|
if(!llarp::dns::DecodePTR(msg.questions[0].qname, ip))
|
|
|
|
return false;
|
|
|
|
if(ip == m_IfAddr)
|
|
|
|
{
|
|
|
|
RouterID us = Router()->pubkey();
|
|
|
|
msg.AddAReply(us.ToString(), 300);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto itr = m_IPToKey.find(ip);
|
|
|
|
if(itr != m_IPToKey.end()
|
|
|
|
&& m_SNodeKeys.find(itr->second) != m_SNodeKeys.end())
|
|
|
|
{
|
|
|
|
RouterID them = itr->second;
|
|
|
|
msg.AddAReply(them.ToString());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(msg.questions[0].qtype == llarp::dns::qTypeA)
|
|
|
|
{
|
|
|
|
// forward dns for snode
|
|
|
|
RouterID r;
|
|
|
|
if(r.FromString(msg.questions[0].qname))
|
|
|
|
{
|
|
|
|
huint32_t ip;
|
|
|
|
if(m_SNodeKeys.find(r.data()) == m_SNodeKeys.end())
|
|
|
|
{
|
|
|
|
// we do not have it mapped
|
|
|
|
// map it
|
|
|
|
ip = ObtainServiceNodeIP(r);
|
|
|
|
msg.AddINReply(ip);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we have it mapped already as a service node
|
|
|
|
auto itr = m_KeyToIP.find(r.data());
|
|
|
|
if(itr != m_KeyToIP.end())
|
|
|
|
{
|
|
|
|
ip = itr->second;
|
|
|
|
msg.AddINReply(ip);
|
|
|
|
}
|
|
|
|
else // fallback case that should never happen (probably)
|
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
|
|
|
reply(msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-02 18:07:07 +00:00
|
|
|
llarp_time_t
|
2018-11-28 12:32:38 +00:00
|
|
|
ExitEndpoint::Now() const
|
|
|
|
{
|
|
|
|
return m_Router->Now();
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
void
|
2018-11-28 16:38:20 +00:00
|
|
|
ExitEndpoint::Flush()
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2018-11-28 12:32:38 +00:00
|
|
|
auto now = Now();
|
2018-11-17 18:40:13 +00:00
|
|
|
m_InetToNetwork.Process([&](Pkt_t &pkt) {
|
2018-11-15 13:13:19 +00:00
|
|
|
llarp::PubKey pk;
|
|
|
|
{
|
|
|
|
auto itr = m_IPToKey.find(pkt.dst());
|
|
|
|
if(itr == m_IPToKey.end())
|
|
|
|
{
|
|
|
|
// drop
|
|
|
|
llarp::LogWarn(Name(), " dropping packet, has no session at ",
|
|
|
|
pkt.dst());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pk = itr->second;
|
|
|
|
}
|
2018-12-13 16:14:44 +00:00
|
|
|
// check if this key is a service node
|
|
|
|
if(m_SNodeKeys.find(pk) != m_SNodeKeys.end())
|
|
|
|
{
|
|
|
|
// check if it's a service node session we made and queue it via our
|
|
|
|
// snode session that we made otherwise use an inbound session that
|
|
|
|
// was made by the other service node
|
|
|
|
auto itr = m_SNodeSessions.find(pk);
|
|
|
|
if(itr != m_SNodeSessions.end())
|
|
|
|
{
|
|
|
|
if(itr->second->QueueUpstreamTraffic(pkt,
|
|
|
|
llarp::routing::ExitPadSize))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-11-15 13:13:19 +00:00
|
|
|
llarp::exit::Endpoint *ep = nullptr;
|
|
|
|
auto range = m_ActiveExits.equal_range(pk);
|
|
|
|
auto itr = range.first;
|
|
|
|
uint64_t min = std::numeric_limits< uint64_t >::max();
|
2018-11-28 12:32:38 +00:00
|
|
|
/// pick non dead looking path with lowest tx rate
|
2018-11-15 13:13:19 +00:00
|
|
|
while(itr != range.second)
|
|
|
|
{
|
2018-11-28 12:32:38 +00:00
|
|
|
if(itr->second->TxRate() < min && !itr->second->LooksDead(now))
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2018-11-15 21:47:05 +00:00
|
|
|
ep = itr->second.get();
|
2018-11-28 12:32:38 +00:00
|
|
|
min = ep->TxRate();
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
2018-12-02 18:07:07 +00:00
|
|
|
|
2018-11-28 12:32:38 +00:00
|
|
|
if(ep == nullptr)
|
|
|
|
{
|
|
|
|
// we may have all dead sessions, wtf now?
|
2018-12-02 18:07:07 +00:00
|
|
|
llarp::LogWarn(Name(), " dropped inbound traffic for session ", pk,
|
|
|
|
" as we have no working endpoints");
|
2018-11-28 12:32:38 +00:00
|
|
|
}
|
|
|
|
else
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2018-11-28 16:38:20 +00:00
|
|
|
if(!ep->QueueInboundTraffic(pkt.Buffer()))
|
2018-11-15 23:07:39 +00:00
|
|
|
{
|
2018-12-02 18:07:07 +00:00
|
|
|
llarp::LogWarn(Name(), " dropped inbound traffic for session ", pk,
|
|
|
|
" as we are overloaded (probably)");
|
2018-11-15 23:07:39 +00:00
|
|
|
}
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
});
|
2018-11-28 16:38:20 +00:00
|
|
|
{
|
2018-12-13 16:14:44 +00:00
|
|
|
auto itr = m_ActiveExits.begin();
|
|
|
|
while(itr != m_ActiveExits.end())
|
2018-11-28 16:38:20 +00:00
|
|
|
{
|
2018-12-13 16:14:44 +00:00
|
|
|
if(!itr->second->Flush())
|
|
|
|
{
|
|
|
|
llarp::LogWarn("exit session with ", itr->first,
|
|
|
|
" dropped packets");
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto itr = m_SNodeSessions.begin();
|
|
|
|
while(itr != m_SNodeSessions.end())
|
|
|
|
{
|
|
|
|
if(!itr->second->FlushUpstreamTraffic())
|
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to flushsnode traffic to ", itr->first,
|
|
|
|
" via outbound session");
|
|
|
|
}
|
|
|
|
++itr;
|
2018-11-28 16:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ExitEndpoint::Start()
|
|
|
|
{
|
2018-11-15 21:47:05 +00:00
|
|
|
if(m_ShouldInitTun)
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
|
|
|
if(!llarp_ev_add_tun(Router()->netloop, &m_Tun))
|
|
|
|
return false;
|
|
|
|
if(m_UpstreamResolvers.size() == 0)
|
|
|
|
m_UpstreamResolvers.emplace_back("8.8.8.8", 53);
|
|
|
|
return m_Resolver.Start(m_LocalResolverAddr, m_UpstreamResolvers);
|
|
|
|
}
|
2018-11-15 21:47:05 +00:00
|
|
|
return true;
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::Router *
|
2018-11-15 13:13:19 +00:00
|
|
|
ExitEndpoint::Router()
|
|
|
|
{
|
|
|
|
return m_Router;
|
|
|
|
}
|
|
|
|
|
2018-12-11 00:53:11 +00:00
|
|
|
llarp::Crypto *
|
2018-11-15 13:13:19 +00:00
|
|
|
ExitEndpoint::Crypto()
|
|
|
|
{
|
|
|
|
return &m_Router->crypto;
|
|
|
|
}
|
|
|
|
|
|
|
|
huint32_t
|
|
|
|
ExitEndpoint::GetIfAddr() const
|
|
|
|
{
|
|
|
|
return m_IfAddr;
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:19:24 +00:00
|
|
|
bool
|
|
|
|
ExitEndpoint::HasLocalMappedAddrFor(const llarp::PubKey &pk) const
|
|
|
|
{
|
2018-11-15 21:47:05 +00:00
|
|
|
return m_KeyToIP.find(pk) != m_KeyToIP.end();
|
2018-11-15 16:19:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
huint32_t
|
2018-11-15 14:44:57 +00:00
|
|
|
ExitEndpoint::GetIPForIdent(const llarp::PubKey pk)
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2018-11-15 16:15:25 +00:00
|
|
|
huint32_t found = {0};
|
2018-11-15 16:19:24 +00:00
|
|
|
if(!HasLocalMappedAddrFor(pk))
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
|
|
|
// allocate and map
|
2018-11-15 21:47:05 +00:00
|
|
|
found.h = AllocateNewAddress().h;
|
2018-11-15 16:00:16 +00:00
|
|
|
if(!m_KeyToIP.emplace(pk, found).second)
|
|
|
|
{
|
|
|
|
llarp::LogError(Name(), "failed to map ", pk, " to ", found);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
if(!m_IPToKey.emplace(found, pk).second)
|
|
|
|
{
|
|
|
|
llarp::LogError(Name(), "failed to map ", found, " to ", pk);
|
|
|
|
return found;
|
|
|
|
}
|
2018-11-15 16:19:24 +00:00
|
|
|
if(HasLocalMappedAddrFor(pk))
|
2018-11-15 15:43:58 +00:00
|
|
|
llarp::LogInfo(Name(), " mapping ", pk, " to ", found);
|
|
|
|
else
|
|
|
|
llarp::LogError(Name(), "failed to map ", pk, " to ", found);
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
2018-11-15 16:12:05 +00:00
|
|
|
else
|
2018-11-15 16:15:25 +00:00
|
|
|
found.h = m_KeyToIP[pk].h;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
|
|
|
MarkIPActive(found);
|
2018-11-15 21:47:05 +00:00
|
|
|
m_KeyToIP.rehash(0);
|
|
|
|
assert(HasLocalMappedAddrFor(pk));
|
2018-11-15 13:13:19 +00:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
huint32_t
|
|
|
|
ExitEndpoint::AllocateNewAddress()
|
|
|
|
{
|
|
|
|
if(m_NextAddr < m_HigestAddr)
|
|
|
|
return ++m_NextAddr;
|
|
|
|
|
|
|
|
// find oldest activity ip address
|
|
|
|
huint32_t found = {0};
|
|
|
|
llarp_time_t min = std::numeric_limits< llarp_time_t >::max();
|
|
|
|
auto itr = m_IPActivity.begin();
|
|
|
|
while(itr != m_IPActivity.end())
|
|
|
|
{
|
|
|
|
if(itr->second < min)
|
|
|
|
{
|
2018-11-15 18:10:09 +00:00
|
|
|
found.h = itr->first.h;
|
|
|
|
min = itr->second;
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
// kick old ident off exit
|
|
|
|
// TODO: DoS
|
|
|
|
llarp::PubKey pk = m_IPToKey[found];
|
|
|
|
KickIdentOffExit(pk);
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ExitEndpoint::QueueOutboundTraffic(llarp_buffer_t buf)
|
|
|
|
{
|
2018-11-23 14:37:26 +00:00
|
|
|
return llarp_ev_tun_async_write(&m_Tun, buf);
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExitEndpoint::KickIdentOffExit(const llarp::PubKey &pk)
|
|
|
|
{
|
|
|
|
llarp::LogInfo(Name(), " kicking ", pk, " off exit");
|
|
|
|
huint32_t ip = m_KeyToIP[pk];
|
|
|
|
m_KeyToIP.erase(pk);
|
|
|
|
m_IPToKey.erase(ip);
|
|
|
|
auto range = m_ActiveExits.equal_range(pk);
|
|
|
|
auto exit_itr = range.first;
|
|
|
|
while(exit_itr != range.second)
|
|
|
|
exit_itr = m_ActiveExits.erase(exit_itr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExitEndpoint::MarkIPActive(llarp::huint32_t ip)
|
|
|
|
{
|
|
|
|
m_IPActivity[ip] = Router()->Now();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExitEndpoint::OnInetPacket(llarp_buffer_t buf)
|
|
|
|
{
|
|
|
|
m_InetToNetwork.EmplaceIf(
|
|
|
|
[buf](Pkt_t &pkt) -> bool { return pkt.Load(buf); });
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
bool
|
|
|
|
ExitEndpoint::QueueSNodePacket(llarp_buffer_t buf, llarp::huint32_t from)
|
|
|
|
{
|
|
|
|
llarp::net::IPv4Packet pkt;
|
|
|
|
if(!pkt.Load(buf))
|
|
|
|
return false;
|
|
|
|
// rewrite ip
|
|
|
|
pkt.UpdateIPv4PacketOnDst(from, m_IfAddr);
|
|
|
|
return llarp_ev_tun_async_write(&m_Tun, pkt.Buffer());
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
llarp::exit::Endpoint *
|
|
|
|
ExitEndpoint::FindEndpointByPath(const llarp::PathID_t &path)
|
|
|
|
{
|
|
|
|
llarp::exit::Endpoint *endpoint = nullptr;
|
|
|
|
llarp::PubKey pk;
|
|
|
|
{
|
|
|
|
auto itr = m_Paths.find(path);
|
|
|
|
if(itr == m_Paths.end())
|
|
|
|
return nullptr;
|
|
|
|
pk = itr->second;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto itr = m_ActiveExits.find(pk);
|
|
|
|
if(itr != m_ActiveExits.end())
|
|
|
|
{
|
2018-11-15 21:47:05 +00:00
|
|
|
if(itr->second->PubKey() == pk)
|
|
|
|
endpoint = itr->second.get();
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ExitEndpoint::UpdateEndpointPath(const llarp::PubKey &remote,
|
|
|
|
const llarp::PathID_t &next)
|
|
|
|
{
|
|
|
|
// check if already mapped
|
|
|
|
auto itr = m_Paths.find(next);
|
|
|
|
if(itr != m_Paths.end())
|
|
|
|
return false;
|
|
|
|
m_Paths.insert(std::make_pair(next, remote));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
bool
|
|
|
|
ExitEndpoint::SetOption(const std::string &k, const std::string &v)
|
|
|
|
{
|
2018-11-15 21:47:05 +00:00
|
|
|
if(k == "type" && v == "null")
|
|
|
|
{
|
|
|
|
m_ShouldInitTun = false;
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-12 16:43:40 +00:00
|
|
|
if(k == "exit")
|
|
|
|
{
|
2018-11-14 12:23:08 +00:00
|
|
|
m_PermitExit = IsTrueValue(v.c_str());
|
2018-11-12 16:43:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-13 16:14:44 +00:00
|
|
|
if(k == "local-dns")
|
|
|
|
{
|
|
|
|
std::string resolverAddr = v;
|
|
|
|
uint16_t dnsport = 53;
|
|
|
|
auto pos = v.find(":");
|
|
|
|
if(pos != std::string::npos)
|
|
|
|
{
|
|
|
|
resolverAddr = v.substr(0, pos);
|
|
|
|
dnsport = std::atoi(v.substr(pos + 1).c_str());
|
|
|
|
}
|
|
|
|
m_LocalResolverAddr = llarp::Addr(resolverAddr, dnsport);
|
|
|
|
llarp::LogInfo(Name(), " local dns set to ", m_LocalResolverAddr);
|
|
|
|
}
|
|
|
|
if(k == "upstream-dns")
|
|
|
|
{
|
|
|
|
std::string resolverAddr = v;
|
|
|
|
uint16_t dnsport = 53;
|
|
|
|
auto pos = v.find(":");
|
|
|
|
if(pos != std::string::npos)
|
|
|
|
{
|
|
|
|
resolverAddr = v.substr(0, pos);
|
|
|
|
dnsport = std::atoi(v.substr(pos + 1).c_str());
|
|
|
|
}
|
|
|
|
m_UpstreamResolvers.emplace_back(resolverAddr, dnsport);
|
|
|
|
llarp::LogInfo(Name(), "adding upstream dns set to ", resolverAddr, ":",
|
|
|
|
dnsport);
|
|
|
|
}
|
2018-11-15 13:13:19 +00:00
|
|
|
if(k == "ifaddr")
|
|
|
|
{
|
|
|
|
auto pos = v.find("/");
|
|
|
|
if(pos == std::string::npos)
|
|
|
|
{
|
|
|
|
llarp::LogError(Name(), " ifaddr is not a cidr: ", v);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string nmask_str = v.substr(1 + pos);
|
|
|
|
std::string host_str = v.substr(0, pos);
|
2018-11-20 00:44:24 +00:00
|
|
|
// string, or just a plain char array?
|
2018-11-20 08:18:29 +00:00
|
|
|
strncpy(m_Tun.ifaddr, host_str.c_str(), sizeof(m_Tun.ifaddr) - 1);
|
2018-11-15 13:13:19 +00:00
|
|
|
m_Tun.netmask = std::atoi(nmask_str.c_str());
|
2018-11-15 13:47:46 +00:00
|
|
|
|
|
|
|
llarp::Addr ifaddr(host_str);
|
2018-12-13 16:14:44 +00:00
|
|
|
m_IfAddr = ifaddr.xtohl();
|
|
|
|
m_OurRange.netmask_bits = netmask_ipv4_bits(m_Tun.netmask);
|
|
|
|
m_OurRange.addr = m_IfAddr;
|
|
|
|
m_NextAddr = m_IfAddr;
|
|
|
|
m_HigestAddr = m_IfAddr | (~m_OurRange.netmask_bits);
|
2018-11-15 13:13:19 +00:00
|
|
|
llarp::LogInfo(Name(), " set ifaddr range to ", m_Tun.ifaddr, "/",
|
2018-11-15 13:47:46 +00:00
|
|
|
m_Tun.netmask, " lo=", m_IfAddr, " hi=", m_HigestAddr);
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
if(k == "ifname")
|
|
|
|
{
|
2018-11-20 08:18:29 +00:00
|
|
|
strncpy(m_Tun.ifname, v.c_str(), sizeof(m_Tun.ifname) - 1);
|
2018-11-15 13:13:19 +00:00
|
|
|
llarp::LogInfo(Name(), " set ifname to ", m_Tun.ifname);
|
|
|
|
}
|
2018-11-12 16:43:40 +00:00
|
|
|
if(k == "exit-whitelist")
|
|
|
|
{
|
|
|
|
// add exit policy whitelist rule
|
|
|
|
// TODO: implement me
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(k == "exit-blacklist")
|
|
|
|
{
|
|
|
|
// add exit policy blacklist rule
|
|
|
|
// TODO: implement me
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-15 13:13:19 +00:00
|
|
|
|
|
|
|
return true;
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
huint32_t
|
|
|
|
ExitEndpoint::ObtainServiceNodeIP(const llarp::RouterID &other)
|
|
|
|
{
|
|
|
|
huint32_t ip = GetIPForIdent(other.data());
|
|
|
|
if(m_SNodeKeys.insert(other.data()).second)
|
|
|
|
{
|
|
|
|
// this is a new service node make an outbound session to them
|
|
|
|
m_SNodeSessions.insert(
|
|
|
|
std::make_pair(other,
|
|
|
|
std::unique_ptr< llarp::exit::SNodeSession >(
|
|
|
|
new llarp::exit::SNodeSession(
|
|
|
|
other,
|
|
|
|
std::bind(&ExitEndpoint::QueueSNodePacket,
|
|
|
|
this, std::placeholders::_1, ip),
|
|
|
|
Router(), 2, 1, true))));
|
|
|
|
}
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
bool
|
2018-11-15 15:46:50 +00:00
|
|
|
ExitEndpoint::AllocateNewExit(const llarp::PubKey pk,
|
2018-11-14 12:23:08 +00:00
|
|
|
const llarp::PathID_t &path,
|
2018-11-15 13:13:19 +00:00
|
|
|
bool wantInternet)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
2018-11-15 13:13:19 +00:00
|
|
|
if(wantInternet && !m_PermitExit)
|
|
|
|
return false;
|
|
|
|
huint32_t ip = GetIPForIdent(pk);
|
2018-12-13 16:14:44 +00:00
|
|
|
if(Router()->paths.TransitHopPreviousIsRouter(path, pk.data()))
|
|
|
|
{
|
|
|
|
// we think this path belongs to a service node
|
|
|
|
// mark it as such so we don't make an outbound session to them
|
|
|
|
m_SNodeKeys.insert(pk.data());
|
|
|
|
}
|
2018-11-22 00:39:09 +00:00
|
|
|
m_ActiveExits.insert(
|
2018-11-26 15:54:52 +00:00
|
|
|
std::make_pair(pk,
|
2018-11-22 00:39:09 +00:00
|
|
|
std::make_unique< llarp::exit::Endpoint >(
|
|
|
|
pk, path, !wantInternet, ip, this)));
|
2018-12-13 16:14:44 +00:00
|
|
|
|
2018-11-15 14:08:42 +00:00
|
|
|
m_Paths[path] = pk;
|
2018-11-15 21:47:05 +00:00
|
|
|
return HasLocalMappedAddrFor(pk);
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
std::string
|
|
|
|
ExitEndpoint::Name() const
|
|
|
|
{
|
|
|
|
return m_Name;
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
void
|
2018-11-15 22:10:12 +00:00
|
|
|
ExitEndpoint::DelEndpointInfo(const llarp::PathID_t &path)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
m_Paths.erase(path);
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
void
|
|
|
|
ExitEndpoint::RemoveExit(const llarp::exit::Endpoint *ep)
|
|
|
|
{
|
|
|
|
auto range = m_ActiveExits.equal_range(ep->PubKey());
|
|
|
|
auto itr = range.first;
|
|
|
|
while(itr != range.second)
|
|
|
|
{
|
2018-11-15 21:47:05 +00:00
|
|
|
if(itr->second->LocalPath() == ep->LocalPath())
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
|
|
|
itr = m_ActiveExits.erase(itr);
|
|
|
|
// now ep is gone af
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
void
|
|
|
|
ExitEndpoint::Tick(llarp_time_t now)
|
|
|
|
{
|
|
|
|
{
|
2018-12-13 16:14:44 +00:00
|
|
|
auto itr = m_SNodeSessions.begin();
|
|
|
|
while(itr != m_SNodeSessions.end())
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2018-12-13 16:14:44 +00:00
|
|
|
if(itr->second->IsExpired(now))
|
|
|
|
itr = m_SNodeSessions.erase(itr);
|
|
|
|
else
|
|
|
|
++itr;
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
auto itr = m_ActiveExits.begin();
|
|
|
|
while(itr != m_ActiveExits.end())
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2018-12-13 16:14:44 +00:00
|
|
|
if(itr->second->IsExpired(now))
|
|
|
|
{
|
|
|
|
itr = m_ActiveExits.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
itr->second->Tick(now);
|
|
|
|
++itr;
|
|
|
|
}
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace handlers
|
2018-11-21 23:37:17 +00:00
|
|
|
} // namespace llarp
|