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:
parent
875137914a
commit
8499c86300
3 changed files with 38 additions and 5 deletions
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
||||||
libnosni.so: nss.o common.o common_cpp.o
|
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
|
nss.o: nss.c
|
||||||
gcc -c -fPIC -I /usr/include/nspr/ nss.c -o nss.o
|
gcc -c -fPIC -I /usr/include/nspr/ nss.c -o nss.o
|
||||||
|
|
28
common.cpp
28
common.cpp
|
@ -1,9 +1,11 @@
|
||||||
|
#include <stdio.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <curlpp/cURLpp.hpp>
|
#include <curlpp/cURLpp.hpp>
|
||||||
#include <curlpp/Easy.hpp>
|
#include <curlpp/Easy.hpp>
|
||||||
|
#include <curlpp/Infos.hpp>
|
||||||
#include <curlpp/Options.hpp>
|
#include <curlpp/Options.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,6 +25,8 @@ std::optional<bool> cached_is_domain_blocked(const char *hostname) {
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
bool is_domain_blocked(const char *hostname) {
|
bool is_domain_blocked(const char *hostname) {
|
||||||
|
bool fres;
|
||||||
|
|
||||||
// Check cache first
|
// Check cache first
|
||||||
{
|
{
|
||||||
auto fres = cached_is_domain_blocked(hostname);
|
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
|
// 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
13
nss.c
|
@ -18,14 +18,19 @@ void *arg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
SECStatus SSL_SetURL(PRFileDesc *fd, const char *url) {
|
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
|
// Get original function
|
||||||
static typeof(SSL_SetURL) *orig = NULL;
|
static typeof(SSL_SetURL) *orig = NULL;
|
||||||
if (!orig) orig = dlsym(RTLD_NEXT, "SSL_SetURL");
|
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
|
// Get IP addr as string
|
||||||
url = resolve_hostname_to_str(url);
|
url = resolve_hostname_to_str(url);
|
||||||
if (!url) return SECFailure;
|
if (!url) return SECFailure;
|
||||||
|
|
Loading…
Add table
Reference in a new issue