Implementing a Terminal Proxy
Problem
As a Linuxer, being proficient with the terminal is an essential skill. But in the terminal, downloading and installing things can sometimes be very slow, which is frustrating. I naturally wondered whether I could add a proxy to improve the speed. I had been using Shadowsocks before, and the browser used SwitchyProxy, which achieved basic proxy-based internet access. So, is there a similar tool in the terminal? The answer is yes. Through targeted searching, I successfully solved the proxy problem in the terminal. I summarize it here and hope it can help everyone.
Tools Needed
Shadowsocks, Polipo
Shadowsocks
Install Command-line Client
1 | pip install shadowsocks |
For Shadowsocks configuration, you can refer to this article. I configured it a long time ago, so I will not recall it here, since I have mostly forgotten it.
Let us start directly from Polipo.
Polipo
Polipo has multiple installation methods. You can install it directly with Python’s package manager pip, or use the package manager of each operating system. I recommend the latter because I personally think it is easier to manage. Here I use Ubuntu as an example.
1 | apt-cache search polipo |
The second package is the one for beginners like us. Install it:
1 | sudo apt-get install polipo |
After installation, it starts automatically by default.
We first need to modify its configuration file, /etc/polipo/config:
1 | logSyslog = true |
For specific configuration options, refer to the file /usr/share/doc/polipo/examples/config.sample.
After configuration, we need to restart the Polipo service. Every time the configuration file is modified, the service must be restarted to load it.
1 | sudo service polipo stop; |
Or directly:
1 | sudo service polipo restart; |
You can use:
1 | sudo polipo -v |
to view the specific configuration items, and you can see that our previous changes have taken effect.
Testing
After configuration is complete, how do you know whether you can already access the internet through the proxy?
You can use the following commands:
1 | ~$ curl -i http://ip.cn |
1 | ~$ http_proxy=http://localhost:8123 curl -i http://ip.cn # polipo的默认端口为8123,如有需要可以自行改动 |
This counts as success.
Recently I discovered that the website ip.cn no longer works and returns a 500 error. Replacing it with ipinfo.io/ip works; this is another website that returns your public IP.
There are also other websites that provide similar IP-returning services:
1 | curl ifconfig.me |
Ways to obtain your public IP from the browser:
- DuckDuckGo
- Wolframalpha, recommended
Going Further
Typing such a long command, http_proxy=http://localhost:8123, every time is really not what we want. For convenience, you can type export http_proxy=http://localhost:8123 in the terminal, meaning it takes effect for all commands in that terminal. Or go one step further and add export http_proxy=http://localhost:8123 to .bashrc, so it runs automatically every time the terminal starts. If you do not want every command to go through the proxy, you can do as I did and add alias hp="http_proxy=http://localhost:8123" to .bashrc. Then every time you need a proxy, just add hp before the command.
Configure Proxy for Git
The speed of git clone is quite moving, only a few dozen KB/s. Configuring a proxy for Git is also very simple.
Add the following setting to the http items of .gitconfig files.
1 | [http] |
You can also configure it using the following config command:
1 | git config --global http.proxy <address of the proxy server>:<port of the proxy server> |
Enjoy proxy-based internet access in the terminal.