uechi.io/source/_posts/2013/osx-http-proxy.md

95 lines
3.1 KiB
Markdown
Raw Permalink Normal View History

2017-10-07 11:43:26 +09:00
---
title: OS Xのネットワーク環境に合わせてHTTP_PROXYを切り替えるシェルスクリプト
2017-12-02 13:24:20 +09:00
date: 2013-11-05 09:00:00 +09:00
2019-08-14 21:24:49 +09:00
redirect_from: "/blog/2013/11/05/osx-http-proxy"
2017-10-07 11:43:26 +09:00
---
2018-06-18 14:04:33 +09:00
![](/uploads/osx-http-proxy.png)
2017-10-07 11:43:26 +09:00
大学のネットワークに接続している時だけプロキシを設定したい時がある。
2018-07-19 20:53:12 +09:00
Mac のネットワーク環境は`networksetup -getcurrentlocation`コマンドで取得することが出来るので、
2017-10-07 11:43:26 +09:00
2022-12-24 03:20:02 +09:00
**.zshrc** 辺りに以下のシェルスクリプトを書いておけば Terminal で新しいタブを開いた時に自動でプロキシを設定してくれる。
2017-10-07 11:43:26 +09:00
```bash
proxy=proxy.hogehoge.ac.jp
switch_trigger=大学
if [ "`networksetup -getcurrentlocation`" = "$switch_trigger" ]; then
export HTTP_PROXY=$proxy
export FTP_PROXY=$proxy
...以下省略
fi
```
2018-07-19 20:53:12 +09:00
## Git のプロキシ設定も書き換えたい
2017-10-07 11:43:26 +09:00
2018-07-19 20:53:12 +09:00
Git は http_proxy を見てくれないのでリモートリポジトリに push 出来なくて困ることがあった。そこで http_proxy と一緒に Git のプロキシ設定も書き換えるようにしたい。
2017-10-07 11:43:26 +09:00
2018-07-19 20:53:12 +09:00
Git のプロキシは以下のコマンドで設定出来る。`--global`の代わりに`--system`を使っても良い。
2017-10-07 11:43:26 +09:00
```bash
git config --global http.proxy $proxy
git config --global https.proxy $proxy
git config --global url."https://".insteadOf git://
```
逆に `git config` から設定を削除したい場合は`git config --gobal --unset {item}`を使えば良い。
先ほどのコマンドと組み合わせることで最終的なコードは以下のようになる。
2022-12-24 03:20:02 +09:00
```bash switch_proxy.sh
2017-10-07 11:43:26 +09:00
proxy=proxy.hogehoge.ac.jp:80
switch_trigger=大学
function set_proxy() {
export http_proxy=$proxy
export HTTP_PROXY=$proxy
export ftp_proxy=$proxy
export FTP_PROXY=$proxy
export all_proxy=$proxy
export ALL_PROXY=$proxy
export https_proxy=$proxy
export HTTPS_PROXY=$proxy
git config --global http.proxy $proxy
git config --global https.proxy $proxy
git config --global url."https://".insteadOf git://
}
function unset_proxy() {
unset http_proxy
unset HTTP_PROXY
unset ftp_proxy
unset FTP_PROXY
unset all_proxy
unset ALL_PROXY
unset https_proxy
unset HTTPS_PROXY
git config --global --unset http.proxy
git config --global --unset https.proxy
git config --global --unset url."https://".insteadOf
}
if [ "`networksetup -getcurrentlocation`" = "$switch_trigger" ]; then
echo "Switch to proxy for university network"
set_proxy
else
unset_proxy
fi
```
2018-07-19 20:53:12 +09:00
このコードを **.zshrc** に保存して適当なターミナルで新しいセッションを開くと、`switch_trigger`で指定されたネットワーク環境下にいる時だけプロキシを通すことが出来る。
2017-10-07 11:43:26 +09:00
2022-12-24 03:20:02 +09:00
しかし既に開いているセッションに対してプロキシを適用する方法はわからなかった。
2017-10-07 11:43:26 +09:00
2018-07-19 20:53:12 +09:00
Workaround として、コードを **~/.switch_proxy** 辺りに置いて、
2017-10-07 11:43:26 +09:00
2022-12-24 03:20:02 +09:00
```bash ~/.zshrc
2017-10-07 11:43:26 +09:00
alias nswitch=~/.switch_proxy
2018-07-19 20:53:12 +09:00
```
2022-12-24 03:20:02 +09:00
2017-10-07 11:43:26 +09:00
`.zshrc`に書いておくことで、`nswitch`とタイプしてプロキシを切り替えられるようになる。