Total Pageviews

Monday 17 August 2015

安装Dnsmasq + DNSCrypt + SNI Proxy

随着技术的发展,国外的大型网站几乎都支持全站 SSL,所以在这里我们推荐一个神器 SNI Proxy 作为科学上网的跳板。
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 域名都加入到代理列表:
table https_hosts {
    # Google
    (.*.|)googleapis.com$ *
    (.*.|)google.com$ *
    (.*.|)google.com.hk$ *
    (.*.|)google.co.jp$ *
    (.*.|)googlehosted.com$ *
    (.*.|)googleusercontent.com$ *
    (.*.|)ggpht.com$ *
    (.*.|)gstatic.com$ *
    (.*.|)googlemail.com$ *
    (.*.|)googlecode.com$ *
    (.*.|)blogspot.com$ *
    (.*.|)gmail.com$ *
    (.*.|)appspot.com$ *

}

table xmpp_imap_smtp {
    (.*.|)google.com$ *
    (.*.|)googlemail.com$ *
    (.*.|)gmail.com$ *
}
然后重启 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 了,大功告成!
--------------
Dnsmasq's SNI Proxy conf


sni.conf

sni.conf is a configuration of dnsmasq.
If you want to use dnsmasq+sniproxy plan to against the "Girl Friend Wall",you will need  /etc/dnsmasq.d/sni.conf .

gfwlist2dnsmasq-sni-conf.py

I didn't wrote this code,it based on https://github.com/cokebar/gfwlist2dnsmasq !

How to use

gfwlist2dnsmasq-sni-conf.py is a tool aim to convert gfwlist into sni.conf ,just use python gfwlist2dnsmasq-sni-conf.pyto start it.
It will automatically download the latest gfwlist and execute convert.
But ensure set your SNI Proxy Server IP before run the code. Edit gfwlist2dnsmasq-sni-conf.py find like this:
sni = '127.0.0.1'
Change 127.0.0.1 to your SNI Proxy Server IP.
Feel free to download the pre-generated sni.conf if you don't need updated version of gfwlist.
from https://github.com/R0uter/Dnsmasq-sniproxy-conf
--------------------
Just another script to auto-generate dnsmasq ipset rules using gfwlist


gfwlist2dnsmasq

Intro

Just another script to auto-generate dnsmasq ipset rules using gfwlist
Notification: Need python2, do not use python3
GFWList is composed by regular expressions, but dnsmasq rules is formed by domain names. So the conversion from GFWList to dnsmasq rule list is not a equivalent conversion. You might need to add extra rules or modify some converted rules.

Using:

Modify gfwlist2dnsmasq.py:
Change this to your DNS server IP&port:
mydnsip = '127.0.0.1'
mydnsport = '5353'
Change this to your ipset name:
ipsetname = 'gfwlist'
Path to save you rule file:
outfile = './dnsmasq_list.conf'
Add your own extra domain here. One domain in a line. eg:
EX_DOMAIN=[ \
'.google.com', \
'.google.com.hk', \
'.google.com.tw', \
'.google.com.sg', \
'.google.co.jp', \
'.blogspot.com', \
'.blogspot.sg', \
'.blogspot.hk', \
'.blogspot.jp', \
'.gvt1.com', \
'.gvt2.com', \
'.gvt3.com', \
'.1e100.net', \
'.blogspot.tw' \
]
Then run gfwlist2dnsmasq.py:
python gfwlist2dnsmasq.py
If you don't want to generate the rules by yourself, you can download the rule file from:

from https://github.com/cokebar/gfwlist2dnsmasq
------------------------------


SNI Proxy

Proxies incoming HTTP and TLS connections based on the hostname contained in the initial request of the TCP session. This enables HTTPS name-based virtual hosting to separate backend servers without installing the private key on the proxy machine.

News

First user survey, please take a moment to offer your input.

Features

  • Name-based proxying of HTTPS without decrypting traffic. No keys or certificates required.
  • Supports both TLS and HTTP protocols.
  • Supports IPv4, IPv6 and Unix domain sockets for both back end servers and listeners.
  • Supports multiple listening sockets per instance.

Usage

