From 0afb3b320bcd5ba0242d4cc7fe9c3cb6dd0e4763 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Fri, 6 Dec 2019 12:32:46 -0500 Subject: [PATCH] add bootstrap list functionality and utility --- contrib/bootstrap/make-bootstrap-list.sh | 4 ++ contrib/bootstrap/readme.txt | 3 ++ llarp/CMakeLists.txt | 1 + llarp/bootstrap.cpp | 34 ++++++++++++ llarp/bootstrap.hpp | 22 ++++++++ llarp/router/router.cpp | 68 ++++++++++++++---------- llarp/router/router.hpp | 5 +- llarp/util/bencode.hpp | 11 ++-- 8 files changed, 109 insertions(+), 39 deletions(-) create mode 100755 contrib/bootstrap/make-bootstrap-list.sh create mode 100644 contrib/bootstrap/readme.txt create mode 100644 llarp/bootstrap.cpp create mode 100644 llarp/bootstrap.hpp diff --git a/contrib/bootstrap/make-bootstrap-list.sh b/contrib/bootstrap/make-bootstrap-list.sh new file mode 100755 index 000000000..c1a2fb7a1 --- /dev/null +++ b/contrib/bootstrap/make-bootstrap-list.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +echo -n 'l' +for arg in $@ ; do cat "$arg" ; done +echo -n 'e' diff --git a/contrib/bootstrap/readme.txt b/contrib/bootstrap/readme.txt new file mode 100644 index 000000000..5bbb9e7c6 --- /dev/null +++ b/contrib/bootstrap/readme.txt @@ -0,0 +1,3 @@ +usage: + +./make-bootstrap-list.sh $(find $HOME/.lokinet/netdb | grep \\.signed$) > bootstrap.signed diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 324d6beef..79c39d738 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -134,6 +134,7 @@ set(DNSLIB_SRC set(LIB_SRC ${DNSLIB_SRC} + bootstrap.cpp context.cpp crypto/constants.cpp crypto/crypto_libsodium.cpp diff --git a/llarp/bootstrap.cpp b/llarp/bootstrap.cpp new file mode 100644 index 000000000..8addcac44 --- /dev/null +++ b/llarp/bootstrap.cpp @@ -0,0 +1,34 @@ +#include +#include + +namespace llarp +{ + void + BootstrapList::Clear() + { + clear(); + } + + bool + BootstrapList::BDecode(llarp_buffer_t* buf) + { + return bencode_read_list( + [&](llarp_buffer_t* b, bool more) -> bool { + if(more) + { + RouterContact rc; + if(not rc.BDecode(b)) + return false; + emplace(std::move(rc)); + } + return true; + }, + buf); + } + + bool + BootstrapList::BEncode(llarp_buffer_t* buf) const + { + return BEncodeWriteList(begin(), end(), buf); + } +} // namespace llarp diff --git a/llarp/bootstrap.hpp b/llarp/bootstrap.hpp new file mode 100644 index 000000000..7f3108cc4 --- /dev/null +++ b/llarp/bootstrap.hpp @@ -0,0 +1,22 @@ +#ifndef LLARP_BOOTSTRAP_HPP +#define LLARP_BOOTSTRAP_HPP + +#include +#include + +namespace llarp +{ + struct BootstrapList final : public std::set< RouterContact > + { + bool + BDecode(llarp_buffer_t* buf); + + bool + BEncode(llarp_buffer_t* buf) const; + + void + Clear(); + }; +} // namespace llarp + +#endif diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 90958777f..5e2631eae 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -49,7 +49,7 @@ namespace llarp , inbound_link_msg_parser(this) , _hiddenServiceContext(this) { - m_keyManager = std::make_shared(); + m_keyManager = std::make_shared< KeyManager >(); // set rational defaults this->ip4addr.sin_family = AF_INET; @@ -195,7 +195,6 @@ namespace llarp bool Router::EnsureIdentity() { - if(whitelistRouters) { #if defined(ANDROID) || defined(IOS) @@ -209,12 +208,12 @@ namespace llarp #endif } - _identity = m_keyManager->getIdentityKey(); + _identity = m_keyManager->getIdentityKey(); _encryption = m_keyManager->getEncryptionKey(); - if (_identity.IsZero()) + if(_identity.IsZero()) return false; - if (_encryption.IsZero()) + if(_encryption.IsZero()) return false; return true; @@ -231,7 +230,7 @@ namespace llarp } _nodedb = nodedb; - if (not m_keyManager->initialize(*conf, true)) + if(not m_keyManager->initialize(*conf, true)) return false; if(!FromConfig(conf)) @@ -446,38 +445,51 @@ namespace llarp std::vector< std::string > configRouters = conf->connect.routers; configRouters.insert(configRouters.end(), conf->bootstrap.routers.begin(), conf->bootstrap.routers.end()); + BootstrapList b_list; for(const auto &router : configRouters) { - // llarp::LogDebug("connect section has ", key, "=", val); - RouterContact rc; - if(!rc.Read(router.c_str())) + bool isListFile = false; { - llarp::LogWarn("failed to decode bootstrap RC, file='", router, - "' rc=", rc); - return false; + std::ifstream inf(router, std::ios::binary); + if(inf.is_open()) + { + const char ch = inf.get(); + isListFile = ch == 'l'; + } } - if(rc.Verify(Now())) + if(isListFile) { - const auto result = bootstrapRCList.insert(rc); - if(result.second) - llarp::LogInfo("Added bootstrap node ", RouterID(rc.pubkey)); - else - llarp::LogWarn("Duplicate bootstrap node ", RouterID(rc.pubkey)); + if(not BDecodeReadFile(router.c_str(), b_list)) + { + LogWarn("failed to read bootstrap list file '", router, "'"); + return false; + } } else { - if(rc.IsExpired(Now())) + RouterContact rc; + if(not rc.Read(router.c_str())) { - llarp::LogWarn("Bootstrap node ", RouterID(rc.pubkey), - " is too old and needs to be refreshed"); - } - else - { - llarp::LogError("malformed rc file='", router, "' rc=", rc); + llarp::LogWarn("failed to decode bootstrap RC, file='", router, + "' rc=", rc); + return false; } + b_list.insert(rc); } } + for(auto &rc : b_list) + { + if(not rc.Verify(Now())) + { + LogWarn("ignoring invalid RC: ", RouterID(rc.pubkey)); + continue; + } + bootstrapRCList.emplace(std::move(rc)); + } + + LogInfo("Loaded ", bootstrapRCList.size(), " bootstrap routers"); + // Init components after relevant config settings loaded _outboundMessageHandler.Init(&_linkManager, _logic); _outboundSessionMaker.Init(&_linkManager, &_rcLookupHandler, _logic, @@ -524,8 +536,7 @@ namespace llarp util::memFn(&IOutboundSessionMaker::OnConnectTimeout, &_outboundSessionMaker), util::memFn(&AbstractRouter::SessionClosed, this), - util::memFn(&AbstractRouter::PumpLL, this) - ); + util::memFn(&AbstractRouter::PumpLL, this)); const auto &key = std::get< LinksConfig::Interface >(serverConfig); int af = std::get< LinksConfig::AddressFamily >(serverConfig); @@ -1161,8 +1172,7 @@ namespace llarp util::memFn(&IOutboundSessionMaker::OnConnectTimeout, &_outboundSessionMaker), util::memFn(&AbstractRouter::SessionClosed, this), - util::memFn(&AbstractRouter::PumpLL, this) - ); + util::memFn(&AbstractRouter::PumpLL, this)); if(!link) return false; diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index 1bdddb9b3..848d7aa93 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -3,6 +3,7 @@ #include +#include #include #include #include @@ -221,7 +222,7 @@ namespace llarp NetConfig_t netConfig; /// bootstrap RCs - std::set< RouterContact > bootstrapRCList; + BootstrapList bootstrapRCList; bool ExitEnabled() const @@ -461,7 +462,7 @@ namespace llarp llarp_time_t m_LastStatsReport = 0; - std::shared_ptr m_keyManager; + std::shared_ptr< llarp::KeyManager > m_keyManager; bool ShouldReportStats(llarp_time_t now) const; diff --git a/llarp/util/bencode.hpp b/llarp/util/bencode.hpp index 05cd575f4..2994a8dd0 100644 --- a/llarp/util/bencode.hpp +++ b/llarp/util/bencode.hpp @@ -104,8 +104,8 @@ namespace llarp template < typename Item_t > bool BEncodeMaybeVerifyVersion(const char* k, Item_t& item, uint64_t expect, - bool& read, const llarp_buffer_t& key, - llarp_buffer_t* buf) + bool& read, const llarp_buffer_t& key, + llarp_buffer_t* buf) { if(key == k) { @@ -312,12 +312,7 @@ namespace llarp f.read((char*)ptr.data(), sz); } llarp_buffer_t buf(ptr); - auto result = t.BDecode(&buf); - if(!result) - { - DumpBuffer(buf); - } - return result; + return t.BDecode(&buf); } /// read entire file and decode its contents into t