paloma blog

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

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

前回インストールしたprivoxyですが、ログが出力されていなかったので有効にします。
ついでに広告ブロックもやってみます。

前回勘違いしていましたが、コンテンツフィルタをしたいわけではないのでProxyサーバに証明書は入れなくても大丈夫ですね。 リバースプロキシとごっちゃになっていたようです。

HTTPSの場合はCONNECTメソッドを投げてプロキシは中継するだけみたいですね。
TLSの終端は通常通りClient - Serverでやると。

Privoxyでやりたいこと

とりあえずこれだけできればOKです。

トラフィックログ出力

マニュアルを読むとデフォルトだとログ出力されず、デバッグを有効にしないといけないようです。

http://www.privoxy.org/user-manual/config.html#LOGFILE

The logfile is where all logging and error messages are written. The level of detail and number of messages are set with the debug option (see below).

デバッグにも出力レベルがあります。 接続先が分かればOKですが、とりあえず推奨レベルをONにします。

debug     1 # Log the destination for each request Privoxy let through. See also debug 1024.
debug   512 # Common Log Format
debug  1024 # Actions that are applied to all sites and maybe overruled later on.
debug  4096 # Startup banner and warnings
debug  8192 # Non-fatal errors

サービス再起動

masashi@DESKTOP-986MNSO:~/privoxy-blocklist$ sudo /etc/init.d/privoxy restart

ログを見てみると…

masashi@DESKTOP-986MNSO:~/privoxy-blocklist$ tail -20 /var/log/privoxy/logfile
(略)
2018-08-18 17:05:29.183 7f0e420b0700 Info: exiting by signal 15 .. bye
2018-08-18 17:05:29.385 7f06e5d30700 Info: Privoxy version 3.0.24
2018-08-18 17:05:29.385 7f06e5d30700 Info: Program name: /usr/sbin/privoxy
2018-08-18 17:05:29.385 7f06e5d30700 Info: Loading filter file: /etc/privoxy/default.filter
2018-08-18 17:05:29.388 7f06e5d30700 Info: Loading filter file: /etc/privoxy/user.filter
2018-08-18 17:05:29.388 7f06e5d30700 Info: Loading actions file: /etc/privoxy/match-all.action
2018-08-18 17:05:29.388 7f06e5d30700 Info: Loading actions file: /etc/privoxy/default.action
2018-08-18 17:05:29.392 7f06e5d30700 Info: Loading actions file: /etc/privoxy/user.action
2018-08-18 17:05:29.393 7f06e5d30700 Info: Listening on port 8118 on IP address 0.0.0.0
2018-08-18 17:05:29.440 7f06e4500700 Request: play.google.com:443/
2018-08-18 17:05:31.147 7f06e30b0700 Request: api.twitter.com:443/
2018-08-18 17:05:31.148 7f06e28a0700 Request: twitter.com:443/
2018-08-18 17:05:34.164 7f06e2090700 Request: tweetdeck.twitter.com:443/
2018-08-18 17:05:39.442 7f06e38c0700 Request: ocsp.digicert.com/
127.0.0.1 - - [18/Aug/2018:17:05:44 +0900] "POST http://ocsp.digicert.com/ HTTP/1.1" 200 0
2018-08-18 17:05:50.232 7f06e38c0700 Request: pbs.twimg.com:443/

おおー取れてますね。
これで変なとこに投げていないか確認できますね。
ということはこのログを分析するツールも作らないといけないわけですね。

あとログローテートもしないとファイルが大きくなってしまうので後でやっておきます。

To prevent the logfile from growing indefinitely, it is recommended to periodically rotate or shorten it.

広告ブロック有効化

Arch Wikiの手順に沿ってやります。
Arch Wikiはいろんな機能の記事が豊富にあるので助かります。

普段FirefoxAdblockを使っているのでこのフィルタを使わせてもらいます。

Privoxy - ArchWiki

ツールをインストール

masashi@DESKTOP-986MNSO:~$ git clone https://github.com/Andrwe/privoxy-blocklist.git
Cloning into 'privoxy-blocklist'...
remote: Counting objects: 7, done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 7
Unpacking objects: 100% (7/7), done.
Checking connectivity... done.
masashi@DESKTOP-986MNSO:~$
masashi@DESKTOP-986MNSO:~$ cd privoxy-blocklist/
masashi@DESKTOP-986MNSO:~/privoxy-blocklist$ ls
privoxy-blocklist.sh  privoxy-blocklist_test.sh  README.md
masashi@DESKTOP-986MNSO:~/privoxy-blocklist$ ./privoxy-blocklist.sh
Root privileges needed. Exit.

…

権限がない?のでsudoで

masashi@DESKTOP-986MNSO:~/privoxy-blocklist$ sudo ./privoxy-blocklist.sh
[sudo] password for masashi:
The config directory /etc/conf.d doesn't exist. Please either adjust the variable SCRIPTCONF in this script or create the directory.

今度はファイルがない…作ってくれないのか。

masashi@DESKTOP-986MNSO:~/privoxy-blocklist$ sudo ./privoxy-blocklist.sh
No config found in /etc/conf.d/privoxy-blacklist. Creating default one and exiting because you might have to adjust it.
masashi@DESKTOP-986MNSO:~/privoxy-blocklist$

ファイルができたようです。
手順に沿ってコメントインします。

