2016年7月19日星期二

在Amazon EC2 AMI上搭建stunnel用以加密通信

stunnel可以在你本地与服务器之间搭建一条加密的https隧道,数据通过这条加密隧道进行传输,使得公司防火墙或者国家防火墙不能对你的数据进行查看,配合服务器端及客户端的代理软件,可以实现越墙和保护数据的目的。缺点也很明显,外面的服务器需要完全掌控,一般是购买vps解决,需要money,安装需要一点点时间。访问速度会变慢,毕竟所有的请求都通过这个隧道进行,也有中间这个加解密的过程,但可以通过代理服务器的缓存一定程度上可以解决这个问题。

这里例子选择的amazon ec2 ami服务器(vps),你也可以用其他的比如Debian/Ubuntu。
服务器端的代理用的是squid,你也可以尝试tinyproxy。
客户端我用的是windows,代理软件建议用chrome+SwithcyOmega,你也可以直接配置系统代理为本地监听的地址。或者你使用的系统是Debian/Ubuntu,需要对下面的步骤做一些改变。

  • 安装squid,搭建本地代理
  • 安装stunnel服务端
  • 安装stunnel客户端
  • 扩展阅读
安装squid

  • $sudo -s
  • #yum -y install squid
  • #vi /etc/squid/squid.conf
增加配置:
dns_nameservers 8.8.8.8
修改配置:
http_port 127.0.0.1:3128

其他保持不变,然后启动squid
  • #service squid start
这样,就在服务器上启动了一个只能本地访问的squid代理。

安装stunnel服务端

  • $sudo -s 
  • #yum -y install stunnel
但是/etc/stunnel下并没有缺省的stunnel.conf,想获取缺省的conf按如下操作,我觉得没有太大必要。
  • #repoquery --list stunnel //ToDo用此命令可得到stunnel软件包的文件列表
/etc/stunnel
/usr/bin/stunnel
/usr/lib64/stunnel
/usr/lib64/stunnel/libstunnel.so
/usr/share/doc/stunnel-4.56
/usr/share/doc/stunnel-4.56/AUTHORS
/usr/share/doc/stunnel-4.56/BUGS
/usr/share/doc/stunnel-4.56/COPYING
/usr/share/doc/stunnel-4.56/COPYRIGHT.GPL
/usr/share/doc/stunnel-4.56/CREDITS
/usr/share/doc/stunnel-4.56/Certificate-Creation
/usr/share/doc/stunnel-4.56/ChangeLog
/usr/share/doc/stunnel-4.56/PORTS
/usr/share/doc/stunnel-4.56/README
/usr/share/doc/stunnel-4.56/TODO
/usr/share/doc/stunnel-4.56/VNC_StunnelHOWTO.html
/usr/share/doc/stunnel-4.56/faq.stunnel-2.html
/usr/share/doc/stunnel-4.56/pop3-redirect.xinetd
/usr/share/doc/stunnel-4.56/sfinger.xinetd
/usr/share/doc/stunnel-4.56/stunnel-pop3s-client.conf
/usr/share/doc/stunnel-4.56/stunnel-sfinger.conf
/usr/share/doc/stunnel-4.56/stunnel.conf-sample
/usr/share/doc/stunnel-4.56/tworzenie_certyfikatow.html
/usr/share/man/fr/man8/stunnel.8.gz
/usr/share/man/man8/stunnel.8.gz
/usr/share/man/pl/man8/stunnel.8.gz

上面的/usr/share/doc/stunnel-4.56/stunnel.conf-sample即为stunnel.conf的样例。
需要的话可以执行拷贝,拷贝到配置文件目录

  • #cp /usr/share/doc/stunnel-4.56/stunnel.conf-sample /etc/stunnel/stunnel.conf

然后先生成一个证书:

  • #cd /etc/stunnel
  • #openssl req -new -x509 -days 365 -nodes -out stunnel.pem -keyout stunnel.pem

公钥和私钥都放在了同一个文件stunnel.pem里面,关于这个命令的详细解释见扩展阅读。
接下来要更改(或者创建)配置文件

  • #vi /etc/stunnel/stunnel.conf
[https]
client = no
accept = 7071
connect = 127.0.0.1:3128
cert = /etc/stunnel/stunnel.pem
key = /etc/stunnel/stunnel.pem

其他内容都可以注释掉,7071这个端口可以修改为你自定义的端口,127.0.0.1:3128就是本地squid的地址和端口号。
然后启动stunnel:
#stunnel //stunnel的启动方式也比较直接,启停方式见扩展阅读。

安装stunnel客户端
这里以windows安装为例,到官方网站(https://www.stunnel.org)下载windows安装包并安装,安装步骤就略过了。
修改配置文件C:\Program Files\stunnel\config\stunnel.conf

[https]
client = yes
accept  = 7071
connect = 你的IP:7071
cert = stunnel.pem
key  = stunnel.pem

client = yes表示这是客户端,accept是你本地监听的端口,connect是远程vps的ip及端口。
当前config目录需要放置刚刚生成的证书文件stunnel.pem,下载方式可以看以下文章任选一种:
http://hellowangyue.blogspot.com/2016/07/amazon-ec2-lrzsz-rz-sz.html (推荐)
http://hellowangyue.blogspot.sg/2016/07/winscpsftpamazon-ec2.html

然后双击启动stunnel.exe就建立好了加密通道,然后通过代理软件连接127.0.0.1:7071就可以愉快地进行加密通信了(有翻墙功效哦),配合SwitchyOmega自动切换,简直不要太爽。

扩展阅读

  • 如何启停stunnel
stunnel启停比较特别,没有启停脚本也不默认加入service,所以启停要暴力一点:
 #killall stunnel //停止服务
 #stunnel  //启动服务
不过想要通过service指令重启也还是有办法的,到http://www.gaztronics.net/scripts/stunnel.php这里下载一个脚本:

 $ sudo -s
 # cd /etc/init.d
 $ wget http://www.gaztronics.net/scripts/stunnel.txt
 # mv stunnel.txt stunnel
 # chmod 755 stunnel
 # chown root:root stunnel

然后就可以通过以下指令控制了:

service stunnel start
service stunnel stop
service stunnel status
service stunnel restart
service stunnel condrestart

  • 关于openssl req -new -x509 -days 365 -nodes -out stunnel.pem -keyout stunnel.pem命令的详细解释,我抄下来的:)
This creates a private key, and self-signed certificate. The arguments mean:

-days 365
make this key valid for 1 year, after which it is not to be used any more
-new
Generate a new key
-x509
Generate an X509 certificate (self sign)
-nodes
Do not put a password on this key.
-out stunnel.pem
where to put the SSL certificate
-keyout stunnel.pem
put the key in this file
This command will ask you the following questions:

Question Example Answers
Country name PL, UK, US, CA
State or Province name Illinois, Ontario
Locality Chicago, Toronto
Organization Name Bill's Meats, Acme Anvils
Organizational Unit Name Ecommerce Division
Common Name (FQDN) www.example.com

Important Note: The Common Name (FQDN) should be the hostname of the machine running stunnel. If you can access the machine by more than one hostname some SSL clients will warn you that the certificate is being used on the wrong host, so it is best to have this match the hostname users will be accessing.
直接使用chrome代理(需要导入证书)的时候如果Comon Name填的不对会引起证书验证错误,但不建议跳过stunnel客户端直接用chrome。


没有评论: