轉自 https://www.hi-linux.com/posts/48321.html
對於技術人員shadowsocks應該不陌生,shadowsocks實質上也是一種socks5代理服務,類似於ssh代理。
與vpn的全局代理不同,shadowsocks僅針對瀏覽器代理,不能代理應用軟件,比如curl、wget等一些命令行軟件。如果要讓終端下的命令行工具都能支持代理,這時我們就要用上proxychains-ng這款神器了。
什麼是 proxychains-ng
項目主頁:https://github.com/rofl0r/proxychains-ng
proxychains-ng 介紹
proxychains ng (new generation) - a preloader which hooks calls to sockets in dynamically linked programs and redirects it through one or more socks/http proxies. continuation of the unmaintained proxychains project.
proxychains-ng是proxychains的加強版,主要有以下功能和不足:
- 支持http/https/socks4/socks5
- 支持認證
- 遠端dns查詢
- 多種代理模式
- 不支持udp/icmp轉發
- 少部分程序和在後臺運行的可能無法代理
proxychains-ng 原理
簡單的說就是這個程序 Hook 了 sockets 相關的操作,讓普通程序的 sockets 數據走 SOCKS/HTTP 代理。
其核心就是利用了 LD_PRELOAD 這個環境變量(Mac 上是 DYLD_INSERT_LIBRARIES)。
在 Unix 系統中,如果設置了 LD_PRELOAD 環境變量,那麼在程序運行時,動態鏈接器會先加載該環境變量所指定的動態庫。也就是說,這個動態庫的加載優先於任何其它的庫,包括 libc。
ProxyChains 創建了一個叫 libproxychains4.so(Mac 上是 libproxychains4.dylib)的動態庫。裏面重寫了 connect、close 以及 sendto 等與 socket 相關的函數,通過這些函數發出的數據將會走代理,詳細代碼可以參考 libproxychains.c。
在主程序裏,它會讀取配置文件,查找 libproxychains4 所在位置,把這些信息存入環境變量後執行子程序。這樣子程序裏對 socket 相關的函數調用就會被 Hook 了,對子程序來說,跟代理相關的東西都是透明的。
可以用 printenv 程序來查看增加的環境變量,在 Mac 上輸出結果類似於:
1 2 3 4 5 6 7 8 9 | $ proxychains4 printenv [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_CONF_FILE=/usr/local/Cellar/proxychains-ng/4.11/etc/proxychains.conf DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=/usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib |
一共設置了三個環境變量,其中 PROXYCHAINS_CONF_FILE 保存的是配置文件路徑,DYLD_INSERT_LIBRARIES 保存的是動態庫路徑,在 Mac 中,必須使DYLD_FORCE_FLAT_NAMESPACE 爲 1 才能保證 DYLD_INSERT_LIBRARIES 起作用。
安裝 proxychains-ng
通過源代碼安裝
- 下載源碼
1
| $ git clone https://github.com/rofl0r/proxychains-ng
|
- 編譯安裝
1 2 3 4 | $ ./configure --prefix=/usr --sysconfdir=/etc $ make $ make install $ make install-config (安裝proxychains.conf配置文件) |
MAC下安裝
關閉 SIP
macOS 10.11 後下由於開啓了 SIP(System Integrity Protection) 會導致命令行下 proxychains-ng 代理的模式失效,如果你要使用 proxychains-ng 這種簡單的方法,就需要先關閉 SIP。
具體的關閉方法如下:
- 部分關閉 SIP
重啓Mac,按住Option鍵進入啓動盤選擇模式,再按⌘ + R進入Recovery模式。
實用工具(Utilities)-> 終端(Terminal)。
輸入命令csrutil enable --without debug
運行。
重啓進入系統後,終端裏輸入 csrutil status,結果中如果有 Debugging Restrictions: disabled 則說明關閉成功。
- 完全關閉 SIP
重啓Mac,按住Option鍵進入啓動盤選擇模式,再按⌘ + R進入Recovery模式。
實用工具(Utilities)-> 終端(Terminal)。
輸入命令csrutil disable
運行。
重啓進入系統後,終端裏輸入 csrutil status,結果中如果有 System Integrity Protection status:disabled. 則說明關閉成功。
安裝 Proxychains-ng
安裝好 Homebrew 後,終端中輸入
1
| $ brew install proxychains-ng
|
配置 proxychains-ng
proxychains-ng默認配置文件名爲proxychains.conf
。
- 通過源代碼編譯安裝的默認爲
/etc/proxychains.conf
。 - Mac下用Homebrew安裝的默認爲
/usr/local/etc/proxychains.conf
。
proxychains-ng的配置非常簡單,只需將代理加入[ProxyList]中即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $ vim proxychains.conf quiet_mode dynamic_chain chain_len = 1 #round_robin_chain和random_chain使用 proxy_dns remote_dns_subnet 224 tcp_read_time_out 15000 tcp_connect_time_out 8000 localnet 127.0.0.0/255.0.0.0 localnet 10.0.0.0/255.0.0.0 localnet 172.16.0.0/255.240.0.0 localnet 192.168.0.0/255.255.0.0 [ProxyList] socks5 127.0.0.1 1086 http 127.0.0.1 1087 |
proxychains-ng支持多種代理模式,默認是選擇 strict_chain。
- dynamic_chain :動態模式,按照代理列表順序自動選取可用代理
- strict_chain :嚴格模式,嚴格按照代理列表順序使用代理,所有代理必須可用
- round_robin_chain :輪詢模式,自動跳過不可用代理
- random_chain :隨機模式,隨機使用代理
proxychains-ng 使用
proxychains-ng 語法
proxychains-ng用法非常簡單,使用格式如下:
1
| $ proxychains4 程序 參數
|
proxychains-ng 測試
1
| $ proxychains4 curl ip.cn
|
proxychains-ng 優化
alias
給proxychains4增加一個別名,在 ~/.zshrc或~/.bashrc末尾加入如下行:
1 2 3 4 | # --------------------------------------- # proxychain-ng config # --------------------------------------- alias pc='proxychains4' |
以後就可以類似$ pc curl http://www.google.com
這樣調用proxychains4,簡化了輸入。
自動補全
你輸了很長一段命令,然後你突然想使用代理功能,怎麼辦?
- iTerm中前綴補全
在 iTerm -> Preferences -> Profiles -> Keys
中,新建一個快捷鍵,例如 ⌥ + p ,Action 選擇 Send Hex Code,鍵值爲 0x1 0x70 0x63 0x20 0xd,保存生效。
以後命令要代理就直接敲命令,然後 ⌥ + p 即可,這樣命令補全也能保留了。
附上 Hex Code 對應表,獲取工具爲keycodes(http://manytricks.com/keycodes/)
Hex Code | Key |
---|---|
0x1 | ⌃ + a |
0x70 | p |
0x63 | c |
0x20 | [space] |
0xd | ↩︎ |
- oh-my-zsh中前綴補全
1 2 | $ git clone [email protected]:six-ddc/zsh-proxychains-ng.git ~/.oh-my-zsh/custom/plugins/zsh-proxychains-ng $ echo "plugins+=(zsh-proxychains-ng)" >> ~/.zshrc |
使用時按[ESC]-P ,自動添加(去除)proxychains4 -q
命令前綴,支持 emacs 和 vi mode 。
- 通過代理SHELL實現全局代理
如果你還是覺得每次使用都要輸入proxychains4或其別名,比較麻煩。你還可以用proxychains-ng代理一個shell,在shell中執行的命令就會自動使用代理了。
方法一
手動設置環境變量
1 2 3 | $ export PROXYCHAINS_CONF_FILE=/usr/local/Cellar/proxychains-ng/4.11/etc/proxychains.conf $ export DYLD_INSERT_LIBRARIES=/usr/local/Cellar/proxychains-ng/4.11/lib/libproxychains4.dylib $ export DYLD_FORCE_FLAT_NAMESPACE=1 |
方法二
proxychains-ng直接調用SHELL
BASH
1
| $ proxychains4 -q /bin/bash
|
ZSH
1
| $ proxychains4 -q /bin/zsh
|
這樣在當前 shell 中運行的所有程序的網絡請求都會走代理了。可以把上面的命令加入到用戶目錄的.bashrc或者.zshrc中,用戶登錄後自動代理一個shell,這就類似一個全局代理了。在這個SHELL下的所有命令都可以使用代理了。
參考文檔
http://www.google.com
http://t.cn/R4tiUB1
http://t.cn/Rxg7oJR
http://t.cn/RG1Hbs3