vps的ubuntu linux下轻松搭建stunnel通过https代理上外网

stunnel的优点和shadowsocks一样,但是却比shadowsocks更安全,stunnel可以来ssl证书,比shadowsocks的密码认证更安全,也能有效的避免中间人攻击,把你的shadowsocks数据拦截下,然后暴力破解应该是易如反掌~~

ssl的非对称加密过程参考: http://article.yeeyan.org/view/90729/174903相当安全

单独的stunnel是无法使用的,必须配合http代理,如squid
ubuntu linux服务器端安装:
sudo apt-get install squid3 stunnel4

squid默认代理端口号3128,可自行修改,默认此代理只能本地有权限访问
grep --color '^http_port' /etc/squid3/squid.conf


接下来是stunnel的证书和服务器端配置
1.生成自定义证书:
sudo -s
cd /etc/stunnel
#stunnel.pem是生成的证书文件名字
openssl req -new -x509 -days 365 -nodes -out stunnel.pem -keyout stunnel.pem
#生成证书的过程中需要填写一些国家城市公司等信息,全部随便填写即可
#下面的命令执行的时间叫长 耐心等待
openssl gendh 2048 >> stunnel.pem
#出现unable to write 'random state'错误的请执行 rm ~/.rnd
#查看生成的证书相关信息
openssl x509 -subject -dates -fingerprint -in stunnel.pem 


2.stunnel服务器对客户端证书的验证,stunnel的配置
#查看stunnel默认配置,下面命令结果的最后一行是默认配置
cat /etc/stunnel/README
#添加默认配置
sudo cp /usr/share/doc/stunnel4/examples/stunnel.conf-sample /etc/stunnel/stunnel.conf

a.编辑/etc/stunnel/stunnel.conf文件找到 chroot = /var/lib/stunnel4/这一行,假如有注释把注释去掉
b.开启调试模式,/etc/stunnel/stunnel.conf文件找到 debug = 7去掉注释,添加foreground = yes到debug = 7下面

#配置证书
cd /var/lib/stunnel4/
sudo mkdir certs
cp /etc/stunnel/stunnel.pem certs/`openssl x509 -hash -noout -in /etc/stunnel/stunnel.pem`.0. 

接下来编辑/etc/stunnel/stunnel.conf
a.找到cert = /etc/stunnel/mail.pem修改成cert = /etc/stunnel/stunnel.pem,设置cert证书路径
b.找到verify = 2去掉注释改成verify = 3,开启证书有效性验证
c.找到CApath = /certs有注释去掉注释,这个目录是建立在chroot = /var/lib/stunnel4/基础上,实际就是/var/lib/stunnel4/certs目录,也就是验证证书的目录
d.找到CAfile = /etc/stunnel/certs.pem,有注释去掉注释,并改成之前生成自定义证书的目录:CAfile = /etc/stunnel/stunnel.pem
关于证书的全部设置结束

3.配置stunnel的端口和squid的http代理
编辑/etc/stunnel/stunnel.conf,找到
; **************************************************************************
; * Service definitions (remove all services for inetd mode)               *
; **************************************************************************
从这里一直文件结尾,全部删除,添加如下内容:
[https]
accept  = 3128
connect = 0.0.0.0:443
简单解释:[https]这个https可以随便写, 3128是squid的http代理默认端口,stunnel将使用这个代理,也就是127.0.0.1:3128. connect部分是stunnel客户端将要连接的代理服务器地址和端口号,端口号443可自信随意修改

stunnel服务器端的所有配置完毕,重新启动:
sudo killall stunnel4
sudo /etc/init.d/stunnel4 start


最后是配置stunnel客户端:本人也是ubuntu系统 ubuntu desktop
安装stunnel:
#安装stunnel
sudo apt-get install stunnel4
#添加默认配置文件
sudo cp /usr/share/doc/stunnel4/examples/stunnel.conf-sample /etc/stunnel/stunnel.conf

开启stunnel调试模式,参考上面stunnel服务器端配置

配置证书
a.把上面stunnel服务器端生成的证书,复制一份到本地
可以这样
scp 你的用户名@服务器地址:/etc/stunnel/stunnel.pem /etc/stunnel/stunnel.pem

b.编辑本地/etc/stunnel/stunnel.conf文件,设置证书路径:
找到cert = /etc/stunnel/mail.pem修改成:cert = /etc/stunnel/stunnel.pem
c.找到; **************************************************************************
; * Service definitions (remove all services for inetd mode)               *
; **************************************************************************
一直到文件结尾全部删除,添加如下内容:
[https]
client = yes
accept  = 127.0.0.1:8080
connect = stunnel服务器地址:443
简单解释:client = yes表示stunnel是运行的是客户端模式,默认没有代表服务器模式
accept  = 127.0.0.1:8080当然就是浏览器需要设置的http代理端口了
connect = stunnel服务器地址:443,请自行修改成你的服务器地址和上面设置的端口号443
重启stunnel客户端:
sudo killall stunnel4
sudo /etc/init.d/stunnel4 start


假如所有一切运行正常,然后可以关闭调试模式.注释 foreground = yes 一行
ubuntu命令行测试非常简单:本地终端执行
export http_proxy='127.0.0.1:8080'
wget www.bing.com

若有失败错误,请自行查看调试信息!!

开机自启动可参考:/etc/stunnel/README文件所描述的,把stunnel服务器端和客户端的/etc/default/stunnel4文件里面的ENABLED设置成1

参考:1 https://www.digitalocean.com/community/tutorials/how-to-set-up-an-ssl-tunnel-using-stunnel-on-ubuntu
2. https://www.stunnel.org/howto.html
3. http://www.fuweiyi.com/others/2014/05/15/a-Centos-Squid-Stunnel-proxy.html 土啬外地址
4. http://stackoverflow.com/questions/94445/using-openssl-what-does-unable-to-write-random-state-mean

猜你喜欢

转载自govfate.iteye.com/blog/2092581
vps