Shadowsocks

Table of Contents

1. Shadowsocks

Shadowsocks 的基本原理是 把 socks5 代理拆成了 server 和 client 两部分(分别对应程序 ssserver 和 sslocal,或者 ss-server 和 ss-local),中间加密通信。

1.1. 安装 Shadowsocks

Ubuntu 中使用下面命令,即可安装和启动 Shadowsocks:

$ sudo apt install shadowsocks
$ sudo ssserver -c /etc/shadowsocks/config.json -d start

1.2. 安装 Shadowsocks-libev

Shadowsocks-libev 是 Shadowsocks 的 libev 版本,它的特点是内存占用更小,CPU 消耗更小,甚至可以安装在基于 OpenWRT 的路由器上。

下面是在 Redhat 中安装 shadowsocks-libev 的方法:

$ cd /etc/yum.repos.d/
$ curl -O https://copr.fedorainfracloud.org/coprs/librehat/shadowsocks/repo/epel-7/librehat-shadowsocks-epel-7.repo
$ yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # 启用EPEL支持,shadowsocks-libev有些依赖在EPEL仓库中
$ yum install -y shadowsocks-libev

说明:Redhat 仓库中没有打好包的 Shadowsocks,但在 COPR (Cool Other Package Repo)中已经有打好包的 shadowsocks-libev。

下面是在 Ubuntu 中安装 shadowsocks-libev 的方法:

$ sudo apt install shadowsocks-libev

成功安装 shadowsocks-libev 后,会有 ss-local, ss-manager, ss-nat, ss-redir, ss-server, ss-tunnel 命令可用。

使用下面命令可以启动服务器(即可执行程序 ss-server):

$ systemctl start shadowsocks-libev  # 启动ss-server

注:ss-server 的默认配置文件为/etc/shadowsocks-libev/config.json,后文有它的格式说明。安装 shadowsocks-libev 时默认生成配置文件中的加密算法为 chacha20-ietf-poly1305,可能有些客户端不支持,建议修改为兼容性更好的 aes-256-cfb;默认监听在 127.0.0.1,需要改为 0.0.0.0。

2. socks5 代理

下面介绍如何使用 Shadowsocks 的 socks5 代理功能进行上网。

参考:
ssserver --help
sslocal --help
Shadowsocks作者原文介绍

2.1. 启动 ssserver

在服务器上启动 ssserver ,其配置文件例子:

{
    "server":"0.0.0.0",
    "server_port":14294,
    "password":"pwd12345",
    "timeout":300,
    "method":"aes-256-cfb"
}

可以使用下面命令启动和停止 ssserver:

$ sudo ssserver -c /etc/shadowsocks/config.json -d start    # -d表示daemon mode
$ sudo ssserver -c /etc/shadowsocks/config.json -d stop

2.2. 启动 sslocal

本地机器上启动 sslocal ,其配置文件例子:

{
    "server":"your_server_ip",
    "server_port":14294,
    "local_port":1080,
    "password":"pwd12345",
    "timeout":600,
    "method":"aes-256-cfb"
}

可以使用下面命令启动和停止 sslocal:

$ sudo sslocal -c /etc/shadowsocks/config.json -d start
$ sudo sslocal -c /etc/shadowsocks/config.json -d stop

2.3. 配置浏览器的 socks5 代理

将本地机器的浏览器设置代理为:

protocol: socks5
hostname: 127.0.0.1
port:     1080   (这个端口就是sslocal配置文件中的local_port参数)

这样,浏览器就可以通过 socks5 代理访问网页了。

3. http 代理

3.1. cow

有一些软件不能配置 socks5 代理,这样我们可能需要 http 代理。

工具 cow (它的默认配置文件为~/.cow/rc)可以在本机启动一个 http 代理服务器,以 socks5 为二级代理:

$ cat ~/.cow/rc
listen = http://127.0.0.1:7777
proxy = socks5://127.0.0.1:1080

如果你本地没有 socks5 代理,也可以直接使用 shadowsocks 作为二级代理:

$ cat ~/.cow/rc
listen = http://127.0.0.1:7777
proxy = ss://aes-256-cfb:pwd12345@your_server_ip:14294

在终端中,设置 http_proxy 和 https_proxy 环境变量:

export http_proxy=http://127.0.0.1:7777
export https_proxy=http://127.0.0.1:7777

参考:
cow --help
https://colobu.com/2017/01/26/how-to-go-get-behind-GFW/

3.2. privoxy

Privoxy 是一款带过滤功能的代理服务器,针对 HTTP、HTTPS 协议。通过 Privoxy 的过滤功能,用户可以保护隐私、对网页内容进行过滤、管理 cookies,以及拦阻各种广告等。

借助 Privoxy,我们还可以将 socks5 代理转换成 http 代理,原理如下:

       |------>|                     |------>|              |------>|
client |       | http proxy (privoxy)|       | socks5 proxy |       | target server
       |<------|                     |<------|              |<------|

一些 Shadowsocks 客户端中内置有 Privoxy 以直接提供 http 代理的能力。

Author: cig01

Created: <2018-08-13 Mon>

Last updated: <2019-12-12 Thu>

Creator: Emacs 27.1 (Org mode 9.4)