Linux 常用工具之 Shadowsocks

Ubuntu 安装 Shadowsocks 客户端

参考安装指南#ubuntu

1
2
3
4
5
PPA is for Ubuntu >= 14.04.
sudo add-apt-repository ppa:hzwhuang/ss-qt5
sudo apt-get update
sudo apt-get install shadowsocks-qt5

以下为我的安装记录:

安装 shadowsocks-qt5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
huzhifeng@Ubuntu16041:~$ sudo add-apt-repository ppa:hzwhuang/ss-qt5
huzhifeng@Ubuntu16041:~$ sudo apt-get update && sudo apt-get install shadowsocks-qt5
huzhifeng@Ubuntu16041:~$ dpkg-query --listfiles shadowsocks-qt5
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/shadowsocks-qt5
/usr/share/doc/shadowsocks-qt5/copyright
/usr/share/doc/shadowsocks-qt5/README.md
/usr/share/doc/shadowsocks-qt5/changelog.Debian.gz
/usr/share/icons
/usr/share/icons/hicolor
/usr/share/icons/hicolor/512x512
/usr/share/icons/hicolor/512x512/apps
/usr/share/icons/hicolor/512x512/apps/shadowsocks-qt5.png
/usr/share/applications
/usr/share/applications/shadowsocks-qt5.desktop
/usr/bin
/usr/bin/ss-qt5
huzhifeng@Ubuntu16041:~$

设置开机自启动

参考 Startup ApplicationsAddingProgramToSessionStartup

1
2
3
1. Use the Dash to find and open `Startup Applications`. Alternatively you can press `Alt+F2` and run the `gnome-session-properties` command.
2. Click Add and enter the command to be executed at login (name and comment are optional). For example, to make Firefox start automatically, it's sufficient to type firefox in the Command field and confirm with Add.

以下为开机脚本内容:

1
2
3
4
5
6
7
8
9
10
11
12
huzhifeng@Ubuntu16041:~$ cat ~/.config/autostart/ss-qt5.desktop
[Desktop Entry]
Type=Application
Exec=/usr/bin/ss-qt5
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=ss
Name=ss
Comment[en_US]=Shadowsocks
Comment=Shadowsocks
huzhifeng@Ubuntu16041:~$

创建桌面快捷图标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
huzhifeng@Ubuntu16041:~$ dpkg-query --listfiles shadowsocks-qt5 | grep desktop
/usr/share/applications/shadowsocks-qt5.desktop
huzhifeng@Ubuntu16041:~$ cp /usr/share/applications/shadowsocks-qt5.desktop ~/Desktop/
huzhifeng@Ubuntu16041:~$ vim ~/Desktop/shadowsocks-qt5.desktop
huzhifeng@Ubuntu16041:~$ chmod a+x ~/Desktop/shadowsocks-qt5.desktop
huzhifeng@Ubuntu16041:~$ cat ~/Desktop/shadowsocks-qt5.desktop
[Desktop Entry]
Name=Shadowsocks-Qt5
Version=2.8.0
Exec=/usr/bin/ss-qt5
Icon=/usr/share/icons/hicolor/512x512/apps/shadowsocks-qt5.png
Terminal=false
Type=Application
huzhifeng@Ubuntu16041:~$

设置快捷键(Ctrl+Shift+S)

进入 System Settings -> Keyboard -> Shortcuts -> Custom Shortcuts
点击 + 号为 Shadowsocks 设置快捷键,例如Ctrl+Shift+S

CentOS 安装 Shadowsocks 客户端

通过 yum 方式安装(推荐)

参考安装指南#fedora

1
2
3
4
如果使用传统的yum包管理工具的话,需要从Copr下载相应版本的repo文件放到/etc/yum.repos.d/下,然后通过yum安装:
sudo yum update
sudo yum install shadowsocks-qt5
RHEL/CentOS请确认已经添加了EPEL源。

安装过程记录如下:
首先下载 repo 文件并更新 yum 源

