自建服务
相比购买服务,自建 Shadowsocks 有更加安全、独享带宽的好处。与此相对,自建服务需要一些网络基础知识和服务器操作基础,别担心,尝试就是一种学习的过程。
如果你对网络基础没有任何了解也无妨,我们有计划撰写一些网络相关的翻墙基础知识。在此期间你可以尝试阅读本指南并通过搜索引擎了解相关知识。(搜索引擎永远是你最好的老师)
准备工作
搭建 Shadowsocks 服务端,你需要:
- 至少一台位于墙外的服务器;
- 能够远程连接到服务器的 SSH 软件;
- 建议服务器使用 Ubuntu/Debian。
安装服务
搭建 Shadowsocks 一般在 shadowsocks 和 shadowsocks-libev 这两种常用版本中择一作为服务端:shadowsocks 为原始版本,使用 Python 编写(后简称 Python 版),支持多用户配置;而 libev 版本(后简称 libev 版)对于新功能更新较快,更加注重节省资源,适合在资源有限的环境中运行。
安装 Python 版:
$ apt-get install python-pip
$ pip install shadowsocks
安装 libev 版:
# Ubuntu 16.10 或 Debian 9 以上版本
$ apt-get install shadowsocks-libev
# Ubuntu 14.04 及 16.04
$ add-apt-repository ppa:max-c-lv/shadowsocks-libev
$ apt-get update
$ apt-get install shadowsocks-libev
运行服务
安装完成后,我们尝试通过命令运行服务:
# Python 版本
$ ssserver -p 443 -k password -m aes-256-cfb
# libev 版本
$ ss-server -p 443 -k password -m aes-256-cfb
命令中的三个参数分别对应了连接 Shadowsocks 服务器所需要的三个基本参数:
| 参数 | 说明 |
|---|---|
| -p <server_port> | 服务端端口 |
| -k <password> | 密码 |
| -m <encrypt_method> | 加密方式 |
除了上述三个基本参数外,Shadowsocks 也有不少别的命令和参数,但是此处我们只是尝试创建服务来测试 Shadowsocks 能否正常运行,故不再赘述,具体内容会在下一步详述。此刻,如果服务运行正常,则通过 Ctrl + C 终止进程。
通过配置文件启动
除了这种通过命令内参数直接设置并创建服务端实例的方法,我们也可以创建配置文件预先设置好所需的各项参数后,直接通过命令调用 -c <config_file> 指向配置文件路径来启动服务。
在 /etc 或其他合适的位置创建 shadowsocks.json:
$ nano /etc/shadowsocks.json
按下面格式编辑:
{
"server":"my_server_ip",
"server_port":8388,
"password":"mypassword",
"timeout":300,
"method":"aes-256-cfb",
"fast_open": false
}
其中各参数说明如下:
| 参数 | 说明 |
|---|---|
| server | 服务端监听地址(一般设置为 0.0.0.0) |
| server_port | 服务端监听端口 |
| password | 密码 |
| timeout | 超时时间(以秒为单位) |
| method | 加密方式 |
| fast_open | 是否开启 TCP Fast Open(设置为 true/false) |
保存后,运行命令调用配置文件启动 Shadowsocks 服务端:
# Python 版本
$ ssserver -c /etc/shadowsocks.json
# Python 版本后台启动/停止
$ ssserver -c /etc/shadowsocks.json -d start
$ ssserver -c /etc/shadowsocks.json -d stop
# libev 版本
$ ss-server -c /etc/shadowsocks.json
对于通过 apt 安装的 libev 版,也可以在 /etc/shadowsocks-libev/config.json 编辑服务配置。之后通过 systemd 启动 libev 版服务:
$ systemctl start shadowsocks-libev
多用户配置(仅限 Python 版)
对于 Python 版用户,如果需要设置多用户(端口)使用,可将上文配置文件中 server_port 和 password 合并为 port_password,如下:
{
"server": "my_server_ip",
"port_password": {
"8381": "foobar1",
"8382": "foobar2",
"8383": "foobar3",
"8384": "foobar4"
},
"timeout": 300,
"method": "aes-256-cfb",
"fast_open": false
}
请注意:libev 版不包括此功能,libev 版多用户需要每个用户(端口)单独运行一个实例。
加密方式
加密方式是 Shadowsocks 服务的重要参数之一,加密方式决定了你与服务器直接通信的安全程度。Shadowsocks 目前可用的加密方式有:
| 加密方式 | 类型 | 说明 |
|---|---|---|
| aes-128-ctr | Stream | |
| aes-192-ctr | Stream | |
| aes-256-ctr | Stream | |
| aes-128-cfb | Stream | |
| aes-192-cfb | Stream | |
| aes-256-cfb | Stream | 在旧设备有较好的兼容性 |
| camellia-128-cfb | Stream | |
| camellia-192-cfb | Stream | |
| camellia-256-cfb | Stream | |
| bf-cfb | Stream | 强烈建议迁移到 AEAD 加密方式 |
| chacha20 | Stream | 强烈建议迁移到 chacha20-ietf-poly1305 等 AEAD 加密方式 |
| chacha20-ietf | Stream | |
| salsa20 | Stream | 强烈建议迁移到 AEAD 加密方式 |
| rc4-md5 | Stream | 强烈建议迁移到 AEAD 加密方式 |
| chacha20-poly1305 | ||
| aes-256-gcm | AEAD | |
| aes-192-gcm | AEAD | |
| aes-128-gcm | AEAD | |
| chacha20-ietf-poly1305 | AEAD | 推荐,兼顾安全性的同时在移动端及路由器有较好的性能 |
总体来说,根据 SIP004 及 #36 讨论,建议所有 Stream 加密用户迁移到 AEAD 加密方式以保证安全性。同类型下比较而言 AES 是安全性较高的选择,chacha20 相比 AES 在移动端有更好的性能。
为 Python 版添加 chacha20 等支持
Python 版需要通过编译安装第三方加密库 libsodium 来支持 chacha20 及 salsa20 等加密方式:
$ apt-get install build-essential
$ wget https://download.libsodium.org/libsodium/releases/LATEST.tar.gz
$ tar xf LATEST.tar.gz && cd libsodium-*
$ ./configure
$ make && make check
$ make install
$ ldconfig
未安装 libsodium 会导致 Python 版 Shadowsocks 运行报错,而 libev 版本自带加密库,无需按此步骤安装。