1
0
mirror of https://github.com/pi-hole/pi-hole synced 2024-12-22 14:58:08 +00:00

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