1
2
3
4
5
6
7
8
9
10
11
12
13
[huzhifeng@CentOS72 ~]$ sudo wget https://copr.fedorainfracloud.org/coprs/librehat/shadowsocks/repo/epel-7/librehat-shadowsocks-epel-7.repo -P /etc/yum.repos.d/
[huzhifeng@CentOS72 ~]$ cat /etc/yum.repos.d/librehat-shadowsocks-epel-7.repo
[librehat-shadowsocks]
name=Copr repo for shadowsocks owned by librehat
baseurl=https://copr-be.cloud.fedoraproject.org/results/librehat/shadowsocks/epel-7-$basearch/
type=rpm-md
skip_if_unavailable=True
gpgcheck=1
gpgkey=https://copr-be.cloud.fedoraproject.org/results/librehat/shadowsocks/pubkey.gpg
repo_gpgcheck=0
enabled=1
enabled_metadata=1
[huzhifeng@CentOS72 ~]$ sudo yum update

然后安装 shadowsocks-qt5

1
2
3
4
5
6
7
8
9
10
11
[huzhifeng@CentOS72 ~]$ sudo yum install -y shadowsocks-qt5
Installed:
shadowsocks-qt5.x86_64 0:2.8.0-1.el7.centos
Dependency Installed:
GraphicsMagick.x86_64 0:1.3.25-1.el7 botan.x86_64 0:1.10.14-3.el7 libQtShadowsocks.x86_64 0:1.10.0-1.el7.centos libappindicator.x86_64 0:12.10.0-11.el7 libdbusmenu.x86_64 0:12.10.2-9.el7
libdbusmenu-gtk2.x86_64 0:12.10.2-9.el7 libindicator.x86_64 0:12.10.1-5.el7 libwmf-lite.x86_64 0:0.2.8.4-41.el7_1 qt5-qtbase.x86_64 0:5.6.1-10.el7 qt5-qtbase-common.noarch 0:5.6.1-10.el7
zbar.x86_64 0:0.10-27.el7
Complete!
[huzhifeng@CentOS72 ~]$

启动的时候报错了,提示找不到libQt5Widgets.so.5

1
2
3
[huzhifeng@CentOS72 ~]$ ss-qt5
ss-qt5: error while loading shared libraries: libQt5Widgets.so.5: cannot open shared object file: No such file or directory
[huzhifeng@CentOS72 ~]$

该问题可以参考 centos 7 安装后无法打开 #283 CentOS 7安装后无法打开 #288
libQt5Widgets.so.5是由包qt5-qtbase-gui所提供,所以安装qt5-qtbase-gui即可解决问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[huzhifeng@CentOS72 ~]$ yum provides libQt5Widgets.so.5
qt5-qtbase-gui-5.6.1-10.el7.i686 : Qt5 GUI-related libraries
Repo : base
Matched from:
Provides : libQt5Widgets.so.5
[huzhifeng@CentOS72 ~]$ sudo yum install qt5-qtbase-gui -y
Installed:
qt5-qtbase-gui.x86_64 0:5.6.1-10.el7
Dependency Installed:
xcb-util-image.x86_64 0:0.4.0-2.el7 xcb-util-keysyms.x86_64 0:0.4.0-1.el7 xcb-util-renderutil.x86_64 0:0.3.9-3.el7 xcb-util-wm.x86_64 0:0.4.1-5.el7
Complete!
[huzhifeng@CentOS72 ~]$ rpm -ql qt5-qtbase-gui | grep "libQt5Widgets.so.5"
/usr/lib64/libQt5Widgets.so.5
/usr/lib64/libQt5Widgets.so.5.6
/usr/lib64/libQt5Widgets.so.5.6.1
[huzhifeng@CentOS72 ~]$

Share over LAN

Share over LAN 是一个非常实用的功能,它把 Shadowsocks 变成了一个代理中转器,允许局域网内的其它设备连接到开启了 Share over LAN 功能的机器上科学上网。

Windows 系统

How to Use Share over LAN #73
How to use share over the LAN #83
Windows 下开启 Share over LAN 非常简单,只需要在 Shadowsocks 客户端工具中勾选 Share Over LAN,如下图:

