core: don't crash on errors

fixes #19
This commit is contained in:
Vaxry
2025-04-20 23:41:54 +01:00
parent 9d3922d1d3
commit c60abae9bb
7 changed files with 35 additions and 11 deletions

View File

@@ -31,7 +31,7 @@ CConfig::CConfig() {
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");
Debug::die("No config or config has bad format");
m_config = json.value();
@@ -46,7 +46,7 @@ CConfig::CConfig() {
parsed.exclude_regex = std::make_unique<re2::RE2>(ic.exclude_regex);
if (parsed.exclude_regex->error_code() != RE2::NoError) {
Debug::log(CRIT, "Regex \"{}\" failed to parse", ic.exclude_regex);
throw std::runtime_error("Failed to parse regex");
Debug::die("Failed to parse regex");
}
}

View File

@@ -3,13 +3,15 @@
#include <algorithm>
#include <stdexcept>
#include "../debug/log.hpp"
CIP::CIP(const std::string& ip) {
if (std::count(ip.begin(), ip.end(), '.') == 3)
parseV4(ip);
else if (std::count(ip.begin(), ip.end(), ':') >= 2)
parseV6(ip);
else
throw std::runtime_error("IP not valid");
Debug::die("Invalid IP: {}", ip);
}
void CIP::parseV4(const std::string& ip) {
@@ -32,7 +34,7 @@ void CIP::parseV4(const std::string& ip) {
m_blocks.push_back(std::stoul(std::string{curr}));
if (m_blocks.back() > 0xFF)
throw std::runtime_error("Invalid IPv4 byte");
Debug::die("Invalid ipv4 byte: {}", curr);
}
}
@@ -74,13 +76,13 @@ void CIP::parseV6(const std::string& ip) {
m_blocks.push_back(std::stoul(std::string{curr}, nullptr, 16));
if (m_blocks.back() > 0xFFFF)
throw std::runtime_error("Invalid IPv6 byte");
Debug::die("Invalid ipv6 byte: {}", curr);
}
}
CIPRange::CIPRange(const std::string& range) {
if (!range.contains('/'))
throw std::runtime_error("Range has no subnet");
Debug::die("IP range {} has no subnet", range);
m_subnet = std::stoul(range.substr(range.find('/') + 1));

View File

@@ -17,11 +17,12 @@
constexpr const char* KEY_FILENAME = "privateKey.key";
CCrypto::CCrypto() {
if (!std::filesystem::exists(NFsUtils::dataDir() + "/" + KEY_FILENAME)) {
std::error_code ec;
if (!std::filesystem::exists(NFsUtils::dataDir() + "/" + KEY_FILENAME, ec) || ec) {
Debug::log(LOG, "No private key, generating one.");
if (!genKey()) {
Debug::log(CRIT, "Couldn't generate a key.");
throw std::runtime_error("Keygen failed");
Debug::die("Keygen failed");
}
} else {
auto f = fopen((NFsUtils::dataDir() + "/" + KEY_FILENAME).c_str(), "r");
@@ -31,7 +32,7 @@ CCrypto::CCrypto() {
if (!m_evpPkey) {
Debug::log(CRIT, "Couldn't read the key.");
throw std::runtime_error("Key read openssl failed");
Debug::die("Key reading from openssl failed");
}
Debug::log(LOG, "Read private key");

View File

@@ -37,4 +37,12 @@ namespace Debug {
std::cout << logMsg << "\n";
}
template <typename... Args>
void die(fmt::format_string<Args...> fmt, Args&&... args) {
const std::string logMsg = fmt::vformat(fmt.get(), fmt::make_format_args(args...));
std::cout << "[ERR] " << logMsg << "\n";
exit(1);
}
};

View File

@@ -1,6 +1,7 @@
#include "FsUtils.hpp"
#include <fstream>
#include <filesystem>
#include "../GlobalState.hpp"
#include "../config/Config.hpp"
@@ -9,6 +10,13 @@ bool NFsUtils::isAbsolute(const std::string& sv) {
return sv.size() > 0 && (*sv.begin() == '/' || *sv.begin() == '~');
}
bool NFsUtils::exists(const std::string& path) {
const std::string p = isAbsolute(path) ? path : g_pGlobalState->cwd + "/" + path;
std::error_code ec;
return std::filesystem::exists(p) && !ec;
}
std::expected<std::string, std::string> NFsUtils::readFileAsString(const std::string& path) {
std::ifstream ifs(path);
if (!ifs.good())

View File

@@ -8,4 +8,5 @@ namespace NFsUtils {
std::expected<std::string, std::string> readFileAsString(const std::string& path);
std::string htmlPath(const std::string& resource);
std::string dataDir();
bool exists(const std::string& path);
};

View File

@@ -17,6 +17,7 @@
#include "headers/xrealip.hpp"
#include "debug/log.hpp"
#include "helpers/FsUtils.hpp"
#include "core/Handler.hpp"
#include "core/Crypto.hpp"
@@ -63,12 +64,15 @@ int main(int argc, char** argv, char** envp) {
g_pConfig = std::make_unique<CConfig>();
if (g_pConfig->m_config.html_dir.empty() || g_pConfig->m_config.data_dir.empty())
return 1;
Debug::die("No data / html dir");
if (!NFsUtils::exists(g_pConfig->m_config.html_dir) || !NFsUtils::exists(g_pConfig->m_config.data_dir))
Debug::die("data / html dir does not exist");
sigset_t signals;
if (sigemptyset(&signals) != 0 || sigaddset(&signals, SIGTERM) != 0 || sigaddset(&signals, SIGINT) != 0 || sigaddset(&signals, SIGQUIT) != 0 ||
sigaddset(&signals, SIGPIPE) != 0 || sigaddset(&signals, SIGALRM) != 0 || sigprocmask(SIG_BLOCK, &signals, nullptr) != 0)
return 1;
Debug::die("Failed to set sighandlers");
int threads = 1;
Pistache::Address address = {Pistache::Ipv4::any(), (uint16_t)g_pConfig->m_config.port};