lokinet/llarp/bootstrap.cpp
2024-01-31 07:54:12 -08:00

71 lines
1.4 KiB
C++

#include "bootstrap.hpp"
#include "util/bencode.hpp"
#include "util/logging.hpp"
#include "util/logging/buffer.hpp"
namespace llarp
{
bool
BootstrapList::bt_decode(std::string_view buf)
{
try
{
oxenc::bt_list_consumer btlc{buf};
while (not btlc.is_finished())
emplace(btlc.consume_dict_consumer());
}
catch (...)
{
log::warning(logcat, "Unable to decode bootstrap RemoteRC");
return false;
}
return true;
}
std::string_view
BootstrapList::bt_encode() const
{
oxenc::bt_list_producer btlp{};
for (const auto& it : *this)
btlp.append(it.view());
return btlp.view();
}
void
BootstrapList::read_from_file(const fs::path& fpath)
{
bool isListFile = false;
if (std::ifstream inf(fpath.c_str(), std::ios::binary); inf.is_open())
{
const char ch = inf.get();
isListFile = ch == 'l';
}
if (isListFile)
{
auto content = util::file_to_string(fpath);
if (not bt_decode(content))
{
throw std::runtime_error{fmt::format("failed to read bootstrap list file '{}'", fpath)};
}
}
else
{
RemoteRC rc;
if (not rc.read(fpath))
{
throw std::runtime_error{
fmt::format("failed to decode bootstrap RC, file='{}', rc={}", fpath, rc.to_string())};
}
insert(rc);
}
}
} // namespace llarp