自建DNS解锁教程:SNI Proxy 和 Smartdns

最后更新于 2024-10-26 576 次阅读


写在前面

现实使用中,尝尝遇到被部分流媒体屏蔽的情款,比如马来西亚不能使用迪士尼plus。这时SNI Proxy就派上了用场。本文将利用SNI Proxy 和 Smartdns 实现流媒体解锁。本文之所以选择用smartdns取代Dnsmasq,在于smartdns 无论是在查询速度、查询到的IP访问速度和广告过滤都强于Dnsmasq。

为实现自建DNS解锁往往采取一下两种办法:第一种直接在被解锁服务器设置解锁,适用于单个服务器需要解锁的应用场景。第二种建立私人DNS服务器,适用于多台服务器需要解锁的应用场景。

方案一方案二
搭建sni代理
搭建DNS服务器×
被解锁机本地设置仅需修改系统DNS需要在所有被解锁机搭建smartdns
灵活性快捷较为复杂
可靠性自建DNS服务器宕机,本地DNS瘫痪自建DNS服务器宕机,本地DNS正常
应用场景多台服务器单台服务器

必备内容

1台解锁流媒体的服务器(443和80端口不能被占用)

搭建教程

由于存在两种实现方法,教程除了sni代理为统一步骤外,其余部分因差异性将分开阐述。

安装SNI Proxy

wget --no-check-certificate -O dnsmasq_sniproxy.sh https://raw.githubusercontent.com/myxuchangbin/dnsmasq_sniproxy_install/master/dnsmasq_sniproxy.sh && bash dnsmasq_sniproxy.sh -is

设置SNI Proxy

修改配置文件,将所需dns解锁的网址格式化输入

/etc/sniproxy.conf

格式示例

user daemon
pidfile /var/tmp/sniproxy.pid

error_log {
    syslog daemon
    priority notice
}

resolver {
    nameserver 8.8.8.8
    nameserver 8.8.4.4 # local dns should be better
    mode ipv4_only
}
listener 0.0.0.0:80 {
    proto http
    access_log {
        filename /var/log/sniproxy/http_access.log
        priority notice
    }
}
listener 0.0.0.0:443 {
    proto tls
    access_log {
        filename /var/log/sniproxy/https_access.log
        priority notice
    }
}

table {
    .*ai\.com$ *
    .*openai\.com$ *
    .*chatgpt.com$ *
    .*oaistatic.com$ *
    .*oaiusercontent.com$ *
}

重启SNI Proxy

systemctl restart sniproxy

解锁机设置解锁白名单

如未安装ufw,请先执行

sudo apt install ufw
ufw enable

修改下面示例的IP(写被解锁机IP),执行即可。

sudo ufw allow from 2.2.2.2 to any port 443 proto tcp
sudo ufw allow from 2.2.2.2 to any port 443 proto udp
sudo ufw allow from 2.2.2.2 to any port 80 proto tcp
sudo ufw allow from 2.2.2.2 to any port 80 proto udp

方案一:安装smartdns 搭建DNS服务器

此处可以用另一台服务器,也可以就用搭建sni代理那台服务器。

wget https://github.com/pymumu/smartdns/releases/download/Release46/smartdns.1.2024.06.12-2222.x86-linux-all.tar.gz
tar zxf smartdns.1.2024.06.12-2222.x86-linux-all.tar.gz
cd smartdns
chmod +x ./install
./install -i

设置smardns

vi /etc/smartdns/smartdns.conf

编辑配置,自行添加需要解锁的网址,常见流媒体域名可以参考1stream写好的

bind[::]:53@eth0 -no-dualstack-selection -no-speed-check #不要动
dualstack-ip-selection no
speed-check-mode none
serve-expired-prefetch-time 21600
prefetch-domain yes
cache-size 32768
cache-persist yes
cache-file /etc/smartdns/cache
prefetch-domain yes
serve-expired yes
serve-expired-ttl 259200
serve-expired-reply-ttl 3
prefetch-domain yes
serve-expired-prefetch-time 21600
cache-checkpoint-time 86400
#force-AAAA-SOA yes
server 210.0.255.250 #默认上游DNS
server 210.0.255.251 #默认上游DNS

# ---------- > Global Plaform
# > GPT
address /openai.com/解锁机IP
address /chatgpt.com/解锁机IP
address /oaistatic.com/解锁机IP
address /oaiusercontent.com/解锁机IP

保存并启动

systemctl enable smartdns
systemctl start smartdns

DNS服务器开启DNS白名单

强烈建议开启DNS白名单,不开启会被别人白嫖,也可能被D哥用作DNS放大攻击

