当前位置: > Linux服务器 > 服务器设置 >

HTTP/HTTPS自动加密上网方案

时间:2015-06-22 17:28来源:linux.it.net.cn 作者:IT

这里主要介绍电脑无需任何设置,就能够自动加密代理特定网站的HTTP/HTTPS协议。

 

方案介绍

 

涉及到的软件

  • BIND: 一个流行的域名解析服务器,我们可以设置哪些域名需要走加密线路。
  • Stunnel: 使用TLS对tcp协议进行加密,也就是对tcp建立一条加密线路。
  • SNI Proxy: 代理软件。对于HTTP协议,它可以根据Host请求头解析得出目标站IP;对于HTTPS协议,它可以根据SNI扩展中的域名解析得出目标站IP。

 

此方案优缺点

优点:
无需手动设置任何代理,就能够自动加密代理特定网站的HTTP或HTTPS协议
相对于我们常用的ssh隧道,ssh隧道是单路,而此方案是支持多并发连接,可以极大加速网站访问。

缺点:
对于代理HTTPS协议,需要发起HTTPS连接的客户端,比如浏览器支持TLS的SNI扩展。好消息是目前浏览器几乎都支持此扩展,但对于一些非浏览器的客户端,不支持SNI扩展。我们只能设置正向代理来解决此问题。

 

方案原理

流程图:
加密反向代理

原理介绍:

  • 1、首先我们需要准备三台服务器,一台是内网DNS服务器(安装bind),一台是内网代理服务器(安装stunnel),另一台国外服务器(安装stunnel,sniproxy)。
  • 2、我们还需要设置DNS为内网的DNS,并在内网bind dns设置谷歌域名解析的IP为内网代理服务器
  • 3、当我们访问谷歌网站时,首先会向内网DNS服务器发送DNS A记录查询,此时内网DNS服务器会返回内网代理服务器的IP。
  • 4、浏览器得到谷歌域名的解析IP后(即内网代理服务器的IP),会向内网代理服务器发送HTTP或HTTPS请求。
  • 5、此时内网代理服务器(即stunnel),会接收到请求,经过加密,把请求转发到国外服务器(stunnel)的指定端口上。
  • 6、国外服务器(stunnel)接收到来自国内服务器(stunnel)的加密数据后,经过解密,把请求转发到sniproxy。
  • 7、sniproxy再根据HTTP Host请求头或者HTTPS sni扩展的域名解析出谷歌服务器的IP,并把请求转发给谷歌服务器。
  • 8、谷歌服务器收到来自sniproxy发送的请求后,马上返回网页内容给sniproxy,sniproxy再原路返回数据给浏览器。

 

方案实施

由于时间有限,我们仅在Ubuntu server 12.04演示安装。

环境介绍

  • 系统:Ubuntu server 12.04
  • 内网DNS IP: 10.96.153.201(主),10.96.153.204(从)
  • 内网代理服务器: 10.96.153.204
  • 国外服务器IP: 1.2.3.4

安装BIND9

1、在主DNS和从DNS安装bind,即10.96.153.201(主),10.96.153.204(从)。

  1. wget http://www.isc.org/downloads/file/bind-9-10-0b1-2/?version=tar.gz -O bind-9-10-0b1-2.tar.gz
  2. tar xzf bind-9-10-0b1-2.tar.gz
  3. cd bind-9-10-0b1-2
  4. ./configure --prefix=/usr/local/bind
  5. make && make install


2、配置主DNS服务器(10.96.153.201)
2.1、生成/usr/local/bind/etc/rndc.key密钥文件

  1. /usr/local/bind/sbin/rndc-confgen -a -k rndckey -c /usr/local/bind/etc/rndc.key

