2022-06-10 22:55:25 +03:00
|
|
|
#!/usr/bin/nu
|
|
|
|
|
|
|
|
# /etc/hosts update handler
|
|
|
|
module hosts {
|
|
|
|
def display_heads [old, new] {
|
|
|
|
echo "Current file:"
|
|
|
|
echo $old
|
|
|
|
echo "New file:"
|
|
|
|
echo $new
|
|
|
|
print ""
|
|
|
|
}
|
|
|
|
|
|
|
|
def are_the_same [old_head, new_head] {
|
|
|
|
(($old_head | first) == ($new_head | first))
|
|
|
|
}
|
|
|
|
|
|
|
|
# Updater function for /etc/hosts
|
|
|
|
export def update [
|
|
|
|
--force (-f) # force replace /etc/hosts
|
|
|
|
] {
|
|
|
|
# just sample values, feel free to change it but note that it works for StevenBlack files
|
|
|
|
let LINK = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/gambling-porn/hosts"
|
|
|
|
let WHITELIST = ["multipasko"]
|
|
|
|
let BLACKLIST = ["0.0.0.0 tiktok.com"]
|
|
|
|
|
2023-04-03 16:47:25 +03:00
|
|
|
let whitelisted = "(" + ($WHITELIST | str join "|") + ")"
|
2022-06-10 22:55:25 +03:00
|
|
|
let pattern = ($"0.0.0.0.*($whitelisted).*$")
|
|
|
|
let OLD_FILE = "/etc/hosts"
|
|
|
|
let TMP_FILE = (fetch $LINK | lines)
|
|
|
|
|
|
|
|
if ($env.LAST_EXIT_CODE == 0) {
|
|
|
|
let OLD_HEAD = (open $OLD_FILE --raw | lines | first 8 | last 3)
|
|
|
|
let TMP_HEAD = ($TMP_FILE | first 8 | last 3)
|
|
|
|
|
|
|
|
display_heads $OLD_HEAD $TMP_HEAD
|
|
|
|
if (not ((are_the_same $OLD_HEAD $TMP_HEAD) and (not $force))) {
|
|
|
|
echo "Do you want to update the /etc/hosts file? [Y/n]"
|
|
|
|
let choice = (input)
|
|
|
|
if $choice in ["" "Y" "y"] {
|
2022-09-05 17:43:01 +03:00
|
|
|
let TMP_FILE = if ($WHITELIST|is-empty) {
|
2022-06-10 22:55:25 +03:00
|
|
|
($TMP_FILE)
|
|
|
|
} else {
|
|
|
|
($TMP_FILE | where {|line| $line !~ $pattern})
|
|
|
|
}
|
|
|
|
|
|
|
|
let TMP_FILE = ($TMP_FILE | append $BLACKLIST)
|
|
|
|
|
2022-07-28 11:07:02 +03:00
|
|
|
$TMP_FILE | save /tmp/temphostsfile
|
2022-06-10 22:55:25 +03:00
|
|
|
if ($env.LAST_EXIT_CODE == 0) {
|
2022-07-28 11:07:02 +03:00
|
|
|
sudo mv /tmp/temphostsfile $OLD_FILE
|
2022-06-10 22:55:25 +03:00
|
|
|
echo "Done!"
|
|
|
|
} else {
|
2022-07-28 11:07:02 +03:00
|
|
|
error make -u {msg: "Something went wrong while overwriting the /etc/hosts file"}
|
2023-04-03 16:47:25 +03:00
|
|
|
}
|
|
|
|
}
|
2022-06-10 22:55:25 +03:00
|
|
|
} else {
|
|
|
|
echo "No updates available."
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-28 11:07:02 +03:00
|
|
|
error make -u {msg: "Failed downloading the hosts file, try again."}
|
2023-04-03 16:47:25 +03:00
|
|
|
}
|
2022-06-10 22:55:25 +03:00
|
|
|
}
|
|
|
|
}
|