From 75cd6913ee6a5f3258e6fdc3b48888965ea100a8 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 15 Feb 2023 21:09:46 +0100 Subject: [PATCH] Do not use a new option but instead automatically detect if ABP-style domains are present in the database. This ensures that this addition comes at no extra costs to any installs using pure HOSTS-style adlists. Signed-off-by: DL6ER --- .devcontainer/devcontainer.json | 2 +- src/config.c | 14 ------------ src/config.h | 1 - src/database/gravity-db.c | 40 ++++++++++++++++++++++++++++++++- test/pihole-FTL.conf | 1 - 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index eabc0949..d4420369 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,6 +1,6 @@ { "name": "FTL x86_64 Build Env", - "image": "ghcr.io/pi-hole/ftl-build:x86_64-musl", + "image": "ghcr.io/pi-hole/ftl-build:x86_64", "extensions": [ "jetmartin.bats", "ms-vscode.cpptools", diff --git a/src/config.c b/src/config.c index 8d90629a..eae5cdb8 100644 --- a/src/config.c +++ b/src/config.c @@ -770,20 +770,6 @@ void read_FTLconf(void) logg(" CHECK_DISK: Warning if certain disk usage exceeds %d%%", config.check.disk); - // GRAVITY_ABP_STYLE - // Should FTL check for ABP-style domain entries in the gravity database? - // This option should only be enabled if you are using ABP-style entries - // in your gravity database as it adds a significant overhead to the - // database query. - // defaults to: false - buffer = parse_FTLconf(fp, "GRAVITY_ABP_STYLE"); - config.gravityABP = read_bool(buffer, false); - - if(config.gravityABP) - logg(" GRAVITY_ABP_STYLE: Enabled"); - else - logg(" GRAVITY_ABP_STYLE: Disabled"); - // Read DEBUG_... setting from pihole-FTL.conf read_debuging_settings(fp); diff --git a/src/config.h b/src/config.h index 507a3028..356f00e6 100644 --- a/src/config.h +++ b/src/config.h @@ -51,7 +51,6 @@ typedef struct { bool edns0_ecs :1; bool show_dnssec :1; bool addr2line :1; - bool gravityABP:1; struct { bool mozilla_canary :1; bool icloud_private_relay :1; diff --git a/src/database/gravity-db.c b/src/database/gravity-db.c index 1c9b3b8e..abe87954 100644 --- a/src/database/gravity-db.c +++ b/src/database/gravity-db.c @@ -47,6 +47,7 @@ static sqlite3 *gravity_db = NULL; static sqlite3_stmt* table_stmt = NULL; static sqlite3_stmt* auditlist_stmt = NULL; bool gravityDB_opened = false; +static bool gravity_abp_format = false; // Table names corresponding to the enum defined in gravity-db.h static const char* tablename[] = { "vw_gravity", "vw_blacklist", "vw_whitelist", "vw_regex_blacklist", "vw_regex_whitelist" , "" }; @@ -95,6 +96,40 @@ void gravityDB_forked(void) gravityDB_open(); } +static void gravity_check_ABP_format(void) +{ + // Check if we have a valid ABP format + // We do this by checking if there are any domains in the gravity table that match the + // ABP format. If there are, we remember that we have seen this format. + + // Prepare statement + sqlite3_stmt *stmt = NULL; + int rc = sqlite3_prepare_v2(gravity_db, + "SELECT EXISTS(SELECT 1 FROM gravity WHERE domain LIKE '||%^');", + -1, &stmt, NULL); + + if( rc != SQLITE_OK ) + { + logg("gravity_check_ABP_format() - SQL error prepare: %s", sqlite3_errstr(rc)); + return; + } + + // Execute statement + rc = sqlite3_step(stmt); + if( rc != SQLITE_ROW ) + { + logg("gravity_check_ABP_format() - SQL error step: %s", sqlite3_errstr(rc)); + sqlite3_finalize(stmt); + return; + } + + // Get result + gravity_abp_format = sqlite3_column_int(stmt, 0) > 0; + + // Finalize statement + sqlite3_finalize(stmt); +} + // Open gravity database bool gravityDB_open(void) { @@ -194,6 +229,9 @@ bool gravityDB_open(void) logg("gravityDB_open() - Cannot set busy handler: %s", sqlite3_errstr(rc)); } + // Check if there are any ABP-style entries in the database + gravity_check_ABP_format(); + if(config.debug & DEBUG_DATABASE) logg("gravityDB_open(): Successfully opened gravity.db"); return true; @@ -1300,7 +1338,7 @@ enum db_result in_gravity(const char *domain, clientsData *client) // Return early if we are not supposed to check for ABP-style regex // matches. This needs to be enabled in the config file as it is // computationally expensive and not needed in most cases (HOSTS lists). - if(!config.gravityABP) + if(!gravity_abp_format) return NOT_FOUND; // Make a copy of the domain we will slowly truncate diff --git a/test/pihole-FTL.conf b/test/pihole-FTL.conf index 8b0bdedb..1f941296 100644 --- a/test/pihole-FTL.conf +++ b/test/pihole-FTL.conf @@ -7,4 +7,3 @@ LOCAL_IPV4=10.100.0.10 LOCAL_IPV6=fe80::10 BLOCK_IPV4=10.100.0.11 BLOCK_IPV6=fe80::11 -GRAVITY_ABP_STYLE=true