开启白名单

修改下面示例的IP即可。

ufw allow from 2.2.2.2 to any port 53 proto udp

被解锁服务器修改DNS

修改DNS为解锁机或者DNS服务器的IP。

vim /etc/resolv.conf

方案二

在被解锁服务器本地搭建smartdns

安装步骤和方案一相同,此处不再赘述。

配置文件有所改变,具体改动为第一行的DNS设置,该设置限制了仅本地可以访问

bind :53@lo -no-dualstack-selection -no-speed-check #不要动
dualstack-ip-selection no
speed-check-mode none
serve-expired-prefetch-time 21600
prefetch-domain yes
cache-size 32768
cache-persist yes
cache-file /etc/smartdns/cache
prefetch-domain yes
serve-expired yes
serve-expired-ttl 259200
serve-expired-reply-ttl 3
prefetch-domain yes
serve-expired-prefetch-time 21600
cache-checkpoint-time 86400
#force-AAAA-SOA yes
server 210.0.255.250 #默认上游DNS
server 210.0.255.251 #默认上游DNS


# ---------- > Global Plaform
# > GPT
address /openai.com/解锁机IP
address /chatgpt.com/解锁机IP
address /oaistatic.com/解锁机IP
address /oaiusercontent.com/解锁机IP

被解锁服务器修改DNS

修改DNS为127.0.0.1

vim /etc/resolv.conf

高级玩法

玩法一 嵌套解锁

其实,在某些商家提供DNS解锁的服务器也可以利用本教程给其他服务器提供解锁,就是嵌套解锁(该行为可能违反TOS)。
借助方案一,使用自建DNS,只需要改一下上游DNS设置和域名对应的DNS设置即可实现。

server 210.0.255.250 #默认上游DNS
server 210.0.255.251 #默认上游DNS
server 此处写商家提供的DNS IP -group dnsproxy -exclude-default-group

# > Netflix
nameserver /netflix.com/dnsproxy
nameserver /netflix.net/dnsproxy
nameserver /nflximg.com/dnsproxy 
nameserver /nflximg.net/dnsproxy
nameserver /nflxvideo.net/dnsproxy 
nameserver /nflxext.com/dnsproxy 
nameserver /nflxso.net/dnsproxy 

玩法二 自建DOH服务器

某些厂商强制劫持了53端口的UDP流量,导致无论怎么改DNS都无法摆脱商家的解锁服务,此时DOH就派上了用处。
DOH简单说就是DNS over HTTPS,通过DOH 走TCP流量使用TLS可以完美绕过DNS劫持。
smartdns可以搭建DOH服务器,但是我没搭建成功,so sad。所以仅介绍adguardhome搭建DOH。
具体adguardhome搭建DOH的教程网络已有很多,此处就省去了。搭建过程中,唯一需要注意的是,上游DNS需要写127.0.0.1。
被DNS劫持的服务器只需要在smartdns中将默认DNS设置为自己刚建好的DOH地址即可。

server-https https://cloudflare-dns.com/dns-query

小建议由于TLS三次握手的特性,查询DNS的延迟肯定会提升,建议开启smartdns的缓存功能。

玩法三 smartdns开启广告过滤

下载配置文件到/etc/smartdns目录:

wget https://anti-ad.net/anti-ad-for-smartdns.conf -O /etc/smartdns/anti-ad-smartdns.conf

修改该/etc/smartdns/smartdns.conf文件以包含上述配置文件:

conf-file /etc/smartdns/anti-ad-smartdns.conf

扩展聊一聊(与本文主题无关)

其实直接通过修改系统DNS方式解锁流媒体弊端有很多,常见的弊端比如:DNS服务器宕机导致本地服务无法解析域名、由于厂商分流规则没有细分导致常见CDN 分配的IP在别的地区(常见于akamai的CDN),这时候DNS分流就尤为重要。利用smartdns,可以实现对解锁域名走厂商DNS,对其他域名走公共DNS。举个例子,下面那个配置意思是:DAZN和Hotstar走DNS,其他的全用8.8.8.8解析,这样即使66.66.66.66宕机,本地只要不访问DAZN和Hotstar就不受任何影响。

server 8.8.8.8
server 66.66.66.66(厂商给的dns ip) -group dns -exclude-default-group

# ---------- > Global Plaform
# > DAZN
nameserver /upos-hz-mirrorakam.akamaized.net/dns
# > Hotstar
nameserver /hotstar.com/dns

相关项目官网

sni代理
smartdns
AdGuardHome

此作者没有提供个人介绍
最后更新于 2024-10-26