2.2、编辑/usr/local/bind/etc/named.conf,写入如何内容:

  1. include "/usr/local/bind/etc/rndc.key";
  2. controls { inet 127.0.0.1 port 953 allow { 127.0.0.1; } keys { "rndckey"; }; };
  3. logging {
  4. channel default_syslog { syslog local2; severity notice; };
  5. channel audit_log { file "/var/log/bind.log"; severity notice; print-time yes; };
  6. category default { default_syslog; };
  7. category general { default_syslog; };
  8. category security { audit_log; default_syslog; };
  9. category config { default_syslog; };
  10. category resolver { audit_log; };
  11. category xfer-in { audit_log; };
  12. category xfer-out { audit_log; };
  13. category notify { audit_log; };
  14. category client { audit_log; };
  15. category network { audit_log; };
  16. category update { audit_log; };
  17. category queries { audit_log; };
  18. category lame-servers { audit_log; };
  19. };
  20. options {
  21.     directory "/usr/local/bind/etc";
  22. pid-file "/usr/local/bind/var/run/bind.pid";
  23. transfer-format many-answers;
  24. interface-interval 0;
  25. forward only;
  26. forwarders { 202.96.128.166;202.96.134.133; };
  27. allow-query {any;};
  28. };
  29. zone "google.com" {
  30. type master;
  31. file "google.com.zone";
  32. allow-transfer { 10.96.153.204; };
  33. };

在这个named.conf文件中,我们只需要关心如下内容:
对于options{}区域,202.96.128.166和202.96.134.133这两个是ISP提供的本地DNS,需要修改为自己所在ISP的本地DNS。
对于zone "google.com"{}区域,这里定义了google.com域名的区域文件google.com.zone,还有允许10.96.153.204(即从DNS)同步区域文件。
2.3、建立google.com.zone区域文件

  1. $TTL 3600
  2. @ IN SOA ns1.google.com. hostmaster.google.com. (
  3. 2014072015  ; Serial
  4. 3600 ; Refresh
  5. 900 ; Retry
  6. 3600000 ; Expire
  7. 3600 ) ; Minimum
  8. @ IN NS ns1.google.com.
  9. @ IN NS ns2.google.com.
  10. ns1 IN A 10.96.153.201
  11. ns2 IN A 10.96.153.204
  12. @ IN A 10.96.153.204
  13. * IN A 10.96.153.204

对于这个区域文件,
ns1 IN A 10.96.153.201 指向第一个dns服务器,即主DNS。
ns2 IN A 10.96.153.204 指向第二个dns服务器,即从DNS。
@ IN A 10.96.153.204和* IN A 10.96.153.204指向内网的代理服务器(stunnel)。我们只需要修改这三个地方就好了。

3、配置从DNS服务器(10.96.153.204)
编辑named.conf,写入如下内容

  1. logging {
  2. channel default_syslog { syslog local2; severity notice; };
  3. channel audit_log { file "/var/log/bind.log"; severity notice; print-time yes; };
  4. category default { default_syslog; };
  5. category general { default_syslog; };
  6. category security { audit_log; default_syslog; };
  7. category config { default_syslog; };
  8. category resolver { audit_log; };
  9. category xfer-in { audit_log; };
  10. category xfer-out { audit_log; };
  11. category notify { audit_log; };
  12. category client { audit_log; };
  13. category network { audit_log; };
  14. category update { audit_log; };
  15. category queries { audit_log; };
  16. category lame-servers { audit_log; };
  17. };
  18. options {
  19.     directory "/usr/local/bind/etc";
  20. pid-file "/usr/local/bind/var/run/bind.pid";
  21. transfer-format many-answers;
  22. interface-interval 0;
  23. forward only;
  24. forwarders { 202.96.128.166;202.96.134.133; };
  25. allow-query {any;};
  26. };
  27.  
  28. zone "google.com" {
  29. type slave;
  30. file "google.com.zone";
  31. masters { 10.96.153.201; };
  32. };

配置从DNS就简单得多,只需要写入如上内容到named.conf文件。同样的,
options{}中202.96.128.166和202.96.134.133这两个是当地ISP本地dns。
zone "google.com"{}中10.96.153.201指明主DNS服务器IP。
4、启动bind dns服务器

  1. /usr/local/bind/sbin/named

安装Stunnel

1、在内网代理服务器和国外主机安装stunnel

  1. apt-get install stunnel4

2、内网代理服务器stunnel配置
编辑/etc/default/stunnel4,设置ENABLED=1。
编辑/etc/stunnel/stunnel.conf,内容如下:

  1. client = yes
  2. pid = /etc/stunnel/stunnel.pid
  3. [http]
  4. accept = 80
  5. connect = 1.2.3.4:8082
  6.  
  7. [https]
  8. accept = 443
  9. connect = 1.2.3.4:4433

