Shadowsocks

支持代理命令行

开发时各种包管理的数据源都是国外服务器,比如NPM/PIP/APT/YUM/GoogleSource等等,更新时非常慢。

Shadowsocks使用的是socks5协议,但终端的这些更新源都还是使用的HTTP/HTTPS,所以我们需要将Shadowsocks的socks5转换成HTTP/HTTPS协议。

这里可以使用polipo来代理,polipo是一款轻量级缓存web代理程序。

安装

# Mac
brew install polipo

# Ubuntu
sudo apt-get install polipo  

配置

# Linux
sudo vim /etc/polipo/config  
socksParentProxy = "localhost:1080"  
socksProxyType = socks5  
logFile=/var/log/polipo  
logLevel=4

# Mac (增加 <string>socksParentProxy=localhost:1080</string>)
vim /usr/local/Cellar/polipo/1.1.1/homebrew.mxcl.polipo.plist  
<?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>  

启动

# Linux
sudo service polipo stop  
sudo service polipo start

# Mac每次登陆时启动
ln -sfv /usr/local/Cellar/polipo/1.1.1/homebrew.mxcl.polipo.plist ~/Library/LaunchAgents

# Mac
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.polipo.plist  
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.polipo.plist  

验证

curl ip.gs  
当前 IP:153.127.76.231 来自:日本北海道石狩

设置代理

# 为本次执行的命令设置代理
http_proxy=http://localhost:8123 npm install -g cordova

# 或者先写到alias
vim ~/.bashrc  
alias hp="http_proxy=http://localhost:8123"  
hp npm install -g cordova


# 设置全局代理
vim ~/.bashrc  
export http_proxy=http://localhost:8123  
export https_proxy=$http_proxy