爲 Mac Terminal 設置proxychains4代理

更好的方案

爲了使用 proxychains4 需要關閉 System Integrity Protection(SIP)系統完整性保護。不少小夥伴提出這樣影響系統穩定性。最近有網友提供了更好的方案。使用一下辦法添加倆 alias 來開啓和關閉終端代理。

.bash_profile 文件添加一下 alias:


 

1

2


 

alias setproxy="export http_proxy=http://127.0.0.1:8899; export https_proxy=$http_proxy; echo 'HTTP Proxy on';"

alias unsetproxy="unset http_proxy; unset https_proxy; echo 'HTTP Proxy off';"


 

1


 

http://127.0.0.1:8899 你的 ss 客戶端本地端口

使用方法:


 

1

2

3

4


 

$ setproxy #開啓代理

HTTP Proxy on

$ unsetproxy #關閉代理

HTTP Proxy off

推薦使用 PlutoX 作爲 SS 客戶端

SSH 隧道方法

如果沒有 ss 代理,可以使用 SSH 隧道方式使用 proxychains4。


 

1


 

ssh -D 1089 remote_user@remote_host -p ssh_port

然後 proxychains4 配置文件增加 socks5 127.0.0.1 1089

安裝 proxychains4

使用 shadowsocks 發現用全局模式也不能爲 Terminal 設置代理,可以用一下方法爲 Terminal 設置代理。


 

1


 

brew install proxychains-ng

修改配置文件


 

1


 

sudo nano /usr/local/Cellar/proxychains-ng/4.7/etc/proxychains.conf

在文件末尾添加


 

1


 

socks5 127.0.0.1 1080

其中1080爲 Shadowsocks 本地端口。

用法


 

1

2


 

proxychains4 pip install shadowsocks

proxychains4 git push -u origin master

注:不知爲什麼代理下 ping 還是不通,但是其他命令可以正常走代理。

————————————2015-08-05 更新————————————

如果按照上述路徑打開設置文件爲空文件,則運行命令


 

1

2


 

$  /usr/local/bin/proxychains4 curl http://ident.me/

[proxychains] config file found: /usr/local/Cellar/proxychains-ng/4.10/etc/proxychains.conf

可以輸出配置文件目錄,修改配置文件後再次運行,則可以測試是否配置正確。配置正確情況下最後一行 IP 爲代理服務器 IP


 

1

2

3

4

5

6


 

$ /usr/local/bin/proxychains4 curl http://ident.me/

[proxychains] config file found: /usr/local/Cellar/proxychains-ng/4.10/etc/proxychains.conf

[proxychains] preloading /usr/local/Cellar/proxychains-ng/4.10/lib/libproxychains4.dylib

[proxychains] DLL init: proxychains-ng 4.10

[proxychains] Strict chain ... 127.0.0.1:1080 ... ifconfig.me:80 ... OK

xxx.xxx.xxx.xxx

————————————2015-08-05 更新————————————

————————————2015-10-28 更新————————————

El Capitan 支持

升級 El Capitan 後 proxychains4 沒法使用了,每次提示如下


 

1

2

3

4


 

$ /usr/local/bin/proxychains4 curl http://ident.me/

[proxychains] config file found: /usr/local/Cellar/proxychains-ng/4.10/etc/proxychains.conf

[proxychains] preloading /usr/local/Cellar/proxychains-ng/4.10/lib/libproxychains4.dylib

xxx.xxx.xxx.xxx - 真實IP

查一些資料後發現是 System Integrity Protection(SIP)系統完整性保護 導致 proxychains 無法正常運行。根據https://github.com/rofl0r/proxychains-ng/issues/78 暫時的辦法就是禁用 SIP。禁用 SIP 步驟如下。

  1. 重啓 Mac,按住Option鍵進入啓動盤選擇模式,再按⌘ + R進入 Recovery 模式。
  2. 實用工具(Utilities)-> 終端(Terminal)。
  3. 輸入命令csrutil disable運行。
  4. 重啓

————————————2015-08-05 更新————————————

————————————2016-06-12 更新————————————

優化體驗

每次使用 proxychains4 命令其實有些不爽,太長容易輸錯。
編輯 ~/.bash_profile 文件,添加下面一行命令爲 proxychains4 設置別名爲 gfw 或者你喜歡的任何命令。重啓終端。


 

1


 

alias gfw='proxychains4'

現在可以簡化使用方式,具體爲


 

1

2


 

gfw pip install shadowsocks

gfw git push -u origin master

————————————2016-06-12 更新————————————

————————————2016-11-1 更新————————————

Xcode 8 Command line tool 引起的問題

最近升級 Serria 選擇了全盤格式化安裝。安裝完系統難免要安裝各類依賴,第一不就是先安裝 proxychains-ng。 不過此時進入了怪圈,需要安裝 Homebrew 來安裝 proxychains-ng ,但是爲了安裝 Homebrew 需要用 proxychains-ng 來翻牆。

還好 proxychains-ng 也可以自己手動編譯,具體的 Github 上就有。

Homebrew 上最新版本已經修復了這個問題,如果有問題先卸載 proxychains-ng,再重新安裝即可,不需要替換 Xcode7 的 Command Line Tool。

如果 Serria 上還有問題,按照上面 El Capitan 支持步驟重新打開 SIP,重啓電腦即可

但是不管手動編譯還是 Homebrew 安裝安裝完成後,運行 proxychains4 curl ip.cn 能正常翻牆,其他命令的報一下錯誤。


 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16


 

[proxychains] config file found: /usr/local/Cellar/proxychains-ng/4.11/etc/proxychains.conf

[proxychains] preloading /usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib

[proxychains] DLL init: proxychains-ng 4.11

[proxychains] DLL init: proxychains-ng 4.11

[proxychains] DLL init: proxychains-ng 4.11

[proxychains] DLL init: proxychains-ng 4.11

Updating spec repo `master`

[proxychains] Strict chain ... 127.0.0.1:1080 ... api.github.com:443 ... OK

$ /usr/bin/git pull --ff-only

[proxychains] DLL init: proxychains-ng 4.11

dyld: warning: could not load inserted library '/usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib' into library validated process because no suitable image found. Did find:

/usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib: code signing blocked mmap() of '/usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib'

dyld: warning: could not load inserted library '/usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib' into library validated process because no suitable image found. Did find:

/usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib: code signing blocked mmap() of '/usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib'

dyld: warning: could not load inserted library '/usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib' into library validated process because no suitable image found. Did find:

/usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib: code signing blocked mmap() of '/usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib'

研究折騰很久後,才發現 proxychains-ng 編譯需要用 Xcode command line tool,然而我這裏默認的 Xcode 8 的 command line tool 太新導致編譯有問題。解決辦法:

  1. 先刪除 proxychains-ng, brew uninstall proxychains-ng
  2. 切換 command line tool 爲 Xcode 7.x 版本的。sudo xcode-select -s /Applications/Xcode7.app/Contents/Developer
  3. 重新安裝 proxychains-ng brew install proxychains-ng

————————————2016-11-1 更新————————————

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章