Usage: sniproxy [-c <config>] [-f] [-n <max file descriptor limit>] [-V]
    -c  configuration file, defaults to /etc/sniproxy.conf
    -f  run in foreground, do not drop privileges
    -n  specify file descriptor limit
    -V  print the version of SNIProxy and exit

Installation

For Debian or Fedora based Linux distributions see building packages below.
Prerequisites
  • Autotools (autoconf, automake, gettext and libtool)
  • libev4, libpcre and libudns development headers
  • Perl and cURL for test suite
Install
./autogen.sh && ./configure && make check && sudo make install
Building Debian/Ubuntu package
This is the preferred installation method on recent Debian based distributions:
  1. Install required packages
    sudo apt-get install autotools-dev cdbs debhelper dh-autoreconf dpkg-dev gettext libev-dev libpcre3-dev libudns-dev pkg-config fakeroot devscripts
    
  2. Build a Debian package
    ./autogen.sh && dpkg-buildpackage
    
  3. Install the resulting package
    sudo dpkg -i ../sniproxy_<version>_<arch>.deb
    
Building Fedora/RedHat package
This is the preferred installation method for modern Fedora based distributions.
  1. Install required packages
    sudo yum install autoconf automake curl gettext-devel libev-devel pcre-devel perl pkgconfig rpm-build udns-devel
    
  2. Build a distribution tarball:
    ./autogen.sh && ./configure && make dist
    
  3. Build a RPM package
    rpmbuild --define "_sourcedir `pwd`" -ba redhat/sniproxy.spec
    
  4. Install resulting RPM
    sudo yum install ../sniproxy-<version>.<arch>.rpm
    
I've used Scientific Linux 6 a fair amount, but I prefer Debian based distributions. RPM builds are tested in Travis-CI on Ubuntu, but not natively. This build process may not follow the current Fedora packaging standards, and may not even work.
Building on OS X with Homebrew
  1. install dependencies.
    brew install libev pcre udns autoconf automake gettext libtool
    
  2. Read the warning about gettext and force link it so autogen.sh works. We need the GNU gettext for the macro AC_LIB_HAVE_LINKFLAGS which isn't present in the default OS X package.
    brew link --force gettext
    
  3. Make it so
    ./autogen.sh && ./configure && make
    
OS X support is a best effort, and isn't a primary target platform.

Configuration Syntax

user daemon

pidfile /tmp/sniproxy.pid

error_log {
    syslog daemon
    priority notice
}

listener 127.0.0.1:443 {
    protocol tls
    table TableName

    # Specify a server to use if the initial client request doesn't contain
    # a hostname
    fallback 192.0.2.5:443
}

table TableName {
    # Match exact request hostnames
    example.com 192.0.2.10:4343
    example.net [2001:DB8::1:10]:443
    # Or use regular expression to match
    .*\\.com    [2001:DB8::1:11]:443
    # Combining regular expression and wildcard will resolve the hostname
    # client requested and proxy to it
    .*\\.edu    *:443
}

DNS Resolution

Using hostnames or wildcard entries in the configuration requires sniproxy to be built with UDNS. SNIProxy will still build without UDNS, but these features will be unavailable.
UDNS uses a single UDP socket for all queries, so it is recommended you use a local caching DNS resolver (with a single socket each DNS query is protected by spoofing by a single 16 bit query ID, which makes it relatively easy to spoof).
UDNS is currently not available in Debian stable, but a package can be easily built from the Debian testing or Ubuntu source packages:
mkdir udns_packaging
cd udns_packaging
wget http://archive.ubuntu.com/ubuntu/pool/universe/u/udns/udns_0.4-1.dsc
wget http://archive.ubuntu.com/ubuntu/pool/universe/u/udns/udns_0.4.orig.tar.gz
wget http://archive.ubuntu.com/ubuntu/pool/universe/u/udns/udns_0.4-1.debian.tar.gz
tar xfz udns_0.4.orig.tar.gz
cd udns-0.4/
tar xfz ../udns_0.4-1.debian.tar.gz
dpkg-buildpackage
cd ..
sudo dpkg -i libudns-dev_0.4-1_amd64.deb libudns0_0.4-1_amd64.deb
from https://github.com/dlundquist/sniproxy
---------------------------------------------

