企业内网DNS搭建,SmartDNS,网站访问加速,解决dns污染等问题

smart安装链接
[https://github.com/pymumu/smartdns/releases](https://github.com/pymumu/smartdns/releases)
wget https://github.com/pymumu/smartdns/releases/download/all-best-ip/smartdns.1.2022.05.03-1046.x86_64-linux-all.tar.gz

[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  smartdns.1.2022.05.03-1046.x86_64-linux-all.tar.gz
[root@localhost ~]# 

tar zxf  smartdns.1.2022.05.03-1046.x86_64-linux-all.tar.gz
[root@localhost ~]# tar zxf  smartdns.1.2022.05.03-1046.x86_64-linux-all.tar.gz
[root@localhost ~]# cd smartdns
[root@localhost smartdns]# chmod +x ./install
[root@localhost smartdns]# ./install -i
install: 正在创建目录"/etc/smartdns"
"usr/sbin/smartdns" -> "/usr/sbin/smartdns"
"etc/smartdns/smartdns.conf" -> "/etc/smartdns/smartdns.conf"
"etc/default/smartdns" -> "/etc/default/smartdns"
"etc/init.d/smartdns" -> "/etc/init.d/smartdns"
"systemd/smartdns.service" -> "/usr/lib/systemd/system/smartdns.service"
Created symlink from /etc/systemd/system/smartdns.service to /usr/lib/systemd/system/smartdns.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/smartdns.service to /usr/lib/systemd/system/smartdns.service.
[root@localhost smartdns]# 

vi /etc/smartdns/smartdns.conf

systemctl enable smartdns
systemctl start smartdns

飞书收集

#飞书
.feishu.cn
.q9jvw0u5f5.feishu.cn
.ccm-frontier.feishu.cn
.internal-api.feishu.cn
.hryn145hsh.feishu.cn
.internal-api-drive-stream.feishu.cn
.internal-api-lark-api.feishu.cn
.slardar-bd.feishu.cn
.ccm-frontier-hl.feishu.cn
.mcs-bd.feishu.cn
.feishucdn.com
.sf3-scmcdn2-cn.feishucdn.com
.s1-imfile.feishucdn.com 
.sf6-scmcdn-cn.feishucdn.com
.ai.feishu.com
.lf9-img-sign.bytehwm.com
.lf3-short.ibytedapm.com
.mon.zijieapi.com
#百度
.baidu.com 
#京东
.jd.com
#阿里
#淘宝
.taobao.com
#wechat
.weixin.qq.com
.qq.com
.wechat.com
.wx.qq.com
#csdn
.csdn.net 


配置文件

vi /etc/smartdns/smartdns.conf


# dns server name, default is host name
# server-name,
# example:
server-name smartdns1
#

# Include another configuration options
# conf-file [file]
# conf-file blacklist-ip.conf

# dns server bind ip and port, default dns server port is 53, support binding multi ip and port
# bind udp server
#   bind [IP]:[port] [-group [group]] [-no-rule-addr] [-no-rule-nameserver] [-no-rule-ipset] [-no-speed-check] [-no-cache] [-no-rule-soa] [-no-dualstack-selection]
# bind tcp server
# option:
#   -group: set domain request to use the appropriate server group.
#   -no-rule-addr: skip address rule.
#   -no-rule-nameserver: skip nameserver rule.
#   -no-rule-ipset: skip ipset rule.
#   -no-speed-check: do not check speed.
#   -no-cache: skip cache.
#   -no-rule-soa: Skip address SOA(#) rules.
#   -no-dualstack-selection: Disable dualstack ip selection.
#   -force-aaaa-soa: force AAAA query return SOA.
# example:
#  IPV4:
#    bind :53
#    bind :6053 -group office -no-speed-check
#  IPV6:
#    bind [::]:53
#    bind-tcp [::]:53
bind [::]:53

# tcp connection idle timeout
# tcp-idle-time [second]

# dns cache size
# cache-size [number]
#   0: for no cache
cache-size 4096

# enable persist cache when restart
# cache-persist yes

# cache persist file
# cache-file /tmp/smartdns.cache

# prefetch domain
# prefetch-domain [yes|no]
#启用域名预获取
prefetch-domain yes

# cache serve expired
# serve-expired [yes|no]
#启用过期缓存服务
serve-expired yes

# cache serve expired TTL
# serve-expired-ttl [num]
#
serve-expired-ttl 0

# reply TTL value to use when replying with expired data
# serve-expired-reply-ttl [num]
# serve-expired-reply-ttl 30

# List of hosts that supply bogus NX domain results
# bogus-nxdomain [ip/subnet]

# List of IPs that will be filtered when nameserver is configured -blacklist-ip parameter
# blacklist-ip [ip/subnet]

# List of IPs that will be accepted when nameserver is configured -whitelist-ip parameter
# whitelist-ip [ip/subnet]

# List of IPs that will be ignored
# ignore-ip [ip/subnet]

# speed check mode
# speed-check-mode [ping|tcp:port|none|,]
# example:
#测速模式选择,一般只检测两种协议
#   speed-check-mode ping,tcp:80
   speed-check-mode tcp:443,ping
#   speed-check-mode none

# force AAAA query return SOA
# force-AAAA-SOA [yes|no]
#禁用IPV6解析
force-AAAA-SOA yes

# force specific qtype return soa
# force-qtype-SOA [qtypeid |...]
# force-qtype-SOA 65 28

# Enable IPV4, IPV6 dual stack IP optimization selection strategy
# dualstack-ip-selection-threshold [num] (0~1000)
# dualstack-ip-selection [yes|no]
dualstack-ip-selection no


# edns client subnet
# edns-client-subnet [ip/subnet]
# edns-client-subnet 192.168.1.1/24
# edns-client-subnet [8::8]/56

# ttl for all resource record
# rr-ttl: ttl for all record
# rr-ttl-min: minimum ttl for resource record
# rr-ttl-max: maximum ttl for resource record
# tr-ttl-reply-max: maximum reply ttl for resource record
# example:
#设置TTL最小值和最大值
#rr-ttl 300
rr-ttl-min 60
rr-ttl-max 86400
# rr-ttl-reply-max 60

# set log level
# log-level: [level], level=fatal, error, warn, notice, info, debug
# log-file: file path of log file.
# log-size: size of each log file, support k,m,g
log-num: number of logs
log-level info
log-file /var/log/smartdns.log
log-size 128k
# dns audit
# audit-enable [yes|no]: enable or disable audit.
# audit-enable yes
# audit-SOA [yes|no]: enable or disable log soa result.
# audit-size size of each audit file, support k,m,g
# audit-file /var/log/smartdns-audit.log
# audit-size 128k
# audit-num 2

# certificate file
# ca-file [file]
# ca-file /etc/ssl/certs/ca-certificates.crt

# certificate path
# ca-path [path]
# ca-path /etc/ss/certs

# remote udp dns server list
# server [IP]:[PORT] [-blacklist-ip] [-whitelist-ip] [-check-edns] [-group [group] ...] [-exclude-default-group]
# default port is 53
#   -blacklist-ip: filter result with blacklist ip
#   -whitelist-ip: filter result whth whitelist ip,  result in whitelist-ip will be accepted.
#   -check-edns: result must exist edns RR, or discard result.
#   -group [group]: set server to group, use with nameserver /domain/group.
#   -exclude-default-group: exclude this server from default group.
# server 8.8.8.8 -blacklist-ip -check-edns -group g1 -group g2

#飞书
server 114.114.114.114 -group feishu -exclude-default-group
nameserver /.feishu.cn/feishu
nameserver /.feishucdn.com/feishu
nameserver /.feishu.com/feishu
nameserver /.bytehwm.com/feishu
nameserver /.ibytedapm.com/feishu
nameserver /.zijieapi.com/feishu
nameserver /.feelgood.cn/feishu
# 百度
server 114.114.114.114 -group baidu -exclude-default-group
nameserver /.baidu.com/baidu
#京东
server 114.114.114.114 -group jd -exclude-default-group
nameserver /.jd.com/jd
#淘宝天猫
server 114.114.114.114 -group taobao -exclude-default-group
nameserver /.taobao.com/taobao
nameserver /.tmall.com/taobao
#阿里
server 114.114.114.114 -group ali -exclude-default-group
nameserver /.aliyun.com/ali
#csdn
server 114.114.114.114 -group csdn -exclude-default-group
nameserver /.csdn.net/csdn
#华为
server 114.114.114.114 -group huawei -exclude-default-group
nameserver /.huaweicloud.com/huawei
#todesk
server 114.114.114.114 -group todesk  -exclude-default-group
nameserver /.todesk.com/todesk
#QQ
server 114.114.114.114 -group qq -exclude-default-group
nameserver /.qq.com/qq

# remote tcp dns server list
# server-tcp [IP]:[PORT] [-blacklist-ip] [-whitelist-ip] [-group [group] ...] [-exclude-default-group]
# default port is 53
# server-tcp 8.8.8.8
server 8.8.8.8
server 8.8.4.4
##server 120.53.129.197
##server 124.70.4.50


# remote tls dns server list
# server-tls [IP]:[PORT] [-blacklist-ip] [-whitelist-ip] [-spki-pin [sha256-pin]] [-group [group] ...] [-exclude-default-group]
#   -spki-pin: TLS spki pin to verify.
#   -tls-host-verify: cert hostname to verify.
#   -host-name: TLS sni hostname.
#   -no-check-certificate: no check certificate.
# Get SPKI with this command:
# default port is 853
server-tls 8.8.8.8
server-tls 1.0.0.1

# remote https dns server list
# server-https https://[host]:[port]/path [-blacklist-ip] [-whitelist-ip] [-spki-pin [sha256-pin]] [-group [group] ...] [-exclude-default-group]
#   -spki-pin: TLS spki pin to verify.
#   -tls-host-verify: cert hostname to verify.
#   -host-name: TLS sni hostname.
#   -http-host: http host.
#   -no-check-certificate: no check certificate.
# default port is 443
server-https https://cloudflare-dns.com/dns-query

# specific nameserver to domain
# nameserver /domain/[group|-]
# nameserver /www.example.com/office, Set the domain name to use the appropriate server group.
# nameserver /www.example.com/-, ignore this domain


# specific address to domain
# address /domain/[ip|-|-4|-6|#|#4|#6]
# address /www.example.com/1.2.3.4, return ip 1.2.3.4 to client
# address /www.example.com/-, ignore address, query from upstream, suffix 4, for ipv4, 6 for ipv6, none for all
# address /www.example.com/#, return SOA to client, suffix 4, for ipv4, 6 for ipv6, none for all

# enable ipset timeout by ttl feature
# ipset-timeout [yes]

# specific ipset to domain
# ipset /domain/[ipset|-]
# ipset /www.example.com/block, set ipset with ipset name of block
# ipset /www.example.com/-, ignore this domain

# set domain rules
# domain-rules /domain/ [-speed-check-mode [...]]
# rules:
#   [-c] -speed-check-mode [mode]: speed check mode
#                             speed-check-mode [ping|tcp:port|none|,]
#   [-a] -address [address|-]: same as address option
#   [-n] -nameserver [group|-]: same as nameserver option
#   [-p] -ipset [ipset|-]: same as ipset option
#   [-d] -dualstack-ip-selection [yes|no]: same as dualstack-ip-selection option
smartdns 是 python 语言编写,基于 twisted 框架实现的dns server,能够支持针对不同的dns请求根据配置返回不同的解析结果。smartdns获取dns请求的源IP或者客户端IP(支持edns协议的请 求可以获取客户端IP),根据本地的静态IP库获取请求IP的特性,包括所在的国家、省份、城市、ISP等,然后根据我们的调度配置返回解析结果。 smartdns的使用场景: 服务的多机房流量调度,比如电信流量调度到电信机房、联通流量调度到联通机房; 用户访问控制,将用户调度到离用户最近或者链路质量最好的节点上。 举个简单的例子,我们的一个站点test.test.com同时部署在电信和联通两个机房,该站点在电信机房的ip为1.1.1.1、在联通机房的ip为 2.2.2.2,就可以通过smartdns做到该站点域名解析时判断源IP为电信的IP时返回1.1.1.1、判断源IP为联通的IP时返回 2.2.2.2,从而达到不同运营商机房流量调度的目的。 支持的功能 支持A、SOA、NS记录的查询,支持DNS forward功能 性能 在虚拟机2.4G CPU上能够处理1000QPS查询请求,打开debug日志后可以到800QPS。3-5台dns server组成的集群已经能够满足大部分站点的需求。 目前我们正在实现和小流量测试go语言实现的smartdns,能够达到3wQPS以上,后续测试稳定后会开源出来,大家敬请期待:) 原理 smartdns响应dns请求的处理流程如下: IPPool类的初始化和该类中FindIP方法进行解析处理是smartdns中最关键的两个要素,这两个要素在下面详细介绍。其他的特性比如继 承twisted中dns相关类并重写处理dns请求的方法、升级twisted代码支持解析和处理edns请求等大家可以通过代码了解。edns知识可 以猛戳这里:DNS support edns-client-subnet IPPool初始化 ip.csv内容格式如下:200000001, 200000010,中国,陕西,西安,电信 其中各个字段含义分别为 IP段起始,IP段截止,IP段所属国家,IP段所属省份,IP段所属城市,IP段所属ISP a.yaml配置文件格式: test.test.com:   ttl: 3600   default: 5.5.5.5 2.2.2.2   中国,广东,,联通: 1.1.1.1 3.3.3.1   中国,广东,,电信: 1.1.1.2 3.3.3.2 配置中地域信息的key包括四个字段,分别带有不同的权重: 国家:    8 省份: 4 城市: 2 运营商:  1 初始化阶段,会生成一个名为iphash的dict,具体数据结构如下图: 其中,iphash的key为ip.csv每一条记录的起始IP,value为一个list,list长度为6,list前5个字段分别为以该 key为起始IP记录的IP段截止、IP段所属国家、IP段所属省份、IP段所属城市、IP段所属ISP,第六个字段是一个hash,key为 a.yaml里面配置的域名,value为长度为2的list,iphash[IP段起始][6][域名1][0]为域名1在该IP段的最优解 析,iphash[IP段起始][6][域名1][1]为该最优解析的总权值,该总权值暂时只做参考。 iphash初始化过程中最关键的是iphash[IP段起始][6][域名1]的最优解析的计算,最简单直接的方式是直接遍历域名1的所有调度配 置,挑选出满足条件且总权值最高的解析,即为最优解析。这种方式记录整个iphash的时间复杂度为O(xyz),x为ip.csv记录数,y为域名总数 量,z为各个域名的调度配置数。为了优化启动速度,优化了寻找最优解析的方法:事先将每个域名调度配置生成一颗树,这棵树是用dict模拟出来的,这样需 要最优解的时候就不需要遍历所有调度配置,而是最多检索15次即可找到最优,即时间复杂度为O(15xy),具体实现参考IPPool的 LoadRecord和JoinIP两个方法。 有了初始化后的iphash数据结构之后,每次请求处理的时候,只需要定位请求IP处在哪个IP段,找到IP段起始IP,然后从iphash中取出最优解析,取出最优解析的过程是O(1)的。具体流程如下: 代码 github: https://github.com/xiaomi-sa/smartdns 安装 依赖: python 2.6或者2.7 Twisted 12.2.0 zope.interface 4.0.1 安装: git clone smartdns到本地路径,进入script目录,执行install_smartdns.sh即可将smartdns安装在本地,同时python环境和相关的依赖都是使用virtualenv来进行管理,不会对系统环境造成影响。 启动: 进入smartdns的bin路径下,执行sh run_dns.sh即可启动smartdns 测试 本地测试 dig test.test.com @127.0.0.1 或者将搭建smartdns加到测试域名的ns中进行测试。 支持 mail: fangshaosen@xiaomi.com github: jerryfang8 EDNS相关请参考:DNS support edns-client-subnet 标签:smartdns
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aggy阿吉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值