Cloudflare: IPv4アドレスからIPv6アドレス専用サーバへのアクセス、Certbot DNS-01チャレンジ、WARP Client

Cloudflareの無料プロキシサービスを利用することにより、IPv4アドレスのみを付与されたクライアントからIPv6グローバルアドレスのみで稼働しているサーバへアクセスできるようにします。

同じことはクラウドサービスの仮想マシンを利用することでもできますが、有料であること、設定や維持管理がより面倒です。

ドメイン取得

Cloudflareを利用するためには、事前に専用のドメインを取得する必要があります。今回はPorkbunで取得したドメインを使用します。Porkbunでのドメイン取得、CertbotによるTLS認証については下記の記事を参照して下さい。

Porkbun管理画面より

Cloudflare: ドメイン登録

上記取得したドメインをCloudflareのFreeプランに登録します。ルートドメインとサブドメインのAAAA (IPv6) DNSレコードを追加します。

下記同じ画面でCloudflareのネームサーバが割当てられるので、Porkbunの管理画面で、PorkbunのネームサーバをCloudflareのネームサーバに置換えます。

サーバは既に、CertbotによりLet’s EncryptのTLS証明書を保有しているため、下記SSL/TLS設定画面で Full(strict) を選択します。SSL/TLS Recommenderを有効にします。

Cloudflare提供のTLS証明書を利用する場合

以上、Porkbunでのネームサーバ変更が反映されるまで時間を要しますが(最大で48時間)、以下DNSチェッカーでDNSレコードが反映されたかどうか確認できます。

また、ターミナルからは以下コマンドで、ネームサーバの確認と、IPv4アドレスからIPv6サーバへと通信ができるかどうか確認できると思います。

$ dig ficusonline.com NS +short
$ ping -4 ficusonline.com

DNSSEC

DNSSECの設定については下記参照のこと。

Cloudflareプロキシ経由のクライアントIPアドレスの引継ぎはNginxの ngx_http_realip_module を利用します。以下記事参照。

Nginxで追加する設定ファイルは以下の通りです。

real_ip_headerCloudflareが指定する CF-Connecting-IP に置換えます。

/etc/nginx/conf.d/real_ip_recursive.conf

set_real_ip_from    173.245.48.0/20;
set_real_ip_from    103.21.244.0/22;
set_real_ip_from    103.22.200.0/22;
set_real_ip_from    103.31.4.0/22;
set_real_ip_from    141.101.64.0/18;
set_real_ip_from    108.162.192.0/18;
set_real_ip_from    190.93.240.0/20;
set_real_ip_from    188.114.96.0/20;
set_real_ip_from    197.234.240.0/22;
set_real_ip_from    198.41.128.0/17;
set_real_ip_from    162.158.0.0/15;
set_real_ip_from    104.16.0.0/13;
set_real_ip_from    104.24.0.0/14;
set_real_ip_from    172.64.0.0/13;
set_real_ip_from    131.0.72.0/22;
set_real_ip_from    192.168.0.0/16;
set_real_ip_from    2400:cb00::/32;
set_real_ip_from    2606:4700::/32;
set_real_ip_from    2803:f800::/32;
set_real_ip_from    2405:b500::/32;
set_real_ip_from    2405:8100::/32;
set_real_ip_from    2a06:98c0::/29;
set_real_ip_from    2c0f:f248::/32;
real_ip_header    CF-Connecting-IP;
real_ip_recursive on;

Restoring original visitor IPs

CF-Connecting-IP (HTTP request headers)

Cloudflare IP Ranges

Web Application Firewall (WAF)

DNS-01チャレンジ

Welcome to certbot-dns-cloudflare’s documentation!

https://certbot-dns-cloudflare.readthedocs.io/en/stable/#welcome-to-certbot-dns-cloudflare-s-documentation

CertbotをDockerコンテナとして起動し、DNS-01チャレンジによるドメインのTLS認証と証明書の取得を行います。CertbotのデフォルトDockerイメージには、CloudflareのDNS-01チャレンジのプラグインが含めれていないため、以下のイメージを利用します。

Dockerイメージ
https://hub.docker.com/r/certbot/dns-cloudflare

チャレンジに必要なファイルは、以下のAPIトークンを含むINIファイルのみです。
Cloudflareのユーザ管理画面からDNSゾーン限定のトAPIークンを作成しcloudflare.iniファイルとして保存します。アクセス権は600に設定して下さい。

cloudflare.ini

# Cloudflare API token used by Certbot
dns_cloudflare_api_token = 0123456789abcdef0123456789abcdef01234567

DockerコンテナとしてCertbotを起動します。起動時にコンテナ内にcloudflare.iniファイルが存在する状態にしないと、ファイルが見つからないエラーが発生します。

これを回避するため、-vオプションで明示的にcloudflare.iniファイルをコンテナ内にコピーするようにして下さい(コンテナのワーキングディレクトリは/opt/certbot)。

