WireGuardのVPN上のパケット節約のためにAdBlockなDNSをdnsmasqで建てた
3 min read
こんにちは、無能です。
絶賛1.5Mbps回線なのですが広告あると一気に読み込み始まるのと無駄なパケットが発生するためこれをやめたい。
1.5Mbps使い放題とはいえど、3日で10GB制限はあるようで200kbps通信になったとき地獄だったので申し訳ないのですがVPN上で問い合わせするDNSを自分環境下で建てて広告ブロックします。
dsnmasqをインストール
私の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回線だと、広告配信されるだけで結構致命的なので導入してよかったかな?
それではまた。よろしくお願いします。