2022-01-09 11:58:38 +01:00
|
|
|
#ifndef FORTUNA_SEED_FILE_MANAGER_CPP
|
|
|
|
#define FORTUNA_SEED_FILE_MANAGER_CPP
|
|
|
|
|
|
|
|
#include "seed_file_management.h"
|
|
|
|
#include "accumulator.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include <cryptopp/secblock.h>
|
2022-01-20 05:36:06 +01:00
|
|
|
#include <cryptopp/secblockfwd.h>
|
2022-01-13 02:49:45 +01:00
|
|
|
#include <fmt/core.h>
|
2022-01-20 05:36:06 +01:00
|
|
|
#include <sys/stat.h>
|
2022-01-13 02:49:45 +01:00
|
|
|
|
2022-01-20 05:36:06 +01:00
|
|
|
#include <cassert>
|
2022-01-14 08:27:52 +01:00
|
|
|
#include <cstdint>
|
2022-01-17 07:20:16 +01:00
|
|
|
#include <fstream>
|
2022-01-20 05:36:06 +01:00
|
|
|
#include <memory>
|
2022-01-21 05:32:23 +01:00
|
|
|
#include <mutex>
|
2022-01-09 11:58:38 +01:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace fortuna {
|
|
|
|
|
2022-01-10 04:25:03 +01:00
|
|
|
SeedFileManager::SeedFileManager(
|
2022-01-22 18:36:51 +01:00
|
|
|
std::shared_ptr<fortuna::accumulator::Accumulator>
|
|
|
|
da_accumulator) noexcept {
|
|
|
|
this->_p_accumulator = da_accumulator;
|
2022-01-09 11:58:38 +01:00
|
|
|
}
|
2022-01-10 04:25:03 +01:00
|
|
|
SeedFileManager::~SeedFileManager() noexcept {
|
2022-01-13 02:49:45 +01:00
|
|
|
set_job_running(false); // RIP, well, yeah, not running when the obj dies
|
2022-01-10 04:25:03 +01:00
|
|
|
}
|
2022-01-09 11:58:38 +01:00
|
|
|
|
2022-01-21 05:35:47 +01:00
|
|
|
auto SeedFileManager::is_job_running() const -> bool {
|
2022-01-21 05:32:23 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(mtx);
|
|
|
|
|
|
|
|
return this->IS_RUNNING;
|
2022-01-09 11:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto SeedFileManager::set_job_running(bool running) -> void {
|
2022-01-21 05:32:23 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(mtx);
|
|
|
|
|
|
|
|
this->IS_RUNNING = running;
|
2022-01-09 11:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto SeedFileManager::do_stuff() -> void {
|
2022-01-21 05:32:23 +01:00
|
|
|
std::unique_lock<std::recursive_mutex> ul(mtx);
|
|
|
|
|
2022-01-09 11:58:38 +01:00
|
|
|
update_seed_file();
|
2022-01-10 04:25:03 +01:00
|
|
|
do_task.thread_pls(config.write_interval, [this] { write_seed_file(); });
|
2022-01-09 11:58:38 +01:00
|
|
|
}
|
|
|
|
|
2022-01-23 20:18:59 +01:00
|
|
|
auto SeedFileManager::get_write_interval() const -> std::chrono::seconds {
|
|
|
|
std::unique_lock<std::recursive_mutex> ul(mtx);
|
|
|
|
return this->config.write_interval;
|
|
|
|
}
|
|
|
|
|
2022-01-20 05:36:06 +01:00
|
|
|
auto SeedFileManager::seed_file_exists() const -> bool {
|
2022-01-21 05:32:23 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(mtx);
|
|
|
|
|
2022-01-20 05:36:06 +01:00
|
|
|
// based on https://stackoverflow.com/a/6296808
|
|
|
|
struct stat buf;
|
|
|
|
if (stat(config.seed_f_path.c_str(), &buf) != -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-09 11:58:38 +01:00
|
|
|
auto SeedFileManager::update_seed_file() -> void {
|
2022-01-21 05:32:23 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(mtx);
|
|
|
|
|
|
|
|
fortuna::SeedFileManager::IS_RUNNING = true;
|
2022-01-09 11:58:38 +01:00
|
|
|
CryptoPP::SecByteBlock buff{config.seed_f_length};
|
|
|
|
|
2022-01-20 05:45:27 +01:00
|
|
|
try {
|
|
|
|
{
|
|
|
|
if (!seed_file_exists()) {
|
|
|
|
fmt::print("\t[i] sfm: SEED FILE NOT PRESENT, creating...\n");
|
|
|
|
std::fstream f_stream;
|
|
|
|
f_stream.open(config.seed_f_path,
|
|
|
|
std::fstream::out | std::fstream::trunc);
|
|
|
|
f_stream << "";
|
|
|
|
f_stream.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ifstream f_stream{config.seed_f_path, std::ios::binary};
|
|
|
|
|
|
|
|
if (!f_stream.is_open()) {
|
|
|
|
const std::string msg{"\t[!] sfm: error opening seed file!\n"};
|
|
|
|
fmt::print("{} {}\n", msg, config.seed_f_path);
|
|
|
|
|
2022-01-21 05:32:23 +01:00
|
|
|
fortuna::SeedFileManager::IS_RUNNING = false;
|
2022-01-20 05:45:27 +01:00
|
|
|
throw std::runtime_error(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
f_stream.read(reinterpret_cast<char*>(buff.BytePtr()),
|
|
|
|
static_cast<int64_t>(config.seed_f_length));
|
|
|
|
|
|
|
|
if (static_cast<std::size_t>(f_stream.gcount()) !=
|
|
|
|
config.seed_f_length) {
|
|
|
|
const std::string msg{
|
|
|
|
"\t[!] sfm: error reading seed from file, length mismatch"};
|
|
|
|
fmt::print("{} {}, length: {} vs desired length: {}\n",
|
|
|
|
msg,
|
|
|
|
config.seed_f_path,
|
|
|
|
f_stream.gcount(),
|
|
|
|
config.seed_f_length);
|
|
|
|
|
2022-01-21 05:32:23 +01:00
|
|
|
fortuna::SeedFileManager::IS_RUNNING = false;
|
2022-01-20 05:45:27 +01:00
|
|
|
throw std::runtime_error(msg);
|
|
|
|
}
|
2022-01-09 11:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string str_buff(reinterpret_cast<const char*>(&buff[0]),
|
2022-01-24 19:25:04 +01:00
|
|
|
buff.SizeInBytes());
|
2022-01-20 05:36:06 +01:00
|
|
|
|
2022-01-22 18:36:51 +01:00
|
|
|
this->_p_accumulator->call_reseed(str_buff);
|
2022-01-09 11:58:38 +01:00
|
|
|
write_seed_file();
|
2022-01-10 04:25:03 +01:00
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2022-01-20 05:36:06 +01:00
|
|
|
fmt::print("{}\n", e.what());
|
2022-01-21 05:32:23 +01:00
|
|
|
fortuna::SeedFileManager::IS_RUNNING = false;
|
2022-01-20 05:36:06 +01:00
|
|
|
throw;
|
2022-01-09 11:58:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto SeedFileManager::write_seed_file() -> void {
|
2022-01-21 05:32:23 +01:00
|
|
|
std::lock_guard<std::recursive_mutex> lg(mtx);
|
|
|
|
|
|
|
|
fortuna::SeedFileManager::IS_RUNNING = true;
|
2022-01-20 05:36:06 +01:00
|
|
|
CryptoPP::SecByteBlock buff{config.seed_f_length};
|
2022-01-22 18:36:51 +01:00
|
|
|
const std::string da_buff{this->_p_accumulator->get_random_data(
|
2022-01-20 05:36:06 +01:00
|
|
|
static_cast<unsigned int>(config.seed_f_length))};
|
|
|
|
|
|
|
|
assert(da_buff.length() % 2 == 0);
|
|
|
|
assert(da_buff.size() == config.seed_f_length * 2); // da_buff is
|
|
|
|
// hex-encoded
|
|
|
|
|
|
|
|
// account for hex encoding that is returned from get_random_data(), i.e.
|
|
|
|
// the total length is half of the actual number of characters
|
|
|
|
const size_t length{da_buff.length() / 2};
|
|
|
|
|
|
|
|
CryptoPP::StringSource src(
|
|
|
|
da_buff.c_str(),
|
|
|
|
true /*pumpAll*/,
|
|
|
|
new CryptoPP::HexDecoder(new CryptoPP::ArraySink(&buff[0], length)));
|
|
|
|
|
2022-01-09 11:58:38 +01:00
|
|
|
fmt::print("[*] sfm: writing seed file\n");
|
|
|
|
|
2022-01-13 02:49:45 +01:00
|
|
|
try {
|
|
|
|
std::ofstream f_stream{config.seed_f_path,
|
|
|
|
std::ios::binary | std::ios::trunc};
|
|
|
|
f_stream.write(reinterpret_cast<const char*>(buff.BytePtr()),
|
2022-01-14 08:27:52 +01:00
|
|
|
static_cast<int64_t>(config.seed_f_length));
|
2022-01-13 02:49:45 +01:00
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
|
|
|
fmt::print("[!] sfm: error writing to seed file!\n");
|
|
|
|
fmt::print("{}\n", e.what());
|
2022-01-21 05:32:23 +01:00
|
|
|
fortuna::SeedFileManager::IS_RUNNING = false;
|
2022-01-20 05:36:06 +01:00
|
|
|
throw;
|
2022-01-13 02:49:45 +01:00
|
|
|
}
|
2022-01-09 11:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace fortuna
|
|
|
|
|
2022-01-10 04:25:03 +01:00
|
|
|
#endif // FORTUNA_SEED_FILE_MANAGER_CPP
|