Last active
March 18, 2024 13:54
-
-
Save Lathanao/a1371ccbb7a529107d0f64a643e4ba41 to your computer and use it in GitHub Desktop.
Enhance performance through prefetching lookup IP in hosts file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash +xe | |
# Enhance performance through prefetching lookup IP in hosts file (MIT License) | |
# by Lathanao (last update: 24/02/2024) | |
USER=$(last | grep "login screen" | tail -n 1 | awk '{print $1}') | |
TMP_DIR="$(mktemp -d)" | |
PATH_HISTORY="/home/$USER/.config/chromium/Default/History" | |
STRING_ALERT="# AUTOMATICALY ADDED BY SCRIPT" | |
UPDATE_ALERT="# UPDATED ON $(date +'%Y-%m-%d %T')" | |
## REMOVE PREVIOUS RECORDED IPS FROM HOSTS FILE | |
################################################################################ | |
sudo sed -i "/${STRING_ALERT}/q" /etc/hosts | |
sudo sed -i "/$STRING_ALERT/d" /etc/hosts | |
sudo echo $STRING_ALERT >> /etc/hosts | |
sudo echo $UPDATE_ALERT >> /etc/hosts | |
## DUMP IN CSV THE BROWSER HISTORY FROM the SQLITE FILE | |
################################################################################ | |
cp $PATH_HISTORY $TMP_DIR | |
cd $TMP_DIR | |
sqlite3 $TMP_DIR/History << EOF | |
.headers on | |
.mode csv | |
.output history.csv | |
SELECT url FROM urls ORDER BY last_visit_time DESC; | |
.quit | |
EOF | |
## SORT AND LOOKUP ALL TOP 100 DOMAINS FOUND IN THE HISTORY | |
################################################################################ | |
TMP_CSV=$TMP_DIR/history.csv | |
for url in $(awk -F, '{print $1}' $TMP_CSV | tr "/" " " | awk '{print $2}' | sort | uniq -c | sort -nr | awk '{print $2}' | grep -P "^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$" | head -n 50) ; do | |
echo $(dig +short @9.9.9.9 $url | tail -n 1) $url >> /etc/hosts | |
done | |
## LOG | |
################################################################################ | |
LOG_DIR=/var/log/usercron | |
LOG_FILE=/var/log/usercron/update-hosts.log | |
if [[ ! -d "$LOG_DIR" ]];then | |
sudo mkdir -p $LOG_DIR | |
sudo chmod -R 0700 $LOG_DIR | |
sudo chown -R $USER:$USER $LOG_DIR | |
fi | |
if [[ ! -f "$LOG_FILE" ]];then | |
sudo touch $LOG_FILE | |
sudo chmod 0700 $LOG_FILE | |
sudo chown $USER:$USER $LOG_FILE | |
fi | |
DATE=$(date +"%Y-%m-%d %T") | |
echo $DATE $(whoami) "updated /etc/host file" >> $LOG_FILE | |
echo "Log file updated in $LOG_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment