SNI Proxy 可以直接代理 https 网站,并且 SSL 证书也是可信的,这极大的方便了用户直接浏览支持全站 SSL 的网站。所需要的准备工作,一台国外的 VPS / 云主机 / 服务器,生产环境系统为 Ubuntu 12.04 64 bit
一、安装 SNI Proxy
首先导入 PPA,然后直接 apt-get 安装即可
apt-get install python-software-properties add-apt-repository ppa:dlundquist/sniproxy apt-get update && apt-get install sniproxy
接着修改 /etc/sniproxy.conf
比如我这边访问 Google 有点慢,所以我需要把所有的 Google 域名都加入到代理列表:
我的sniproxy.conf配置文件内容如下:
# sniproxy example configuration file
# lines that start with # are comments
# lines with only white space are ignored
user daemon
# PID file
pidfile /var/run/sniproxy.pid
resolver {
nameserver 8.8.8.8
}
error_log {
# Log to the daemon syslog facility
syslog daemon
# Alternatively we could log to file
#filename /var/log/sniproxy/sniproxy.log
# Control the verbosity of the log
priority notice
}
# blocks are delimited with {...}
#listen 80 {
# proto http
# table http_hosts
# Fallback backend server to use if we can not parse the client request
# fallback localhost:8080
# access_log {
# filename /var/log/sniproxy/http_access.log
# priority notice
# }
#}
listen 127.0.0.1:443 {
proto tls
table https_hosts
access_log {
filename /var/log/sniproxy/https_access.log
priority notice
}
}
# named tables are defined with the table directive
table xmpp_imap_smtp {
(.*.|)google.com$ *
(.*.|)googlemail.com$ *
(.*.|)gmail.com$ *
}
# named tables are defined with the table directive
table https_hosts {
# When proxying to local sockets you should use different tables since the
# local socket server most likely will not autodetect which protocol is
# being used
# WordPress
(.*\.|)wp\.com$ *
(.*\.|)w\.org$ *
(.*\.|)wordpress\.com$ *
(.*\.|)gravatar\.com$ *
# Wikipedia
(.*\.|)wikipedia\.org$ *
# Twitter
(.*\.|)twimg\.com$ *
(.*\.|)tinypic\.com$ *
(.*\.|)twitpic\.com$ *
(.*\.|)twitter\.com$ *
(.*\.|)tweetdeck\.com$ *
(.*\.|)t\.co$ *
# Facebook
(.*\.|)facebook\.com$ *
(.*\.|)fbstatic\.com$ *
(.*\.|)fbcdn\.net$ *
# Flickr
(.*\.|)flickr\.com$ *
(.*\.|)staticflickr\.com$ *
# bit.ly
bitly\.com$ *
bit\.ly$ *
# Google
(.*\.|)googleapis\.com$ *
(.*\.|)google\.com$ *
(.*\.|)google\.co\.jp$ *
(.*\.|)google\.com\.hk$ *
(.*\.|)google\.com\.tw$ *
(.*\.|)google\.fr$ *
(.*\.|)youtube\.com$ *
(.*\.|)ytimg\.com$ *
(.*\.|)googlevideo\.com$ *
(.*\.|)googlehosted\.com$ *
(.*\.|)googleusercontent\.com$ *
(.*\.|)ggpht\.com$ *
(.*\.|)gstatic\.com$ *
(.*\.|)googlemail\.com$ *
(.*\.|)googlecode\.com$ *
(.*\.|)googledrive\.com$ *
(.*\.|)blogspot\.com$ *
(.*\.|)appspot\.com$ *
(.*\.|)gmail\.com$ *
(.*\.|)googlezip\.net$ *
(.*\.|)googlesource\.com$ *
(.*\.|)g\.cn$ *
(.*\.|)google\.cn$ *
# ingress
(.*\.|)panoramio.com$ *
# Imgur
(.*\.|)imgur\.com$ *
# Amazon AWS
(.*\.|)amazonaws.com *
# CDN
github\.global\.ssl\.fastly\.net *
cdn\.sstatic\.net *
example.org unix:/var/run/server.sock
}
# if no table specified the default 'default' table is defined
然后重启 SNI Proxy 即可生效
killall sniproxy && sniproxy -c /etc/sniproxy.conf
接着你可以在本地修改 hosts ,比如 VPS 的 IP 是 192.0.2.2 试试是否可以流畅地使用 https 访问 Gmail 了:
192.0.2.2 www.google.com 192.0.2.2 accounts.google.com 192.0.2.2 mail.google.com
但是直接访问 http 的时候会出错,所以我们可以装一个 Nginx 实现 http 跳转到 https
首先,安装 Nginx:
apt-get install nginx
然后修改默认的配置文件,修改 /etc/nginx/sites-available/default
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
# Make site accessible from http://localhost/
server_name _;
location / {
rewrite ^ https://$host$request_uri permanent;
}
}
然后启动 Nginx 即可
service nginx start
这样你本地访问 http://www.google.com/ ,就会自动跳转到 https://www.google.com/
二、安装 Dnsmasq + DNSCrypt
但是修改 hosts 这一步骤毕竟麻烦,而且在手机上修改更麻烦并且不安全,我们可以搭建一台 Dnsmasq + DNSCrypt 的 DNS 服务器,直接解析 Google 的所有域名到你 VPS 的 IP。假设你人在国内,那么你就需要一台国内的 VPS / 云主机 / 服务器,并且最好和你所使用的 ISP 一个线路,比如都在电信机房。或者你整天开着电脑,直接在虚拟机里装也行,某些支持 OpenWRT 的路由器也默认集成了 Dnsmasq。
增加 PPA 并使用 apt-get 安装 Dnsmasq 和 DNSCrypt
apt-get install python-software-properties add-apt-repository ppa:shnatsel/dnscrypt apt-get update && apt-get install dnsmasq && apt-get install dnscrypt-proxy
修改 DNSCrypt 配置文件 /etc/default/dnscrypt-proxy local-address 的端口改成 5301
默认的 DNSCrypt 服务器使用 OpenDNS 默认的即可, 毕竟这货是他们发明的,嘿嘿
local-address=127.0.0.1:5301
重启 DNSCrypt
service dnscrypt-proxy restart
增加猫猫同学配置的国内网站 Dnsmasq 配置文件,这是为了保证国内的网站域名不需要通过国外的 DNS 服务器进行解析
apt-get install git git clone https://github.com/felixonmars/dnsmasq-china-list.git cd dnsmasq-china-list ln -s /root/dnsmasq-china-list/accelerated-domains.china.conf /etc/dnsmasq.d/ ln -s /root/dnsmasq-china-list/bogus-nxdomain.china.conf /etc/dnsmasq.d/
小提醒,使用苹果硬件设备的同学,可以用 V2EX 提供的 DNS 对 Apple.com 进行加速,直接增加一个配置文件即可,比如新建一个 /etc/dnsmasq.d/apple.conf 增加
server=/.apple.com/199.91.73.222
然后新建一个配置文件,让 google.com 用上刚才搭建的 SNI Proxy,比如新建个 /etc/dnsmasq.d/sni.conf
address=/google.com/192.0.2.2 address=/google.com.hk/192.0.2.2 address=/google.co.jp/192.0.2.2 address=/gmail.com/192.0.2.2 address=/googleusercontent.com/192.0.2.2 address=/gstatic.com/192.0.2.2 address=/googleapis.com/192.0.2.2 address=/googlehosted.com/192.0.2.2 address=/ggpht.com/192.0.2.2 address=/googlecode.com/192.0.2.2 address=/appspot.com/192.0.2.2 address=/android.clients.google.com/192.0.2.2
再修改一下默认的 Dnsmasq 配置文件 /etc/dnsmasq.conf
# 不读取 /etc/resolv.conf ,取消注释即可 no-resolv no-poll # 添加上游服务器为 DNSCrypt,如果还有其他的 server= 记得取消注释。 server=127.0.0.1#5301 # 在所有网卡上关闭 DHCP,用不着这个功能。如果有多个网卡那么一行一个。 no-dhcp-interface=eth0 no-dhcp-interface=eth1 # 添加自定义 hosts 文件 addn-hosts=/etc/dns/hosts
如果需要单独对某个域名进行 hosts 修改,可以直接放在 /etc/dns/hosts 里
然后重启 Dnsmasq
service dnsmasq restart
然后在 VPS 里用 nslookup google.com 测试一下,看看是否解析到了你国外的 VPS 上。
测试没问题以后,接着就是修改你本地电脑或者路由器里的 DNS 了,大功告成!
