ubuntu18.04下系统级代理自动路由(包括浏览器、终端、apt-get)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

一、远程服务端(s服务)

远端服务服务器安装s服务端,建议使用docker方式。

  • 安装docker环境:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
  •  安装s服务
docker pull s/s-libev
docker run -e PASSWORD=pwd123 -e METHOD=aes-256-cfb -p0.0.0.0:3306 -p0.0.0.0:3306/udp -d s/s-libev

 

以上命令在3306端口启动服务,密码为pwd123, 加密方式为aes-256-cfb

二、本地s客户端

  • 安装客户端依赖库
sudo apt-get install python-pip
sudo pip install s
  • 编辑s的配置文件
sudo vi /etc/s.json

然后在s.json里面添加配置信息,如:

{
  "server":"my_server_ip",
  "local_address": "0.0.0.0",
  "local_port":8123,
  "server_port":3306,
  "password":"pwd123",
  "timeout":300,
  "method":"aes-256-cfb"
}

注意将my_server_ip替换为你的s服务器IP

  • 测试启动:
sslocal -c /etc/s.json
  • 测试代理是否联通
curl --proxy socks5://localhost:8123 http://ipinfo.io

返回的ip是你的代理服务器IP说明代理可以正常连接。

  • 每次都要手动启动客户端不方便,接下来使用Systemd来实现s开机自启
sudo vim /etc/systemd/system/s.service

在里面填写如下内容:

[Unit]
Description=Shadowsocks Client Service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/bin/sslocal -c /etc/s.json

[Install]
WantedBy=multi-user.targe

注意sslocal的路径可以用whereis sslocal来查找,路径不对无法启动服务。

配置生效:

systemctl enable /etc/systemd/system/s.service

输入管理员密码就可以了。现在你可以马上重启试试上面测试连接是否成功。

三、全局代理自动路由

  • 使用GenPAC生成代理路由配置文件

安装:

pip install -U genpac

用以下命令生成pac文件

genpac --proxy="SOCKS5 127.0.0.1:8123" --gfwlist-proxy="SOCKS5 127.0.0.1:8123" -o autoproxy.pac --gfwlist-url="https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"
  • 设置全局代理

进入系统设置->网络->网络代理->自动

输入:autoproxy.pac文件所在的路径,格式:file:///home/cherokee/PAC/autoproxy.pac

ubuntu18.04下系统级代理自动路由(包括浏览器、终端、apt-get)_第1张图片

可以用浏览器(chrome、firefox)打开谷歌等被墙的页面测试一下自动路由是否成功。

如果只需要在chrome环境下使用自动代理,你也可以直接使用Proxy SwitchyOmega

以上自动路由只对浏览器起效, 如果要在apt、终端环境下需要自动代理请继续下面的操作。

四、使用privoxy给apt、终端自动代理

严格意义上apt-get、终端下不支持自动代理规则,要么设置代理要么不走代理。所谓的自动代理就是用privoxy实现一个http代理,apt-get和终端使用这个http代理,由privoxy来决定墙内的流量直接访问,墙外的走代理服务器。

  • 安装 privoxy
sudo apt install privoxy 
  • 用gfwlist2privoxy生成privoxy自动路由规则文件
curl --proxy socks5://localhost:8123 -4sSkLO https://raw.github.com/zfl9/gfwlist2privoxy/master/gfwlist2privoxy

bash gfwlist2privoxy 127.0.0.1:8123

sudo mv -f gfwlist.action /etc/privoxy/

以上命令已经生成了自动路由规则,并且拷贝到/etc/privoxy/gfwlist.action。如果你本地的socks5代理端口和我不一样,请做相应改动。

  • 配置privoxy
sudo vim /etc/privoxy/config

打开文件找到4.1节 listen-address
找到#listen-address 127.0.0.1:8118,取消注释。表示http代理端口在8118

找到#  actionsfile default.action位置,在下面加一行:

actionsfile /etc/privoxy/gfwlist.action
  • 重新启动privory:
service privoxy restart

systemctl restart privoxy.service
  • 为apt-get配置代理
sudo vim /etc/apt/apt.conf

填入:

Acquire::http::Proxy "http://127.0.0.1:8118";
Acquire::https::Proxy "http://127.0.0.1:8118";

这样apt-get update时可以下载墙外的内容了。

  • 为终端配置代理,直接在~/.bashrc或者~/.zshrc添加下面内容
export http_proxy="http://127.0.0.1:8118"
export https_proxy="http://127.0.0.1:8118"

或者直接设置ALL_PROXY

export ALL_PROXY=socks5://127.0.0.1:8118

可以用curl测试一下谷歌网站是否能够访问,正常情况下应该是谷歌可以访问,当你curl http://ipinfo.io网站是返回的IP是你本地IP,就实现了自动路由。

转载于:https://my.oschina.net/waterbear/blog/3016639

你可能感兴趣的:(ubuntu18.04下系统级代理自动路由(包括浏览器、终端、apt-get))