Centos7安装配置Shadowsocks python客户端

192次阅读
没有评论

共计 8799 个字符,预计需要花费 22 分钟才能阅读完成。

需求:因工作需要,需要配置一个科学上网服务器 以前搭建过一个,后来由于某种原因不能用了。所以重新搭建一个 此文档做个记录
服务器:Centos7.x

1、安装配置Shadowsocks客户端

安装Shadowsocks客户端

1
2
3
4
$ yum install -y epel-release
$ yum install -y python-pip
$ pip install shadowsocks
# pip 是 Python 的包管理工具,这里我们用 pip 安装 shadowsocks。

配置Shadowsocks客户端

注意 Shadowsocks的配置文件自己创建

1
2
mkdir /etc/shadowsocks
vim /etc/shadowsocks/shadowsocks.json

添加一下配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
  "server":"x.x.x.x",             #你的 ss 服务器 地址
  "server_port":14131,                #你的 ss 服务器端口
  "local_address": "127.0.0.1",   #本地ip
  "local_port":1080,                 #本地端口
  "password":"password",          #连接 ss 密码
  "timeout":300,                  #等待超时
  "method":"aes-256-cfb",         #加密方式
  "fast_open": false,             #fast_open:true或false。开启fast_open以降低延迟,但要求Linux内核在3.7+。
  "workers": 1                    #工作线程数
                                  
}

设置成自启动

为了方便启动和关闭 这里设置成自启动

vim /etc/systemd/system/shadowsocks.service

1
2
3
4
5
6
7
8
9
[Unit]
Description=Shadowsocks

[Service]
TimeoutStartSec=0
ExecStart=/usr/bin/sslocal -c /etc/shadowsocks/shadowsocks.json

[Install]
WantedBy=multi-user.target

启动Shadowsocks客户端

1
2
3
systemctl enable shadowsocks.service
systemctl start shadowsocks.service
systemctl status shadowsocks.service
注意:此处可能有坑。
如果你的加密方式是aes-256-cfb方式就可以跳过这段直接看“安装配置Privovy”的内容。 如果你的加密方式:chacha20-ietf-poly1305这种就得掉坑里了

我的ss服务器的加密方式是chacha20-ietf-poly1305