Linux 系统

修改 Local Address

打开 Shadowsocks-Qt5 配置界面,修改 Local Address,默认是 127.0.0.1

1
2
3
4
huzhifeng@Ubuntu16041:~$ netstat -nlp | grep 1080
tcp 0 0 127.0.0.1:1080 0.0.0.0:* LISTEN 5075/ss-qt5
udp 0 0 127.0.0.1:1080 0.0.0.0:* 5075/ss-qt5
huzhifeng@Ubuntu16041:~$

修改为 0.0.0.0(注意: 修改后需断开再重新连接)

1
2
3
4
huzhifeng@Ubuntu16041:~$ netstat -nlp | grep 1080
tcp 0 0 0.0.0.0:1080 0.0.0.0:* LISTEN 5075/ss-qt5
udp 0 0 0.0.0.0:1080 0.0.0.0:* 5075/ss-qt5
huzhifeng@Ubuntu16041:~$

添加防火墙规则开放1080端口

Ubuntu 16.04.1 系统
关闭防火墙

1
2
3
4
5
huzhifeng@Ubuntu16041:~$ sudo ufw disable
Firewall stopped and disabled on system startup
huzhifeng@Ubuntu16041:~$ sudo ufw status
Status: inactive
huzhifeng@Ubuntu16041:~$

或者开启防火墙并开放端口 1080
参考 Enable or block firewall access

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
huzhifeng@Ubuntu16041:~$ sudo ufw enable
huzhifeng@Ubuntu16041:~$ sudo ufw allow 1080
Rule added
Rule added (v6)
huzhifeng@Ubuntu16041:~$ sudo ufw reload
huzhifeng@Ubuntu16041:~$ sudo ufw status
Status: active
To Action From
-- ------ ----
5900 ALLOW Anywhere
22/tcp ALLOW Anywhere
1080 ALLOW Anywhere
5900 (v6) ALLOW Anywhere (v6)
22/tcp (v6) ALLOW Anywhere (v6)
1080 (v6) ALLOW Anywhere (v6)
huzhifeng@Ubuntu16041:~$

CentOS 7.2 系统
关闭防火墙 How to Stop and Disable Firewalld on CentOS 7

1
2
➜ ~ systemctl stop firewalld
➜ ~ systemctl status firewalld

或者开启防火墙 How to Start and Enable Firewalld on CentOS 7 并开放端口 1080
参考 Open a Port on CentOS/RHEL 7How to open http port 80 on Redhat 7 Linux using firewall-cmd

1
2
3
4
5
6
7
8
9
10
➜ ~ systemctl start firewalld
➜ ~ firewall-cmd --zone=public --add-port=1080/tcp --permanent
success
➜ ~ firewall-cmd --zone=public --add-port=1080/udp --permanent
success
➜ ~ firewall-cmd --reload
success
➜ ~ firewall-cmd --list-all | grep 1080
ports: 1080/tcp 1080/udp
➜ ~

测试

连接 Ubuntu 16.04.1 (192.168.10.159)

1
2
3
4
5
6
7
8
[huzhifeng@CentOS72 ~]$ curl --proxy http://192.168.10.159:1080 -I www.google.com
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.com.hk/?gfe_rd=cr&ei=ft2rWKCQM8rU8AfQjpLIBw
Content-Length: 262
Date: Tue, 21 Feb 2017 06:26:06 GMT
[huzhifeng@CentOS72 ~]$

连接 CentOS 7.2 (192.168.10.10)

1
2
3
4
5
6
7
8
[huzhifeng@CentOS72 ~]$ curl --proxy http://192.168.10.10:1080 -I www.google.com
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.com.hk/?gfe_rd=cr&ei=3N2rWOSOAsvU8AePnaL4DA
Content-Length: 262
Date: Tue, 21 Feb 2017 06:27:40 GMT
[huzhifeng@CentOS72 ~]$

HTTP Proxy vs Socks5 Proxy