此配置文件表示,监听了80端口,并把此端口流量转发到1.2.3.4:8082,监听了443端口,并把此端口流量转发到1.2.3.4:4433
3、国外服务器stunnel配置
3.1、生成ssl证书stunnel.pem文件

  1. openssl genrsa -out key.pem 2048
  2. openssl req -new -x509 -key key.pem -out cert.pem -days 1095
  3. cat key.pem cert.pem >> /etc/stunnel/stunnel.pem

3.2、编辑/etc/stunnel/stunnel.conf文件

  1. client = no
  2. [http]
  3. accept = 1.2.3.4:8082
  4. connect = 127.0.0.1:8082
  5. cert = /etc/stunnel/stunnel.pem
  6.  
  7. [https]
  8. accept = 1.2.3.4:4433
  9. connect = 127.0.0.1:4433
  10. cert = /etc/stunnel/stunnel.pem

此配置文件表示,监听了1.2.3.4:8082,并转发此地址流量到127.0.0.1:8082,监听了1.2.3.4:4433,并转发给地址流量到127.0.0.1:4433。
3.3、编辑/etc/default/stunnel4,设置ENABLED=1。
4、启动stunnel

  1. service stunnel4 start

安装sniproxy

sniproxy项目地址:https://github.com/dlundquist/sniproxy
1、安装sniproxy
同样只演示在ubuntu server 12.04安装。
1.1、安装UDNS

  1. mkdir udns_packaging
  2. cd udns_packaging
  3. wget http://archive.ubuntu.com/ubuntu/pool/universe/u/udns/udns_0.4-1.dsc
  4. wget http://archive.ubuntu.com/ubuntu/pool/universe/u/udns/udns_0.4.orig.tar.gz
  5. wget http://archive.ubuntu.com/ubuntu/pool/universe/u/udns/udns_0.4-1.debian.tar.gz
  6. tar xfz udns_0.4.orig.tar.gz
  7. cd udns-0.4/
  8. tar xfz ../udns_0.4-1.debian.tar.gz
  9. dpkg-buildpackage
  10. cd ..
  11. dpkg -i *.deb

1.2、安装sniproxy

  1. apt-get install autotools-dev cdbs debhelper dh-autoreconf dpkg-dev gettext libev-dev libpcre3-dev libudns-dev pkg-config
  2. wget https://github.com/dlundquist/sniproxy/archive/master.zip
  3. unzip master.zip
  4. cd sniproxy-master/
  5. dpkg-buildpackage
  6. cd ..
  7. dpkg -i *.deb

2、配置sniproxy
/etc/sniproxy.conf内容如下:

  1. user daemon
  2. pidfile /var/run/sniproxy.pid
  3. error_log {
  4.     syslog deamon
  5.     priority notice
  6. }
  7. listen 127.0.0.1:8082 {
  8.     proto http
  9.     table http_hosts
  10. }
  11. table http_hosts {
  12.         .*      *:80
  13. }
  14.  
  15. listen 127.0.0.1:4433 {
  16.     proto tls
  17.     table https_hosts
  18. }
  19. table https_hosts {
  20. .* *:443
  21. }

此配置文件表示,监听了127.0.0.1:8082地址,并解析http协议中的Host请求头为IP,然后转发请求到此IP;监听了127.0.0.1:4433地址,并解析TLS中SNI扩展中的域名为IP,并转发请求到此IP。
3、启动sniproxy

  1. sniproxy

 

结束

到目前为止,我们已经搭建完成了整套HTTP/HTTPS加密代理方案。方案中的HTTP明文协议,利用stunnel使用了TLS加密,变成了HTTPS协议,使得数据包无法被解析出明文。方案中的HTTPS协议,本身是加密的,但为了防止SNI扩展的中域名被嗅探,还是走了stunnel的加密通道。对于发送HTTPS请求而不支持SNI扩展的客户端,需要手动设置下代理。下一篇博文我们来介绍加密的正向代理方案。



(责任编辑:IT)
------分隔线----------------------------