Skip to content

Commit 41ca503

Browse files
committed
Feature: Disable Auto-fetching (bindhosts#62)
1 parent 94500c0 commit 41ca503

1 file changed

Lines changed: 79 additions & 10 deletions

File tree

module/bindhosts.sh

100644100755
Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,20 @@ adblock() {
330330
echo "[x] sources.txt needs correction 💢"
331331
return
332332
}
333-
# download routine start!
334-
for url in $(sed '/#/d' $PERSISTENT_DIR/sources.txt | grep http) ; do
335-
echo "[>] fetching $url"
336-
(download "$url" >> $rwdir/temphosts || echo "[x] failed downloading $url") &
337-
done
338-
# wait until all download jobs done
339-
wait
333+
# If UPDATESTATUS is false and "yes" exists in disable/update.txt, skip download routine
334+
if [ "$UPDATESTATUS" != true ] && grep -q "yes" "$PERSISTENT_DIR/disable/update.txt"; then
335+
echo "[>] Auto-Fetching disabled, On --force-update -> hosts will be renewed"
336+
return
337+
fi
338+
339+
# Download routine start!
340+
for url in $(sed '/#/d' $PERSISTENT_DIR/sources.txt | grep http); do
341+
echo "[>] fetching $url"
342+
(download "$url" >> $rwdir/temphosts || echo "[x] failed downloading $url") &
343+
done
344+
345+
# Wait for all background downloads to finish before proceeding
346+
wait
340347
# if temphosts is empty
341348
# its either user did something
342349
# or inaccessible urls / no internet
@@ -346,10 +353,20 @@ adblock() {
346353
# strip first two lines since thats just localhost
347354
tail -n +3 $target_hostsfile > $rwdir/temphosts
348355
}
356+
# Save the downloaded URLs to custom*.txt if "yes" exists in disable/update.txt
357+
if grep -q "yes" "$PERSISTENT_DIR/disable/update.txt"; then
358+
cp "$rwdir/temphosts" "$PERSISTENT_DIR/customtemphosts.txt"
359+
rm "$rwdir/temphosts"
360+
echo "[+] auto fetch disabled"
361+
fi
349362
# localhost
350363
printf "127.0.0.1 localhost\n::1 localhost\n" > $target_hostsfile
351-
# always restore user's custom rules
352-
sed '/#/d' $PERSISTENT_DIR/custom*.txt >> $target_hostsfile
364+
# If "yes" exists in disable/update.txt, append customtemphosts.txt to target_hostsfile
365+
if grep -q "yes" "$PERSISTENT_DIR/disable/update.txt"; then
366+
cat "$PERSISTENT_DIR/customtemphosts.txt" >> "$target_hostsfile"
367+
fi
368+
# Append custom rules to target_hostsfile, excluding comments and skipping customtemphosts.txt
369+
sed '/#/d' "$PERSISTENT_DIR/custom*.txt" | grep -vFf "$PERSISTENT_DIR/customtemphosts.txt" >> "$target_hostsfile"
353370
# blacklist.txt
354371
for i in $(sed '/#/d' $PERSISTENT_DIR/blacklist.txt ); do echo "0.0.0.0 $i" >> $rwdir/temphosts; done
355372
# whitelist.txt
@@ -381,6 +398,24 @@ reset() {
381398
}
382399

383400
run() {
401+
echo "[+] checking fetch opt in & opt out"
402+
# Define the ignore flag (you can set this dynamically from outside or by passing a parameter)
403+
UPDATESTATUS=false # Default behavior is to check for "yes" in disable/update.txt
404+
405+
# Check if the script is called with a flag to ignore the "yes" check
406+
for arg in "$@"; do
407+
case $arg in
408+
-update)
409+
UPDATESTATUS=true
410+
;;
411+
*)
412+
# Ignore other arguments
413+
;;
414+
esac
415+
done
416+
417+
# Export the variable if needed in other functions
418+
export UPDATESTATUS
384419
adblock
385420
# store these as variables
386421
# this way we dont do the grepping twice
@@ -466,6 +501,36 @@ hosts_lastmod () {
466501
echo "[+] Last update at: $(date -r $target_hostsfile)"
467502
}
468503

504+
update () {
505+
run -update
506+
}
507+
508+
disable_fetching() {
509+
# Define the path for the file and directory
510+
local disable_file="$PERSISTENT_DIR/disable/update.txt"
511+
local disable_dir="$PERSISTENT_DIR/disable"
512+
513+
# Check if the disable file exists
514+
if [ -f "$disable_file" ]; then
515+
# If the file exists, it means fetching is disabled, so remove the file and the directory to enable it again
516+
rm "$disable_file"
517+
518+
# Remove the directory if it's empty
519+
if [ ! "$(ls -A "$disable_dir")" ]; then
520+
rmdir "$disable_dir"
521+
fi
522+
523+
echo "Fetching is now enabled."
524+
else
525+
# Otherwise, disable fetching by creating the file and the directory
526+
mkdir -p "$disable_dir"
527+
echo "yes" > "$disable_file"
528+
529+
echo "Fetching is now disabled."
530+
run -update
531+
fi
532+
}
533+
469534
show_help () {
470535
echo "[%] $( grep '^description=' $MODDIR/module.prop | sed 's/description=//' )"
471536
echo "usage:"
@@ -477,18 +542,22 @@ show_help () {
477542
printf "\t\t\tif you do NOT know this, use --enable-cron\n"
478543
printf " --enable-cron \t\tenables scheduled updates (10AM daily)\n"
479544
printf " --disable-cron \tdisables scheduled updates\n"
545+
printf " --disable-fetching \tdisables auto-fetching\n"
546+
printf "\t\t\talong with --disable-fetching, --enable-cron [Recom.]\n"
547+
printf "\t\t\telse please --force-update, timely\n"
480548
printf " --help \t\tdisplays this message\n"
481549
}
482550

483551
# add arguments
484552
case "$1" in
485553
--action) action; exit ;;
486554
--tcpdump) tcpdump; exit ;;
487-
--force-update) run; exit ;;
555+
--force-update) update; exit ;;
488556
--force-reset) reset; exit ;;
489557
--custom-cron) custom_cron "$@"; exit ;;
490558
--enable-cron) enable_cron; exit ;;
491559
--disable-cron) disable_cron; exit ;;
560+
--disable-fetching) disable_fetching; exit;;
492561
--toggle-updatejson) toggle_updatejson; exit ;;
493562
--hosts-lastmod) hosts_lastmod; exit ;;
494563
--help|*) show_help; exit ;;

0 commit comments

Comments
 (0)