旧版本的 youtube-dl 不支持 Socks5 代理,只支持 HTTP 代理,详见如何配置才能让 Youtube-dl 走代理呢SOCKS proxy support #402
新版本已经能够支持 Socks5 SOCKS proxy support has been added in youtube-dl 2016.05.10

HTTP 代理

CentOS 系统上需要安装shadowsocks-qt5才能提供 HTTP 代理

1
2
3
4
5
6
7
8
9
10
[huzhifeng@CentOS72 ~]$ rpm -ql shadowsocks-qt5
/usr/bin/ss-qt5
/usr/share/applications
/usr/share/applications/shadowsocks-qt5.desktop
/usr/share/icons
/usr/share/icons/hicolor
/usr/share/icons/hicolor/512x512
/usr/share/icons/hicolor/512x512/apps
/usr/share/icons/hicolor/512x512/apps/shadowsocks-qt5.png
[huzhifeng@CentOS72 ~]$ /usr/bin/ss-qt5

测试一下 HTTP 代理是否已生效

1
2
3
4
5
6
7
8
9
[huzhifeng@CentOS72 ~]$ curl -i -x http://127.0.0.1:1080 google.com
[huzhifeng@CentOS72 ~]$ youtube-dl --proxy http://127.0.0.1:1080 https://www.youtube.com/watch?v=33326XobwYg
[youtube] 33326XobwYg: Downloading webpage
[youtube] 33326XobwYg: Downloading video info webpage
[youtube] 33326XobwYg: Extracting video information
[youtube] 33326XobwYg: Downloading MPD manifest
[download] Destination: Relax-and-Recover demo-33326XobwYg.webm
[download] 100% of 2.65MiB in 00:13
[huzhifeng@CentOS72 ~]$

Socks5 代理

2016.05.10之后的版本都可以支持 Socks5 Proxy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[huzhifeng@CentOS72 ~]$ youtube-dl --version
2016.06.14
[huzhifeng@CentOS72 ~]$ youtube-dl --help
...
Network Options:
--proxy URL Use the specified HTTP/HTTPS/SOCKS proxy. To enable experimental SOCKS proxy, specify a proper scheme. For example
socks5://127.0.0.1:1080/. Pass in an empty string (--proxy "") for direct connection
[huzhifeng@CentOS72 ~]$ youtube-dl --proxy socks5://127.0.0.1:1080 https://www.youtube.com/watch?v=key9SkCU3io
[youtube] key9SkCU3io: Downloading webpage
[youtube] key9SkCU3io: Downloading video info webpage
[youtube] key9SkCU3io: Extracting video information
[youtube] key9SkCU3io: Downloading MPD manifest
[download] Destination: 7 - Create custom linux with Buildroot for Raspberry Pi 2-key9SkCU3io.mp4
[download] 100% of 62.73MiB in 10:03
[huzhifeng@CentOS72 ~]$
[huzhifeng@CentOS72 ~]$ youtube-dl --proxy socks5://127.0.0.1:1080 https://www.youtube.com/watch?v=33326XobwYg
[youtube] 33326XobwYg: Downloading webpage
[youtube] 33326XobwYg: Downloading video info webpage
[youtube] 33326XobwYg: Extracting video information
[youtube] 33326XobwYg: Downloading MPD manifest
[download] Destination: Relax-and-Recover demo-33326XobwYg.webm
[download] 100% of 2.65MiB in 00:02
[huzhifeng@CentOS72 ~]$

参考

Shadowsocks Clients
Shadowsocks-Qt5
安装指南
各种系统下Shadowsocks客户端的安装与配置
CentOS 安装shadowsocks 客户端 和 chrome 浏览器
Shadowsocks (简体中文))
CentOS命令行使用shadowsocks代理的方法
Ubuntu14.04使用Shadowsocks及转换HTTP代理
CentOS命令行下使用代理:Shadowsocks+privoxy+redsocks实现全局代理
五大开源 Web 代理服务器横评:Squid、Privoxy、Varnish、Polipo、Tinyproxy
ShadowSocks(影梭)不完全指南

EOF