add bootstrap list functionality and utility

pull/949/head
Jeff Becker 5 years ago
parent 51fae100ee
commit 0afb3b320b
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -0,0 +1,4 @@
#!/usr/bin/env bash
echo -n 'l'
for arg in $@ ; do cat "$arg" ; done
echo -n 'e'

@ -0,0 +1,3 @@
usage:
./make-bootstrap-list.sh $(find $HOME/.lokinet/netdb | grep \\.signed$) > bootstrap.signed

@ -134,6 +134,7 @@ set(DNSLIB_SRC
set(LIB_SRC set(LIB_SRC
${DNSLIB_SRC} ${DNSLIB_SRC}
bootstrap.cpp
context.cpp context.cpp
crypto/constants.cpp crypto/constants.cpp
crypto/crypto_libsodium.cpp crypto/crypto_libsodium.cpp

@ -0,0 +1,34 @@
#include <bootstrap.hpp>
#include <util/bencode.hpp>
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

@ -0,0 +1,22 @@
#ifndef LLARP_BOOTSTRAP_HPP
#define LLARP_BOOTSTRAP_HPP
#include <router_contact.hpp>
#include <set>
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

@ -49,7 +49,7 @@ namespace llarp
, inbound_link_msg_parser(this) , inbound_link_msg_parser(this)
, _hiddenServiceContext(this) , _hiddenServiceContext(this)
{ {
m_keyManager = std::make_shared<KeyManager>(); m_keyManager = std::make_shared< KeyManager >();
// set rational defaults // set rational defaults
this->ip4addr.sin_family = AF_INET; this->ip4addr.sin_family = AF_INET;
@ -195,7 +195,6 @@ namespace llarp
bool bool
Router::EnsureIdentity() Router::EnsureIdentity()
{ {
if(whitelistRouters) if(whitelistRouters)
{ {
#if defined(ANDROID) || defined(IOS) #if defined(ANDROID) || defined(IOS)
@ -209,12 +208,12 @@ namespace llarp
#endif #endif
} }
_identity = m_keyManager->getIdentityKey(); _identity = m_keyManager->getIdentityKey();
_encryption = m_keyManager->getEncryptionKey(); _encryption = m_keyManager->getEncryptionKey();
if (_identity.IsZero()) if(_identity.IsZero())
return false; return false;
if (_encryption.IsZero()) if(_encryption.IsZero())
return false; return false;
return true; return true;
@ -231,7 +230,7 @@ namespace llarp
} }
_nodedb = nodedb; _nodedb = nodedb;
if (not m_keyManager->initialize(*conf, true)) if(not m_keyManager->initialize(*conf, true))
return false; return false;
if(!FromConfig(conf)) if(!FromConfig(conf))
@ -446,38 +445,51 @@ namespace llarp
std::vector< std::string > configRouters = conf->connect.routers; std::vector< std::string > configRouters = conf->connect.routers;
configRouters.insert(configRouters.end(), conf->bootstrap.routers.begin(), configRouters.insert(configRouters.end(), conf->bootstrap.routers.begin(),
conf->bootstrap.routers.end()); conf->bootstrap.routers.end());
BootstrapList b_list;
for(const auto &router : configRouters) for(const auto &router : configRouters)
{ {
// llarp::LogDebug("connect section has ", key, "=", val); bool isListFile = false;
RouterContact rc;
if(!rc.Read(router.c_str()))
{ {
llarp::LogWarn("failed to decode bootstrap RC, file='", router, std::ifstream inf(router, std::ios::binary);
"' rc=", rc); if(inf.is_open())
return false; {
const char ch = inf.get();
isListFile = ch == 'l';
}
} }
if(rc.Verify(Now())) if(isListFile)
{ {
const auto result = bootstrapRCList.insert(rc); if(not BDecodeReadFile(router.c_str(), b_list))
if(result.second) {
llarp::LogInfo("Added bootstrap node ", RouterID(rc.pubkey)); LogWarn("failed to read bootstrap list file '", router, "'");
else return false;
llarp::LogWarn("Duplicate bootstrap node ", RouterID(rc.pubkey)); }
} }
else else
{ {
if(rc.IsExpired(Now())) RouterContact rc;
if(not rc.Read(router.c_str()))
{ {
llarp::LogWarn("Bootstrap node ", RouterID(rc.pubkey), llarp::LogWarn("failed to decode bootstrap RC, file='", router,
" is too old and needs to be refreshed"); "' rc=", rc);
} return false;
else
{
llarp::LogError("malformed rc file='", router, "' rc=", rc);
} }
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 // Init components after relevant config settings loaded
_outboundMessageHandler.Init(&_linkManager, _logic); _outboundMessageHandler.Init(&_linkManager, _logic);
_outboundSessionMaker.Init(&_linkManager, &_rcLookupHandler, _logic, _outboundSessionMaker.Init(&_linkManager, &_rcLookupHandler, _logic,
@ -524,8 +536,7 @@ namespace llarp
util::memFn(&IOutboundSessionMaker::OnConnectTimeout, util::memFn(&IOutboundSessionMaker::OnConnectTimeout,
&_outboundSessionMaker), &_outboundSessionMaker),
util::memFn(&AbstractRouter::SessionClosed, this), util::memFn(&AbstractRouter::SessionClosed, this),
util::memFn(&AbstractRouter::PumpLL, this) util::memFn(&AbstractRouter::PumpLL, this));
);
const auto &key = std::get< LinksConfig::Interface >(serverConfig); const auto &key = std::get< LinksConfig::Interface >(serverConfig);
int af = std::get< LinksConfig::AddressFamily >(serverConfig); int af = std::get< LinksConfig::AddressFamily >(serverConfig);
@ -1161,8 +1172,7 @@ namespace llarp
util::memFn(&IOutboundSessionMaker::OnConnectTimeout, util::memFn(&IOutboundSessionMaker::OnConnectTimeout,
&_outboundSessionMaker), &_outboundSessionMaker),
util::memFn(&AbstractRouter::SessionClosed, this), util::memFn(&AbstractRouter::SessionClosed, this),
util::memFn(&AbstractRouter::PumpLL, this) util::memFn(&AbstractRouter::PumpLL, this));
);
if(!link) if(!link)
return false; return false;

@ -3,6 +3,7 @@
#include <router/abstractrouter.hpp> #include <router/abstractrouter.hpp>
#include <bootstrap.hpp>
#include <config/key_manager.hpp> #include <config/key_manager.hpp>
#include <constants/link_layer.hpp> #include <constants/link_layer.hpp>
#include <crypto/types.hpp> #include <crypto/types.hpp>
@ -221,7 +222,7 @@ namespace llarp
NetConfig_t netConfig; NetConfig_t netConfig;
/// bootstrap RCs /// bootstrap RCs
std::set< RouterContact > bootstrapRCList; BootstrapList bootstrapRCList;
bool bool
ExitEnabled() const ExitEnabled() const
@ -461,7 +462,7 @@ namespace llarp
llarp_time_t m_LastStatsReport = 0; llarp_time_t m_LastStatsReport = 0;
std::shared_ptr<llarp::KeyManager> m_keyManager; std::shared_ptr< llarp::KeyManager > m_keyManager;
bool bool
ShouldReportStats(llarp_time_t now) const; ShouldReportStats(llarp_time_t now) const;

@ -104,8 +104,8 @@ namespace llarp
template < typename Item_t > template < typename Item_t >
bool bool
BEncodeMaybeVerifyVersion(const char* k, Item_t& item, uint64_t expect, BEncodeMaybeVerifyVersion(const char* k, Item_t& item, uint64_t expect,
bool& read, const llarp_buffer_t& key, bool& read, const llarp_buffer_t& key,
llarp_buffer_t* buf) llarp_buffer_t* buf)
{ {
if(key == k) if(key == k)
{ {
@ -312,12 +312,7 @@ namespace llarp
f.read((char*)ptr.data(), sz); f.read((char*)ptr.data(), sz);
} }
llarp_buffer_t buf(ptr); llarp_buffer_t buf(ptr);
auto result = t.BDecode(&buf); return t.BDecode(&buf);
if(!result)
{
DumpBuffer(buf);
}
return result;
} }
/// read entire file and decode its contents into t /// read entire file and decode its contents into t

Loading…
Cancel
Save