-
Notifications
You must be signed in to change notification settings - Fork 5
/
hoststool
executable file
·99 lines (89 loc) · 3.11 KB
/
hoststool
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
# ====== Configuration =====
download_dir="$HOME/etc/hoststool" # 卸载时需要注意系统权限组的路径与用户管理员路径不同,但都必须是root用户
if [ ! -d "$download_dir" ]; then
sudo mkdir -p $download_dir
fi
source="https://raw.githubusercontent.com/JohyC/Hosts/main/hosts.txt"
# ====== Functions =====
showHelp() {
echo "-u [source_url] update github hosts. You can specify a custom host source"
echo "-rm remove appended hosts in /etc/hosts file"
echo "-b backup /etc/hosts to /etc/hosts.backup"
echo "-r recover hosts.backup to hosts"
echo "-l list files in /etc which contains \"hosts\""
}
remove(){
# Remove old content
begin=$(sed -n '/# ==== Github Start ====/=' /etc/hosts | awk 'NR==1{print}')
end=$(sed -n '/# ==== Github End ====/=' /etc/hosts | awk 'END{print}')
echo "[INFO] Removing old hosts. Start at line \"${begin}\", End at line \"${end}\""
sudo sh -c "cat /etc/hosts | sed \"${begin},${end}d\" > ${download_dir}/hosts.tmp"
if [ $? -ne 0 ]; then
## Trip failed
echo "[INFO] 当前 Host中 无旧的 Github Host 标记可清除"
else
## Trip succeed, move result
echo "[INFO] 清除旧的 Github Host 标记"
sudo cp /etc/hosts /etc/hosts.backup && sudo cp ${download_dir}/hosts.tmp /etc/hosts;
fi
}
# ====== Main =====
if [ $# -eq 0 ]; then showHelp;exit 0;fi
case "$1" in
-l)
ls /etc | grep hosts;
exit 0;;
-b)
sudo cp /etc/hosts /etc/hosts.backup;
exit 0;;
-r)
sudo cp /etc/hosts.backup /etc/hosts;
exit 0;;
-rm)
remove;
exit 0;;
-u)
if [ $2 ]; then source=$2;fi
sudo curl -o ${download_dir}/hosts ${source};
if [ $? -ne 0 ]; then
echo "[ERROR] 获取远程 host 出错,请尝试更换 source 或检查 $download_dir 读写权限"
osascript -e 'display notification "获取远程 host 出错,请尝试更换 source" with title "hoststool"'
exit 1
fi
# Validate host content length
lines=$(awk '{print NR}' ${download_dir}/hosts | tail -n1)
if [ $lines -lt 10 ]
then
echo '[ERROR] source无效,请更换为可用hosts源'
osascript -e 'display notification "source无效,请更换为可用hosts源" with title "hoststool"'
sudo rm ${download_dir}/hosts
exit 1
fi
# remove old contents
remove
# Add new hosts
sudo bash -c "echo '# ==== Github Start ====' >> /etc/hosts" # Add github host
if [ $? -ne 0 ]; then
echo "[ERROR] 无root权限,请尝试运行脚本手动输入密码"
osascript -e 'display notification "无root权限,请尝试运行脚本手动输入密码" with title "hoststool"'
sudo rm ${download_dir}/hosts.tmp
sudo rm ${download_dir}/hosts
exit 1;
fi
sudo bash -c "echo \"# Updated at $(date)\" >> /etc/hosts" # Add github host
sudo bash -c "cat ${download_dir}/hosts >> /etc/hosts";
sudo bash -c "echo '# ==== Github End ====' >> /etc/hosts"
sudo rm ${download_dir}/hosts.tmp
sudo rm ${download_dir}/hosts
echo "[INFO] Github Hosts 块更新于 $(date)"
osascript -e 'display notification "Github Hosts 已更新" with title "hoststool"'
exit 0;;
-h|--help)
showHelp;
exit 0;;
*)
echo "[ERROR] Unknown command";
showHelp;
exit 1;;
esac