linux 開発 OS Windows

WSL2にSquidをインストール

2021年8月2日

WSL2にProxyサーバとして動作させるためにプロキシサーバ(Squid)をインストールしました。

WSL2ではIPアドレスがホストOSと分離しているため、外部PCからWSL2のSquidに接続させるには結構手間がかかります。(+PC起動ごとにIPアドレスが変更されるので継続利用不向き)
外部PC・ホストOSからも利用想定の場合はWSL1で接続することをオススメします。

コンピュータ情報

WSL2を利用しており、LinuxのディストリビューションはUbuntu20.04.2 LTSで試します。
WSL2のインストール設定はこちらを参考にしてください。

root@DESKTOP-0MO29FJ:~# uname -a
Linux DESKTOP-0MO29FJ 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
root@DESKTOP-0MO29FJ:~# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.2 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.2 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

squidインストール

Ubuntuのパッケージにsquidがあるため、「apt-get install」でインストールします。
「sudo su -」で予めrootに入っていますが、各項目sudoでの操作でも可能です。
インストールについては特に何事もなくインストール完了できると思います。

root@DESKTOP-0MO29FJ:~# apt-get install squid
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libdbi-perl libecap3 squid-common squid-langpack ssl-cert
Suggested packages:
  libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl squidclient squid-cgi squid-purge resolvconf
  smbclient winbind openssl-blacklist
The following NEW packages will be installed:
  libdbi-perl libecap3 squid squid-common squid-langpack ssl-cert
