@@ -2,18 +2,12 @@
|
||||
|
||||
#include <glaze/glaze.hpp>
|
||||
|
||||
#include "../helpers/FsUtils.hpp"
|
||||
#include "../GlobalState.hpp"
|
||||
|
||||
static std::string readFileAsText(const std::string& path) {
|
||||
std::ifstream ifs(path);
|
||||
auto res = std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
||||
if (res.back() == '\n')
|
||||
res.pop_back();
|
||||
return res;
|
||||
}
|
||||
|
||||
CConfig::CConfig() {
|
||||
auto json = glz::read_jsonc<SConfig>(readFileAsText(g_pGlobalState->cwd + "/" + g_pGlobalState->configPath));
|
||||
auto json = glz::read_jsonc<SConfig>(
|
||||
NFsUtils::readFileAsString(NFsUtils::isAbsolute(g_pGlobalState->configPath) ? g_pGlobalState->configPath : g_pGlobalState->cwd + "/" + g_pGlobalState->configPath).value());
|
||||
|
||||
if (!json.has_value())
|
||||
throw std::runtime_error("No config / bad config format");
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
#include "../GlobalState.hpp"
|
||||
#include "../config/Config.hpp"
|
||||
#include "../debug/log.hpp"
|
||||
#include "../helpers/FsUtils.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <string_view>
|
||||
|
||||
@@ -16,28 +16,15 @@
|
||||
|
||||
constexpr const char* KEY_FILENAME = "privateKey.key";
|
||||
|
||||
static std::string dataDir() {
|
||||
static const std::string dir = std::filesystem::canonical(g_pGlobalState->cwd + "/" + g_pConfig->m_config.data_dir).string();
|
||||
return dir;
|
||||
}
|
||||
|
||||
static std::string readFileAsText(const std::string& path) {
|
||||
std::ifstream ifs(path);
|
||||
auto res = std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
||||
if (res.back() == '\n')
|
||||
res.pop_back();
|
||||
return res;
|
||||
}
|
||||
|
||||
CCrypto::CCrypto() {
|
||||
if (!std::filesystem::exists(dataDir() + "/" + KEY_FILENAME)) {
|
||||
if (!std::filesystem::exists(NFsUtils::dataDir() + "/" + KEY_FILENAME)) {
|
||||
Debug::log(LOG, "No private key, generating one.");
|
||||
if (!genKey()) {
|
||||
Debug::log(CRIT, "Couldn't generate a key.");
|
||||
throw std::runtime_error("Keygen failed");
|
||||
}
|
||||
} else {
|
||||
auto f = fopen((dataDir() + "/" + KEY_FILENAME).c_str(), "r");
|
||||
auto f = fopen((NFsUtils::dataDir() + "/" + KEY_FILENAME).c_str(), "r");
|
||||
PEM_read_PrivateKey(f, &m_evpPkey, nullptr, nullptr);
|
||||
fclose(f);
|
||||
}
|
||||
@@ -113,7 +100,7 @@ bool CCrypto::genKey() {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto f = fopen((dataDir() + "/" + KEY_FILENAME).c_str(), "w");
|
||||
auto f = fopen((NFsUtils::dataDir() + "/" + KEY_FILENAME).c_str(), "w");
|
||||
PEM_write_PrivateKey(f, m_evpPkey, nullptr, nullptr, 0, nullptr, nullptr);
|
||||
fclose(f);
|
||||
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
#include "../debug/log.hpp"
|
||||
#include "../GlobalState.hpp"
|
||||
#include "../config/Config.hpp"
|
||||
#include "../helpers/FsUtils.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
@@ -26,13 +26,6 @@ constexpr const uint64_t TOKEN_MAX_AGE_MS = 1000 * 60 * 60; // 1hr
|
||||
constexpr const char* TOKEN_COOKIE_NAME = "checkpoint-token";
|
||||
|
||||
//
|
||||
static std::string readFileAsText(const std::string& path) {
|
||||
std::ifstream ifs(path);
|
||||
auto res = std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
||||
if (res.back() == '\n')
|
||||
res.pop_back();
|
||||
return res;
|
||||
}
|
||||
|
||||
static std::string generateNonce() {
|
||||
static std::random_device dev;
|
||||
@@ -186,8 +179,8 @@ void CServerHandler::onRequest(const Pistache::Http::Request& req, Pistache::Htt
|
||||
}
|
||||
|
||||
if (isResourceCheckpoint(req.resource())) {
|
||||
response.send(Pistache::Http::Code::Ok,
|
||||
readFileAsText(g_pGlobalState->cwd + "/" + g_pConfig->m_config.html_dir + "/" + req.resource().substr(req.resource().find("checkpoint/") + 11)));
|
||||
// no directory traversal is possible when resource is checkpoint
|
||||
response.send(Pistache::Http::Code::Ok, NFsUtils::readFileAsString(NFsUtils::htmlPath(req.resource().substr(req.resource().find("checkpoint/") + 11))).value());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -282,10 +275,10 @@ void CServerHandler::challengeSubmitted(const Pistache::Http::Request& req, Pist
|
||||
}
|
||||
|
||||
void CServerHandler::serveStop(const Pistache::Http::Request& req, Pistache::Http::ResponseWriter& response) {
|
||||
static const auto PATH = std::filesystem::canonical(g_pGlobalState->cwd + "/" + g_pConfig->m_config.html_dir).string();
|
||||
static const auto PAGE_INDEX = readFileAsText(PATH + "/index.min.html");
|
||||
static const auto PAGE_INDEX = NFsUtils::readFileAsString(NFsUtils::htmlPath("/index.min.html")).value();
|
||||
static const auto PAGE_ROOT = PAGE_INDEX.substr(0, PAGE_INDEX.find_last_of("/") + 1);
|
||||
CTinylates page(PAGE_INDEX);
|
||||
page.setTemplateRoot(PATH);
|
||||
page.setTemplateRoot(PAGE_ROOT);
|
||||
|
||||
const auto NONCE = generateNonce();
|
||||
const auto DIFFICULTY = 4;
|
||||
|
||||
30
src/helpers/FsUtils.cpp
Normal file
30
src/helpers/FsUtils.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "FsUtils.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "../GlobalState.hpp"
|
||||
#include "../config/Config.hpp"
|
||||
|
||||
bool NFsUtils::isAbsolute(const std::string& sv) {
|
||||
return sv.size() > 0 && (*sv.begin() == '/' || *sv.begin() == '~');
|
||||
}
|
||||
|
||||
std::expected<std::string, std::string> NFsUtils::readFileAsString(const std::string& path) {
|
||||
std::ifstream ifs(path);
|
||||
if (!ifs.good())
|
||||
return std::unexpected("No file");
|
||||
auto res = std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
||||
if (res.back() == '\n')
|
||||
res.pop_back();
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string NFsUtils::htmlPath(const std::string& resource) {
|
||||
static const std::string htmlRoot = isAbsolute(g_pConfig->m_config.html_dir) ? g_pConfig->m_config.html_dir : g_pGlobalState->cwd + "/" + g_pConfig->m_config.html_dir;
|
||||
return htmlRoot + "/" + resource;
|
||||
}
|
||||
|
||||
std::string NFsUtils::dataDir() {
|
||||
static const std::string dataRoot = isAbsolute(g_pConfig->m_config.data_dir) ? g_pConfig->m_config.data_dir : g_pGlobalState->cwd + "/" + g_pConfig->m_config.data_dir;
|
||||
return dataRoot;
|
||||
}
|
||||
11
src/helpers/FsUtils.hpp
Normal file
11
src/helpers/FsUtils.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <expected>
|
||||
|
||||
namespace NFsUtils {
|
||||
bool isAbsolute(const std::string& path);
|
||||
std::expected<std::string, std::string> readFileAsString(const std::string& path);
|
||||
std::string htmlPath(const std::string& resource);
|
||||
std::string dataDir();
|
||||
};
|
||||
Reference in New Issue
Block a user