paloma blog

NWエンジニアやってます。主に自宅環境のお遊びを書きます。Pythonもちょっと。タイトルは好きなカクテルから。

privoxyをdockerで作り直す 自分でbuild編

自宅のメイン機windowsのwsl上でprivoxyを建てていたのですが、PCが壊れてリプレースした際に環境が無くなってしまいました。

元々広告ブロック用途で使っていて、今どきのブラウザにも広告ブロックのプラグインはたくさんあるので特段困りませんが、低いレイヤのうちに防げるのであればそうすべきですよね。

リプレースの際にwsl2をインストールしたのでコンテナ環境も使えます。
lxdはサブ機で運用中なのでここはdockerで作り直してみようと思います。
そのまま作り直しは面白くないですしね。

dockerの管理ツールはたくさんありすが、私は詳しくなく基本からということで今回はシンプルなdockerコマンドを使います。

環境

wsl2にubuntuを入れています。

PS C:\Users\masashi> wsl --list --verbose
  NAME      STATE           VERSION
* Ubuntu    Running         2
masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi$ uname -a ; lsb_release -a
Linux DESKTOP-HBP3520 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
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04 LTS
Release:        20.04
Codename:       focal

インストール

ドキュメントに沿ってインストール。
ここは割愛します。

Install Docker Engine on Ubuntu | Docker Documentation

起動確認

サンプルのコンテナを起動してみます。

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi$ sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.
masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi$ sudo systemctl
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

あら、systemdが入っていない?

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi$ /etc/init.d/docker status
 * Docker is not running
masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi$ sudo /etc/init.d/docker start
 * Starting Docker: docker

initdで起動できたのでとりあえずこのままいきます。

dockerfile作成

docker hubにprivoxyのイメージはたくさんありますがpullして終わりでは面白くありません。
イメージも自分でbuildしてみます。

設定ファイルはデフォルトのイメージから修正するのもいいですが、
以前使用していたものが残っているので流用しましょう。

イメージは使い慣れているubuntuのLTSを使います。

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ cat dockerfile
FROM ubuntu:20.04

RUN apt-get update
RUN apt-get -y install privoxy

ADD config /etc/privoxy/config
ADD easylistgermany.script.action /etc/privoxy/easylistgermany.script.action
ADD easylistgermany.script.filter /etc/privoxy/easylistgermany.script.filter

EXPOSE 8118

CMD ["/usr/sbin/privoxy", "--no-daemon", "/etc/privoxy/config"]

デーモンで動かすと起動時に終了してしまう様なのでno-daemonオプションをつけてます。
(自分でも作成中に体験し、ドキュメントに仕様の旨書いてありました)

また、以前の記事で書いた際の広告ブロックファイルスクリプトですが、実は作れていませんでした。

WSLのPrivoxyの機能をいろいろ有効にする - paloma blog

スクリプトが正常完了すると以下のファイルが作成され、configファイルにも転記されます。

  • easylistgermany.script.action
  • easylistgermany.script.filter

上記ファイルとconfigの中身は長くなるので割愛します。

build

buildします。
イメージ名だけ指定しておきます。

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker build -t privoxy .
Sending build context to Docker daemon  326.1kB
Step 1/8 : FROM ubuntu:20.04
20.04: Pulling from library/ubuntu
ea362f368469: Pull complete
Digest: sha256:b5a61709a9a44284d88fb12e5c48db0409cfad5b69d4ff8224077c57302df9cf
Status: Downloaded newer image for ubuntu:20.04
 ---> d13c942271d6
Step 2/8 : RUN apt-get update
 ---> Running in 864589a5dcf5
()
Removing intermediate container 814c0ba9f29d
 ---> bf1ec1f0d64d
Step 4/8 : ADD config /etc/privoxy/config
 ---> e607594a8c2c
Step 5/8 : ADD easylistgermany.script.action /etc/privoxy/easylistgermany.script.action
 ---> c9e67ca752f3
Step 6/8 : ADD easylistgermany.script.filter /etc/privoxy/easylistgermany.script.filter
 ---> f1a2fbe29033
Step 7/8 : EXPOSE 8118
 ---> Running in a7f7a6c0f99d
Removing intermediate container a7f7a6c0f99d
 ---> 8c586e8febd8
Step 8/8 : CMD ["/usr/sbin/privoxy", "--no-daemon", "/etc/privoxy/config"]
 ---> Running in ab94d59f07d0
Removing intermediate container ab94d59f07d0
 ---> 640c3f36b965
Successfully built 640c3f36b965
Successfully tagged privoxy:latest

イメージ確認。

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
privoxy       latest    640c3f36b965   30 seconds ago   154MB
ubuntu        20.04     d13c942271d6   23 hours ago     72.8MB
hello-world   latest    feb5d9fea6a5   3 months ago     13.3kB

OK。

run

