Linux - Proxy Setup

  • Using Unix Proxy Environment Variable

Add this script to ~/.bash_profile.

export http_proxy=socks5://127.0.0.1:1080
export https_proxy=socks5://127.0.0.1:1080
  • Config apt-get through proxy
$ vim /etc/apt/apt.conf

Acquire::http::proxy "http://localhost:1080";
Acquire::https::proxy "https://localhost:1080";

对于上面apt的代理设置,实际操作中有时候会出现无法连接的现象:

Err:1 https://packagecloud.io/github/git-lfs/ubuntu bionic/main amd64 git-lfs amd64 2.5.2
  Could not wait for server fd - select (11: Resource temporarily unavailable) [IP: 127.0.0.1 1080]
Err:2 https://packages.microsoft.com/ubuntu/18.04/prod bionic/main amd64 dotnet-host amd64 2.1.4-1
  Could not wait for server fd - select (11: Resource temporarily unavailable) [IP: 127.0.0.1 1080]
实践证明,对于apt更有效的方式是通过proxychanins来进行apt更新
  • Config curl

Add the following line to ~/.curlrc. Since homebrew use curl to download the package, this will also enable homebrew to use SOCKS5.

socks5 = "socks5://127.0.0.1:1080"

There are no environment variables for SOCKS5 proxy servers in unix, so in order to use SOCKS5 in other utilities, check the man pages for existing tools to see if they have a configuration option for a SOCKS5 proxy and whether they have a configuration file that the configuration can be added to.

  • Config ~/.gitconfig
[https]
    proxy = https://127.0.0.1:1080
[http]
    proxy = http://127.0.0.1:1080
注意:.gitconfig中的代理设置会作用于git以及git-lfs。git可以支持socks5/http/https协议,但是git-lfs只支持https;另外代理的主机要使用IP地址(如127.0.0.1),因为如果使用主机名(如localhost)git-lfs会无法识别。
  • Config SSH

在linux环境下,也可以使用netcat(包名:openbsd-netcat)

Host *
    ProxyCommand /usr/bin/nc -x localhost:1080 %h %p

如果对局域网内不采用代理,可以参考:

Host !192.*
    ProxyCommand /bin/nc -x localhost:1080 %h %p

关于ssh的配置可以查阅man page:

$ man ssh_config

对于Windows系统,也可以自行编译connect工具(https://bitbucket.org/gotoh/connect/wiki/Home),编译好之后复制connect.exe到PATH路径下,编辑当前用户目录下的.ssh\config文件(如果没有就创建)

Host *
ProxyCommand connect -S localhost:1080 %h %p
  • Setup Shadowsocks Server
# 安装docker及镜像

apt install docker.io
docker pull shadowsocks/shadowsocks-libev
#!/usr/bin/bash


# 服务端口

PORT=[nnnn]

# 接入密码

PASSWORD=[******]

# 网速控制,100个包每秒

PACKETS=100
iptables -A FORWARD -p tcp -m tcp --sport $PORT -m limit --limit $PACKETS/sec --limit-burst 1000 -j ACCEPT
iptables -A FORWARD -p tcp -m tcp --sport $PORT -j DROP

# 启动服务

echo "Start proxy with aes-256-gcm on port: $PORT..."
docker run -e PASSWORD=$PASSWORD -e METHOD=aes-256-gcm -p $PORT:8388 -d shadowsocks/shadowsocks-libev