1
0
Fork 0
mirror of https://gitlab.com/niansa/nosni.git synced 2025-03-06 20:53:26 +01:00
nosni/common.cpp
2023-04-13 10:11:13 +02:00

65 lines
1.4 KiB
C++

#include <stdio.h>
#include <string>
#include <string_view>
#include <unordered_map>
#include <optional>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Infos.hpp>
#include <curlpp/Options.hpp>
static
std::unordered_map<std::string, bool> cache;
static
std::optional<bool> cached_is_domain_blocked(const char *hostname) {
auto fres = cache.find(hostname);
if (fres != cache.end()) {
return fres->second;
}
return {};
}
extern "C"
bool is_domain_blocked(const char *hostname) {
bool fres;
// Check cache first
{
auto fres = cached_is_domain_blocked(hostname);
if (fres.has_value()) {
return fres.value();
}
}
// Use curlpp to check for HTTP != 200
try {
// Send request
curlpp::Cleanup cleanup;
curlpp::Easy req;
req.setOpt<curlpp::options::Url>(std::string("http://")+hostname);
req.setOpt<curlpp::options::NoBody>(true);
req.setOpt<curlpp::options::Timeout>(6);
req.perform();
// Get status code
auto status = curlpp::infos::ResponseCode::get(req);
// Check result
fres = status == 403;
} catch (...) {
fres = true;
}
// Store result in cache
cache[hostname] = fres;
// Debug result
printf("Domain %s is%s\n", hostname, fres?" probably blocked":"n't blocked");
// Return result
return fres;
}