動かしてみます。

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker run -d privoxy
5a6cf1fea892963235cb6fef689a28392be4008180dc11a86a65e4a2d2722b4c
masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS      NAMES
5a6cf1fea892   privoxy   "/usr/sbin/privoxy -…"   7 seconds ago   Up 5 seconds   8118/tcp   angry_blackburn
masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker logs 5a6cf1fea892
(略)
2022-01-08 01:28:35.201 7f8195cd8180 Info: Listening on port 8118 on IP address 127.0.0.1

Listen IPをミスりました。anyで開けないと外から繋げないので再buildします。

再build

configファイルを修正して再build。
2回目なのでイメージのインストールはありません。
修正してもコマンド一発なので楽ですね。

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker build -t privoxy .
Sending build context to Docker daemon  326.1kB
Step 1/8 : FROM ubuntu:20.04
 ---> d13c942271d6
Step 2/8 : RUN apt-get update
 ---> Using cache
 ---> cf23bd572ee9
Step 3/8 : RUN apt-get -y install privoxy
 ---> Using cache
 ---> bf1ec1f0d64d
Step 4/8 : ADD config /etc/privoxy/config
 ---> 7af0693b72e4
Step 5/8 : ADD easylistgermany.script.action /etc/privoxy/easylistgermany.script.action
 ---> 88a12542d9b2
Step 6/8 : ADD easylistgermany.script.filter /etc/privoxy/easylistgermany.script.filter
 ---> 98a5188a6f71
Step 7/8 : EXPOSE 8118
 ---> Running in da034c635718
Removing intermediate container da034c635718
 ---> b8bf9110b82b
Step 8/8 : CMD ["/usr/sbin/privoxy", "--no-daemon", "/etc/privoxy/config"]
 ---> Running in f1f2b4f953c7
Removing intermediate container f1f2b4f953c7
 ---> 8bfb6a713d7d
Successfully built 8bfb6a713d7d
Successfully tagged privoxy:latest
masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
privoxy       latest    8bfb6a713d7d   8 seconds ago   154MB
<none>        <none>    640c3f36b965   5 minutes ago   154MB
ubuntu        20.04     d13c942271d6   23 hours ago    72.8MB
hello-world   latest    feb5d9fea6a5   3 months ago    13.3kB

同じ名前でbuildすると、前のイメージのものはnoneになってしまうようですね。

再度run

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker logs 0bb8b25f6539
2022-01-08 01:33:54.762 7f3c0bf78180 Info: Listening on port 8118 on IP address 0.0.0.0

接続確認。

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ curl -X GET -I -x localhost:8118 https://www.google.c
o.jp
curl: (7) Failed to connect to localhost port 8118: Connection refused

あら、ポートが上がっていない?
ポートフォワードのオプションを忘れていました。

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker run -d -p 8118:8118 privoxy
78af2722008edc8b06a653909606823ac4e37ea30e9a9ba8307a15305aec5b4f
masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS
     NAMES
78af2722008e   privoxy   "/usr/sbin/privoxy -…"   6 seconds ago   Up 3 seconds   0.0.0.0:8118->8118/tcp, :::8118->8118/tcp   compassionate_shamir

もう一回接続確認。

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ curl -X GET -I -x localhost:8118 https://www.google.c
o.jp
HTTP/1.1 200 Connection established

HTTP/2 200
()

OKですね。

ログ

ログを見てみます。
さっきのcurlが取れてますね。

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker logs 78af2722008e
2022-01-08 01:38:06.050 7fea7a619180 Info: Privoxy version 3.0.28
2022-01-08 01:38:06.050 7fea7a619180 Info: Program name: /usr/sbin/privoxy
2022-01-08 01:38:06.051 7fea7a619180 Info: Loading filter file: /etc/privoxy/default.filter
2022-01-08 01:38:06.052 7fea7a619180 Info: Loading filter file: /etc/privoxy/user.filter
2022-01-08 01:38:06.052 7fea7a619180 Info: Loading actions file: /etc/privoxy/match-all.action
2022-01-08 01:38:06.052 7fea7a619180 Info: Loading actions file: /etc/privoxy/default.action
2022-01-08 01:38:06.054 7fea7a619180 Info: Loading actions file: /etc/privoxy/easylistgermany.script.action
2022-01-08 01:38:06.057 7fea7a619180 Error: Missing content filter 'easylistgermany'
2022-01-08 01:38:06.057 7fea7a619180 Error: Invalid action section in file '/etc/privoxy/easylistgermany.script.action', starting at line 1300: { +filter{easylistgermany} }
2022-01-08 01:38:06.057 7fea7a619180 Info: Loading actions file: /etc/privoxy/user.action
2022-01-08 01:38:06.058 7fea7a619180 Info: Listening on port 8118 on IP address 0.0.0.0
2022-01-08 01:38:49.458 7fea7a617700 Request: www.google.co.jp:443/
172.17.0.1 - - [08/Jan/2022:01:38:49 +0000] "CONNECT www.google.co.jp:443 HTTP/1.1" 200 15551