在启动Shadowsocks时 启动失败 显示一下内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
[root@k8s-77-71 ~]# systemctl status shadowsocks.service
● shadowsocks.service - Shadowsocks
   Loaded: loaded (/etc/systemd/system/shadowsocks.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 三 2019-05-15 13:01:08 CST; 12s ago
  Process: 13171 ExecStart=/usr/bin/sslocal -c /etc/shadowsocks/shadowsocks.json (code=exited, status=1/FAILURE)
 Main PID: 13171 (code=exited, status=1/FAILURE)

5月 15 13:01:08 k8s-77-71 systemd[1]: Started Shadowsocks.
5月 15 13:01:08 k8s-77-71 sslocal[13171]: INFO: loading config from /etc/shadowsocks/shadowsocks.json
5月 15 13:01:08 k8s-77-71 sslocal[13171]: 2019-05-15 13:01:08 ERROR    method chacha20-ietf-poly1305 not supported
5月 15 13:01:08 k8s-77-71 systemd[1]: shadowsocks.service: main process exited, code=exited, status=1/FAILURE
5月 15 13:01:08 k8s-77-71 systemd[1]: Unit shadowsocks.service entered failed state.
5月 15 13:01:08 k8s-77-71 systemd[1]: shadowsocks.service failed.

可以看出 method chacha20-ietf-poly1305 not supported 不支持chacha20-ietf-poly1305加密方式

填坑操作

经过一顿谷歌、百度。找到了以下解决办法(当然你也可以找其他解决办法)

安装libsodium:

1
yum install libsodium -y

然后再启动,发现还是上面的问题。没有启动起来
执行以下操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[root@k8s-77-71 ~]# pip install https://github.com/shadowsocks/shadowsocks/archive/master.zip -U
Collecting https://github.com/shadowsocks/shadowsocks/archive/master.zip
  Downloading https://github.com/shadowsocks/shadowsocks/archive/master.zip (115kB)
    100% |████████████████████████████████| 122kB 871kB/s
Installing collected packages: shadowsocks
  Found existing installation: shadowsocks 2.8.2
    Uninstalling shadowsocks-2.8.2:
      Successfully uninstalled shadowsocks-2.8.2
  Running setup.py install for shadowsocks ... done
Successfully installed shadowsocks-3.0.0
You are using pip version 8.1.2, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

然后重启一下Shadowsocks再查看状态,可以看到已经正常启动了:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[root@k8s-77-71 ~]# systemctl status shadowsocks.service
● shadowsocks.service - Shadowsocks
   Loaded: loaded (/etc/systemd/system/shadowsocks.service; disabled; vendor preset: disabled)
   Active: active (running) since 三 2019-05-15 13:07:00 CST; 1s ago
 Main PID: 13249 (sslocal)
    Tasks: 1
   Memory: 9.3M
   CGroup: /system.slice/shadowsocks.service
           └─13249 /usr/bin/python2 /usr/bin/sslocal -c /etc/shadowsocks/shadowsocks.json

5月 15 13:07:00 k8s-77-71 systemd[1]: Started Shadowsocks.
5月 15 13:07:00 k8s-77-71 sslocal[13249]: INFO: loading config from /etc/shadowsocks/shadowsocks.json
5月 15 13:07:01 k8s-77-71 sslocal[13249]: 2019-05-15 13:07:01 INFO     loading libsodium from libsodium.so.23
5月 15 13:07:01 k8s-77-71 sslocal[13249]: 2019-05-15 13:07:01 INFO     starting local at 127.0.0.1:1080

验证Shadowsocks客户端是都正常运行

1
curl --socks5 127.0.0.1:1080 http://httpbin.org/ip

如果Shadowsocks客户端已经正常运行,则结果如下:

1
2
3
{
"origin": "x.x.x.x" #你的Shadowsock服务器IP
}

2、安装配置Privoxy

Shadowsocks是一个 socket5 服务,我们需要使用 Privoxy 把流量转到 http/https 上。 虽然socks代理起来了,但是还不能直接用,需要privoxy转发一下

安装Privoxy

安装Privoxy

1
yum -y install privoxy

启动Privoxy

1
2
3
systemctl enable privoxy
systemctl start privoxy
systemctl status privoxy

配置Privoxy

修改配置文件 /etc/privoxy/config

1
[root@k8s-77-71 ~]# vim /etc/privoxy/config

确保以下内容没有被注释掉 这两行不在一起

1
2
listen-address 127.0.0.1:8118 # 8118 是默认端口,不用改  
forward-socks5t / 127.0.0.1:1080 . #转发到本地端口,注意别忘了最后的.

注意:上面listen-address一行。
如果想让其他机器也通过这台机器连接代理就需要设置成ip:端口

设置http/https代理
修改配置文件/etc/profile

1
vim /etc/profile

添加以下内容:

1
2
export http_proxy=http://127.0.0.1:8118
export https_proxy=http://127.0.0.1:8118

保存好执行一下:source /etc/profile
注意:端口和Privoxy中监听端口保持一致

3、验证及优化

验证是否可用

  • 方法一、
    执行 curl www.google.com 返回一大堆HTML 则说明正常
  • 方法二、
    执行 curl -I www.google.com 。有如下相似显示则说明正常
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 [root@k8s-77-71 ~]# curl -I www.google.com
HTTP/1.1 200 OK
Date: Wed, 15 May 2019 07:08:30 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2019-05-15-07; expires=Fri, 14-Jun-2019 07:08:30 GMT; path=/; domain=.google.com
Set-Cookie: NID=183=Bpl52R_n_4pib3A-dmbDsxeXYP0qnw0HFqfGpDIrBELfCum44Cbh8VtwrFdKZwJWvTxidcrCDEwnQZf9qgpijF8DoCPHwKuBteNPqwzWIDdhV5ieOvxFQBaZqtBNslmiuoANoQlxkxiQKnCWxilUWM30NtUnGVGux0uQ2DPMFio; expires=Thu, 14-Nov-2019 07:08:30 GMT; path=/; domain=.google.com; HttpOnly
Transfer-Encoding: chunked
Accept-Ranges: none
Vary: Accept-Encoding
Proxy-Connection: keep-alive
[root@k8s-77-71 ~]#
  • 方法三、
    也是最简单的方法 如果你有桌面环境 直接用浏览器访问Google

优化

在windows或mac下用过Shadowsocks的都知道,Shadowsocks有PAC模式和全局模式

  • PAC模式:也就是智能分流模式,只有被墙的才会走代理。
  • 全局模式:在全局模式下,所有网站默认走代理。

上面的默认配置是全局模式

配置好Shadowsocks和Privoxy后 服务器访问公司内网服务:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[root@k8s-77-71 ~]# curl 172.16.77.221:10021

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
  <title>502 - No server or forwarder data received (Privoxy@k8s-77-71)</title>
  <meta http-equiv="Content-Style-Type" content="text/css">
  <meta http-equiv="Content-Script-Type" content="text/javascript">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="robots" content="noindex,nofollow">
  <link rel="shortcut icon" href="http://config.privoxy.org/error-favicon.ico">
  <style type="text/css">
......

发现无法访问内网服务,显然访问内网服务时也默认走了代理导致。 这显然不符合我们的要求

解决内网不走代理问题

  • 方法一、
    设置/etc/privoxy/config
1
vi /etc/privoxy/config

加入以下内容:

1
forward    172.16.*.*/     .

重启Privoxy

1
systemctl restart privoxy

再访问内网服务:

1
2
3
[root@k8s-77-71 ~]# curl 172.16.77.221:10021
<html><body>You are being <a href="http://172.16.77.221:10021/users/sign_in">redirected</a>.</body></html>
[root@k8s-77-71 ~]#

可以正常访问了
– 方法二、
> 此方法有个缺点,就是需要一个一个的添加地址 也可以添加域名

配置 /etc/profile

1
vim /etc/profile

加入以下内容

1
export no_proxy="172.16.77.221"

保存后执行 source /etc/profile

再访问内网服务:

1
2
3
[root@k8s-77-71 ~]# curl 172.16.77.221:10021
<html><body>You are being <a href="http://172.16.77.221:10021/users/sign_in">redirected</a>.</body></html>
[root@k8s-77-71 ~]#

也可以达到预期效果。

4、配置PAC模式

由于前面已经配置好全局模式了。先为了验证PAC模式 需要将全局模式的一些配置恢复一下

恢复配置:vi /etc/privoxy/config 将前面添加的这两行配置注释掉

1
2
forward-socks5t / 127.0.0.1:1080 . #转发到本地端口,注意别忘了最后的.
forward    172.16.*.*/     .

下载生成Privoxy-action配置的脚本 >此脚本是在github上找到的 有兴趣的可以去github上看看 链接

1
curl -skL https://raw.github.com/zfl9/gfwlist2privoxy/master/gfwlist2privoxy -o gfwlist2privoxy

生成配置启动

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@k8s-77-71 ~]# bash gfwlist2privoxy '127.0.0.1:1080'  #注意‘127.0.0.1:1080’为你的sock5代理地址
Generated file: 'gfwlist.action'. Please put it in privoxy config directory.
Usually, the directory is '/etc/privoxy'. If yes, then exec following command:

