たぶん動く...

多分GIS系の人。あくまで個人的見解であり、所属団体を代表するものではありません。

AdGuard HomeをAzure VMで稼働させる

筆者のスマホソフトバンクのミニフィットプランを契約しています。外出中はスマホでゲームをしたり、動画をみたりしないので、3GBあればデータ通信量は十分なはずですでした。しかし最近出社する頻度が増えたことで通勤の電車の中でスマホでXをみたり、2chまとめを見ていたせいか、3GB以上を使い通信制限に遭うようになりました。(契約変えろよって話ですが...)
そこで今回は、AdGuard Homeを使用して、自前のDNSサーバを作成しネット広告を減らすことでスマホのデータ通信料を削減したいと思います。

AdGuard Homeとは

簡単にいうとDNSサーバのソフトです。広告とトラッキング(追跡)をブロックすることができます。 サイトに埋め込まれている広告の読み込みをはじくことでスマホがサイトを読み込むときの通信量の削減ができます。

最安のインスタンスを立てる

2024年1月時点でのAzureで提供されている最小構成VMはStandard_B1ls (1 コア 0.5 GB RAM) になります。リージョンは日本です。
OSはUbuntuにしました。 DNSサーバ以外の用途は考えていないのでこのスペックで十分と考えます。

項目 設定値
インスタンスタイプ Standard_B1ls
ストレージ 32 GB
ストレージタイプ Standard HDD
OS Ubuntu 20.04.6 LTS

AdGuard Homeをインストール、初期設定する

インストールと初期設定は、以下のサイトが分かりやすかったです。 Raspberry Piへインストールする方法と変わりません。

🍺 UbuntuにAdGuard Homeをインストールする

ドメイン取得

Azure App Serviceからドメインを取得してもよいのですが、今回は欲しいドメインがAzureから取得できなかったのでお名前.comからドメインを取得しました。 お名前ドットコムで取得したドメインDNSゾーンでホストします。 (ドメインをAzureで管理しているわけではない。名前解決の手段としてDNSをホストしている。) 設定方法はこちらを参照しました。

【Azure×お名前.com】カスタムドメインを追加する #Azure - Qiita

お名前.comでドメイン取得

欲しいドメインを取得してください。

DNSゾーン作成

Azure PortalDNSゾーンを作成します。 このネームサーバーをメモします。

ネームサーバ設定

上の手順でメモしたネームサーバ名を、ドメインのネームサーバに設定します。
お名前.comのドメイン設定->ネームサーバ変更へと進みます。 他のネームサーバを利用を選択し、ネームサーバに上の手順でメモしたネームサーバに設定します。

ドメインチェック

ネームサーバを設定して、しばらく時間がたってから、digコマンドでチェックするとドメイン情報が返ってくるはずです。 以下はコマンド出力のサンプルになります。

$ dig example.net

; <<>> DiG 9.16.1-Ubuntu <<>> example.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 8685
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;example.net.                   IN      A

;; AUTHORITY SECTION:
example.net.            300     IN      SOA     XXXXXX.azure-dns.com. azuredns-hostmaster.microsoft.com. 1 3600 300 2419200 300

;; Query time: 12 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue Jan 23 13:42:51 UTC 2024
;; MSG SIZE  rcvd: 126

Get AnserでNOERRORと返ってきていること、 AUTHORITY SECTIONで SOAが返ってきていれば設定できています。

SSL証明書発行

この時点で、URLを使ってブラウザからアクセスすることはできますが、HTTP接続です。デバイスによっては通常のDNSを使用することができずDNS-over-TLSを使用することを求められます。 まずはCertbotを使用してSSL証明書を作成します。
ここでは、example.comというドメインを取得し、DNSサーバにはdns.example.comというレコードを設定すると仮定しています。

Certbotインストール
$ sudo snap install --classic certbot
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
$ certbot --version
certbot 2.8.0
SSL証明書発行

certbotを利用してSSL証明書を発行します。

$ certbot certonly --manual --preferred-challenges=dns --preferred-chain="ISRG Root X1"

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel):  [メールアドレスを入力]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o:   [規約を読んで同意するのであればY]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: n [団体からの通知を受け取るのであれば Y 必要なければ N]
Account registered.

Please enter the domain name(s) you would like on your certificate (comma and/or
space separated) (Enter 'c' to cancel):  dns.example.com   ← 設定するドメインを入力する
Requesting a certificate for dns.example.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:

_acme-challenge.dns.example.com.

with the following value:

2d9fIS2Svbi0vP-r1qivM8_oSLh4XQgCn7cpZ00C_AU

Before continuing, verify the TXT record has been deployed. Depending on the DNS
provider, this may take some time, from a few seconds to multiple minutes. You can
check if it has finished deploying with aid of online tools, such as the Google
Admin Toolbox: https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.dns.example.com.
Look for one or more bolded line(s) below the line ';ANSWER'. It should show the
value(s) you've just added.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

ここまで来たらAzure Portal上の操作に戻ります。
DNSゾーンのレコードセットから上で指示されたTXTレコードを登録します。

レコードを登録したら Admin Toolbox でTXTレコードが登録されたか確認します。

TXTレコードの登録が確認できました。

SSL証明書が発行できると、以下のフォルダに証明書と秘密鍵が出力されます。

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/dns.example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/dns.example.com/privkey.pem
This certificate expires on 2024-04-24.
These files will be updated when the certificate renews.

NEXT STEPS:
- This certificate will not be renewed automatically. Autorenewal of --manual certificates requires the use of an authentication hook script (--manual-auth-hook) but one was not provided. To renew this certificate, repeat this same certbot command before the certificate's expiry date.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

AdGuard Home HTTPS化

SSL証明書が発行できたのでAdGuard HomeをHTTPS化します。
AdGuard Homeの設定 -> 暗号化設定へと進みます。

Certbotが出力した証明書、秘密鍵のパスを入力し、構成を保存します。

ここまでできれば、AdGuard HomeでHTTPS接続、DNS-over-TLSを使用できます。

スマホ DNSサーバ設定

Androidの場合、設定 -> ネットワークとインターネット ->プライベートDNSへと進みます。
DNSサーバのURLを入力して保存します。

しばらく使用していれば、 DNSクエリの処理状況をAdGuard Homeで確認できます。

使用結果

AdGuard Homeを使用した削減効果はいかほどでしょうか。1/9にAdGuard Homeを導入しました。 12月と1月でデータ通信量を比較します。

↓12月 4.5GB使用

↓ 1月 1.81GB使用

約40%のデータ通信量削減に成功しました。

参考文献

Certbot Instructions | Certbot

広告除去ができるAdGuardHomeの設定方法 ​ #AdBlock - Qiita

Encryption · AdguardTeam/AdGuardHome Wiki · GitHub