ブラウザのProxy有効化

Firefoxをメインで使っています。
オプションからプロキシを有効化して再度ログを見てみます。

docker logsだとパイプが聞かなかったので抜粋です。

()
2022-01-08 01:42:33.450 7fea2cff9700 Request: safebrowsing.googleapis.com:443/
172.17.0.1 - - [08/Jan/2022:01:42:33 +0000] "CONNECT safebrowsing.googleapis.com:443 HTTP/1.1" 200 1794
2022-01-08 01:42:34.997 7fea2cff9700 Request: token.services.mozilla.com:443/
2022-01-08 01:42:35.333 7fea07fff700 Request: youtube.com:443/
2022-01-08 01:42:35.517 7fea6b7fe700 Request: ocsp.digicert.com/
172.17.0.1 - - [08/Jan/2022:01:42:35 +0000] "POST http://ocsp.digicert.com/ HTTP/1.1" 200 471
2022-01-08 01:42:35.752 7fea45ffb700 Request: sync-1-us-west1-g.sync.services.mozilla.com:443/
2022-01-08 01:42:35.832 7fea2effd700 Request: ocsp.digicert.com/
172.17.0.1 - - [08/Jan/2022:01:42:35 +0000] "POST http://ocsp.digicert.com/ HTTP/1.1" 200 471
172.17.0.1 - - [08/Jan/2022:01:42:45 +0000] "CONNECT token.services.mozilla.com:443 HTTP/1.1" 200 4807
2022-01-08 01:43:20.759 7fea2cff9700 Request: detectportal.firefox.com/canonical.html
172.17.0.1 - - [08/Jan/2022:01:43:20 +0000] "GET http://detectportal.firefox.com/canonical.html HTTP/1.1" 200 90
2022-01-08 01:43:20.851 7fea2effd700 Request: detectportal.firefox.com/success.txt?ipv4
2022-01-08 01:43:20.856 7fea6b7fe700 Request: detectportal.firefox.com/success.txt?ipv6
172.17.0.1 - - [08/Jan/2022:01:43:20 +0000] "GET http://detectportal.firefox.com/success.txt?ipv4 HTTP/1.1" 200 8
172.17.0.1 - - [08/Jan/2022:01:43:20 +0000] "GET http://detectportal.firefox.com/success.txt?ipv6 HTTP/1.1" 200 8
172.17.0.1 - - [08/Jan/2022:01:43:25 +0000] "CONNECT profile.accounts.firefox.com:443 HTTP/1.1" 200 4325
172.17.0.1 - - [08/Jan/2022:01:43:25 +0000] "CONNECT api.accounts.firefox.com:443 HTTP/1.1" 200 10190
172.17.0.1 - - [08/Jan/2022:01:44:17 +0000] "CONNECT firefox.settings.services.mozilla.com:443 HTTP/1.1" 200 5588

ちゃんとログ出てますね。
相変わらず裏でいろいろなところにアクセスしてます。
これで広告ブロック付きプロキシが復活しました。

残課題

アクセスログはdocker logsで確認できますが、フォアグラウンドで動かすとログファイルが作成されないようです。

masashi@DESKTOP-HBP3520:/mnt/c/Users/masashi/tools/docker/privoxy$ sudo docker exec -it 78af2722008e /bin/bash
root@78af2722008e:/# ls -l /var/log/privoxy/
total 0

前回はデーモンで動かしていましたが、フォアグラウンドだとコンソールに慣れ流しということでしょうか。
フォアグラウンド起動はdockerの仕様なので仕方ないですが、ログは残したいですね。
課題として残しておいてとりあえずは運用してみます。

感想

いつもはLXDのコンテナで遊んでいますがDocker環境も欲しいということで作ってみました。
LXDも面白いですが、コンテナ構築のお手軽さはやはりdockerですね。
サービスとして運用するにはいろいろ考えないといけませんが…とはいっても大企業はとっくに採用していると思いますが。

今回は自宅環境用のコンテナ作成ですが、ちゃんとしたdockerfileを作るまで何度もコンテナを作っては捨て、とできるのがいいですね。
wsl環境で一発ではなく、別の仮想環境でdockerfile作成からrunまでを繰り返して動作するprivoxyコンテナを作りました。

仮想環境と若干カーネルバージョンが違ったのでイメージの引っ越しではなくwslでもbuildしましたが、
最終版ファイルの管理と手順が甘く本番へのデプロイも少しミスしてしまいました。

自宅の環境なので問題ないですが、この辺もAnsibleとかで自動化したいですね。
まあ今回はお遊びで、もちろん仕事では手順書完備でやってますのでご心配なく。