├── Github Host.alfredworkflow ├── uninstall.sh ├── hoststool.plist ├── install.sh ├── README.md └── hoststool /Github Host.alfredworkflow: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sansui233/mac-githubhosts-autoupdate/HEAD/Github Host.alfredworkflow -------------------------------------------------------------------------------- /uninstall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | sudo hoststool -rm 3 | sudo rm /usr/local/bin/hoststool # generated by install.sh 4 | sudo launchctl unload -w /Library/LaunchDaemons/hoststool.plist 5 | sudo rm /Library/LaunchDaemons/hoststool.plist # generated by install.sh 6 | sudo rm /etc/hosts.backup # generated by hoststool 7 | sudo rm -rf /etc/hoststool # generated by hoststool in launchdaemon 8 | sudo rm -rf $HOME/etc/hoststool # generated by hoststool in staff group 9 | sudo rm -r $HOME/etc >/dev/null 2>&1 # remove folder if empty 10 | sudo rm /Library/Logs/hoststool.log # generated by system launchDaemons 11 | echo "[INFO] Totally uninstalled. Bye" 12 | -------------------------------------------------------------------------------- /hoststool.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Labelhoststool 6 | 7 | ProgramArguments 8 | 9 | /usr/local/bin/hoststool 10 | -u 11 | 12 | 13 | RunAtLoad 14 | 15 | 16 | StartInterval 17 | 3600 18 | 19 | StandardOutPath 20 | /Library/Logs/hoststool.log 21 | 22 | StandardErrorPath 23 | /Library/Logs/hoststool.log 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ ! -d "/usr/local/bin" ]; then 4 | mkdir -p /usr/local/bin 5 | fi 6 | read -p "[INFO] Test before installing? 安装前是否测试脚本?(y/n)(Default:y): " flag 7 | case $flag in 8 | "n") 9 | ;; 10 | *) 11 | sudo ./hoststool -u; 12 | if [ $? -ne 0 ] 13 | then 14 | echo "[ERROR] Failed, exit. 脚本运行失败,退出安装" 15 | exit 1; 16 | fi 17 | esac 18 | 19 | echo "[INFO] Installing(1/3) Copy hoststool to /usr/local/bin" 20 | sudo cp ./hoststool /usr/local/bin/ 21 | echo "[INFO] Installing(2/3) Add hoststool.plist to /Library/LaunchDaemons" 22 | sudo cp ./hoststool.plist /Library/LaunchDaemons/ 23 | sudo chown root:admin /Library/LaunchDaemons/hoststool.plist 24 | echo "[INFO] Installing(3/3) Load launch daemon" 25 | sudo launchctl load -w /Library/LaunchDaemons/hoststool.plist 26 | 27 | read -p "[INFO] This launch daemon has run once. Show log file? 定时任务已运行一次,是否查看运行日志?(y/n)(Default:y): " flag 28 | case $flag in 29 | "n") 30 | ;; 31 | *) 32 | if [ ! -f "/Library/Logs/hoststool.log" ] 33 | then 34 | echo "[ERROR] Log file is not generated, some error occured during installation. Log 文件未生成,安装过程中出现问题。" 35 | else 36 | open /Library/Logs/hoststool.log 37 | fi 38 | esac 39 | 40 | echo "[INFO] DONE" 41 | exit 0; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Github Hosts Autoupdate for mac 2 | 3 | 此轻量级小工具将在 mac 上自动定时更新 github hosts 4 | 5 | - 默认情况下每小时更新一次。如需更改间隔请修改 hoststool.plist 文件中的`StartInterval`。 6 | - 使用的 hosts 源为 [JohyC/GithubHosts](https://github.com/JohyC/GithubHosts) 项目的 Github 源,可能偶尔无法连接。如需更换 hosts 源,请修改 `hoststool` 里的 `source` 变量,或在[手动运行](#使用)时指定url。 7 | 8 | ## 安装 9 | 10 | 下载源代码并安装。 11 | 12 | ```shell 13 | $ git clone https://github.com/Sansui233/mac-githubhosts-autoupdate.git && cd mac-githubhosts-autoupdate 14 | $ sudo ./install.sh 15 | ``` 16 | 17 | 成功安装后会立即运行一次。 18 | 19 | 此脚本使用 launchdaemon 定时运行,不会在后台长期挂起。可以使用 launchctl 查看其状态。 20 | 21 | ```shell 22 | $ sudo launchctl list | grep hoststool 23 | # 正常情况下的输出为。- 表示当前未运行,0 表示上次运行的状态正常。 24 | - 0 hoststool 25 | ``` 26 | 27 | 查看 log 可以打开系统的日志程序 Console,右边选择 Log,搜索 hoststool 查看运行日志。 28 | 29 | ## 使用 30 | 31 | 安装完成之后将自动运行,无需其他配置。 32 | 33 | 如需手动运行: 34 | 35 | ```shell  36 | $ hoststool # 查看可选参数 37 | $ hoststool -u # 更新 Github hosts 38 | $ hoststool -u remote_url # 更新时指定 hosts 源 39 | $ hoststool -rm # 清除 Github hosts 40 | ``` 41 | 42 | ## Alfred Workflow 43 | 44 | 此工作流提供了手动运行 hoststool 的 Alfred 快捷方式。 workflow 使用的 hoststool 独立于系统中安装的 hoststool。 45 | 46 | 下载仓库中的 alfred workflow,双击安装即可。由于修改 host 需要管理员权限,安装时需要在 `ROOTPASSWORD` 中填入当前用户密码字段。 47 | 48 | 之后在 Alfred 中运行 `hoststool` 即可更新 hosts。 49 | 50 | ## Log 中常见错误 51 | 52 | - [ERROR] 获取远程 host 出错,请尝试更换 source 或检查 download_dir 读写权限 53 | 54 | 此情况会在开机后第一次运行出现,因为开机时无网络连接,为正常报错。若有网络的情况下出现此信息,请按信息内容排查。 55 | 56 | - [ERROR] source无效,请更换为可用hosts源 57 | 58 | 此情况发生于获取到了远程文件,但文件内容可能被屏蔽,在Gitee上的托管的文件偶尔出现此类情况,建议更换 hoststool 文件中的 source 后重新安装此工具。 59 | 60 | ## 卸载 61 | 62 | ```shell 63 | $ ./uninstall.sh 64 | ``` 65 | 66 | ## Thanks 67 | 68 | - [JohyC/GithubHosts](https://github.com/JohyC/GithubHosts) 69 | - [Icon](https://icon-icons.com/zh/%E5%9B%BE%E6%A0%87/github/59223) © CC BY 4.0 -------------------------------------------------------------------------------- /hoststool: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # ====== Configuration ===== 4 | download_dir="$HOME/etc/hoststool" # 卸载时需要注意系统权限组的路径与用户管理员路径不同,但都必须是root用户 5 | if [ ! -d "$download_dir" ]; then 6 | sudo mkdir -p $download_dir 7 | fi 8 | source="https://raw.githubusercontent.com/JohyC/Hosts/main/hosts.txt" 9 | 10 | # ====== Functions ===== 11 | showHelp() { 12 | echo "-u [source_url] update github hosts. You can specify a custom host source" 13 | echo "-rm remove appended hosts in /etc/hosts file" 14 | echo "-b backup /etc/hosts to /etc/hosts.backup" 15 | echo "-r recover hosts.backup to hosts" 16 | echo "-l list files in /etc which contains \"hosts\"" 17 | } 18 | 19 | remove(){ 20 | # Remove old content 21 | begin=$(sed -n '/# ==== Github Start ====/=' /etc/hosts | awk 'NR==1{print}') 22 | end=$(sed -n '/# ==== Github End ====/=' /etc/hosts | awk 'END{print}') 23 | echo "[INFO] Removing old hosts. Start at line \"${begin}\", End at line \"${end}\"" 24 | sudo sh -c "cat /etc/hosts | sed \"${begin},${end}d\" > ${download_dir}/hosts.tmp" 25 | if [ $? -ne 0 ]; then 26 | ## Trip failed 27 | echo "[INFO] 当前 Host中 无旧的 Github Host 标记可清除" 28 | else 29 | ## Trip succeed, move result 30 | echo "[INFO] 清除旧的 Github Host 标记" 31 | sudo cp /etc/hosts /etc/hosts.backup && sudo cp ${download_dir}/hosts.tmp /etc/hosts; 32 | fi 33 | 34 | } 35 | 36 | # ====== Main ===== 37 | if [ $# -eq 0 ]; then showHelp;exit 0;fi 38 | 39 | case "$1" in 40 | -l) 41 | ls /etc | grep hosts; 42 | exit 0;; 43 | -b) 44 | sudo cp /etc/hosts /etc/hosts.backup; 45 | exit 0;; 46 | -r) 47 | sudo cp /etc/hosts.backup /etc/hosts; 48 | exit 0;; 49 | -rm) 50 | remove; 51 | exit 0;; 52 | -u) 53 | if [ $2 ]; then source=$2;fi 54 | sudo curl -o ${download_dir}/hosts ${source}; 55 | if [ $? -ne 0 ]; then 56 | echo "[ERROR] 获取远程 host 出错,请尝试更换 source 或检查 $download_dir 读写权限" 57 | osascript -e 'display notification "获取远程 host 出错,请尝试更换 source" with title "hoststool"' 58 | exit 1 59 | fi 60 | 61 | # Validate host content length 62 | lines=$(awk '{print NR}' ${download_dir}/hosts | tail -n1) 63 | if [ $lines -lt 10 ] 64 | then 65 | echo '[ERROR] source无效,请更换为可用hosts源' 66 | osascript -e 'display notification "source无效,请更换为可用hosts源" with title "hoststool"' 67 | sudo rm ${download_dir}/hosts 68 | exit 1 69 | fi 70 | 71 | # remove old contents 72 | remove 73 | 74 | # Add new hosts 75 | sudo bash -c "echo '# ==== Github Start ====' >> /etc/hosts" # Add github host 76 | if [ $? -ne 0 ]; then 77 | echo "[ERROR] 无root权限,请尝试运行脚本手动输入密码" 78 | osascript -e 'display notification "无root权限,请尝试运行脚本手动输入密码" with title "hoststool"' 79 | sudo rm ${download_dir}/hosts.tmp 80 | sudo rm ${download_dir}/hosts 81 | exit 1; 82 | fi 83 | sudo bash -c "echo \"# Updated at $(date)\" >> /etc/hosts" # Add github host 84 | sudo bash -c "cat ${download_dir}/hosts >> /etc/hosts"; 85 | sudo bash -c "echo '# ==== Github End ====' >> /etc/hosts" 86 | sudo rm ${download_dir}/hosts.tmp 87 | sudo rm ${download_dir}/hosts 88 | 89 | echo "[INFO] Github Hosts 块更新于 $(date)" 90 | osascript -e 'display notification "Github Hosts 已更新" with title "hoststool"' 91 | exit 0;; 92 | -h|--help) 93 | showHelp; 94 | exit 0;; 95 | *) 96 | echo "[ERROR] Unknown command"; 97 | showHelp; 98 | exit 1;; 99 | esac 100 | --------------------------------------------------------------------------------