0 upgraded, 6 newly installed, 0 to remove and 48 not upgraded.
Need to get 3687 kB of archives.
After this operation, 15.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libecap3 amd64 1.0.1-3.2ubuntu1 [17.4 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 squid-langpack all 20191103-1 [167 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 squid-common all 4.10-1ubuntu1.4 [194 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal/main amd64 libdbi-perl amd64 1.643-1 [730 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal/main amd64 ssl-cert all 1.0.39 [17.0 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 squid amd64 4.10-1ubuntu1.4 [2562 kB]
Fetched 3687 kB in 3s (1195 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libecap3:amd64.
(Reading database ... 32232 files and directories currently installed.)
Preparing to unpack .../0-libecap3_1.0.1-3.2ubuntu1_amd64.deb ...
Unpacking libecap3:amd64 (1.0.1-3.2ubuntu1) ...
Selecting previously unselected package squid-langpack.
Preparing to unpack .../1-squid-langpack_20191103-1_all.deb ...
Unpacking squid-langpack (20191103-1) ...
Selecting previously unselected package squid-common.
Preparing to unpack .../2-squid-common_4.10-1ubuntu1.4_all.deb ...
Unpacking squid-common (4.10-1ubuntu1.4) ...
Selecting previously unselected package libdbi-perl:amd64.
Preparing to unpack .../3-libdbi-perl_1.643-1_amd64.deb ...
Unpacking libdbi-perl:amd64 (1.643-1) ...
Selecting previously unselected package ssl-cert.
Preparing to unpack .../4-ssl-cert_1.0.39_all.deb ...
Unpacking ssl-cert (1.0.39) ...
Selecting previously unselected package squid.
Preparing to unpack .../5-squid_4.10-1ubuntu1.4_amd64.deb ...
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
Unpacking squid (4.10-1ubuntu1.4) ...
Setting up squid-langpack (20191103-1) ...
Setting up ssl-cert (1.0.39) ...
Setting up libdbi-perl:amd64 (1.643-1) ...
Setting up libecap3:amd64 (1.0.1-3.2ubuntu1) ...
Setting up squid-common (4.10-1ubuntu1.4) ...
Setting up squid (4.10-1ubuntu1.4) ...
Setcap worked! /usr/lib/squid/pinger is not suid!
invoke-rc.d: could not determine current runlevel
Created symlink /etc/systemd/system/multi-user.target.wants/squid.service → /lib/systemd/system/squid.service.
invoke-rc.d: could not determine current runlevel
Processing triggers for ufw (0.36-6) ...
Processing triggers for systemd (245.4-4ubuntu3.6) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
root@DESKTOP-0MO29FJ:~#

squid設定

後で設定変更の確認ができるようにconfファイルのコピーを取っておきます。

変に設定が変になった場合でもバックアップ取ってると戻しやすくなりますね。

root@DESKTOP-0MO29FJ:~# cp -a /etc/squid/squid.conf /etc/squid/squid.conf_20210801

設定ファイルで必要な箇所を確認・変更していきます。
※56行目のみ変更してます。

root@DESKTOP-0MO29FJ:~# vim /etc/squid/squid.conf
~~1188行目付近~~
# ローカルネットワークの設定
acl localnet src 0.0.0.1-0.255.255.255  # RFC 1122 "this" network (LAN)
acl localnet src 10.0.0.0/8             # RFC 1918 local private network (LAN)
acl localnet src 100.64.0.0/10          # RFC 6598 shared address space (CGN)
acl localnet src 169.254.0.0/16         # RFC 3927 link-local (directly plugged) machines
acl localnet src 172.16.0.0/12          # RFC 1918 local private network (LAN)
acl localnet src 192.168.0.0/16         # RFC 1918 local private network (LAN)
acl localnet src fc00::/7               # RFC 4193 local private network range
acl localnet src fe80::/10              # RFC 4291 link-local (directly plugged) machines

# SSLポートの設定
acl SSL_ports port 443

# Safe_portの設定(設定したポート以外はProxy接続を許可しない設定)
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http

# https通信時などに利用されるCONNECTメソッドの設定
acl CONNECT method CONNECT

~~1391行目付近~~
# Deny requests to certain unsafe ports(Safe_ports以外のポートはproxy接続禁止)
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports(SSL通信(443ポート)以外はCONNECTメソッド利用禁止)
http_access deny CONNECT !SSL_ports

# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
include /etc/squid/conf.d/*

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
# localnetを有効化
http_access allow localnet
http_access allow localhost

~~1906行目~~
#Proxyのポート番号を設定
# Squid normally listens to port 3128
http_port 3128
黒字に青背景はものすごくみにくい…

設定変更したらserviceコマンドでsquidを起動します。

root@DESKTOP-0MO29FJ:~# service squid start
 * Starting Squid HTTP Proxy squid                                                                               [ OK ]

WSL2でsystemctl使おうとすると「System has not been booted with systemd as init system (PID 1). Can't operate.」でエラーが発生するためserviceコマンドで実行しています。
(理由はエラーに記載されている通りPID1がsystemdじゃないのが原因なのですが、直すにはごにょごにょしないといけないので割愛)

動作確認

別PCもしくはホストからproxyが使える確認します。
まずはcurlで実行してみます。

root@DESKTOP-0MO29FJ:~# curl https://kaede.jp --head -x http://localhost:3128
HTTP/1.1 200 Connection established

HTTP/2 200
server: nginx
date: Sun, 01 Aug 2021 14:32:57 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
x-cached: Sun, 01 Aug 2021 14:32:57 GMT
last-modified: Sun, 01 Aug 2021 14:32:57 GMT
link: <https://kaede.jp/wp-json/>; rel="https://api.w.org/"
x-accel-expires: 86400
strict-transport-security: max-age=31536000; includeSubDomains
x-cache: MISS
access-control-allow-origin: https://kaede.jp
access-control-allow-methods: POST, GET, OPTIONS
access-control-allow-credentials: true
access-control-allow-headers: Origin, Authorization, Accept
x-frame-options: SAMEORIGIN
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
allow: GET, POST, HEAD

squidのアクセスログ(/var/log/squid/access.log)からアクセスされているか確認できます。
HTTPSでの通信なのでCONNECTメソッドで利用になっています。

1627828368.288   1069 127.0.0.1 TCP_TUNNEL/200 4913 CONNECT kaede.jp:443 - HIER_DIRECT/133.130.119.115 -

雑感

WSL2ではsquidのインストールは簡単にできました。

ローカルからsquidを利用した通信は簡単にできますが、ホストOS及び外部PCからsquidにアクセスさせようとすると大変なので、そこまでは需要なさそう。

WSL1でもsquidのインストール難易度は同じなので、別PCからのアクセスも考慮するならWSL1のほうがいいと思います。

-linux, 開発, OS, Windows
-, ,