LoginSignup
17
19

More than 5 years have passed since last update.

ncでお手軽tcpプロキシ

Last updated at Posted at 2016-01-15

tl;dr

起動

# 8080に来たリクエストを192.168.59.103:8080に流す
# リクエスト保存先: outgoing.log
# レスポンス保存先: incoming.log
while :; do [[ -p fifo ]] || mkfifo fifo && nc -l 8080 < fifo | tee outgoing.log | nc 192.168.59.103 8080 | tee fifo > incoming.log; done
# ^Cで終了

ユースケース

Boot2Dockerでnginxを起動して手元のスマホからアクセス

# nginx起動してブラウザで確認
docker run -d --name nginx -p 8080:80 -v $DIR_PATH_DOCUMENT_ROOT:/usr/share/nginx/html:ro nginx

# プロキシ開始
while :; do [[ -p fifo ]] || mkfifo fifo && nc -l 8080 < fifo | tee outgoing.log | nc `boot2docker ip` 8080 | tee fifo > incoming.log; done

#### 別のシェルを開く
# ローカルホストでLISTENしていることを確認
netstat -anf inet | grep LISTEN | grep 8080

# ローカルホストのIPを確認
LOCAL_IP=`ifconfig en0 | grep "inet[^6]" | cut -d" " -f2`

# アクセス先アドレスを表示
echo http://$LOCAL_IP/

PCとスマホが同じLANに接続している必要があります。
PCのChromeで開けば、スマホのChromeの履歴から、PCで開いたアドレスを開くことが出来ます。

参考URL

17
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
19