目标:使用系统脚本(MacOS 的.sh 或 Windows的.bat)修改hosts文件。用于访问GitHub等DNS被污染的域名。

有自动更新hosts的程序,但不喜欢。

脚本逻辑:

  • 下载网络上最新的hosts文件。此处收集了三个。默认为第一个。
  • 将最新的hosts文件写入到本地的hosts文件
  • 添加需要的其他解析

注:脚本需修改remote_host的路径,即远程hosts保存在本地的路径。

MacOS

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
#! /bin/bash
#:: 获取网络上github hosts 并更新本地文件,同时更新网络缓存
github_host_url1="https://raw.hellogithub.com/hosts"
github_host_url2="https://hosts.gitcdn.top/hosts.txt"
github_host_url3="https://gitlab.com/ineo6/hosts/-/raw/master/next-hosts"

remote_host="/Users/Name/desktop/hosts"
local_host="/etc/hosts"
echo -e "\033[43m -----请选择host文件----- \033[0m"
echo "[1] GitHub520 --> hellogithub.com 每小时52分更新"
echo "[2] fetch-github-hosts --> gitcdn.top 每小时26分更新"
echo "[3] ineo6-hosts : gitlab.com 每小时11分更新"

read -t 3 user_input || echo "超时,执行默认选项 1 :"

echo -e "\033[48;5;202m 获取时间:$(date "+%H:%M") \033[0m"

url=$github_host_url1
# 执行基于用户输入的操作
if [ -n "$user_input" ]; then
#echo "你输入了:$user_input"
case $user_input in
2)
url=$github_host_url2
;;
3)
url=$github_host_url3
;;
*)
;;
esac
# 在这里执行基于用户输入的操作
fi

# 下载文件
curl -o "$remote_host" "$url"

content=$(cat $remote_host)
echo "$content"> $local_host

# figma
figma="18.65.216.112 www.figma.com"
echo "$figma" >> $local_host

echo "删除临时文件..."
rm "$remote_host"

echo "已更新hosts..."
echo ""
echo ""
#dscacheutil -flushcache
killall -HUP mDNSResponder

将以上的脚本保存为github-hosts.sh,然后执行脚本:sudo ./github-hosts.sh

Windows

  • wget是一个命令行下载工具,可在这里下载。(Windows10自带?)
  • 下载解压完成后将wget.exe文件复制到C:\Windows\System32\目录下。
  • 或在“系统变量”中添加wget.ext所在路径到PATH变量中。
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
@echo off
:: 2023-7-22 脚本
:: 获取网络上github hosts 并更新本地文件,同时更新网络缓存
set github_host_url1=https://raw.hellogithub.com/hosts
set github_host_url2=https://hosts.gitcdn.top/hosts.txt
set github_host_url3=https://gitlab.com/ineo6/hosts/-/raw/master/next-hosts

set remote_host=C:\Users\Name\Desktop\hosts
set local_host=C:\Windows\System32\drivers\etc\hosts

choice /c 123 /t 3 /d 1 /m "Please Choose Github Hosts Url\n :"
if %errorlevel%==2 (
set url=%github_host_url2%
) else if %errorlevel%==3 (
set url=%github_host_url3%
) else (
set url=%github_host_url1%
)
wget %url% -O %remote_host%
echo =================================================================
echo =================================================================
echo ==== ====
echo ==== Hosts File Has Download ====
echo ==== ====
echo =================================================================
echo =================================================================
type %remote_host% > %local_host%

echo #### 追加写入内容1 >> %local_host%
echo #### 追加写入内容2 >> %local_host%
echo #### 追加写入内容3 >> %local_host%

del %remote_host%

ipconfig /flushdns

choice /c "exit" /t 3 /d i /m "Finish !"

将以上脚本保存为github-hosts.bat,系统中的hosts文件权限设置为用户可写。然后右键以管理员身份运行

其他

stackoverflow的访问。由于其部分使用了Google的CDN,所以会导致访问速度变慢,甚至人机验证码无法显示。可以在浏览器安装ReplaceGoogleCDN插件解决。