12行目から3行をコメントイン

 12 PRIVOXY_USER="privoxy"
 13 PRIVOXY_GROUP="privoxy"
 14 PRIVOXY_CONF="/etc/privoxy/config"

インストールします

masashi@DESKTOP-986MNSO:~/privoxy-blocklist$ sudo ./privoxy-blocklist.sh
Processing https://easylist-downloads.adblockplus.org/easylistgermany.txt ...

Downloading https://easylist-downloads.adblockplus.org/easylistgermany.txt ...
.. downloading done.
install: invalid group ‘privoxy’

(略)

install: invalid group ‘privoxy’
... http://adblockplus.mozdev.org/easylist/easylist.txt installed successfully.

フィルターをダウンロードしてprivoxyのフォーマットに変換して書き込んでくれたようです。


訂正

上記スクリプトですが、上手く動いていませんでした。
成功するとeasylistgermany.script.xxxというファイルとconfigファイルに追記してくれます。
別の環境ですが、以下のようになります。

失敗
vagrant@vagrant:~/privoxy-blocklist$ sudo ./privoxy-blocklist.sh
Processing https://easylist-downloads.adblockplus.org/easylistgermany.txt ...

Downloading https://easylist-downloads.adblockplus.org/easylistgermany.txt ...
.. downloading done.
sed: character class syntax is [[:space:]], not [:space:]
install: invalid group ‘privoxy’
(略)

よく見るとグループ無効のメッセージが…

vagrant@vagrant:~/privoxy-blocklist$ diff -u /etc/privoxy/config ../config.old

configも変化なしです。

成功

スクリプトの中身に沿ってグループを作成します。(ユーザーはパッケージインストール時に作成済み)

vagrant@vagrant:~/privoxy-blocklist$ sudo groupadd privoxy
vagrant@vagrant:~/privoxy-blocklist$ sudo ./privoxy-blocklist.sh
Processing https://easylist-downloads.adblockplus.org/easylistgermany.txt ...

Downloading https://easylist-downloads.adblockplus.org/easylistgermany.txt ...
.. downloading done.
sed: character class syntax is [[:space:]], not [:space:]

Modifying /etc/privoxy/config ...
... modification done.

Installing new config ...
... installation done
(略)

invalid groupのメッセージが消えました。

vagrant@vagrant:~/privoxy-blocklist$ diff -u /etc/privoxy/config ../config.old
--- /etc/privoxy/config 2022-01-08 05:03:49.799406948 +0000
+++ ../config.old       2022-01-08 05:01:43.028048888 +0000
@@ -383,7 +383,6 @@
 #
 actionsfile match-all.action # Actions that are applied to all sites and maybe overruled later on.
 actionsfile default.action   # Main actions file
-actionsfile easylistgermany.script.action
 actionsfile user.action      # User customizations
 #
 #  2.6. filterfile

configファイルに追記されてます。

vagrant@vagrant:~/privoxy-blocklist$ ls -l /etc/privoxy/
total 332
-rwxr-xr-x 1 privoxy privoxy 74163 Jan  8 05:03 config
-rw-r--r-- 1 root    root    92760 Mar 18  2021 default.action
-rw-r--r-- 1 root    root    40764 Mar 18  2021 default.filter
-rwxr-xr-x 1 privoxy privoxy 53215 Jan  8 05:03 easylistgermany.script.action
-rwxr-xr-x 1 privoxy privoxy    54 Jan  8 05:03 easylistgermany.script.filter
-rw-r--r-- 1 privoxy root      827 Mar 18  2021 match-all.action
-rw-r--r-- 1 root    root    39828 Mar 18  2021 regression-tests.action
drwxr-xr-x 2 root    root     4096 Dec 31 04:19 templates
-rw-r--r-- 1 privoxy root     3646 Mar 18  2021 trust
-rw-r--r-- 1 privoxy root     8685 Mar 18  2021 user.action
-rw-r--r-- 1 root    root     3236 Mar 18  2021 user.filter

easylistgermany.script.xxxが作成されてます。


再起動して試してみます。 FirefoxAdblockを切って…

f:id:paloma69:20180818172353p:plain

効いているようです!
でもブロックログとかは出ないんですかね。
膨大になっちゃうか。

とりあえず動いたのでよし!
でも画面表示が1秒くらい遅くなった気がします。
この辺は支障がでてきたらキャッシュサーバも検討しようと思います。
というか読み込みが遅くなったらわざわざ広告カットした意味がないような(笑)
スマホとか見るときに見やすくなるからいいか。

ちなみにスマホからプロキシONにしたけどページが表示されませんでした。
デフォゲのルータのほう向いちゃってるのが悪いのかな?
ルータにリダイレクト入れればできるか…これは今度やってみます。

前回の宿題

WSLのubuntuはsystemdが無いって書きましたがissueに上がっていたようです。
中身まだ読めてません…。

https://github.com/Microsoft/WSL/issues/1579

参考サイト
https://blog.himanoa.net/entries/19

追記

広告ブロックスクリプトが実はうまくいっていなかったので訂正を追加しました。
このファイルがないのでブロックできていなかったことになりますが、uriのパターンで弾くものがいくつかあったので効果はありました。

広告ブロック機能がうまくいくかは再度デプロイしたものがありますのでこちらを運用してみて確かめます。

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