為了在 WireGuard VPN 上節省封包,使用 dnsmasq 建立了 AdBlock DNS
大家好,我是個無能的人。
我目前使用的是1.5Mbps的網路連線,但有廣告時會突然開始載入,並產生不必要的封包,所以我希望停止這種情況。
雖然是1.5Mbps吃到飽,但似乎有三天10GB的限制,當速度降到200kbps時簡直是地獄,所以很抱歉,我將在自己的環境中建立一個DNS伺服器,透過VPN查詢來阻擋廣告。
安裝 dnsmasq
我的WireGuard伺服器是FreeBSD環境,所以使用pkg。
$ pkg search dnsmasq
dnsmasq-2.90_2,1 Lightweight DNS forwarder, DHCP, and TFTP server
$ pkg install dnsmasq
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 7 package(s) will be affected (of 0 checked):
New packages to be INSTALLED:
dnsmasq: 2.90_4,1
gettext-runtime: 0.23
gmp: 6.3.0
indexinfo: 0.3.1
libidn2: 2.3.7
libunistring: 1.2
nettle: 3.10.1
Number of packages to be installed: 7
The process will require 15 MiB more space.
3 MiB to be downloaded.
Proceed with this action? [y/N]: y
[1/7] Fetching indexinfo-0.3.1.pkg: 100% 6 KiB 5.9kB/s 00:01
[2/7] Fetching libidn2-2.3.7.pkg: 100% 155 KiB 158.7kB/s 00:01
[3/7] Fetching dnsmasq-2.90_4,1.pkg: 100% 390 KiB 398.9kB/s 00:01
[4/7] Fetching nettle-3.10.1.pkg: 100% 2 MiB 1.6MB/s 00:01
[5/7] Fetching libunistring-1.2.pkg: 100% 683 KiB 699.4kB/s 00:01
[6/7] Fetching gmp-6.3.0.pkg: 100% 498 KiB 510.4kB/s 00:01
[7/7] Fetching gettext-runtime-0.23.pkg: 100% 235 KiB 241.2kB/s 00:01
Checking integrity... done (0 conflicting)
[1/7] Installing indexinfo-0.3.1...
[1/7] Extracting indexinfo-0.3.1: 100%
[2/7] Installing libunistring-1.2...
[2/7] Extracting libunistring-1.2: 100%
[3/7] Installing gmp-6.3.0...
[3/7] Extracting gmp-6.3.0: 100%
[4/7] Installing libidn2-2.3.7...
[4/7] Extracting libidn2-2.3.7: 100%
[5/7] Installing nettle-3.10.1...
[5/7] Extracting nettle-3.10.1: 100%
[6/7] Installing gettext-runtime-0.23...
[6/7] Extracting gettext-runtime-0.23: 100%
[7/7] Installing dnsmasq-2.90_4,1...
[7/7] Extracting dnsmasq-2.90_4,1: 100%
=====
Message from dnsmasq-2.90_4,1:
--
To enable dnsmasq, edit /usr/local/etc/dnsmasq.conf and
set dnsmasq_enable="YES" in /etc/rc.conf[.local]
Further options and actions are documented inside
/usr/local/etc/rc.d/dnsmasq
SECURITY RECOMMENDATION
~~~~~~~~~~~~~~~~~~~~~~~
It is recommended to enable the wpad-related options
at the end of the configuration file (you may need to
copy them from the example file to yours) to fix
CERT Vulnerability VU#598349.
$ echo 'dnsmasq_enable="YES" ' >> /etc/rc.local
將以下內容作為要套用的設定檔路徑追記到/usr/local/etc/dnsmasq.conf。
conf-dir=/usr/local/etc/dnsmasq.d
建立資料夾
mkdir /usr/local/etc/dnsmasq.d
cd /usr/local/etc/dnsmasq.d
接著導入adblock清單。
curl -o /usr/local/etc/dnsmasq.d/adblock.conf https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
因為dnsmasq無法讀取上述內容,所以需要進行格式化。
cat adblock.conf | awk '/^0.0.0.0/ {print "address=/"$2"/0.0.0.0"}' > tmp
rm adblock.conf
mv tmp adblock.conf
確認設定檔
最低限的設定如下。
# cat dnsmasq.conf | grep -v '^#' | grep [A-z]
no-resolv
server=1.1.1.1
interface=wg0
listen-address=10.0.0.1
log-queries
conf-dir=/usr/local/etc/dnsmasq.d
啟動
這樣就可以啟動了。
# service dnsmasq start
Starting dnsmasq.
WireGuard 用戶端
將用戶端的DNS伺服器設定為dnsmasq的ListenIP。因此,這次會是10.0.0.1。
測試
這次,對於adblock.conf中記載的網域,由於用戶端是智慧型手機,因此用手機連線後可以確認無法成功存取。
這樣一來,就可以透過根據需要添加提供廣告的網域來進行阻擋。
dnsmasq 包含子網域的指定
如下所示,在網域前加上.即可指定包含子網域的網域。
address=/.test.com/0.0.0.0
對於我智慧型手機的1.5Mbps網路連線來說,光是廣告投放就相當致命,所以導入這個功能應該是正確的吧?
那麼,下次再見。請多指教。