$ docker run -it --rm --name certbot_cloudflare -v "/etc/letsencrypt:/etc/letsencrypt" -v "./cloudflare.ini:/opt/certbot/cloudflare.ini" certbot/dns-cloudflare:latest certonly --dns-cloudflare --dns-cloudflare-propagation-seconds 60 --dns-cloudflare-credentials ./cloudflare.ini -d example.com -d *.example.com

更新手続き

$ docker run -it --rm --name certbot_cloudflare -v "$PWD/cloudflare_letsencrypt:/etc/letsencrypt" -v "$PWD/cloudflare.ini:/opt/certbot/cloudflare.ini" certbot/dns-cloudflare:latest renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Certificate not yet due for renewal

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certificates are not due for renewal yet:
  /etc/letsencrypt/live/example.com/fullchain.pem expires on 2024-06-09 (skipped)
No renewals were attempted.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

DNS-01 Challengeプロバイダーリスト

DNS Hosting Provider ACME Client Support Cost
Akamai Edge DNS Certbot , lego , Posh-ACME , acme.sh Contract Specific
Aliyun (CN) & Alibaba Cloud DNS (EN) acme.sh , lego , Posh-ACME Bundled with domain registration or Cloud DNS pricing
Amazon Route53 Certbot , acme.sh , others ~$0.50/mo per domain
Azure DNS acme.sh , lego , Posh-ACME ~$0.50/mo per domain
Cloudflare Certbot , acme.sh , others Free (except for Freenom domains)
ClouDNS acme.sh , lego , Posh-ACME , others >= $2.95/mo (with API-support)
CloudXNS Certbot , acme.sh , lego Free, Chinese only
deSEC Certbot , acme.sh , others Free
DigitalOcean Certbot , acme.sh , others Free
DNS Made Easy Certbot , acme.sh , others $59.95/yr (Business Membership gives API access)
DNSimple Certbot , acme.sh , others $5/mo
DNSPod.com acme.sh , lego , Posh-ACME Free
DuckDNS acme.sh , lego , others Free
Dyn acme.sh , lego , others $7/mo
Dynu acme.sh , lego , Posh-ACME Free
EasyDNS acme.sh , lego , Posh-ACME Bundled with domain registration (250k queries/month) or $20/yr for 1 MM queries/month
FreeDNS/afraid.org acme.sh , Posh-ACME (no API, HTTP emulation) Free (if you share your domain with others )
Google Cloud DNS Certbot , acme.sh , others ~$0.20/mo
Hetzner lego , Posh-ACME Free
Hurricane Electric acme.sh , Posh-ACME (no API, HTTP emulation) Free
IBM Cloud DNS Posh-ACME $275/mo per domain for Standard plan
Luadns Certbot , acme.sh , others Free
MyDNS.jp acme.sh , lego Free
NS1 Certbot , acme.sh , others ? (Free “developer” plan)
Open Telekom Cloud lego 0,36 €/mo per zone
OVH Certbot , acme.sh , others Free
PointHQ acme.sh , Posh-ACME $25/mo per 10 domains
Rackspace Cloud DNS acme.sh , lego , Posh-ACME , others Free
Selectel acme.sh , lego , Posh-ACME Free
Shellrent Certbot 1, €/y per zone
StackPath lego $10/mo
Vultr acme.sh (via Lexicon), lego , others Free
Yandex.Mail acme.sh , lego , Posh-ACME Free
Zilore acme.sh , Posh-ACME $5/mo or higher for API access
Zonomi acme.sh , lego , Posh-ACME Free
Domain Registrar: Active24 acme.sh Bundled with domain registration
Domain Registrar: alwaysdata acme.sh Bundled with domain registration
Domain Registrar: ConoHa acme.sh , lego Bundled with domain registration (Japanese)
Domain Registrar: cyon.ch acme.sh Bundled with domain registration
Domain Registrar: do.de acme.sh , lego , Posh-ACME Bundled with domain registration
Domain Registrar: domeneshop acme.sh , Posh-ACME Bundled with domain registration.Note that domainname.shop (in English) is an alias of domene.shop (in Norwegian)
Domain Registrar: DreamHost acme.sh , lego , Posh-ACME ? (bundled with domain registration or hosting?)
Domain Registrar: Euserv acme.sh Bundled with domain registration
Domain Registrar: Exoscale acme.sh , lego Bundled with domain registration
Domain Registrar: Futurehosting acme.sh Bundled with domain registration
Domain Registrar: Gandi acme.sh , lego , Posh-ACME Bundled with domain registration
Domain Registrar: GoDaddy acme.sh , lego , Posh-ACME Bundled with domain registration, 10+ domains in account required to use
Domain Registrar: GratisDNS.dk acme.sh Bundled with domain registration (Danish)
Domain Registrar: hosting.de acme.sh , lego Bundled with domain registration (German)
Domain Registrar: infomaniak.com acme.sh , lego , Posh-ACME Bundled with domain registration (Swiss)
Domain Registrar: internetx.com acme.sh , lego , Posh-ACME Bundled with domain registration
Domain Registrar: inwx.de acme.sh , lego Bundled with domain registration
Domain Registrar: Loopia.se acme.sh , Posh-ACME Bundled with domain registration (Swedish)
Domain Registrar: name.com acme.sh , lego , Posh-ACME Bundled with domain registration
Domain Registrar: Namesilo Certbot , acme.sh , lego Bundled with domain registration
Domain Registrar: Neodigit.net acme.sh Bundled with domain registration (Spanish)
Domain Registrar: netcup acme.sh , lego Bundled with domain registration
Domain Registrar: Nexcess acme.sh Bundled with domain registration
Domain Registrar: Online.net acme.sh Bundled with domain registration
Domain Registrar: reg.ru (reg.com ) acme.sh , lego , Posh-ACME Bundled with domain registration (Russian)
Domain Registrar: Servercow acme.sh , lego Bundled with domain registration (German)
Domain Registrar: TELE3 acme.sh Bundled with domain registration (Czech)
Domain Registrar: UnoEuro acme.sh , Posh-ACME Bundled with domain registration
Domain Registrar: Zone.eu acme.sh , lego Bundled with domain registration
Web Host: KingHost acme.sh Free (adult-only web host)
Web Host: Linode Certbot , acme.sh , others Bundled with hosting
Web Host: Thermo.io acme.sh Variable hosting fee
Self-Hosted: acme-dns Certbot , acme.sh , others Free, Open Source
Self-Hosted: BlueCat Posh-ACME Enterprise DDI (Contract Specific)
Self-Hosted: cPanel Certbot , lego $20/mo licence or variable cost for shared cPanel hosting
Self-Hosted: DirectAdmin acme.sh Free
Self-Hosted: Infoblox acme.sh , Posh-ACME Enterprise DDI (Contract Specific)
Self-Hosted: ISPConfig acme.sh Free
Self-Hosted: Knot (knsupdate) acme.sh Free, Open Source
Self-Hosted: PowerDNS acme.sh , lego Free, Open Source
Self-Hosted: Simple DNS Plus Posh-ACME $79 for 5 zone license
Self-Hosted: Windows DNS Posh-ACME Free with Windows Server OS license

