diff --git a/src/config/Config.cpp b/src/config/Config.cpp index 310cef2..6cd3ef6 100644 --- a/src/config/Config.cpp +++ b/src/config/Config.cpp @@ -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(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"); } } diff --git a/src/config/IPRange.cpp b/src/config/IPRange.cpp index 901d1d4..1457c36 100644 --- a/src/config/IPRange.cpp +++ b/src/config/IPRange.cpp @@ -3,13 +3,15 @@ #include #include +#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)); diff --git a/src/core/Crypto.cpp b/src/core/Crypto.cpp index eb1ea44..e21019e 100644 --- a/src/core/Crypto.cpp +++ b/src/core/Crypto.cpp @@ -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"); diff --git a/src/debug/log.hpp b/src/debug/log.hpp index d9ec762..146c3eb 100644 --- a/src/debug/log.hpp +++ b/src/debug/log.hpp @@ -37,4 +37,12 @@ namespace Debug { std::cout << logMsg << "\n"; } + + template + void die(fmt::format_string fmt, Args&&... args) { + const std::string logMsg = fmt::vformat(fmt.get(), fmt::make_format_args(args...)); + + std::cout << "[ERR] " << logMsg << "\n"; + exit(1); + } }; diff --git a/src/helpers/FsUtils.cpp b/src/helpers/FsUtils.cpp index 42cd4dc..ca59790 100644 --- a/src/helpers/FsUtils.cpp +++ b/src/helpers/FsUtils.cpp @@ -1,6 +1,7 @@ #include "FsUtils.hpp" #include +#include #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 NFsUtils::readFileAsString(const std::string& path) { std::ifstream ifs(path); if (!ifs.good()) diff --git a/src/helpers/FsUtils.hpp b/src/helpers/FsUtils.hpp index d391a60..bff3d90 100644 --- a/src/helpers/FsUtils.hpp +++ b/src/helpers/FsUtils.hpp @@ -8,4 +8,5 @@ namespace NFsUtils { std::expected readFileAsString(const std::string& path); std::string htmlPath(const std::string& resource); std::string dataDir(); + bool exists(const std::string& path); }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 1270f18..4580ffb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); 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};