以前に Ubuntu 22.04LTS へ Squid をインストールする というメモを書きました。 Squid でホワイトリストによるアクセス制限を実施する設定例をメモしておきます。
インストール
apt
でインストールします。
apt -y install squid apache2-utils
今回はバージョン 5.2 がインストールされました。
# squid -v
Squid Cache: Version 5.2
設定
Squid 本体の設定は以下の通りです。 下記の 2 行がポイントです。
acl whitelist dstdomain "/etc/squid/whitelist.txt"
http_access allow whitelist
cat << EOF > /etc/squid/squid.conf
acl any src 0.0.0.0/0
acl any src ::/0
acl SSL_ports port 443
acl Safe_ports port 80
acl Safe_ports port 443
acl CONNECT method CONNECT
include /etc/squid/conf.d/*.conf
acl whitelist dstdomain "/etc/squid/whitelist.txt"
http_access allow whitelist
http_access deny all
http_port 8080
cache deny all
httpd_suppress_version_string on
visible_hostname unknown
forwarded_for off
logformat squid %ts.%03tu %6tr %>a %Ss/%03>Hs %<st %rm %ru %[un %Sh/%<a %mt %tl
coredump_dir /var/spool/squid
EOF
ホワイトリストは以下としました。 ホワイトリストは Squid のプロセスが起動するタイミングでしか読み込まれません (Squid 実行中に、動的にホワイトリストを変更しても読み込まれません)。 その為、ホワイトリストを更新した場合は Squid の再起動が必要です。
cat << EOF > /etc/squid/whitelist.txt
.google.com
EOF
Squid を再起動し、設定を反映します。
systemctl restart squid
動作確認
今回はホワイトリストに .google.com
を登録しています。 dig
で名前解決すると、このタイミングでは以下の値が得られました。
# dig +short www.google.com.
172.217.26.228
Squid を経由させた上で「1. FQDN 指定」と「2. IP アドレス直接指定」を試すと、ホワイトリストによって許可された FQDN 指定のみ、コンテンツが得られるはずです。
curl -x http://10.0.0.1:8080 https://www.google.com
curl -x http://10.0.0.1:8080 https://172.217.26.228
尚、後者の「2. IP アドレス直接指定」は Squid によって拒否される為、curl
の実行結果は以下になりました。
# curl -x http://172.29.0.101:8080 https://172.217.26.228
curl: (56) Received HTTP code 403 from proxy after CONNECT
コメント