MENU

linux 配置 shadowsocks 代理全局代理

December 25, 2018 • Read: 6955 • 应用配置 , 环境配置

公司采集 1688 功能不能用,瞅了一眼,是阿里给禁掉了,暂时考虑方案为为服务器新建 vpn
233.png

一. centos7
客户端为 CentOS (一般 Linux 环境) 时,(尤其是命令行里的命令)使用 shadowsocks 搭配 Privoxy 上网的方法。
安装客户端 shadowsocks
其实 shadowsocks 安装时是不分客户端还是服务器端的,只不过安装后有两个脚本一个是 sslocal 代表以客户端模式工作,一个是 ssserver 代表以服务器端模式工作。

yum install epel-release -y
yum install python-pip

pip install --upgrade pip

pip install shadowsocks

依次执行上述两个命令,先安装 Python 的 pip,然后安装 shadowsocks。然后执行下述命令后台启动:
执行前请更改命令中的默认参数

nohup sslocal -s your_server_ip -p your_server_port -l 1080 -k your_server_passwd -t 600 -m rc4-md5 > /dev/null

注意
1, 使用的是 sslocal 这个命令,表示 shadowsocks 以客户端模式工作
2, 将上述命令里的 your_server_ip,your_server_port,your_server_passwd 换成自己的,这三个分别代表服务器 ip,服务器上 shadowsocks 的端口以及密码。后面的 rc4-md5 加密方式也要换成跟 server 端一致。
3, 前面的 nohub 表示后台执行,否则将会阻塞 shell 端口.
为了更方便,建议新建一个.json 的文件,将上述信息放里面,如新建 /etc/shadowsocks.json 文件,内容为:

ps : 用的时候把注释删除

vi /etc/shadowsocks.json

配置如下:

{
    "server":"your_server_ip",
    #ss服务器IP

    "server_port":your_server_port,
    #端口

    "local_address": "127.0.0.1",
    #本地ip

    "local_port":1080,
    #本地端口

    "password":"your_server_passwd",
    #连接ss密码

    "timeout":300,
    #等待超时

    "method":"rc4-md5",
    #加密方式

    "fast_open": false,
    # true 或 false。如果你的服务器 Linux 内核在3.7+,可以开启 fast_open 以降低延迟。开启方法: echo 3 > /proc/sys/net/ipv4/tcp_fastopen 开启之后,将 fast_open 的配置设置为 true 即可

    "workers": 1
    # 工作线程数
}

然后运行

nohup sslocal -c /etc/shadowsocks.json /dev/null 2>&1 & 

启动 shadowsocks。
如果想增加开启自动启动,执行:
echo "nohup sslocal -c /etc/shadowsocks.json/dev/null 2>&1 &" /etc/rc.local 执行
ps aux |grep sslocal |grep -v "grep" 查看后台 sslocal 是否运行。
安装 Privoxy
安好了 shadowsocks 后,但它是 socks5 代理,我门在 shell 里执行的命令,发起的网络请求现在还不支持 socks5 代理,只支持 http/https 代理。为了我门需要安装 privoxy 代理,它能把电脑上所有 http 请求转发给 shadowsocks。
访问官网 http://www.privoxy.org/ 获得 Privoxy 的最新源码:privoxy-3.0.24-stable-src.tar.gz, 执行 tar -zxvf privoxy-3.0.24-stable-src.tar.gz 解压,然后 cd privoxy-3.0.24-stable 进去。
下载

wget http://www.silvester.org.uk/privoxy/source/3.0.26%20%28stable%29/privoxy-3.0.26-stable-src.tar.gz

解压

tar -vxf privoxy-3.0.26-stable-src.tar.gz

安装前需要执行 useradd privoxy 创建一个用户 privoxy,然后依次执行如下三条命令:

autoheader && autoconf
./configure
make && make install

改之前备份配置文件

cp  /etc/privoxy/config /etc/privoxy/config.bak

查看 vim /etc/privoxy/config 文件,
先搜索关键字:listen-address 找到 listen-address 127.0.0.1:8118 这一句,保证这一句没有注释,8118 就是将来 http 代理要输入的端口。
然后搜索 forward-socks5t, 将 forward-socks5t / 127.0.0.1:1080 . 此句的注释去掉.
执行如下命令启动 privoxy,参考官网,不同的平台对应不同的方法:

systemctl start privoxy

配置 /etc/profile
执行 vim /etc/profile, 添加如下三句:

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

第三句 ftp 的代理根据需要,不需要的话可以不添加。然后 source /etc/profile,执行 curl www.google.com 或 wget www.google.com 判断是否成功访问。
或可以先执行:

curl ip.gs

显示为本地 IP 信息(例:当前 IP:222.xxx.xxx.xxx 来自:中国四川 电信) 直接执行:
export http_proxy=http://127.0.0.1:8118 再执行:
curl ip.gs 显示代理后的 IP 信息(例:当前 IP:138.xxx.xxx.xxx 来自:美国)
如果不能访问,请重启机器,依次打开 shadowsocks 和 privoxy 再测试.

nohup sslocal -c /etc/shadowsocks.json /dev/null 2>&1 &
systemctl start privoxy

二.ubuntu
配置全局代理
启动 shawdowsocks 服务后,发现并不能翻墙上网,这是因为 shawdowsocks 是 socks 5 代理,需要客户端配合才能翻墙。
为了让整个系统都走 shawdowsocks 通道,需要配置全局代理,可以通过 polipo 实现。
首先是安装 polipo:
1

sudo apt-get install polipo

接着修改 polipo 的配置文件 /etc/polipo/config:

logSyslog = true
logFile = /var/log/polipo/polipo.log
proxyAddress = "0.0.0.0"
socksParentProxy = "127.0.0.1:1080"
socksProxyType = socks5
chunkHighMark = 50331648
objectHighMark = 16384
serverMaxSlots = 64
serverSlots = 16
serverSlots1 = 32

重启 polipo 服务:

sudo /etc/init.d/polipo restart

为终端配置 http 代理:

export http_proxy="http://127.0.0.1:8123/"

接着测试下能否翻墙:

curl www.google.com

如果有响应,则全局代理配置成功。
注意事项
服务器重启后,下面两句需要重新执行:

sudo sslocal -c shawdowsocks.json -d start
export http_proxy="http://127.0.0.1:8123/"

兼总条贯 知至知终

无标签
最后编辑于: March 10, 2020
Leave a Comment

已有 1 条评论
  1. buy cialis

    Incredible! This blog looks exactly like my old one!
    It's on a totally different topic but it has pretty much the same layout
    and design. Outstanding choice of colors!