1
0
Fork 0
mirror of https://gitlab.com/niansa/nosni.git synced 2025-03-06 20:53:26 +01:00

Attempt to only apply to blocked websites

This commit is contained in:
niansa/tuxifan 2023-04-13 10:01:56 +02:00
parent 875137914a
commit 8499c86300
3 changed files with 38 additions and 5 deletions

View file

@ -1,5 +1,5 @@
libnosni.so: nss.o common.o common_cpp.o
g++ -g -fPIC -shared -ldl -lcurl -lcurlpp *.o -o libnosni.so
g++ -g -fPIC -shared *.o -ldl -lcurl -lcurlpp -o libnosni.so
nss.o: nss.c
gcc -c -fPIC -I /usr/include/nspr/ nss.c -o nss.o

View file

@ -1,9 +1,11 @@
#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>
@ -23,6 +25,8 @@ std::optional<bool> cached_is_domain_blocked(const char *hostname) {
extern "C"
bool is_domain_blocked(const char *hostname) {
bool fres;
// Check cache first
{
auto fres = cached_is_domain_blocked(hostname);
@ -32,5 +36,29 @@ bool is_domain_blocked(const char *hostname) {
}
// 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.perform();
// Get status code
auto status = curlpp::infos::ResponseCode::get(req);
// Check result
fres = status > 400;
} 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;
}

13
nss.c
View file

@ -18,14 +18,19 @@ void *arg) {
}
SECStatus SSL_SetURL(PRFileDesc *fd, const char *url) {
// Debug
printf("Interrupted URL setter for %s; setting IP address on success.\n", url);
fflush(stdout);
// Get original function
static typeof(SSL_SetURL) *orig = NULL;
if (!orig) orig = dlsym(RTLD_NEXT, "SSL_SetURL");
// Don't do anything if domain isn't blocked
if (!is_domain_blocked(url)) {
return orig(fd, url);
}
// Debug
printf("Interrupted URL setter for %s; setting IP address on success.\n", url);
fflush(stdout);
// Get IP addr as string
url = resolve_hostname_to_str(url);
if (!url) return SECFailure;