2ch読むときに画像っぽいリンクをimgタグに書き換えるプロキシサーバー
プロキシサーバー通して2chを読む
前回、プロキシサーバーをMacOSに立てるのを書きましたが、それを進化させました。
やりたいこと(=できたこと)
これを
まとめサイトっぽくこう読みたい
どうやって
nginxでプロキシしてごにょごにょします。 前回と違って、「ngx_http_substitutions_filter_module」というモジュールを 使いたいのでnginxをビルドしなおします。 ngx_http_substitutions_filter_moduleは、複数の置換ルールと、正規表現が使えます。
ビルド。 (ビルドの仕方はhistoryから引っ張ってきた、違ってたらすいません)
./configure --prefix=/opt/nginx \ --conf-path=/opt/etc/nginx/nginx.conf \ --add-module=~/go/src/github.com/yaoweibin/ngx_http_substitutions_filter_module
nginx.confに置換ルールとか書く
# 大事 proxy_set_header Accept-Encoding ""; # http://pele.bbspink.com/test/read.cgi/ascii2d/1419084535/l50 location ~ /test/read.cgi/ { # ttp始まりの画像をimgに置換 subs_filter \sttp:\/\/(\S+)(jpg|jpeg|gif|png)\s "<img width=160 src=http://$1$2>" ir; # こんなのを置換 ->: <a href="http://pinktower.com/?i.imgur.com/DP70ncu.jpg" target="_blank">http://i.imgur.com/DP70ncu.jpg</a> subs_filter <a\shref="(\S+)"\starget(\S+)">http(\S+)(jpg|jpeg|gif|png)<\/a> "<img width=160 src=http$3$4>" ir; # こんなのを置換 ->: <a href="http://pinktower.com/?imgur.com/MeZwX4F" target="_blank">http://imgur.com/MeZwX4F</a> subs_filter <a\shref="(\S+)"\starget(\S+)">http:\/\/imgur(\S+)<\/a> "<img width=160 src=http://imgur$3.jpg>" ir; # プロキシを通したいホスト proxy_pass http://$host; } # dummyとしてproxy_pass登録しておけば"no resolver defined to resolve nginx" が防げるという、たぶんもっといいやりかたある location /dummy { proxy_pass http://pele.bbspink.com; } location /dummy2 { proxy_pass http://kilauea.bbspink.com; } location /dummy3 { proxy_pass http://aoi.bbspink.com; } # proxy.pacも返しちゃう location /proxy.pac { root html; index index.html index.htm; }
proxy.pac を書く
// proxy.pac function FindProxyForURL(url, host) { var proxy_yes = "PROXY 127.0.0.1:8080;"; var proxy_no = "DIRECT"; if (dnsDomainIs(host, "pele.bbspink.com")) { return proxy_yes; } if (dnsDomainIs(host, "kilauea.bbspink.com")) { return proxy_yes; } if (dnsDomainIs(host, "aoi.bbspink.com")) { return proxy_yes; } return proxy_no; }
大事な注意点
- グロ画像がダイレクトに目に入る時があるので、要注意です