





















我有一个web项目,使用域名访问,需要同时运行线上环境和测试环境,为了防止一些css、js缓存影响,在不同的浏览器里分别访问线上环境和测试环境,比如Chrome浏览器访问测试环境,而Safari浏览器访问线上环境。
通常,需要切换环境的时候,通过修改/etc/hosts进行切换,但是这样比较麻烦,有时候忘记切换了就会导致不同环境的cookie相互污染。
于是想到了这样一个可以一劳永逸的方案:
dnsmasq是一个轻量级的dns服务,支持配置指定域名的解析,未配置的域名解析请求支持转发到其他dns。
推荐使用jpillora/dnsmasq这个镜像,他提供了一个web接口,可以直接通过web修改dnsmasq的配置文件。
最好创建一个目录,保存这个服务的配置文件和日志等。。。
mkdir /home/dnsmasq
cd /home/dnsmasq
touch dnsmasq.conf
touch dnsmasq_add_hosts
cat dnsmasq.conf
#dns解析日志
log-queries
# 给不同的域,设置不同的dns
server=/github.com/8.8.8.8
server=/github.com/223.5.5.5
server=/google.com/8.8.8.8
server=/google.com/223.5.5.5
# Change this line if you want dns to get its upstream servers from
# somewhere other that /etc/resolv.conf
# 外部dns服务器地址,格式和 /etc/resolv.conf 一样,如果请求解析的域名没有在下面的列表中,就转发到这些dns进行查询
resolv-file=/usr/local/etc/resolv.dnsmasq.conf
# If you don't want dnsmasq to read /etc/hosts, uncomment the
# following line.
no-hosts
# or if you want it to read another file, as well as /etc/hosts, use
# this. 意思就是从这里读取自定义的域名解析,格式和/etc/hosts一样
addn-hosts=/usr/local/etc/dnsmasq_add_hosts
#域名与IP映射,支持配置多个
address=/zhangsan.sanzhang.com/192.168.100.233
address=/lisi.sanzhang.com/192.168.100.234
docker run -d --name dnsmasq -p 53:53/udp -p 8080:8080 -v /home/dnsmasq/dnsmasq.conf:/etc/dnsmasq.conf -v /home/dnsmasq:/usr/local/etc --log-opt "max-size=100m" -e "HTTP_USER=admin" -e "HTTP_PASS=123456" jpillora/dnsmasq
dnsmasq]# dig @127.0.0.1 sanzhang.zhangsan.com
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> @127.0.0.1 sanzhang.zhangsan.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61007
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;sanzhang.zhangsan.com. IN A
;; ANSWER SECTION:
sanzhang.zhangsan.com. 0 IN A 192.168.100.233
;; Query time: 1 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: 三 12月 13 20:58:38 CST 2023
;; MSG SIZE rcvd: 66
我们使用开源项目github.com/m13253/dns-over-https来实现Doh服务。
docker pull m13253/dns-over-https-server:latest
docker run --rm -ti doh-server cat doh-server.conf > doh-server.conf
# Upstream DNS resolver
# If multiple servers are specified, a random one will be chosen each time.
# You can use "udp", "tcp" or "tcp-tls" for the type prefix.
# For "udp", UDP will first be used, and switch to TCP when the server asks to
# or the response is too large.
# For "tcp", only TCP will be used.
# For "tcp-tls", DNS-over-TLS (RFC 7858) will be used to secure the upstream connection.
upstream = [
"udp:192.168.100.233:53"
]
当然也可以修改下默认的端口,默认是8053:
# HTTP listen port
listen = [
":8053",
## To listen on both 0.0.0.0:8053 and [::]:8053, use the following line
# ":8053",
]
docker run -d --name doh-server --restart=always -v /root/doh-server.conf:/doh-server.conf --network host --privileged m13253/dns-over-https-server:latest
import dns.message
import requests
import base64
import json
doh_url = "http://192.168.100.233:8053/dns-query"
domain = "sanzhang.zhangsan.com"
rr = "A"
result = []
cert = ()
ca_verify = True
message = dns.message.make_query(domain, rr)
dns_req = base64.b64encode(message.to_wire()).decode("UTF8").rstrip("=")
r = requests.get(doh_url + "?dns=" + dns_req,
headers={"Content-type": "application/dns-message"},
cert=cert,
verify=ca_verify)
for answer in dns.message.from_wire(r.content).answer:
dns = answer.to_text().split()
result.append({"Query": dns[0], "TTL": dns[1], "RR": dns[3], "Answer": dns[4]})
print(json.dumps(result))
# python3 -m pip install dns.message
# python3 q.py
[{"Query": "sanzhang.zhangsan.com.", "TTL": "0", "RR": "A", "Answer": "192.168.100.233"}]
测试没问题,但是chrome浏览器请求安全dns始终是通过https协议的,因此我们还需要在外面在搭建一个https服务,用来代理dos的http服务。
这里我们选择自签名证书,这样想搞多久就搞多久,反正都是自己用。
关于OpenSSL生成证书的问题很多,这里着重讲下容易踩坑的地方:
https服务的 server端,其证书需要和运行server的ip地址一样。echo 192.168.100.233 zhangsan.de.dns >> /etc/hosts
整理了下生成证书的命令,按照这个shell脚本生成就可以了:
mkdir https_pem
cd http_pem
cat gen_pems.sh
大家在使用这个脚本的时候,注意下修改里面的zhangsan.de.dns、192.168.100.233为你server端的域名和IP,yzc为你client端(chrome浏览器所在设备)的hostname。
#!/bins/sh
echo "current datetime: `date +"%F %T"`"
if [ ! -f "ssl.conf" ];then
echo "start create ssl.conf"
echo -e "[v3_req]\nsubjectAltName=DNS:zhangsan.de.dns,DNS:192.168.100.233,DNS:localhost" > ssl.conf
fi
# 创建我们自己CA的私钥
# 创建我们自己CA的CSR,并且用自己的私钥自签署之,得到CA的身份证:
openssl genrsa -out ca.key 2048
if [[ $? != 0 ]];then echo "create ca.key failed."; exit 1; fi
openssl req -new -x509 -days 730 -key ca.key -subj "/C=CN/ST=GD/L=SZ/O=Acme, Inc./CN=ZhangSan De Root CA" -out ca.crt
if [[ $? != 0 ]];then echo "create ca.crt failed."; exit 1; fi
# 创建server的私钥,CSR,并且用CA的私钥自签署server的身份证:
CN_Names="CN=zhangsan.de.dns/CN=192.168.100.233/CN=localhost"
openssl req -newkey rsa:2048 -nodes -keyout server.key -subj "/C=CN/ST=GD/L=SZ/O=ZhangSan, Inc./${CN_Names}" -out server.csr
if [[ $? != 0 ]];then echo "create server.key failed."; exit 1; fi
openssl x509 -req -extfile ssl.conf -extensions v3_req -days 730 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
if [[ $? != 0 ]];then echo "create server.crt failed."; exit 1; fi
# 创建client的私钥,CSR,以及用ca.key签署client的身份证:
CN_Names="CN=localhost/CN=yzc"
openssl req -newkey rsa:2048 -nodes -keyout client.key -subj "/C=CN/ST=GD/L=SZ/O=Acme, Inc./${CN_Names}" -out client.csr
if [[ $? != 0 ]];then echo "create client.key failed."; exit 1; fi
openssl x509 -req -extfile ssl.conf -extensions v3_req -days 730 -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt
if [[ $? != 0 ]];then echo "create client.crt failed."; exit 1; fi
生成的证书列表里这些是有用的:
# 根证书
ca.crt
ca.key
# nginx使用的证书,提供https服务
server.crt
server.key
# pc端的证书,访问我们自己搭建的https服务要使用这个证书
client.crt
client.key
将client.crt,client.key 和根证书ca.crt拷贝到本机,一会修改下python脚本,验证https访问是否正确。
scp root@192.168.100.233:/root/https_pams/client* ./https_pams/
scp root@192.168.100.233:/root/https_pams/ca.crt ./https_pams/
按照这个配置文件搞就可以了:
cat /root/https_pams/https-dns.conf
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name 192.168.100.233;
ssl_client_certificate /root/https_pams/ca.crt;
ssl_certificate /root/https_pams/server.crt;
ssl_certificate_key /root/https_pams/server.key;
#ssl_ciphers HIGH:!aNULL:!MD5;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache shared:SSL:10m;
resolver 192.168.100.233 valid=300s; # Replace with your local resolver
resolver_timeout 5s;
# HTTP Security Headers
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header Strict-Transport-Security "max-age=63072000";
location /dns-query {
proxy_pass http://localhost:8053/dns-query;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
在nginx配置中,添加如下配置,引入我们的https配置:
include /root/https_pams/https-dns.conf;
然后执行nginx -s reload,使配置生效,正常是不会有什么报错的,我这里连waring信息都没有。
然后我们来修改下刚刚那个python脚本,增加证书设置:
其实,就是把代码里doh地址修改下,以及将cert 和 ca_verify ,修改为我们从远程拷贝而来的client证书和ca证书:
doh_url = "https://192.168.100.233/dns-query"
cert = ("./https_pams/client.crt", "./https_pams/client.key")
ca_verify = "./https_pams/ca.crt"
在测试下,如果python脚本报错:
啊,那就检查下nginx证书配置那块,还有生成证书里的域名配置是否正确。
正常是不会有什么报错的。
在访达中找到证书位置,双击"ca.crt"和"client.crt",进入钥匙串后,我们找到导入的证书,在显示简介中信任一栏展开,并选择为始终信任即可。
不同版本chrome有不同设置方法,自行搜索安全dns设置即可。
找到后,将安全dns地址设置为:
chrome默认有几个安全dns地址,比如,也可以python脚本测试下,都是可以用的
然后我们在不同的浏览器访问 sanzhang.zhangsan.com,就访问到了不同的主机了,再也不用切换hosts了。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。