SNI Proxy用于科学上网


SNI是一个扩展的TLS计算机联网协议,这允许在握手过程开始时通过客户端告诉它正在连接的服务器的主机名称。 这里介绍SNI Proxy科学上网的应用。
  • 优点:客户端不需要安装任何客户端,也不用做什么帐号认证之类的东西。 自己的SNI Proxy服务器配置好 只要哪个网站需要用到代理只要把域名解析到自己的SNI Proxy上即可进行代理 方便各种手机不能Root之类的直接使用。
  • 缺点:没有认证机制,只要知道IP的人都可以用来作为自己的代理使用,让自己的VPS损失流量在这里就不说具体原理了,直接用就行。
因为中国移动香港已经为大家建立好了SNI服务器 我们就不需要再自己花钱买VPS来建立,我们只需要HOST\路由器的Dnsmasq或者自己搞一个DNS都行,把需要域名执行SNI服务器即可完成.
最简单的使用的时候方法直接加入Hosts就好,指哪打哪,不用导入额外证书(防止中间人攻击)。
我 本人整理了一份SNI替换版Host,常用的都已包括在内,以后会不定时更新(中国移动香港的IP失效的机会应该很小吧 ),链接: pan.baidu.com/s/1i5EtqCl 提取密码:wn6g 解压密码:Firefox
--------------------------
部署 SNI Proxy,加速网页访问 反代 无需证书

运行 sniproxy

直接使用命令sniproxy 即可运行,默认配置文件就是“/etc/sniproxy.conf”它会自动加载,如果你使用了其他路径或者配置文件名,那么你需要使用“-c”选项来指定路径:

端口重定向

那么,一般我们访问网站不喜欢输入端口号或者协议名称,那么默认访问的是80端口怎么办?作为辅助,我们安装一个轻量级的 nginx,让它把所有访问80端口的流量转移到443上边去,使用301重定向即可。
我们编辑 nginx 的配置文件“/etc/nginx/sites-available/default
改为如下内容:

域名解析

sniproxy 搭建成功,但是它是不能被直接访问的,你需要将域名解析过去,这样它才能根据域名来代理你的 ssl 链接,那么你可能就需要在自己的 hosts 上修改解析啦。不过,另外一个更简便的办法是使用 dnsmasq,那么你只需要在这个服务器当中增加dnsmasq 的解析配置即可。
比如说新增一个 /etc/dnsmasq.d/sni.conf ,往里边写入需要解析到你 sniproxy 服务器的域名即可。为此,github上的一个项目,这里加入了常用网站的解析,你只需要将它下载并且替换里边的 IP 为你 sniproxy 服务器的 IP 即可!
记得修改里边的 IP 地址!
这样,你的 dns 就增加了常见认证网站的自动反代功能!
-----------

一个 反代无需证书 适用于DNS/Hosts的小工具 —— SNI Proxy代理 安装教程

那些可以 科学上网的DNS和hosts,都是使用的自建或者公共SNI代理。

原理简单说明

SNI详细的我也不懂,假如你有一台 海外的服务器 IP为: 233.233.233.233 ,上面搭建了 SNI Proxy,并且配置正常并启动。
然后你本地Hosts文件在最后添加一条:
  1. 233.233.233.233 www.google.com
保存Hosts文件并打开浏览器访问 https://www.google.com ,然后你就会发现你可以进入 https://www.google.com 网站了。

原理解析:

  1. Hosts设置 233.233.233.233 www.google.com 后,浏览器访问 https://www.google.com 
  2. =>> 浏览器搜索Hosts文件发现设置的解析IP(233.233.233.233)
  3. =>> 浏览器访问 SNI Proxy(233.233.233.233)
  4. =>> SNI Proxy收到信息然后去访问 https://www.google.com 并获取网站数据,然后把网站数据原封不动的返回给你
  5. =>> 浏览器收到 SNI Proxy返回的 网站数据并显示出来
  6. =>> 你看到了 https://www.google.com 网页
简单的来说,SNI Proxy 会把请求的网站比如 https://www.google.com 获取并原封不动的返回请求者,不需要对证书进行解密和加密,所以不需要配置证书。
SNI Proxy 可以简单的实现这样的 反向代理功能。

