Linux实现终端翻墙(shadowsocks和Privoxy)
注:提前条件有境外的云服务器并配置好shadowsocks server端。
安装基础环境:
apt-get update
apt-get install python-pip -y
pip install shadowsocks
####编辑配置文件:
cat > /etc/ssclient.json << EOF
{
“server”:“shadowsocks的IP”,
“server_port”:8020,
“local_address”: “127.0.0.1”,
“local_port”:1080,
“password”:“linkfacexxxxx”,
“timeout”:300,
“method”:“aes-256-cfb”,
“fast_open”: false,
“workers”: 1
}
EOF
其中local_address是本地绑定的IP。
method是加密码算法,这个必须跟shadowsocks的服务端的一致。
启动、停止、状态
启动方式:
nohup ssserver -c /etc/shadowsocks.json & (或者
$ sudo sslocal -c /etc/shadowsocks.json -d start)
要停止
$ sudo sslocal -c /etc/shadowsocks.json -d stop
查看进程:
ps -ef |grep shadowsocks
netstat -ntlp|grep 8020
配置启动Privoxy实现翻墙
sudo apt-get install privoxy
编辑配置文件/etc/privoxy/config,加入下面两行内容。
forward-socks5t / 127.0.0.1:1080 .
listen-address localhost:8118
需要注意:
1)127.0.0.1:1080是shadowsocks的客户端的IP和端口,要与上面shadowsocks里的配置相符。
2)请不要忽略这行配置后面的.,这可不是句号的意思,这个必须有。
启动服务:
$ sudo service privoxy restart
这样就配置好了。试一下
$ export http_proxy=http://localhost:8118
$ export https_proxy=http://localhost:8118
$ curl ip.cn
配置环境变量中:
echo "
export http_proxy=http://127.0.0.1:8118
export https_proxy=http://127.0.0.1:8118
" >> /etc/profile
source /etc/profile
curl www.google.com
测试如下:已配置成功!!!
linkface@rancher-docker:~/init_shadowsocks$ source /etc/profile
linkface@rancher-docker:~/init_shadowsocks$ curl www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/%3Fgws_rd%3Dcr&ust=1537324576032649&usg=AOvVaw0UTYdnOO87fKZOBQtIbxkJ">here</A>.
</BODY></HTML>
linkface@rancher-docker:~/init_shadowsocks$
1、privoxy安装
安装很简单用brew安装:
brew install privoxy
2、privoxy配置
打开配置文件 /usr/local/etc/privoxy/config
vim /usr/local/etc/privoxy/config
加入下面这两项配置项
listen-address 0.0.0.0:8118
forward-socks5 / localhost:1080 .
第一行设置privoxy监听任意IP地址的8118端口。第二行设置本地socks5代理客户端端口,注意不要忘了最后有一个空格和点号。
3、启动privoxy
因为没有安装在系统目录内,所以启动的时候需要打全路径。
sudo /usr/local/sbin/privoxy /usr/local/etc/privoxy/config
4、查看是否启动成功
netstat -na | grep 8118
看到有类似如下信息就表示启动成功了
tcp4 0 0 *.8118 *.* LISTEN
如果没有,可以查看日志信息,判断哪里出了问题。打开配置文件找到 logdir 配置项,查看log文件。
5、privoxy使用
在命令行终端中输入如下命令后,该终端即可翻墙了。
export http_proxy='http://localhost:8118'
export https_proxy='http://localhost:8118'
他的原理是讲socks5代理转化成http代理给命令行终端使用。
如果不想用了取消即可
unset http_proxy
unset https_proxy
如果关闭终端窗口,功能就会失效,如果需要代理一直生效,则可以把上述两行代码添加到 ~/.bash_profile 文件最后。
vim ~/.bash_profile
-----------------------------------------------------
export http_proxy='http://localhost:8118'
export https_proxy='http://localhost:8118'
-----------------------------------------------------
使配置立即生效
source ~/.bash_profile
还可以在 ~/.bash_profile 里加入开关函数,使用起来更方便
function proxy_off(){
unset http_proxy
unset https_proxy
echo -e "已关闭代理"
}
function proxy_on() {
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
export http_proxy="http://127.0.0.1:8118"
export https_proxy=$http_proxy
echo -e "已开启代理"
}
测试验证:
memorycancel:linkface zlord$ curl ip.cn
当前 IP:47.75.206.106 来自:香港特别行政区 阿里云
memorycancel:linkface zlord$ curl www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=http://www.google.com.hk/%3Fgws_rd%3Dcr&ust=1537325539292812&usg=AOvVaw1sgOJ4jA8AIul6tMjqHkL9">here</A>.
</BODY></HTML>
memorycancel:linkface zlord$