Wiki instructions:

Intermittent 521 522 523 error

pingテスト

HTTPS接続では、ポート443以外はキャッシュされないことが原因か?

HTTP ports supported by Cloudflare

  • 80
  • 8080
  • 8880
  • 2052
  • 2082
  • 2086
  • 2095

HTTPS ports supported by Cloudflare

  • 443
  • 2053
  • 2083
  • 2087
  • 2096
  • 8443

Ports supported by Cloudflare, but with caching disabled

  • 2052
  • 2053
  • 2082
  • 2083
  • 2086
  • 2087
  • 2095
  • 2096
  • 8880
  • 8443

ポート指定でのアクセスはサポートしていないようです。

Cloudflare Access does not support port numbers in URLs. Port numbers are stripped from requests for URLs protected through Cloudflare Access.


解決

サブドメインでルートドメインとは異なる別アドレスを指定していたため、CloudflareのProxyに起因するエラー。

Proxyを利用しない設定に変更することでエラー回避できるが、IPv4アドレスからのアクセスは出来なくなります。

SIPアドレスのAレコード追加(ポート限定)

サブドメインにルートドメインとは異なるIPアドレスを指定した場合、521, 522, 523 エラー(サーバ応答エラー)が発生するため、プロキシーステータスをDNSのみとすること。

Cloudflare WARP Client on Ubuntu 24.04

インストール

https://pkg.cloudflareclient.com/

gpg key(cloudflare) の追加

$ curl -fsSL https://pkg.cloudflareclient.com/pubkey.gpg | sudo gpg --yes --dearmor --output /usr/share/keyrings/cloudflare-warp-archive-keyring.gpg

レポジトリ追加

$ echo "deb [signed-by=/usr/share/keyrings/cloudflare-warp-archive-keyring.gpg] https://pkg.cloudflareclient.com/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/cloudflare-client.list

インストール

$ sudo apt-get update && sudo apt-get install cloudflare-warp

クライアント登録

$ warp-cli registration new

接続

$ warp-cli connect

切断

$ warp-cli disconnect

ステータス確認

$ curl https://www.cloudflare.com/cdn-cgi/trace/

warp=on または warp=off を確認


モード変更

ヘルプ

$ warp-cli mode --help

DNS only mode via DoH( DNS over HTTPS)

$ warp-cli mode doh

WARP with DoH

$ warp-cli mode warp+doh