mv -f gfwlist.action /etc/privoxy
echo 'actionsfile gfwlist.action' >>/etc/privoxy/config
service privoxy restart (via SysVinit)
systemctl restart privoxy.service (via Systemd)

Enjoy it!
[root@k8s-77-71 ~]#

根据提示操作:

1
2
3
[root@k8s-77-71 ~]# cp -af gfwlist.action /etc/privoxy/
[root@k8s-77-71 ~]# echo 'actionsfile gfwlist.action' >> /etc/privoxy/config
[root@k8s-77-71 ~]# systemctl restart privoxy.service

测试PAC是否正常

  • 测试谷歌,可以正常访问。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
[root@k8s-77-71 ~]# curl -I  www.google.com
HTTP/1.1 200 OK
Date: Wed, 15 May 2019 09:51:58 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2019-05-15-09; expires=Fri, 14-Jun-2019 09:51:58 GMT; path=/; domain=.google.com
Set-Cookie: NID=183=r2vRw0LzqNXglFgwyEeMifKKSLNuP-lQuzmF5QQXjCsVjRqrur9ka8eUom7SF7poH9V3pfajnLx8paBNgDTcX0YTbxqfd49bW-i0aep8KMXuGTq6x2N-mnjHqU8IAaTokKPOtdv5YNBA3idCSh5HQLkw0Tiv7efmtLBwdyYnJgI; expires=Thu, 14-Nov-2019 09:51:58 GMT; path=/; domain=.google.com; HttpOnly
Transfer-Encoding: chunked
Accept-Ranges: none
Vary: Accept-Encoding
Proxy-Connection: keep-alive

[root@k8s-77-71 ~]#
  • 测试内网服务,可以正常访问。
1
2
3
[root@k8s-77-71 ~]# curl 172.16.77.221:10021
<html><body>You are being <a href="http://172.16.77.221:10021/users/sign_in">redirected</a>.</body></html>
[root@k8s-77-71 ~]#

5、小技巧

  • 如果别的机器需要连接此机器的代理只需要在/etc/profile加入代理信息即可
1
2
export http_proxy=http://127.0.0.1:8118
export https_proxy=http://127.0.0.1:8118

保存后 source /etc/profile – 还可以使用别名方式来选择打开和关闭代理

vim /etc/profile

1
2
alias proxy='export all_proxy=http://127.0.0.1:8118'
alias unproxy='unset all_proxy'

保存后 source /etc/profile

执行proxy来打开代理
执行unproxy来关闭代理

对于是使用PAC模式还是全局模式,可根据自己的需求来决定

正文完
 
admin
版权声明:本站原创文章,由 admin 2020-04-20发表,共计8799字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码