avatar

Catalog
「转」为macOS打造一个防污染的本地DNS

本文转自 https://yojigen.tech/archives/post28/

前言

家里这边DNS劫持实在是有点严重,非常影响开发速度,所以我就使用dnsmasq和dnscrypt-proxy搭建了一个防污染防劫持的本地DNS。

搭建过程

首先要安装Homebrew,这个绝大多数用macOS的人应该都安装了吧,官网 https://brew.sh/ ,安装指令如下。

Code
1
/usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

然后就是安装dnsmasqdnscrypt-proxy两大组件了。

Code
1
2
brew install dnsmasq
brew install dnscrypt-proxy

装好之后,编辑配置文件,首先是dnsmasq的配置,文件路径/usr/local/etc/dnsmasq.conf

Code
1
2
3
4
5
6
7
8
#忽略本机DNS解析结果
no-resolv

#增加配置文件夹
conf-dir=/usr/local/etc/dnsmasq.d

#设置上游服务器为dnscrypt-proxy
server=127.0.0.1#5300

然后是dnscrypt-proxy的配置,文件路径
/usr/local/etc/dnscrypt-proxy.toml

Code
1
2
3
4
5
6
7
8
#本地监听5300端口
listen_addresses = [‘127.0.0.1:5300’]

#防劫持DNS使用谷歌和Cloudflare
server_names = [‘google’, ‘cloudflare’]

#设置容错DNS为114和OpenDNS
fallback_resolvers = [‘114.114.114.114:53’, ‘208.67.222.222:5353’]

配置好了之后,重启两个组件的服务。

Code
1
2
sudo brew services restart dnsmasq
sudo brew services restart dnscrypt-proxy

之后再把网络设置里的DNS地址修改为127.0.0.1,应用之后就可以享受无污染的DNS了。

国内白名单

虽然DNS没有了污染,但是国内网站DNS全都变慢了,这个肯定不能接受,所以我们接入 dnsmasq-china-list 项目,来优化本地的DNS。

Code
1
2
3
4
5
6
7
8
9
10
11
mkdir /usr/local/etc/dnsmasq.d
WORKDIR=“$(mktemp -d)”
curl https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf -o “$WORKDIR/accelerated-domains.china.conf”
curl https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf -o “$WORKDIR/bogus-nxdomain.china.conf”
curl https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/google.china.conf -o “$WORKDIR/google.china.conf”
curl https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf -o “$WORKDIR/apple.china.conf”
sudo cp -f “$WORKDIR/accelerated-domains.china.conf” /usr/local/etc/dnsmasq.d/accelerated-domains.china.conf
sudo cp -f “$WORKDIR/bogus-nxdomain.china.conf" /usr/local/etc/dnsmasq.d/bogus-nxdomain.china.conf
sudo cp -f “$WORKDIR/google.china.conf” /usr/local/etc/dnsmasq.d/google.china.conf
sudo cp -f “$WORKDIR/apple.china.conf" /usr/local/etc/dnsmasq.d/apple.china.conf
rm -rf “$WORKDIR”

OK大功告成。

一键安装配置脚本

项目地址: https://github.com/mouyase/mac_dnscrypt_installer

脚本使用:

Code
1
2
3
git clone https://github.com/mouyase/mac_dnscrypt_installer
cd mac_dnscrypt_installer
./install.sh
2