使用 Unbound 作为广告拦截器

来自 Alpine Linux

基本组件

你应该在你的网络上同时运行着 dnsmasq (或其他 DHCP 服务器) 和 unbound

设置 Unbound 以阻止/拒绝不需要的地址

网络上有许多免费可用的黑名单。上面提到的安装程序默认使用这些列表

或者,在 https://github.com/StevenBlack/hosts 有一组精选的列表。那里有各种类别的列表。文件的格式是 "host" (所以你可以把它放在 /etc/hosts 中并完成)。我们将使用 hosts 文件格式

Unbound 需要将 blacklists.conf 文件包含到其主配置文件中。为此,我们需要创建以下格式的包含文件

内容 /etc/unbound/blacklists.conf

server: local-zone: "bad-site.com" refuse local-zone: "bad-bad-site.com" refuse local-zone: "xyz.ads-r-us.com" refuse

这是一个 shell 脚本示例,用于下载 StevenBlack hosts 文件,并将其格式化为 unbound 可用的格式

#!/bin/sh

echo "server:" >/etc/unbound/blacklist.conf
curl -s https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts | \
        grep ^0.0.0.0 - | \
        sed 's/ #.*$//;
        s/^0.0.0.0 \(.*\)/local-zone: "\1" refuse/' \
        >>/etc/unbound/blacklist.conf


你可以运行一次,或者作为定期 cron 任务的一部分运行。

/etc/unbound/unbound.conf 中,在配置文件的某处添加以下行

内容 /etc/unbound/unbound.conf

#include "/etc/unbound/blacklist.conf"

重新加载 unbound,并验证配置加载。

Dnsmasq 配置

Dnsmasq 默认使用 /etc/resolv.conf 中的解析器——如果 unbound 正在 127.0.0.1 上监听,则让它使用该解析器。

或者,如果 unbound 运行在另一个接口上,或者在单独的机器上——在 dnsmasq 中使用 dhcp-option 配置

dhcp-option=6,[ip-of-unbound-server]


享受无广告浏览!