Create and fill gravity.db during pihole -g

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2019-02-03 13:04:31 +01:00
parent 1e284f69ea
commit 09c4c88a6d
No known key found for this signature in database
GPG key ID: FB60471F0575164A
2 changed files with 65 additions and 4 deletions

View file

@ -0,0 +1,13 @@
CREATE TABLE whitelist (domain TEXT UNIQUE NOT NULL, comment TEXT, DateAdded DATETIME);
CREATE TABLE blacklist (domain TEXT UNIQUE NOT NULL, comment TEXT, DateAdded DATETIME);
CREATE TABLE gravity (domain TEXT UNIQUE NOT NULL);
CREATE VIEW vw_gravity AS SELECT DISTINCT a.domain
FROM gravity a
WHERE a.domain NOT IN (SELECT domain from whitelist);
CREATE VIEW vw_blacklist AS SELECT DISTINCT a.domain
FROM blacklist a
WHERE a.domain NOT IN (SELECT domain from whitelist);

View file

@ -35,6 +35,10 @@ blackList="${piholeDir}/black.list"
localList="${piholeDir}/local.list"
VPNList="/etc/openvpn/ipp.txt"
piholeGitDir="/etc/.pihole"
gravityDBfile="${piholeDir}/gravity.db"
gravityDBschema="${piholeGitDir}/advanced/Templates/gravity.db.schema"
domainsExtension="domains"
matterAndLight="${basename}.0.matterandlight.txt"
parsedMatter="${basename}.1.parsedmatter.txt"
@ -83,6 +87,11 @@ if [[ -r "${piholeDir}/pihole.conf" ]]; then
echo -e " ${COL_LIGHT_RED}Ignoring overrides specified within pihole.conf! ${COL_NC}"
fi
# Generate new sqlite3 file from schema template
generate_gravity_database() {
sqlite3 "${gravityDBfile}" < "${gravityDBschema}"
}
# Determine if Pi-hole blocking is disabled
# If this is the case, we want to update
# gravity.list.bck and black.list.bck instead of
@ -582,12 +591,38 @@ gravity_ParseBlacklistDomains() {
cp "${piholeDir}/${preEventHorizon}" "${piholeDir}/${accretionDisc}"
fi
# Move the file over as /etc/pihole/gravity.list so dnsmasq can use it
output=$( { mv "${piholeDir}/${accretionDisc}" "${adList}"; } 2>&1 )
# Create database file if not present
if [ ! -e "${gravityDBfile}" ]; then
generate_gravity_database
fi
# Backup gravity database
cp "${gravityDBfile}" "${gravityDBfile}.bck"
# Empty domains
output=$( { sqlite3 "${gravityDBfile}" <<< "DELETE FROM gravity;"; } 2>&1 )
status="$?"
if [[ "${status}" -ne 0 ]]; then
echo -e "\\n ${CROSS} Unable to move ${accretionDisc} from ${piholeDir}\\n ${output}"
echo -e "\\n ${CROSS} Unable to truncate gravity database ${gravityDBfile}\\n ${output}"
gravity_Cleanup "error"
fi
# Store domains in gravity database
output=$( { sqlite3 "${gravityDBfile}" <<< ".import ${piholeDir}/${accretionDisc} gravity"; } 2>&1 )
status="$?"
if [[ "${status}" -ne 0 ]]; then
echo -e "\\n ${CROSS} Unable to create gravity database ${gravityDBfile}\\n ${output}"
gravity_Cleanup "error"
fi
# Empty $adList if it already exists, otherwise, create it
output=$( { : > "${adList}"; } 2>&1 )
status="$?"
if [[ "${status}" -ne 0 ]]; then
echo -e "\\n ${CROSS} Unable to create empty ${adList}\\n ${output}"
gravity_Cleanup "error"
fi
}
@ -633,6 +668,19 @@ gravity_Cleanup() {
echo -e "${OVER} ${TICK} ${str}"
str="Optimizing domains database"
echo -ne " ${INFO} ${str}..."
# Store
output=$( { sqlite3 "${gravityDBfile}" <<< "VACUUM;"; } 2>&1 )
status="$?"
if [[ "${status}" -ne 0 ]]; then
echo -e "\\n ${CROSS} Unable to optimize gravity database ${gravityDBfile}\\n ${output}"
gravity_Cleanup "error"
else
echo -e "${OVER} ${TICK} ${str}"
fi
# Only restart DNS service if offline
if ! pidof ${resolver} &> /dev/null; then
"${PIHOLE_COMMAND}" restartdns
@ -707,7 +755,7 @@ gravity_ShowBlockCount
# Perform when downloading blocklists, or modifying the white/blacklist (not wildcards)
if [[ "${skipDownload}" == false ]] || [[ "${listType}" == *"list" ]]; then
str="Parsing domains into hosts format"
str="Parsing domains"
echo -ne " ${INFO} ${str}..."
gravity_ParseUserDomains