為終端配置proxy


轉自:https://my.oschina.net/u/818848/blog/677225?p=1

 

做開發的同學,應該都會經常接觸終端,有些時候我們在終端會做一些網絡操作,比如下載gradle包等,由於一些你懂我也懂的原因,某些網絡操作不是那么理想,這時候我們就需要設置代理來自由地訪問網絡。

Shadowsocks是我們常用的代理工具,它使用socks5協議,而終端很多工具目前只支持http和https等協議,對socks5協議支持不夠好,所以我們為終端設置shadowsocks的思路就是將socks協議轉換成http協議,然后為終端設置即可。仔細想想也算是適配器模式的一種現實應用吧。

想要進行轉換,需要借助工具,這里我們采用比較知名的polipo來實現。polipo是一個輕量級的緩存web代理程序。閑話休敘,讓我們開始動手吧。

准備工作

安裝

Fedora安裝

1
sudo yum install polipo

Mac下使用Homebrew安裝

1
brew install polipo

Ubuntu安裝

1
sudo apt-get install polipo

修改配置(Linux)

如下打開配置文件

1
sudo vim /etc/polipo/config

設置ParentProxy為Shadowsocks,通常情況下本機shadowsocks的地址如下

1
2
3
4
# Uncomment this if you want to use a parent SOCKS proxy: socksParentProxy = "localhost:1080" socksProxyType = socks5

設置日志輸出文件

1
2
logFile=/var/log/polipo logLevel=4

修改配置(Mac)

設置每次登陸啟動polipo

1
ln -sfv /usr/local/opt/polipo/*.plist ~/Library/LaunchAgents

修改文件/usr/local/opt/polipo/homebrew.mxcl.polipo.plist設置parentProxy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>homebrew.mxcl.polipo</string> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> <key>ProgramArguments</key> <array> <string>/usr/local/opt/polipo/bin/polipo</string> <string>socksParentProxy=localhost:1080</string> </array> <!-- Set `ulimit -n 20480`. The default OS X limit is 256, that's not enough for Polipo (displays 'too many files open' errors). It seems like you have no reason to lower this limit (and unlikely will want to raise it). --> <key>SoftResourceLimits</key> <dict> <key>NumberOfFiles</key> <integer>20480</integer> </dict> </dict> </plist>

修改的地方是增加了<string>socksParentProxy=localhost:1080</string>

啟動(Linux)

先關閉正在運行的polipo,然后再次啟動

1
2
sudo service polipo stop sudo service polipo start

啟動(Mac)

1
2
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.polipo.plist launchctl load ~/Library/LaunchAgents/homebrew.mxcl.polipo.plis

注意:請確保Shadowsocks正常工作。

驗證及使用

安裝完成就需要進行驗證是否work。這里展示一個最簡單的驗證方法,打開終端,如下執行

1
2
3
4
07:56:24-androidyue/var/log$ curl ip.gs 當前 IP:125.39.112.15 來自:中國天津天津 聯通 08:09:23-androidyue/var/log$ http_proxy=http://localhost:8123 curl ip.gs 當前 IP:210.140.193.128 來自:日本日本

如上所示,為某個命令設置代理,前面加上http_proxy=http://localhost:8123 后接命令即可。

注:8123是polipo的默認端口,如有需要,可以修改成其他有效端口。

設置別名

bash中有一個很好的東西,就是別名alias. Linux用戶修改~/.bashrc,Mac用戶修改~/.bash_profile文件,增加如下設置

1
alias hp="http_proxy=http://localhost:8123"

然后Linux用戶執行source ~/.bashrc,Mac用戶執行source ~/.bash_profile

測試使用

1
2
3
4
5
20:39:39-androidyue~$ curl ip.gs 當前 IP:125.39.112.14 來自:中國天津天津 聯通 20:39:44-androidyue~$ hp curl ip.gs 當前 IP:210.140.193.128 來自:日本日本 20:39:48-androidyue~$ 

當前會話全局設置

如果嫌每次為每一個命令設置代理比較麻煩,可以為當前會話設置全局的代理。即使用export http_proxy=http://localhost:8123即可。 如果想撤銷當前會話的http_proxy代理,使用 unset http_proxy 即可。 示例效果如下

1
2
3
4
5
6
7
8
21:29:49-androidyue~$ curl ip.gs 當前 IP:125.39.112.14 來自:中國天津天津 聯通 21:29:52-androidyue~$ export http_proxy=http://localhost:8123 21:30:07-androidyue~$ curl ip.gs 當前 IP:210.140.193.128 來自:日本日本 21:30:12-androidyue~$ unset http_proxy 21:30:37-androidyue~$ curl ip.gs 當前 IP:125.39.112.14 來自:中國天津天津 聯通

如果想要更長久的設置代理,可以將export http_proxy=http://localhost:8123加入.bashrc或者.bash_profile文件

設置Git代理

復雜一些的設置Git代理

1
2
3
4
5
6
7
8
git clone https://android.googlesource.com/tools/repo --config http.proxy=localhost:8123 Cloning into 'repo'... remote: Counting objects: 135, done remote: Finding sources: 100% (135/135) remote: Total 3483 (delta 1956), reused 3483 (delta 1956) Receiving objects: 100% (3483/3483), 2.63 MiB | 492 KiB/s, done. Resolving deltas: 100% (1956/1956), done. 

其實這樣還是比較復雜,因為需要記憶的東西比較多,下面是一個更簡單的實現

首先,在.bashrc或者.bash_profile文件加入這一句。

1
gp=" --config http.proxy=localhost:8123" 

然后執行source操作,更新當前bash配置。

更簡單的使用git的方法

1
2
3
4
5
6
7
8
git clone https://android.googlesource.com/tools/repo $gp Cloning into 'repo'... remote: Counting objects: 135, done remote: Finding sources: 100% (135/135) remote: Total 3483 (delta 1956), reused 3483 (delta 1956) Receiving objects: 100% (3483/3483), 2.63 MiB | 483 KiB/s, done. Resolving deltas: 100% (1956/1956), done. 

開始自由快速的開發吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM