1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00

Merge pull request #1693 from majestrate/inane-fixings-2021-06-30

fixes for 0.9.5
This commit is contained in:
Jason Rhinelander 2021-07-05 17:24:27 -03:00 committed by GitHub
commit 75b4758037
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 160 additions and 150 deletions

View file

@ -16,7 +16,7 @@ if(CCACHE_PROGRAM)
endif()
project(lokinet
VERSION 0.9.4
VERSION 0.9.5
DESCRIPTION "lokinet - IP packet onion router"
LANGUAGES C CXX)
@ -141,7 +141,7 @@ if(LIBUV_FOUND AND NOT BUILD_STATIC_DEPS)
target_link_libraries(libuv INTERFACE PkgConfig::LIBUV)
else()
if(NOT BUILD_STATIC_DEPS)
message(FATAL_ERROR "Could not find libu >= 1.28.0; install it on your system or use -DBUILD_STATIC_DEPS=ON")
message(FATAL_ERROR "Could not find libuv >= 1.28.0; install it on your system or use -DBUILD_STATIC_DEPS=ON")
endif()
endif()

View file

@ -6,6 +6,7 @@ import sys
import time
import platform
import os
import re
from argparse import ArgumentParser as AP
is_windows = lambda : platform.system().lower() == 'windows'
@ -40,12 +41,13 @@ except ImportError:
print("for other linuxs do:")
print("\tpip3 install --user geoip")
print("for other linuxs you are responsible for obtaining your owen geoip databases, glhf")
time.sleep(1)
else:
print("install it with:")
print("\tpip3 install --user geoip")
print("")
print("press enter to continue without geoip")
sys.stdin.read(1)
print()
print("press enter to continue without geoip")
sys.stdin.read(1)
else:
try:
geoip_env_var = 'GEOIP_DB_FILE'
@ -86,6 +88,7 @@ def ip_to_flag(ip):
class Monitor:
_sample_size = 12
filter = lambda x : True
def __init__(self, url, introsetMode=False):
self.txrate = 0
@ -144,7 +147,7 @@ class Monitor:
self.win.addstr(" {} ->".format(hopstr))
self.win.addstr(" [{} ms latency]".format(path["intro"]["latency"]))
self.win.addstr(" [{} until expire]".format(self.time_to(path["expiresAt"])))
self.win.addstr(" [expires: {}]".format(self.time_to(path["expiresAt"])))
if path["expiresSoon"]:
self.win.addstr("(expiring)")
elif path["expired"]:
@ -153,13 +156,17 @@ class Monitor:
@staticmethod
def time_to(timestamp):
""" return time until timestamp in seconds formatted"""
""" return time until timestamp formatted"""
if timestamp:
unit = 'seconds'
val = (timestamp - now()) / 1000.0
if abs(val) > 60.0:
val /= 60.0
unit = 'minutes'
if val < 0:
return "{} seconds ago".format(0-val)
return "{:.2f} {} ago".format(0-val, unit)
else:
return "{} seconds".format(val)
return "in {:.2f} {}".format(val, unit)
else:
return 'never'
@ -201,15 +208,18 @@ class Monitor:
paths = status["paths"]
self.win.addstr("paths: {}".format(len(paths)))
for path in paths:
y_pos = self._render_path(y_pos, path, "inbound")
if self.filter('localhost.loki'):
y_pos = self._render_path(y_pos, path, "localhost.loki")
for session in (status["remoteSessions"] or []):
for path in session["paths"]:
y_pos = self._render_path(
y_pos, path, "[active] {}".format(session["currentConvoTag"])
)
if self.filter(session["remoteIdentity"]):
y_pos = self._render_path(
y_pos, path, "[active] {}".format(session["currentConvoTag"])
)
for session in (status["snodeSessions"] or []):
for path in session["paths"]:
y_pos = self._render_path(y_pos, path, "[snode]")
if self.filter(session["endpoint"]):
y_pos = self._render_path(y_pos, path, "[snode]")
return y_pos
def display_links(self, y_pos, data):
@ -407,18 +417,20 @@ class Monitor:
"""
y_pos += 1
self.win.move(y_pos, 1)
self.win.addstr("localhost.loki")
y_pos = self._display_our_introset(y_pos, service)
y_pos += 1
if self.filter("localhost.loki"):
self.win.addstr("localhost.loki")
y_pos = self._display_our_introset(y_pos, service)
y_pos += 1
remotes = service['remoteSessions'] or []
for session in remotes:
y_pos = self._display_session_introset(y_pos, session)
if self.filter(session['remoteIdentity']):
y_pos = self._display_session_introset(y_pos, session)
def _display_intro(self, y_pos, intro, label, paths):
y_pos += 1
self.win.move(y_pos, 1)
path = 'path' in intro and intro['path'][:4] or '????'
self.win.addstr('{}: ({}|{}) [expires in: {}] [{} paths]'.format(label, intro['router'][:8], path, self.time_to(intro['expiresAt']), self.count_endpoints_in_path(paths, intro['router'])))
self.win.addstr('{}: ({}|{}) [expires: {}] [{} paths]'.format(label, intro['router'][:8], path, self.time_to(intro['expiresAt']), self.count_endpoints_in_path(paths, intro['router'])))
return y_pos
@staticmethod
@ -457,10 +469,13 @@ class Monitor:
#print(context.keys())
y_pos += 1
self.win.move(y_pos, 1)
readyState = context['readyToSend'] and '' or '❌'
readyState = context['readyToSend'] and '' or '❌'
self.win.addstr('{} ({}) [{}]'.format(context['remoteIdentity'], context['currentConvoTag'], readyState))
y_pos += 1
self.win.move(y_pos, 1)
self.win.addstr('created: {}'.format(self.time_to(context['sessionCreatedAt'])))
y_pos += 1
self.win.move(y_pos, 1)
self.win.addstr('last good send: {}'.format(self.time_to(context['lastGoodSend'])))
y_pos += 1
self.win.move(y_pos, 1)
@ -544,6 +559,8 @@ def main():
ap.add_argument("--introset", action='store_const', const=True, default=False, help="run in introset inspection mode")
ap.add_argument("--url", default='tcp://127.0.0.1:1190', type=str, help='url to lokinet rpc')
ap.add_argument('--filter', default='.+', type=str, help="regex to filter entries")
ap.add_argument('--invert-filter', const=True, default=False, action='store_const', help='invert regex filter matching')
args = ap.parse_args()
@ -551,6 +568,10 @@ def main():
args.url,
args.introset
)
mon.filter = lambda x : re.match(args.filter, x) is not None
if args.invert_filter:
old_filter = mon.filter
mon.filter = lambda x : not old_filter(x)
mon.run()
if __name__ == "__main__":

View file

@ -46,6 +46,7 @@ namespace llarp
obj["lastExitUse"] = to_json(m_LastUse);
auto pub = m_ExitIdentity.toPublic();
obj["exitIdentity"] = pub.ToString();
obj["endpoint"] = m_ExitRouter.ToString();
return obj;
}

View file

@ -24,7 +24,7 @@ namespace llarp::iwp
keyManager, getrc, h, sign, before, est, reneg, timeout, closed, pumpDone, worker)
, m_Wakeup{ev->make_waker([this]() { HandleWakeupPlaintext(); })}
, m_PlaintextRecv{1024}
, permitInbound{allowInbound}
, m_Inbound{allowInbound}
{}
@ -34,6 +34,15 @@ namespace llarp::iwp
return "iwp";
}
std::string
LinkLayer::PrintableName() const
{
if (m_Inbound)
return "inbound iwp link";
else
return "outbound iwp link";
}
uint16_t
LinkLayer::Rank() const
{
@ -48,10 +57,10 @@ namespace llarp::iwp
bool isNewSession = false;
if (itr == m_AuthedAddrs.end())
{
Lock_t lock(m_PendingMutex);
Lock_t lock{m_PendingMutex};
if (m_Pending.count(from) == 0)
{
if (not permitInbound)
if (not m_Inbound)
return;
isNewSession = true;
m_Pending.insert({from, std::make_shared<Session>(this, from)});
@ -60,14 +69,13 @@ namespace llarp::iwp
}
else
{
Lock_t lock(m_AuthedLinksMutex);
auto range = m_AuthedLinks.equal_range(itr->second);
session = range.first->second;
if (auto s_itr = m_AuthedLinks.find(itr->second); s_itr != m_AuthedLinks.end())
session = s_itr->second;
}
if (session)
{
bool success = session->Recv_LL(std::move(pkt));
if (!success and isNewSession)
if (not success and isNewSession)
{
LogWarn("Brand new session failed; removing from pending sessions list");
m_Pending.erase(m_Pending.find(from));
@ -78,7 +86,7 @@ namespace llarp::iwp
bool
LinkLayer::MapAddr(const RouterID& r, ILinkSession* s)
{
if (!ILinkLayer::MapAddr(r, s))
if (not ILinkLayer::MapAddr(r, s))
return false;
m_AuthedAddrs.emplace(s->GetRemoteEndpoint(), r);
return true;
@ -93,6 +101,8 @@ namespace llarp::iwp
std::shared_ptr<ILinkSession>
LinkLayer::NewOutboundSession(const RouterContact& rc, const AddressInfo& ai)
{
if (m_Inbound)
throw std::logic_error{"inbound link cannot make outbound sessions"};
return std::make_shared<Session>(this, rc, ai);
}

View file

@ -56,6 +56,9 @@ namespace llarp::iwp
void
AddWakeup(std::weak_ptr<Session> peer);
std::string
PrintableName() const;
private:
void
HandleWakeupPlaintext();
@ -63,7 +66,7 @@ namespace llarp::iwp
const std::shared_ptr<EventLoopWakeup> m_Wakeup;
std::unordered_map<SockAddr, std::weak_ptr<Session>> m_PlaintextRecv;
std::unordered_map<SockAddr, RouterID> m_AuthedAddrs;
const bool permitInbound;
const bool m_Inbound;
};
using LinkLayer_ptr = std::shared_ptr<LinkLayer>;

View file

@ -91,6 +91,7 @@ namespace llarp
LogError("ident key mismatch");
return false;
}
m_RemoteRC = msg->rc;
GotLIM = util::memFn(&Session::GotRenegLIM, this);
auto self = shared_from_this();
@ -171,7 +172,7 @@ namespace llarp
m_Parent->UnmapAddr(m_RemoteAddr);
m_State = State::Closed;
EncryptAndSend(std::move(close_msg));
LogInfo("closing connection to ", m_RemoteAddr);
LogInfo(m_Parent->PrintableName(), " closing connection to ", m_RemoteAddr);
}
bool
@ -330,7 +331,9 @@ namespace llarp
if (m_State == State::Ready || m_State == State::LinkIntro)
{
return now > m_LastRX
&& now - m_LastRX > (m_Inbound ? DefaultLinkSessionLifetime : SessionAliveTimeout);
&& now - m_LastRX
> (m_Inbound and not m_RemoteRC.IsPublicRouter() ? DefaultLinkSessionLifetime
: SessionAliveTimeout);
}
return now - m_CreatedAt >= LinkLayerConnectTimeout;
}
@ -448,13 +451,15 @@ namespace llarp
{
if (not DecryptMessageInPlace(pkt))
{
LogError("failed to decrypt session request from ", m_RemoteAddr);
LogError(
m_Parent->PrintableName(), " failed to decrypt session request from ", m_RemoteAddr);
return;
}
if (pkt.size() < token.size() + PacketOverhead)
{
LogError(
"bad session request size, ",
m_Parent->PrintableName(),
" bad session request size, ",
pkt.size(),
" < ",
token.size() + PacketOverhead,
@ -465,7 +470,7 @@ namespace llarp
const auto begin = pkt.data() + PacketOverhead;
if (not std::equal(begin, begin + token.size(), token.data()))
{
LogError("token mismatch from ", m_RemoteAddr);
LogError(m_Parent->PrintableName(), " token mismatch from ", m_RemoteAddr);
return;
}
m_LastRX = m_Parent->Now();
@ -478,7 +483,7 @@ namespace llarp
{
if (pkt.size() < (Introduction::SIZE + PacketOverhead))
{
LogWarn("intro too small from ", m_RemoteAddr);
LogWarn(m_Parent->PrintableName(), " intro too small from ", m_RemoteAddr);
return;
}
byte_t* ptr = pkt.data() + PacketOverhead;
@ -495,7 +500,7 @@ namespace llarp
pkt.data() + PacketOverhead, Introduction::SIZE - Signature::SIZE);
if (!CryptoManager::instance()->verify(m_ExpectedIdent, verifybuf, Z))
{
LogError("intro verify failed from ", m_RemoteAddr);
LogError(m_Parent->PrintableName(), " intro verify failed from ", m_RemoteAddr);
return;
}
const PubKey pk = m_Parent->TransportSecretKey().toPublic();
@ -529,7 +534,8 @@ namespace llarp
if (pkt.size() < (token.size() + PacketOverhead))
{
LogError(
"bad intro ack size ",
m_Parent->PrintableName(),
" bad intro ack size ",
pkt.size(),
" < ",
token.size() + PacketOverhead,
@ -540,7 +546,7 @@ namespace llarp
Packet_t reply(token.size() + PacketOverhead);
if (not DecryptMessageInPlace(pkt))
{
LogError("intro ack decrypt failed from ", m_RemoteAddr);
LogError(m_Parent->PrintableName(), " intro ack decrypt failed from ", m_RemoteAddr);
return;
}
m_LastRX = m_Parent->Now();
@ -575,7 +581,8 @@ namespace llarp
if (H != expected)
{
LogError(
"keyed hash mismatch ",
m_Parent->PrintableName(),
" keyed hash mismatch ",
H,
" != ",
expected,

View file

@ -12,8 +12,6 @@ static constexpr auto LINK_LAYER_TICK_INTERVAL = 100ms;
namespace llarp
{
static constexpr size_t MaxSessionsPerKey = 16;
ILinkLayer::ILinkLayer(
std::shared_ptr<KeyManager> keyManager,
GetRCFunc getrc,
@ -241,7 +239,7 @@ namespace llarp
auto itr = m_Pending.find(addr);
if (itr != m_Pending.end())
{
if (m_AuthedLinks.count(pk) > MaxSessionsPerKey)
if (m_AuthedLinks.count(pk))
{
LogWarn("too many session for ", pk);
s->Close();
@ -303,21 +301,24 @@ namespace llarp
{
{
Lock_t l(m_AuthedLinksMutex);
if (m_AuthedLinks.count(rc.pubkey) >= MaxSessionsPerKey)
if (m_AuthedLinks.count(rc.pubkey))
{
LogDebug("Too many links to ", RouterID{rc.pubkey}, ", not establishing another one");
LogWarn("Too many links to ", RouterID{rc.pubkey}, ", not establishing another one");
return false;
}
}
llarp::AddressInfo to;
if (!PickAddress(rc, to))
if (not PickAddress(rc, to))
{
LogWarn("router ", RouterID{rc.pubkey}, " has no acceptable inbound addresses");
return false;
}
const SockAddr address{to};
{
Lock_t l(m_PendingMutex);
if (m_Pending.count(address) >= MaxSessionsPerKey)
if (m_Pending.count(address))
{
LogDebug(
LogWarn(
"Too many pending connections to ",
address,
" while establishing to ",
@ -331,12 +332,12 @@ namespace llarp
{
BeforeConnect(std::move(rc));
}
if (PutSession(s))
if (not PutSession(s))
{
s->Start();
return true;
return false;
}
return false;
s->Start();
return true;
}
bool

View file

@ -17,9 +17,31 @@ namespace llarp
Timeout,
RouterNotFound,
InvalidRouter,
NoLink
NoLink,
EstablishFail
};
inline std::ostream&
operator<<(std::ostream& out, const SessionResult& st)
{
switch (st)
{
case SessionResult::Establish:
return out << "success";
case SessionResult::Timeout:
return out << "timeout";
case SessionResult::NoLink:
return out << "no link";
case SessionResult::InvalidRouter:
return out << "invalid router";
case SessionResult::RouterNotFound:
return out << "not found";
case SessionResult::EstablishFail:
return out << "establish failed";
}
return out << "???";
}
using RouterCallback = std::function<void(const RouterID&, const SessionResult)>;
struct IOutboundSessionMaker

View file

@ -137,61 +137,31 @@ namespace llarp
outboundMessageQueues.emplace(zeroID, MessageQueue());
}
void
OutboundMessageHandler::OnSessionEstablished(const RouterID& router)
static inline SendStatus
ToSendStatus(const SessionResult result)
{
FinalizeSessionRequest(router, SendStatus::Success);
}
void
OutboundMessageHandler::OnConnectTimeout(const RouterID& router)
{
FinalizeSessionRequest(router, SendStatus::Timeout);
}
void
OutboundMessageHandler::OnRouterNotFound(const RouterID& router)
{
FinalizeSessionRequest(router, SendStatus::RouterNotFound);
}
void
OutboundMessageHandler::OnInvalidRouter(const RouterID& router)
{
FinalizeSessionRequest(router, SendStatus::InvalidRouter);
}
void
OutboundMessageHandler::OnNoLink(const RouterID& router)
{
FinalizeSessionRequest(router, SendStatus::NoLink);
switch (result)
{
case SessionResult::Establish:
return SendStatus::Success;
case SessionResult::Timeout:
case SessionResult::EstablishFail:
return SendStatus::Timeout;
case SessionResult::RouterNotFound:
return SendStatus::RouterNotFound;
case SessionResult::InvalidRouter:
return SendStatus::InvalidRouter;
case SessionResult::NoLink:
return SendStatus::NoLink;
}
throw std::invalid_argument{
stringify("SessionResult ", result, " has no corrispoding SendStatus when transforming")};
}
void
OutboundMessageHandler::OnSessionResult(const RouterID& router, const SessionResult result)
{
switch (result)
{
case SessionResult::Establish:
OnSessionEstablished(router);
break;
case SessionResult::Timeout:
OnConnectTimeout(router);
break;
case SessionResult::RouterNotFound:
OnRouterNotFound(router);
break;
case SessionResult::InvalidRouter:
OnInvalidRouter(router);
break;
case SessionResult::NoLink:
OnNoLink(router);
break;
default:
LogError("Impossible situation: enum class value out of bounds.");
std::abort();
break;
}
FinalizeSessionRequest(router, ToSendStatus(result));
}
void

View file

@ -114,22 +114,6 @@ namespace llarp
* the messages are dropped and their send status callbacks are invoked with
* the appropriate send status.
*/
void
OnSessionEstablished(const RouterID& router);
void
OnConnectTimeout(const RouterID& router);
void
OnRouterNotFound(const RouterID& router);
void
OnInvalidRouter(const RouterID& router);
void
OnNoLink(const RouterID& router);
void
OnSessionResult(const RouterID& router, const SessionResult result);

View file

@ -35,10 +35,10 @@ namespace llarp
OutboundSessionMaker::OnSessionEstablished(ILinkSession* session)
{
// TODO: do we want to keep it
const RouterContact rc = session->GetRemoteRC();
const auto router = RouterID(session->GetPubKey());
const bool isOutbound = not session->IsInbound();
const std::string remoteType = session->GetRemoteRC().IsPublicRouter() ? "router" : "client";
const std::string remoteType = rc.IsPublicRouter() ? "router" : "client";
LogInfo(
"session with ", remoteType, " [", router, "] ", isOutbound ? "established" : "received");
@ -48,9 +48,12 @@ namespace llarp
return false;
}
work([this, rc = session->GetRemoteRC()] { VerifyRC(rc); });
return true;
if (isOutbound)
{
work([this, rc] { VerifyRC(rc); });
return true;
}
return _rcLookup->CheckRC(rc);
}
void
@ -204,12 +207,10 @@ namespace llarp
}
const auto& job = itr->second;
if (!job->link->TryEstablishTo(job->rc))
if (not job->link->TryEstablishTo(job->rc))
{
// TODO: maybe different failure type?
l.unlock();
FinalizeRequest(router, SessionResult::NoLink);
FinalizeRequest(router, SessionResult::EstablishFail);
}
}
@ -229,7 +230,7 @@ namespace llarp
LinkLayer_ptr link = _linkManager->GetCompatibleLink(rc);
if (!link)
if (not link)
{
l.unlock();
FinalizeRequest(router, SessionResult::NoLink);
@ -244,10 +245,6 @@ namespace llarp
{
_loop->call([this, router] { DoEstablish(router); });
}
else if (_linkManager->HasSessionTo(router))
{
FinalizeRequest(router, SessionResult::Establish);
}
else
{
FinalizeRequest(router, SessionResult::NoLink);
@ -259,16 +256,18 @@ namespace llarp
{
if (router == us or not _rcLookup->SessionIsAllowed(router))
return false;
if (_linkManager->HasOutboundSessionTo(router))
return false;
if (_router->IsServiceNode())
return true;
size_t numPending = 0;
{
util::Lock lock(_mutex);
if (pendingSessions.find(router) == pendingSessions.end())
numPending += pendingSessions.size();
}
if (_linkManager->HasOutboundSessionTo(router))
return false;
if (_router->IsServiceNode())
return true;
return _linkManager->NumberOfConnectedRouters() + numPending < maxConnectedRouters;
}

View file

@ -83,10 +83,6 @@ namespace llarp
{
if (_running)
{
util::StatusObject peerStatsObj = nullptr;
if (m_peerDb)
peerStatsObj = m_peerDb->ExtractStatus();
return util::StatusObject{
{"running", true},
{"numNodesKnown", _nodedb->NumLoaded()},
@ -94,8 +90,7 @@ namespace llarp
{"services", _hiddenServiceContext.ExtractStatus()},
{"exit", _exitContext.ExtractStatus()},
{"links", _linkManager.ExtractStatus()},
{"outboundMessages", _outboundMessageHandler.ExtractStatus()},
{"peerStats", peerStatsObj}};
{"outboundMessages", _outboundMessageHandler.ExtractStatus()}};
}
else
{
@ -679,14 +674,6 @@ namespace llarp
hiddenServiceContext().AddEndpoint(conf);
}
// peer stats
if (IsServiceNode())
{
LogInfo("Initializing peerdb...");
m_peerDb = std::make_shared<PeerDb>();
m_peerDb->configure(conf.router);
}
// Logging config
LogContext::Instance().Initialize(
conf.logging.m_logLevel,
@ -1233,7 +1220,8 @@ namespace llarp
router,
" (",
previous_fails + 1,
" consecutive failures)");
" consecutive failures) result=",
result);
}
else
{

View file

@ -404,6 +404,10 @@ namespace llarp
m_ReadyHooks.clear();
}
if (m_LastInboundTraffic > 0s and lastGoodSend > 0s
and now >= sendTimeout + m_LastInboundTraffic)
return true;
const auto timeout = std::max(lastGoodSend, m_LastInboundTraffic);
if (lastGoodSend > 0s and now >= timeout + (sendTimeout / 2))
{