神代綺凛

[ChinaDNS] 无污染的智能路由 DNS 折腾记 #3
没想到这个系列还能有第三集,应该是完结篇了。这次是因为前不久试用了一下 Linux 的 Windows 子系统,发...
扫描右侧二维码阅读全文
13
2018/06

[ChinaDNS] 无污染的智能路由 DNS 折腾记 #3

没想到这个系列还能有第三集,应该是完结篇了。这次是因为前不久试用了一下 Linux 的 Windows 子系统,发现意外的不错,与 Windows 本身契合度很高,于是在 Windows 里跑 DNSMasq + ChinaDNS 的设想得以实现√

Head Pic: 「夏コミグッズ…」/「三村ざじゃ」のイラスト [pixiv]

Windows 下的本机无污染 DNS 搭建方案

上一篇文章中,我讲到了在国内服务器上使用 DNSMasq + ChinaDNS 以能在任何支持自定义 DNS 的设备上解决 DNS 污染的方案,但这个方案也有缺点:如果平时你主要是自用,在 Windows 上做到无污染即可,那么为此专门去购买国内服务器(即使是很便宜的学生机)其实也有点大材小用。

不久前不小心才想起来还有一个 Windows 子系统的玩意没试过,说是可以在 Windows 上直接跑 Linux,这样是不是就可以解决没办法在 Windows 上跑 DNSMasq 的问题了?(当然能解决啦

Win10 必须是 1607 版本及以后的

这种解决方案还有一个好处,就是可以解决 IPv6 协议的 DNS 设置问题。
当你在 IPv6 下想用其他只支持 IPv4 的服务器做 DNS,IPv6 中的 DNS 地址就只能填写成 IPv4 镜像地址::ffff:1.2.3.4的形式使其立即 fallback 到 IPv4 去解析,虽然日常使用上不会感觉有什么大问题,但是在使用例如nslookup之类的工具时就会发生异常。
而在子系统 Linux 中使用 DNSMasq 可以同时监听 IPv6,因此就可以很好的解决这个问题。

开启 Windows 子系统

1. 开启子系统功能

  1. Win10 系统设置->更新与安全->开发者选项->勾选“开发人员模式”,然后会有文字提示你“正在搜索开发者软件包”之类的,无需理会
  2. 控制面板->程序和功能->启用或关闭 Windows 功能->勾选“适用于 Linux 的 Windows 子系统”->确定,然后等操作完成以后重启

2. 安装子系统 Linux

打开 Win10 应用商店,右上角搜索“Linux”,然后根据结果指引你很容易能找到这里

然后选择你喜欢的 Linux 系统安装即可,我比较喜欢 Debian 所以安装了 Debian

安装完成后打开系统,会让你输入新用户名,直接输入root即可,这样能默认就使用 root 登录,然后根据提示设置一个密码即可

完成后先替换软件源,因为在国内用国外的官方源会非常非常慢

我习惯用nano编辑器,因此之后的所有编辑命令都是nano,你可以用你自己习惯的编辑器命令

nano /etc/apt/sources.list

编辑完之后,nano编辑器是按Ctrl + X再按Y保存

将原来的软件源替换成阿里云的镜像源,以下示例是 Debian 9 的软件源,请你根据自己的系统发行版本来更换软件源

deb http://mirrors.aliyun.com/debian/ stretch main
deb http://mirrors.aliyun.com/debian/ stretch-updates main
deb http://mirrors.aliyun.com/debian-security/ stretch/updates main

然后例行的apt-get二连,并安装后续所需依赖软件

apt-get update
apt-get upgrade -y
apt-get install -y make gcc curl

至此子系统的部署就完成了

部署 ChinaDNS

  1. 下载 ChinaDNS 源码并解压编译
    cd /root
    wget --no-check-certificate  https://github.com/shadowsocks/ChinaDNS/releases/download/1.3.2/chinadns-1.3.2.tar.gz
    tar -zxvf chinadns-1.3.2.tar.gz
    mv chinadns-1.3.2 chinadns
    cd chinadns
    ./configure && make
  2. 更新 chnrouter
    # In /root/chinadns
    curl 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | grep ipv4 | grep CN | awk -F\| '{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > chnroute.txt
  3. 配置 supervisor 进程守护
    # 安装 supervisor
    apt-get install -y supervisor
    # 写入配置(8053是ChinaDNS监听端口,你可以修改成其他的,但是请注意端口冲突问题以及后续DNSMasq配置的同步修改)
    echo "[program:chinadns]
    command = /root/chinadns/src/chinadns -p 8053 -m -c /root/chinadns/chnroute.txt -s 114.114.114.114,208.67.222.222:443
    user = root
    autostart = true
    autorestart = true" > /etc/supervisor/conf.d/chinadns.conf
  4. 启动 supervisor 服务
    service supervisor start
    # 然后检查下 ChinaDNS 是否有正常运行
    supervisorctl status

部署 DNSMasq

# 安装 DNSMasq
apt-get install -y dnsmasq
# 写入配置(使用ChinaDNS做上游DNS)
echo "port=53
no-resolv
no-poll
server=127.0.0.1#8053" > /etc/dnsmasq.conf

然后修改一下 DNSMasq 服务的启动命令(重要,请务必修改,否则 DNSMasq 会无法正常使用配置)

编辑 DNSMasq 服务

nano /etc/init.d/dnsmasq

找到start()函数,按照此图注释掉并修改对应语句,然后保存退出

启动 DNSMasq 服务

service dnsmasq start

修改系统 DNS 为本机

将 IPv4 DNS 改为仅127.0.0.1
将 IPv6 DNS 改为仅::1

然后用nslookup试一下,是不是无污染啦

让上面这一套开机自启

由于子系统 Linux 定制内核的特殊性无法运用rc.local那一套开机自启,因此需要曲线救国

新建/root/.boot.sh文件,写入以下内容

#!/bin/sh
service supervisor start
service dnsmasq start

然后打开 Windows 上的“启动”文件夹
路径是C:\Users\你的用户名\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

在这里新建一个文本文件,写入以下代码

CreateObject("Wscript.Shell").run "bash ~/.boot.sh",vbhide

然后将这个文本文件改名为linux.vbs

Windows 上的bash命令实际上就是子系统 Linux 中的bash,进行到这里我相信你应该懂上面的操作是什么意思了√

搬瓦工VPS优惠套餐,建站稳如狗,支持支付宝,循环出账94折优惠码BWH3HYATVBJW
年付$47CN2线路,1核/1G内存/20G硬盘/1T@1Gbps【点击购买
季付$47CN2 GIA线路,1核/1G内存/20G硬盘/1T@2.5Gbps【点击购买
Last modification:June 30th, 2018 at 07:26 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment Cancel reply

24 comments

  1. 吴晨丶丿残影  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 90.0.4430.93(Google Chrome 90.0.4430.93)
    为什么不用SmartDNS或者Adguard DNS(雾)
  2. 飛鳥  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 68.0.3440.106(Google Chrome 68.0.3440.106)
    感謝大佬,成功了ヾ(≧∇≦*)ゝ
  3. Zero  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 66.0.3359.139(Google Chrome 66.0.3359.139)
    一脸懵逼进来,一脸懵逼出去
    1. 神代綺凜  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 67.0.3396.87(Google Chrome 67.0.3396.87)
  4. Jed Sun  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 67.0.3396.87(Google Chrome 67.0.3396.87)
    请问这个方法可以直接在家中路由器的openwrt中部署吗?
    |´・ω・)ノ
    1. 神代綺凜  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 67.0.3396.87(Google Chrome 67.0.3396.87)
      @Jed Sun openwrt最简单的建议能的话直接刷H大老毛子这种已经带有N多功能的固件,包括ChinaDNS
      http://www.right.com.cn/forum/thread-161324-1-1.html

      自行在openwrt上部署的话参照github项目说明自行编译
      https://github.com/shadowsocks/ChinaDNS

  5. zephyru  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 68.0.3440.7(Google Chrome 68.0.3440.7)
    哎,上班得时候看到得...
    当时就想问..资源占用率如何呀?感觉是跑了一个虚拟机..开销不会爆炸?
    附带一提...我司不知道啥原因把你博客屏蔽了....也可能是网段DNS得原因..
    反正我是在阿里云上挂了个页面代理上来看的..
    就是不能评论..
    1. 神代綺凜  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 67.0.3396.79(Google Chrome 67.0.3396.79)
      @zephyru 不会,这个子系统几乎是相当于内嵌一样的,你在里面运行多少程序就占多少资源,而且你在linux内运行的程序都能在Windows的任务管理器上看到
      1. zephyru  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 68.0.3440.7(Google Chrome 68.0.3440.7)
        @神代綺凜 好吧..不过我运行linux的时候cpu占用虽然不高..但是温度一直不低...
        还有教程里少了一步...
        apt-get install -y make gcc
        一步一步按着来..
        supervice启动报错时我才发现..
        编译没成功..
        编译报错的时候我才发现..
        少这一步..
        不友好...
        1. 神代綺凜  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 67.0.3396.87(Google Chrome 67.0.3396.87)
          @zephyru 因为我之前在实验的时候直接想装宝塔面板试试,然后有些依赖就自动装了,然后我自己都不知道缺什么就干脆不写了等你来回答
        2. zephyru  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 68.0.3440.7(Google Chrome 68.0.3440.7)
          @zephyru 对..还少一个..
          apt-get install -y curl
          1. zephyru  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 68.0.3440.7(Google Chrome 68.0.3440.7)
            @zephyru ....遇到一个奇怪的问题...
            安装curl的时候卡住了..
            Setting up ca-certificates (20161130+nmu1) ...
            看起来是ca证书..
            我等不急关掉..
            提示要输入dpkg --configure -a
            然后刷新源为官方...
            刷新的时候卡..
            Setting up ca-certificates (20161130+nmu1) ...
            再等不及关闭..
            蓝屏..
            重启..
            dpkg --configure -a
            然后卡..
            Setting up ca-certificates (20161130+nmu1) ...
            问题来了..去哪重置这个linux...
            1. 神代綺凜  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 67.0.3396.87(Google Chrome 67.0.3396.87)
              @zephyru 在cmd中:

              # 查看已安装的子系统
              wslconfig /l
              # 卸载子系统
              wslconfig /u 你上面列表里看到的子系统名字

              操作完以后直接再次运行子系统即可

              1. zephyru  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 68.0.3440.7(Google Chrome 68.0.3440.7)
                @神代綺凜 试了下宝塔板..
                反正我在子系统里用这个装node环境..失败了...
                一度以为不能装..自己手动装了一遍才好..
                但是..我是想来问..为啥我在子系统里装了..net-tools后
                netstat -ntpl 后
                Active Internet connections (only servers)
                什么都看不见...
                还有..我在win里挂SS..要怎么样影响方便得影响到子系统...
                我查了下..Linux用SS客户端..总感觉好麻烦..
                你有什么好办法么?
                1. 神代綺凜  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 67.0.3396.87(Google Chrome 67.0.3396.87)
                  @zephyru 宝塔面板在Windows子系统里还是没办法好好工作,装来装去只有nginx和mysql是可以用的……php也有问题
                  node的话直接装Windows版的不是更方便么
                  挂ss的问题我没有这个烦恼,因为我直接挂在路由器上的
                  1. zephyru  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 68.0.3440.7(Google Chrome 68.0.3440.7)
                    @神代綺凜 windows上装了node在子系统里node也不能工作呀...。。
                    你挂在路由上..直接挂全局?
                    下载东西得时候还走SS不是多此一举...
                    还是路由上也有办法让PAC生效...?
                    我这个路由理论上来说也能挂SS毕竟是基于linux得软路由..
                    但是我在Linux里只能基于/etc/profile挂全局代理..pac完全无效呀...
                    1. 神代綺凜  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 67.0.3396.87(Google Chrome 67.0.3396.87)
                      @zephyru 我开发环境也不会装子系统里啊,那多麻烦
                      路由器可以做到gfwlist的pac效果,看来你没玩过路由器的ss,也支持ss-local直接开个socks5监听端口你自己用
                      1. zephyru  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 68.0.3440.7(Google Chrome 68.0.3440.7)
                        @神代綺凜 大佬,大佬..
                        我看了下routerOs上装SS..一水的都是先Metarouter或者kvm一个openWrt再装...
                        然后我看了下我路由器似乎没有这两个高端的功能...
                        所以我想...
                        假如我在树莓派里跑SS..树莓派通过路由器上网..然后让路由器把流量转发到树莓派上..
                        看需不需要走SS..这个思路可不可行?
                        如果可行..在普通的linux下ss要怎么实现类似pac的效果...?
                        能想到的大概就是准备一个pac文件...请求来了先去树莓派上判断一番..在pac文件里..走ss
                        不在直接请求..
                        问题是....怎么做...我搜Linux下的SS全是全局代理...
                        1. zephyru  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 68.0.3440.7(Google Chrome 68.0.3440.7)
                          @zephyru 大佬大佬..
                          我折腾了一上午成功的...在树莓派上基于privoxy实现了pac..虽然gfw list用的14年的..
                          但至少效果有了..
                          然后..我想在路由器上设置连接这个代理....
                          问题是..我发现我完全不知道该去找哪个方面的内容...
                          路由器自带VPN连接设置..什么L2TP OpenVpn..但看设置完全不是我要的..
                          我找到了一个web proxy..问题是..设置都看不懂..
                          搜了半天..似乎路由器内置的防火墙可以作转发...
                          一脸懵逼..转发啥?80端口?..
                          大佬,大佬..帮忙看看这些东西是啥哪个能用..我该去查啥好呗?
                          图片多又大..我给个外链好了...
                          http://saltfish.vip/2018/06/16/cash/
                          1. 神代綺凜  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 67.0.3396.87(Google Chrome 67.0.3396.87)
                            @zephyru 你自己慢慢研究,开心就好
  6. 红尘已逝梦犹存  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 67.0.3396.87(Google Chrome 67.0.3396.87)
    大佬,请问在已经成功拥有SS的情况下折腾这个有什么额外的好处呢?
    1. 神代綺凜  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 67.0.3396.79(Google Chrome 67.0.3396.79)
      @红尘已逝梦犹存 这个只是为了避免DNS污染,有一些被污染了但是没被墙的网站比如www.pixiv.net,直连速度也没问题不需要挂代理,那么就可以用这个
      1. hhh  Android 8.1.0(Android 8.1.0) / Google Chrome 71.0.3578.99(Google Chrome 71.0.3578.99)
        @神代綺凜 为啥弄好了,无污染的本地DNS还是进不去www.pixiv.net?解析是正常的,感觉就是被墙了的。
        1. 神代綺凜  Windows 10 x64 Edition(Windows 10 x64 Edition) / Google Chrome 71.0.3578.98(Google Chrome 71.0.3578.98)
          @hhh 因为现在 GFW 用 SNI 来识别并阻断网站链接
          具体解决方法可以看 https://2heng.xin/2017/09/19/pixiv/