安装环境

本教程只适用于 Ubuntu 14.04 + 以上版本的系统。

安装步骤

  1. apt-get install python-software-properties -y
  2. add-apt-repository ppa:dlundquist/sniproxy
第二行代码执行后提示大概如下:
  1. root@ubuntu:~# add-apt-repository ppa:dlundquist/sniproxy
  2.  
  3. More info: https://launchpad.net/~dlundquist/+archive/ubuntu/sniproxy
  4. Press [ENTER] to continue or ctrl-c to cancel adding it
  5.  
  6. gpg: keyring `/tmp/tmpnr3gi1cx/secring.gpg' created
  7. gpg: keyring `/tmp/tmpnr3gi1cx/pubring.gpg' created
  8. gpg: requesting key ED122FA0 from hkp server keyserver.ubuntu.com
  9. gpg: /tmp/tmpnr3gi1cx/trustdb.gpg: trustdb created
  10. gpg: key ED122FA0: public key "Launchpad PPA for Dustin Lundquist" imported
  11. gpg: Total number processed: 1
  12. gpg: imported: 1 (RSA: 1)
  13. OK
继续安装
  1. apt-get update && apt-get install sniproxy -y

配置说明

SNI Proxy的默认配置文件:/etc/sniproxy.conf
我们可以使用 VIM 等编辑器添加修改,也可以直接用 echo写入文件。
VIM操作简单说明(可选):
  1. echo "" > /etc/sniproxy.conf
  2. # 清空配置文件
  3. vi /etc/sniproxy.conf
  4. # 打开配置文件
然后按 I 键 进入编辑模式,复制下面的内容到配置文件中(手动把 echo -e “和 ” > /etc/sniproxy.conf 去掉),然后按 Esc 键 推出编辑模式,然后输入 :wq 保存并退出vi。
下面是 echo 方式直接写入配置文件。

泛反向代理:

泛反向代理,指的是所有请求 SNI Proxy 的域名都会反向代理。
  1. echo -e "user daemon
  2. pidfile /var/run/sniproxy.pid
  3.  
  4. listen 443 {
  5. proto tls
  6. table https_hosts
  7. access_log {
  8. filename /var/log/sniproxy/https_access.log
  9. priority notice
  10. }
  11. }
  12. table https_hosts {
  13. .* *:443
  14. }" > /etc/sniproxy.conf

自定义反向代理:

自定义反向代理,指的是自己指定域名,只有通过这些域名请求 SNI Proxy 的才会反向代理。
比如只设置了 (.*.|)google.com$ * ,那么你只能通过 www.google.com、google.com 和其他以 google.com 为主的二级 三级域名 访问SNI Proxy并请求反向代理。其他没有设置的域名都会忽略。
要反向代理什么域名就在 table https_hosts {} 中添加规则,例如 我要反向代理 任何以 google.com 为主的二级 三级域名 ,那么就写:
  1. (.*.|)google.com$ *
这样只要是以 google.com 为主的域名都会被反向代理,比如 www.google.com news.google.com mail.google.com 都会反向代理。
  1. echo -e "user daemon
  2. pidfile /var/run/sniproxy.pid
  3.  
  4. listen 443 {
  5. proto tls
  6. table https_hosts
  7. access_log {
  8. filename /var/log/sniproxy/https_access.log
  9. priority notice
  10. }
  11. }
  12. table https_hosts {
  13. .* *:443
  14. }
  15.  
  16. table https_hosts {
  17. (.*.|)google.com$ *
  18. (.*.|)google.com.hk$ *
  19. (.*.|)googlemail.com$ *
  20. (.*.|)googlecode.com$ *
  21. (.*.|)blogspot.com$ *
  22. (.*.|)gmail.com$ *
  23. (.*.|)youtube.com$ *
  24. }" > /etc/sniproxy.conf

使用说明

使用命令

  1. service sniproxy start
  2. # 启动SNI Proxy
  3.  
  4. service sniproxy stop
  5. # 停止SNI Proxy
  6.  
  7. service sniproxy restart
  8. # 重启SNI Proxy
  9.  
  10. service sniproxy status
  11. # 查看状态
启动SNI Proxy后,查看一下 网络连接端口监听情况:
  1. netstat -lntp
