CentOS7.x安装配置Shadowsocks客户端终端翻墙

来自linux中国网wiki
Evan讨论 | 贡献2020年4月17日 (五) 10:02的版本 →‎config
跳到导航 跳到搜索

安装配置Shadowsocks客户端

安装Shadowsocks客户端

sudo yum -y install epel-release
sudo yum -y install python-pip
#pip install --upgrade pip
yum install libsodium-devel
  pip install https://github.com/shadowsocks/shadowsocks/archive/master.zip -U


#sudo pip install shadowsocks     # 有一次居然忘记了这一步安装Shadowsocks客户端

配置Shadowsocks客户端

#新建配置文件
sudo mkdir /etc/shadowsocks
# sudo vi /etc/shadowsocks/shadowsocks.json

cat >>/etc/shadowsocks/shadowsocks.json <<EOF
{
    "server":"linuxsa.org",
    "server_port":443,
    "local_port":1080,
    "password":"lai2240881",
    "timeout":600,
    "method":"aes-256-cfb",
    "fast_open": false,
    "workers": 1
}
EOF

参数说明: server:Shadowsocks服务器地址 server_port:Shadowsocks服务器端口 local_address:本地IP local_port:本地端口 password:Shadowsocks连接密码 timeout:等待超时时间 method:加密方式 workers:工作线程数 fast_open:true或false。开启fast_open以降低延迟,但要求Linux内核在3.7+。 开启方法,不要用 echo 3 > /proc/sys/net/ipv4/tcp_fastopen

配置自启动

#① 新建启动脚本文件/etc/systemd/system/shadowsocks.service,内容如下:
mkdir  /etc/shadowsocks/
cat >>/etc/systemd/system/shadowsocks.service <<EOF
[Unit]
Description=Shadowsocks
[Service]
TimeoutStartSec=0
ExecStart=/usr/bin/sslocal -c /etc/shadowsocks/shadowsocks.json
[Install]
WantedBy=multi-user.target
EOF

#② 启动Shadowsocks客户端

    systemctl enable shadowsocks.service
    systemctl start shadowsocks.service
    systemctl status shadowsocks.service #查看现在状态 和自启动状态

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

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

若Shadowsock客户端已正常运行,则结果如下:

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

安装配置Privoxy

Shadowsocks是一个 socket5 服务,我们需要使用 Privoxy 把流量转到 http/https 上

安装Privoxy

install

    sudo yum -y install privoxy
    systemctl enable privoxy
    systemctl start privoxy
    systemctl status privoxy

config

yum install   w3m -y #lynx 

配置Privoxy
① 修改配置文件/etc/privoxy/config
vi /etc/privoxy/config 

② 确保如下内容没有被注释掉
     listen-address 0.0.0.0:8118  #如果代理给局域网的其它人用 就要用 0.0.0.0
    listen-address 127.0.0.1:8118    ## # 这个可能已打开的了 8118 是默认端口,不用改 这里要写上本机的ip
    forward-socks5t / 127.0.0.1:1080 . #转发到本地端口 转发到本地端口 这个要自己添加

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

添加如下信息:
cat >> /etc/profile <<EOF
export http_proxy=http://127.0.0.1:8118
export https_proxy=http://127.0.0.1:8118
export ftp_proxy=http://127.0.0.1:8118
EOF

 source /etc/profile

#不要代理的
export NO_PROXY='localhost,127.0.0.1,192.168.88.30,192.168.88.31,192.168.88.32,10.96.0.0,10.224.0.0,10.96.0.0/12,10.224.0.0/16'


注:端口和privoxy 中的监听端口保持一致



 systemctl start privoxy
    systemctl status privoxy

    验证是否可用
lynx www.google.com

优化

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

配置PAC模式

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

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


下载生成Privoxy-action配置的脚本 >此脚本是在github上找到的 有兴趣的可以去github上看看
https://github.com/zfl9/gfwlist2privoxy


curl -skL https://raw.github.com/zfl9/gfwlist2privoxy/master/gfwlist2privoxy -o gfwlist2privoxy
生成配置启动
# 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!

cp -af gfwlist.action /etc/privoxy/
echo 'actionsfile gfwlist.action' >> /etc/privoxy/config
 systemctl restart privoxy.service


测试PAC是否正常

    测试谷歌,可以正常访问。

curl -I  www.google.com

Centos7安装配置Shadowsocks客户端

解决内网不走代理问题

privoxy

vi /etc/privoxy/config

加入以下内容: 如果你的内网是172.16开头 

forward    172.16.*.*/     .

vim /etc/profile

vim /etc/profile

export no_proxy="172.16.77.221"
#此方法有个缺点,就是需要一个一个的添加地址 也可以添加域名

see also

Linux 使用 ShadowSocks + Privoxy 实现 PAC 代理

CentOS命令行使用shadowsocks代理的方法

Shadowsocks + Privoxy 搭建 http 代理服务

openwrt 使用Privoxy将socks5代理转为http代理

https://www.zybuluo.com/ncepuwanghui/note/954160