kaikai
发布于 2024-11-04 / 12 阅读
0
0

自建DNS服务器,实现物理BGP

最近BGP.GD的mix2和mix3专线开机了,提供了多个运营商IP,但是需要手动切换节点,不是很方便,所以打算通过自建DNS服务器实现自动根据网络选择连接的IP,也就是物理BGP。

安装准备

  • 域名:可选,如果不打算使用 TLS 或 HTTPS。本文中使用 dns.example.com

  • VPS 服务器:用于搭建 DNS 服务器,必须是国外的,因为国内不能搭建 DNS 服务器。

  • 自定义域名:例如 proxy.com

1. 安装 SmartDNS

(此步骤略过)

2. 自动申请证书

首先,将域名解析到自建的 DNS 服务器 IP。


apt-get install certbot

certbot certonly --standalone -d dns.example.com -d dns.example.com

3. 配置 SmartDNS


# 修改为期望的端口

bind-https 0.0.0.0:853

bind-cert-file /etc/letsencrypt/live/dns.example.com/fullchain.pem

bind-cert-key-file /etc/letsencrypt/live/dns.example.com/privkey.pem 

log-level debug

cache-size 0

# 规则文件,里面是 ipcidr 列表

ip-set -name cn -file /etc/smartdns/cn.conf

ip-set -name cu -file /etc/smartdns/cu.conf

# 规则开始,指定名称为 ChinaNet

group-begin ChinaNet

# 设置匹配规则,如下为匹配 IP、MAC 或者域名

group-match -client-ip ip-set:cn

# 设置相关的规则,修改 proxy.com 为你期望的域名和对应的 IP

domain-rules /proxy.com/ -address 1.2.3.4

# 规则结束

group-end

# 规则开始,指定名称为 ChinaUnicom

group-begin ChinaUnicom

# 设置匹配规则,如下为匹配 IP、MAC 或者域名

group-match -client-ip ip-set:cu

# 设置相关的规则

domain-rules /proxy.com/ -address 4.5.6.7

# 规则结束

group-end

4. 客户端代理设置

目标是将 DNS 服务器设置为 https://dns.example.com:[端口]/dns-query,并最好能够指定域名 proxy.com 的 DNS 服务器。

5. 问题定位

使用以下命令查看日志,判断是否成功解析对应 IP。注意,不得经过任何反向代理软件,包括但不限于 Caddy、Nginx 等。


tail -f /var/log/smartdns/smartdns.log



评论