macにmitmproxyのインストール

スマートフォンバイスの通信内容を確認するにはmitmproxyが便利
Charlesという選択肢もあるけど、無料版には制限があるっぽいのでmitmproxyにしたっすー
モバイルアプリ開発者のための mitmproxy 入門

$ pip install mitmproxy

...中略...

Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 316, in clobber
    ensure_dir(destdir)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/idna'

と出るので、

$ sudo pip install mitmproxy

として再度実行。今度は

  Found existing installation: six 1.4.1
    DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling six-1.4.1:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 778, in install
    requirement.uninstall(auto_confirm=True)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 754, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_uninstall.py", line 115, in remove
    renames(path, new_path)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/utils/__init__.py", line 267, in renames
    shutil.move(old, new)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
    copystat(src, dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
    os.chflags(dst, st.st_flags)
OSError: [Errno 1] Operation not permitted: '/tmp/pip-LZOuw_-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

と出たので、

$ sudo pip install mitmproxy --upgrade --ignore-installed six

として再度実行。今度は

etree.so
    ld: file not found: /usr/lib/system/libsystem_darwin.dylib for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    error: command 'cc' failed with exit status 1
    
    ----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-TWzXvc/lxml/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-WUlpQ7-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/tmp/pip-build-TWzXvc/lxml/

と出てコケるので、

$ sudo cp /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/lib/libSystem.B.tbd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/lib/libSystem.B.tbd.bk
$ sudo vi /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/lib/libSystem.B.tbd
-                       /usr/lib/system/libsystem_coreservices.dylib, /usr/lib/system/libsystem_darwin.dylib, 
+                       /usr/lib/system/libsystem_coreservices.dylib, 

として実行。これでようやく入った。と思ったが起動すると

$ mitmproxy

Error: mitmproxy requires Python 3.5 or above

Starting with version 1.0 released in 12/2016, mitmproxy no longer supports Python 2.

To install the latest version of mitmproxy using pip on Python 3, run:

    pip uninstall mitmproxy
    pip3 install mitmproxy

You can also use our standalone binaries that come with their own Python interpreter:

    http://docs.mitmproxy.org/en/stable/install.html

To get rid of this message and use the last version of mitmproxy that supports Python 2, run:

    pip install "mitmproxy==0.18.2"


Apologies for the inconvenience!

となるので、

$ sudo pip uninstall mitmproxy
$ sudo pip install "mitmproxy==0.18.2" --upgrade --ignore-installed six

と入れ直し。

$ mitmproxy

端末設定

iOSの場合、wifi設定から「HTTPプロキシ」の設定を行う。

  • サーバー:自分のマシンの IP アドレス
  • ポート番号:8080
  • 認証:オフ

これで端末から通信すればコンソール上で内容確認が出来るっすー

コンソール操作は、

  • j,kで上下移動
  • enterで詳細
  • tabでRequest, Response, Detailのタブ切り替え
  • qで一覧へ戻る
  • Ctr+cで終了

モバイルアプリ開発者のための mitmproxy 入門 が大変詳しくて親切です