当出现大概如下所示的信息时,代表正常启动并监听端口443,注意最后的sniproxy。
  1. root@ubuntu:~# netstat -lntp
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp6 0 0 :::443 :::* LISTEN 11673/sniproxy
如果没有发现 sniproxy 的监听端口信息,那么看一下是否有其他的 软件/服务 占用了 443 端口,如果有的话请关闭后再尝试启动 SNI Proxy。
如果没有异常情况,那么我们就可以使用 SNI Proxy 代理了。

Hosts

找到你电脑的Hosts文件,并打开(注意Hosts可能是隐藏文件,需要设置显示出来,还有win10修改Hosts文件需要管理员权限)。
Windows xp / 7 / 10 系统Hosts位置:C:\windows\system32\drivers\etc\hosts
在最后一行添加你要反向代理的网站,比如 www.google.com
  1. 233.233.233.233 www.google.com
其他想要反向代理的域名按这个格式添加(233.233.233.233是你的SNI Proxy服务器IP),当然Hosts设置比较麻烦,而且比如 youtube.com 看视频的话要设置很多 Hosts域名,很麻烦,所以如果只是访问谷歌,那么可以使用这个Hosts方法。

DNS

如果你有一台国内服务器的话,你可以搭建一个DNS服务器,然后把谷歌等域名的解析IP都指向你的SNI Proxy服务器,然后当你使用这个DNS的时候,就会解析到SNI Proxy服务器并反向代理去访问谷歌等网站。
但是搭建DNS服务器,必须有国内服务器,否则会被墙DNS污染,所以没有国内服务器的可以看看下面这几个可以科学上网的DNS。

Xsico DNS

官方网站 – 科学上网
Public Dns:115.159.157.26
Scholar Dns:115.159.158.38

Mogu DNS

官方网站 – 科学上网+广告过滤
北方推荐:122.114.245.45
南方推荐:115.159.96.69

Pure DNS

官方网站 – 科学上网+广告过滤
北方推荐:123.207.137.88
南方推荐:115.159.220.214

GoGo

GoGo新版本也是支持 SNI Proxy 代理的,所以可以添加自己的 SNI Proxy 代理来使用。
GoGo添加SNI Proxy代理很简单,首先进入管理面板-代理列表http://127.0.0.1:9092/proxies
然后点击右上角的 添加SNI代理 按钮,然后出现下图,按下图格式添加,比如你的SNI Proxy代理服务器IP为 233.233.233.233 ,那么就填写 233.233.233.233:443 ,然后点击下面的  确认添加 按钮就行了。

添加完毕后,你可以在 代理列表的筛选中,选择 SNI代理 – 启用 ,然后点击 查看 按钮,筛选出刚才SNI代理。
然后找到我们刚才添加的 SNI代理,并设置 优先(否则可能不会使用这个代理)。
这就算添加完毕了,GoGo客户端使用教程:一个免费且快速的HTTP/SOCKS5代理软件 —— GoGotunnel

dowsDNS

打开配置文件 config.json 找到 "sni_proxy_ip":"219.76.4.3" 参数,把IP改为你自己搭建的 SNI Proxy IP即可.
----------------

Linux 自建DNS 并使用SNI Proxy实现科学上网 —— DNSmasq配置教程

DNSmasq是一款小巧且方便地用于配置DNS服务和DHCP服务器的工具,适用于小型网络,它提供了DNS解析功能和可选择的DHCP功能,并且提供本地缓存查询请求的功能。同时搭配 SNI Proxy代理的话,可以实现只使用DNS就能科学上网!

安装步骤

包管理器 安装

  1. # CentOS 系统:
  2. yum update
  3. yum install -y dnsmasq
  4.  
  5. # Debian/Ubuntu 系统:
  6. apt-get update
  7. apt-get install -y dnsmasq

源码编译

DNSmasq 官方网站:http://www.thekelleys.org.uk/dnsmasq/
编译需要相关组件支持,所以安装编译依赖包:
  1. # CentOS 系统:
  2. yum update
  3. yum -y groupinstall "Development Tools"
  4.  
  5. # Debian/Ubuntu 系统:
  6. apt-get update
  7. apt-get install -y build-essential
