lala.im:Unbound+Pihole+Overture搭建去广告/无污染的DNS

運維技術·VPN代理·lala.im · 2019-12-10 · 113 人浏览

原文地址:https://lala.im/6580.html,請支持原作者!該處僅作轉載。

如果你经常使用adguardhome会发现这货虽然使用起来很方便,但是过滤广告的效果非常不理想,规则里面明明有的域名但有时候它就是屏蔽不了,很玄学的一件事情。。
本文介绍一款船新的方式,广告过滤的效果比单adguardhome要好的多。目前这是我自用的一套方案,具体用到的各个软件用途说明如下:
1.在国外的VPS上用Unbound搭建一个DoT,用于防DNS污染。
2.在国外的VPS上用Pihole搭建一个去广告的普通DNS,用于Unbound的上游。
3.本地搭建Overture,用于Unbound的下游,同时处理国内/国外的域名/IP分DNS解析。
以下步骤在Debian10上操作,安装需要用到的工具:

apt -y update
apt -y dist-upgrade
apt -y install curl certbot ca-certificates unbound knot-dnsutils

使用certbot签一个ssl证书(签之前先把你的域名解析到这台VPS):

certbot certonly --standalone --agree-tos --no-eff-email --email [email protected] -d dns.233.fi

将申请好的证书复制到unbound的目录:

cp /etc/letsencrypt/live/dns.233.fi/privkey.pem /etc/unbound/dns.233.fi-privkey.pem
cp /etc/letsencrypt/live/dns.233.fi/fullchain.pem /etc/unbound/dns.233.fi-fullchain.pem

新建一个unbound的配置文件:

nano /etc/unbound/unbound.conf.d/dns-over-tls.conf

写入:

server:
    username: unbound
    access-control: 0.0.0.0/0 allow
        interface: 0.0.0.0@853
        interface: ::0@853
        tls-port: 853
        tls-service-key: "/etc/unbound/dns.233.fi-privkey.pem"
        tls-service-pem: "/etc/unbound/dns.233.fi-fullchain.pem"
        incoming-num-tcp: 1000
forward-zone:
    name: "."
    forward-addr: 152.44.44.19

注:forward-addr后面的IP改为你VPS的公网IP,也就是把待会搭建好的Pihole当作unbound的上游DNS。
重启unbound/设置开机自启:

systemctl restart unbound
systemctl enable unbound

由于Pihole的手动安装过程略蛋疼,并且容易出错,这里直接上Docker:

curl -sSL https://get.docker.com/ | sh
systemctl start docker
systemctl enable docker

安装docker-compose:

curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

新建一个docker-compose配置文件:

mkdir -p /opt/pi-hole && cd /opt/pi-hole && nano docker-compose.yml

写入如下配置:

version: "3"

services:
  pihole:
    container_name: pihole
    image: pihole/pihole:latest
    ports:
      - "53:53/tcp"
      - "53:53/udp"
      - "67:67/udp"
      - "80:80/tcp"
      - "443:443/tcp"
    environment:
      TZ: 'Asia/Shanghai'
    volumes:
      - './etc-pihole/:/etc/pihole/'
      - './etc-dnsmasq.d/:/etc/dnsmasq.d/'
    dns:
      - 127.0.0.1
      - 1.1.1.1
    cap_add:
      - NET_ADMIN
    restart: unless-stopped

然后直接up起来:

docker-compose up -d

没问题的话查看Pihole的WEB管理员密码:

docker logs pihole | grep random

修改管理员密码:

docker exec -it pihole pihole -a -p

现在测试一下看看Unbound和Pihole能不能正常工作:

kdig +tls @dns.233.fi lala.im

如下有正常解析记录说明是OK的:

範例
接下来在你的本地配置Overture:

apt -y update
apt -y install unzip supervisor
mkdir -p /opt/overture && cd /opt/overture
wget https://github.com/shawn1m/overture/releases/download/v1.6-rc6/overture-linux-amd64.zip
unzip overture-linux-amd64.zip
cp overture-linux-amd64 overture

下载国内IP/GFWLIST:

wget https://cokebar.github.io/gfwlist2dnsmasq/gfwlist_domain.txt
wget https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt

编辑Overture的配置文件:

nano config.json

修改为如下配置:

{
  "BindAddress": ":53",
  "DebugHTTPAddress": "127.0.0.1:5555",
  "PrimaryDNS": [
    {
      "Name": "DNSPod",
      "Address": "119.29.29.29:53",
      "Protocol": "udp",
      "SOCKS5Address": "",
      "Timeout": 6,
      "EDNSClientSubnet": {
        "Policy": "disable",
        "ExternalIP": "",
        "NoCookie": true
      }
    }
  ],
  "AlternativeDNS": [
    {
      "Name": "Pi-hole",
      "Address": "dns.233.fi:853",
      "Protocol": "tcp-tls",
      "SOCKS5Address": "",
      "Timeout": 6,
      "EDNSClientSubnet": {
        "Policy": "disable",
        "ExternalIP": "",
        "NoCookie": true
      }
    }
  ],
  "OnlyPrimaryDNS": false,
  "IPv6UseAlternativeDNS": false,
  "WhenPrimaryDNSAnswerNoneUse": "PrimaryDNS",
  "IPNetworkFile": {
    "Primary": "./china_ip_list.txt",
    "Alternative": ""
  },
  "DomainFile": {
    "Primary": "",
    "Alternative": "./gfwlist_domain.txt",
    "Matcher":  "regex-list"
  },
  "HostsFile": "./hosts_sample",
  "MinimumTTL": 0,
  "DomainTTLFile" : "./domain_ttl_sample",
  "CacheSize" : 0,
  "RejectQType": [255]
}

新建supervisor配置文件:

nano /etc/supervisor/conf.d/overture.conf

写入如下配置:

[program:overture]
priority=1
directory=/opt/overture
command=/opt/overture/overture -c /opt/overture/config.json
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/supervisor/overture.log

更新supervisor配置,查看运行状态,回显RUNNING说明正常:

supervisorctl update
supervisorctl status overture

现在修改路由器的DHCP将网关设置为这台部署Overture的机器IP即可使用这样一个去广告/防DNS污染的服务啦~
一些补充说明:
1.由于在本地有Overture分DNS解析,所以只有在GFWLIST内的域名才会由我们自建的DoT负责解析,理所当然的过滤广告也只会过滤掉这部分网站的广告,如果要想全部过滤就得把Overture的部分功能阉割掉,但阉割掉的代价是国内域名的解析速度会变慢,当然这个可以加钱解决,你有钱买台线路好的机器就可以不用分DNS解析了。
2.除了在本地可以用以外,你还可以把自己的梯子鸡,诸如V2Ray/SS这类代理工具使用的DNS也更改为Pihole的DNS,同样能达到过滤广告的效果。
3.Unbound/Pihole的DNS服务都暴露在公网,如果只想自己独享,切记不要分享出VPSIP,或者用防火墙做一下限制,避免滥用。
4.Pihole也可以部署在本地,然后在Pihole内设置上游DoH,也是一种可行的办法,但缺点是国内解析速度偏慢,方法可以参考:
https://docs.pi-hole.net/guides/dns-over-https/

運維技術 VPN代理 lala.im
Theme Jasmine by Kent Liao