BrowserMob Proxyを使ってみる

github.com

JavaアプリとかSeleniumとかで使うのがメインみたいだけど、とりあえず挙動を掴むためにstandaloneで動かしてみる。

Windows環境では

$ bin/browsermob-proxy.bat

でBrowserMob Proxyを起動する。UNIX系OS(LinuxとかMacとか)ではbin/browsermob-proxyというシェルスクリプトがあるので、そこから起動すれば良さそう。

プロセスが起動したら、その後はREST API経由で使っていくらしい。

新しいインスタンスを作る

$ curl -X POST http://localhost:8080/proxy
{"port":8081}

新しいHARをオープン

$ curl -X PUT http://localhost:8080/proxy/8081/har

適当なURLにアクセス。 そのままだと証明書のエラーが出るので -k オプションでエラーを無視する。

$ curl https://www.google.co.jp/ -k -x http://localhost:8081

HARを取得

$ curl http://localhost:8080/proxy/8081/har

proxyインスタンスを削除

$ curl -X DELETE http://localhost:8080/proxy/8081

証明書エラーを何とかする

$ curl https://www.google.co.jp/ -x http://localhost:8081
curl: (60) SSL certificate problem: self signed certificate in certificate chain
More details here: https://curl.haxx.se/docs/sslcerts.html

GitHub - lightbody/browsermob-proxy: A free utility to help web developers watch and manipulate network traffic from their AJAX applications.

BrowserMob Proxy 2.1.0+ now supports full MITM: For most users, MITM will work out-of-the-box with default settings. Install the ca-certificate-rsa.cer file in your browser or HTTP client to avoid untrusted certificate warnings. Generally, it is safer to generate your own private key, rather than using the .cer files distributed with BrowserMob Proxy. See the README file in the mitm module for instructions on generating or using your own root certificate and private key with MITM.

ということのようなので、BrowserMob Proxyが提供する ca-certificate-rsa.cer をインポートするとよいらしい。

$ export CURL_CA_BUNDLE=$PWD/ca-certificate-rsa.cer
$ curl https://www.google.co.jp/ -x http://localhost:8081