获取最新版本:
  1. dnsmasq_ver=$(wget -qO- http://www.thekelleys.org.uk/dnsmasq/ | awk -F'\"dnsmasq-' '/dnsmasq-[2-9].[0-9][0-9].tar.gz\">dnsmasq/{print $2}' | cut -c 1-4 | sort -V | tail -1)
下载解压编译:
  1. wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-${dnsmasq_ver}.tar.gz
  2. tar -xzf dnsmasq-${dnsmasq_ver}.tar.gz
  3. cd dnsmasq-${dnsmasq_ver}
  4. make install
  5. ldconfig
  6. # 编译安装完毕后,可以删除没用的源码文件了。
  7. cd ..
  8. rm -rf dnsmasq-${dnsmasq_ver} && rm -rf dnsmasq-${dnsmasq_ver}.tar.gz

包管理器 安装后,查看下DNSmasq的版本:
  1. dnsmasq -v
  2. # 以下是输出结果,只要不是提示 找不到命令就没事( -bash: dnsmasq: command not found )
  3. Dnsmasq version x.xx Copyright (c) 2000-2012 Simon Kelley
  4. Compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack
  5.  
  6. This software comes with ABSOLUTELY NO WARRANTY.
  7. Dnsmasq is free software, and you are welcome to redistribute it
  8. under the terms of the GNU General Public License, version 2 or 3.

配置文件

Dnsmasq的配置文件为:dnsmasq.conf
包管理器 方式安装的配置文件位置为:/etc/dnsmasq.conf
本教程只介绍配置DNS服务,以下部分只指出DNS服务的相关配置。
首先复制一份配置文件的备份,然后清空原配置文件,并根据下面的参数说明写入配置内容:
  1. cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
  2. echo "" > /etc/dnsmasq.conf
  3. # 打开配置文件并根据下面的参数说明写入配置内容,按 I键 进入编辑模式,改完后按 ESC键 退出并输入 :wq 保存并退出。
  4. vi /etc/dnsmasq.conf

配置文件参数说明:

  1. port=53
  2. # 指定DNSmasq的监听端口,默认为53端口,也可设置为5353端口从而防止53端口DNS污染(但某些设备如win并不支持非53端口)
  3.  
  4. resolv-file=/xxx/xx.conf
  5.  # 指定DNSmasq获取上游DNS服务器地址的文件,不配置此项则默认从 /etc/resolv.conf(linux默认DNS配置文件) 获取
  6.  
  7. strict-order
  8. # 严格按照 resolv-file 参数指定的文件中按从上到下的顺序发送DNS解析请求,直至获取解析应答成功为止
  9.  
  10. listen-address=
  11. # 指定DNSmasq监听的地址,若仅提供为本机使用可设置为 127.0.0.1 ,留空或设置为 0.0.0.0 即任何人都可访问
  12.  
  13. address=/xxx.xx/x.x.x.x
  14. # 自定义某些地址的解析服务器,可以 过滤或者指定 某些网站(支持ipv6,直接写ipv6的地址即可)
  15. # 过滤广告或者某域名,例如配置 address=/www.nanqinlang.com/127.0.0.1
  16. # 把广告域名解析请求发送到错误的解析服务器IP 127.0.0.1 即可屏蔽该域名的访问
  17. # 也可以把 www.google.com 等指向一个国外的SNI代理IP,即可实现DNS科学上网
  18.  
  19. server=208.67.222.222#5353
  20. # 指定上游DNS解析服务器,此处推荐设置为可使用5353端口的opendns
  21.  
  22. bogus-nxdomain=208.67.222.222
  23. # 用于防止DNS污染/劫持,参数和上面 server 项填写相同的 ip ,但不支持非53端口。
  24.  
  25. conf-dir=/etc/dnsmasq.d
  26. # 加载 /etc/dnsmasq.d 文件夹内的配置文件
配置文件示例:
  1. port=53
  2. strict-order
  3. listen-address=127.0.0.1
  4. server=208.67.222.222#53
  5. bogus-nxdomain=208.67.222.222
  6. conf-dir=/etc/dnsmasq.d

自定义地址解析 单独建立一个配置文件:

  1. vi /etc/dnsmasq.d/sniporxy.conf
然后按格式写入自定义地址解析,示例如下:
这里可以配合上面的另一个教程:"一个反代无需证书 适用于DNS/Hosts的小工具 —— SNI Proxy代理 安装教程"
把谷歌等域名的解析IP指定为 SNI Proxy代理的IP,当你使用这个DNS时,就会通过解析为SNI代理 IP,然后通过SNI代理访问谷歌,这就是使用自建DNS实现科学上网(和Hosts方式原理是一样的,不过一个在云服务器上,一个在本地)。
注意:这里都是默认泛域名,比如 /google.com/1.1.1.1 代表 google.com, www.google.com, api.google.com 等域名全部解析为 1.1.1.1
  1. address=/google.com/1.1.1.1
  2. address=/google.com.hk/1.1.1.1
  3. address=/google.co.jp/1.1.1.1
  4. address=/gmail.com/1.1.1.1
  5. address=/youtube.com/1.1.1.1
注意:如果 这个文件或配置文件 修改了,那么需要重启 DNSmasq后才能生效。

GFWList转为DNSmasq的SNI配置文件

大家可能觉得如果用上面的自己创建的 SNI配置文件的话,要写全被墙的网站域名会很麻烦,于是我就找到了这个 python的脚本(由 @犬走つばき 提供脚本项目地址),可以把 gfwlist的PAC转成 DNSmasq的SNI配置文件。
Python脚本项目:https://github.com/R0uter/Dnsmasq-sniproxy-conf
首先下载这个 python脚本:
  1. wget -N --no-check-certificate https://raw.githubusercontent.com/R0uter/Dnsmasq-sniproxy-conf/master/gfwlist2dnsmasq-sni-conf.py
然后替换脚本内默认的SNI代理IP(127.0.0.1)为自己的IP,加入你的SNI代理IP为1.1.1.1,那么这样(你可以用 vi 等编辑器修改第12行的IP):
  1. sed -i 's/127.0.0.1/1.1.1.1/' gfwlist2dnsmasq-sni-conf.py
修改后,可以使用 cat gfwlist2dnsmasq-sni-conf.py 查看一下第12行的IP是否为自己的SNI代理IP,如果正确,那么继续。
先确定你的 Linux系统中安装了 Python :
  1. python -V
  2.  
  3. # 如果返回的是 python的版本信息那就继续下面步骤,如果返回的是 命令找不到(-bash: python: command not found),那么请安装 python:
  4. # CentOS 系统:
  5. yum install python -y
  6. # Debian/Ubuntu 系统:
  7. apt-get install python -y
然后我们运行脚本:
  1. python gfwlist2dnsmasq-sni-conf.py
运行脚本后会输出转换的信息,输出完后,就会在当前文件夹下生成一个 sni.conf 文件,把他移动到 /etc/dnsmasq.d/ 文件夹内即可。
  1. mv sni.conf /etc/dnsmasq.d/sni.conf
然后重启 DNSmasq就会生效。
  1. service dnsmasq restart
需要注意的是,因为 GFWList是一直更新的,所以你的SNI配置文件也要定时更新,步骤一样,运行脚本就会自动获取最新的GFWList文件并转换成DNSmasq的SNI配置文件。

使用说明

启用DNSmasq服务
  1. service dnsmasq start
停止DNSmasq服务
  1. service dnsmasq stop
重启DNSmasq服务(当修改配置文件后,需要重启)
  1. service dnsmasq restart

卸载 DNSmasq

如果 /etc/dnsmasq.d 目录有文件,那么需要先删除这个目录才能正常卸载:
  1. # 先删除这个目录
  2. rm -rf /etc/dnsmasq.d
  3.  
  4. # CentOS 系统
  5. yum remove dnsmasq -y
  6.  
  7. # Debian/Ubuntu 系统:
  8. apt-get remove --purge dnsmasq -y

其他说明

这个可能是防火墙的问题,因为DNS是UDP传输的,所以只开放UDP端口即可。
  1. iptables -I INPUT -m state --state NEW -m udp -p udp --dport 端口 -j ACCEPT
  2.  
  3. # 删除防火墙规则,内容一样把 -I 换成 -D 就行了:
  4. iptables -D INPUT -m state --state NEW -m udp -p udp --dport 端口 -j ACCEPT

在PC/路由器/移动设备中设置DNS为你的VPS IP,端口设置53(如有) 即可.

------------

Debian系统下,编译安装 SNI Proxy反向代理(可用于Hosts/DNS服务器)

系统要求

本教程只适用于 Debian7 / 8 系统,其他系统都不适用。
安装SNI Proxy依赖需要 最少150MB大小的空间,所以请确保你的系统硬盘空闲空间有 300MB。

简单说明

SNI Proxy 简单的来说是一个 反向代理软件,可以把你的请求原封不动的发送给指定网站,然后再把指定网站返回的数据原封不动的返回给你,目前网上可以 科学上网的Hosts和DNS,都是通过把 谷歌等域名指向 SNI Proxy代理的IP来实现“直连”访问谷歌的,所以在某种程度上,SNI Proxy承担一个流量数据搬运工的作用。
因为 Hosts和DNS科学上网,都必须使用 https 来访问,所以过程都是加密的,而SNI Proxy只会原封不动的把流量转发过去,所以不会去解密(也不能),也就不需要担心安全问题了。
SNI Proxy Github项目:https://github.com/dlundquist/sniproxy

安装步骤

安装依赖

首先为了确保依赖安装正常、完整,我们需要更换系统 软件包源为最新的稳定源 jessie (本步骤必做,否则很容易出错)。
默认下面的代码是 美国的镜像源,可以更换下面代码 us.sources.list 中的 us ,具体可以看这里 逗比云
  1. rm -rf /etc/apt/sources.list && wget -N --no-check-certificate -O "/etc/apt/sources.list" "https://softs.pw/Bash/sources/us.sources.list"
然后我们更新软件包列表,并开始安装依赖
  1. apt-get update
  2. # 安装依赖会很大,可能会有 100-300MB大小,所以一开始的镜像源一定要选近的,否则会很慢。
  3. apt-get install autotools-dev cdbs debhelper dh-autoreconf dpkg-dev gettext libev-dev libpcre3-dev libudns-dev pkg-config fakeroot devscripts unzip

安装SNI Proxy

  1. mkdir sniproxy && cd sniproxy
  2. # 新建一个 SNI Proxy文件夹并进入
  3.  
  4. wget -N --no-check-certificate https://github.com/dlundquist/sniproxy/archive/master.zip
  5. # 下载 SNI Proxy 最新软件包
  6.  
  7. unzip master.zip && cd sniproxy-master
  8. # 解压 SNI Proxy 软件包并进入解压后的文件夹
  9.  
  10. ./autogen.sh && dpkg-buildpackage
  11. # 开始构建 deb 包
  12.  
  13. sniproxy_deb=$(ls ..|grep "sniproxy_.*.deb") && echo ${sniproxy_deb}
  14. # 获取构建后的 deb 包的文件名,正常情况下会返回一个类似于 sniproxy_0.X.X_amd64.deb 这样的文件名
  15.  
  16. [[ ! -z ${sniproxy_deb} ]] && dpkg -i ../${sniproxy_deb}
  17. # 判断获取的文件名是否为空,如果不为空就 安装 deb 包。
最后安装完毕,我们可以用 sniproxy -V 来查看是否安装成功,正常情况下会返回版本号。
安装完毕之后,还需要配置一下配置文件,才能使用,具体看这个教程:”一个 反代无需证书 适用于DNS/Hosts的小工具 —— SNI Proxy代理 安装教程“

DNS服务搭建教程:“Linux 自建DNS 并使用SNI Proxy实现科学上网 —— DNSmasq配置教程”

使用说明

启动:service sniproxy start
停止:service sniproxy stop
重启:service sniproxy restart
查看状态:service sniproxy status
配置文件:/etc/sniproxy.conf
通过 dpkg -S sniproxy 命令可以查询 SNI Proxy 的所有安装文件。

添加开机启动

  1. update-rc.d -f sniproxy defaults

取消开机启动

  1. update-rc.d -f sniproxy remove

卸载SNI Proxy

  1. apt-get remove --purge sniproxy
其他系统的SNI Proxy教程,我会看情况写的。