forked from nushell/nu_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_hosts.nu
63 lines (55 loc) · 2.18 KB
/
update_hosts.nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/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"]
let whitelisted = "(" + ($WHITELIST | str join "|") + ")"
let pattern = ($"0.0.0.0.*($whitelisted).*$")
let OLD_FILE = "/etc/hosts"
let TMP_FILE = (http get $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"] {
let TMP_FILE = if ($WHITELIST|is-empty) {
($TMP_FILE)
} else {
($TMP_FILE | where {|line| $line !~ $pattern})
}
let TMP_FILE = ($TMP_FILE | append $BLACKLIST)
$TMP_FILE | save /tmp/temphostsfile
if ($env.LAST_EXIT_CODE == 0) {
sudo mv /tmp/temphostsfile $OLD_FILE
echo "Done!"
} else {
error make -u {msg: "Something went wrong while overwriting the /etc/hosts file"}
}
}
} else {
echo "No updates available."
}
} else {
error make -u {msg: "Failed downloading the hosts file, try again."}
}
}
}