DoH 服务器的 Python 实现

实现

看到有 Go、PHP 语言实现的 DoH 服务器,于是弄了个 Python 实现的 DoH 服务器(兼容 RFC8484),可以使用 Nginx、Caddy 反向代理 /dns-query 接口完成部署。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
/*
* @Author: Silent YANG
* @Date: 2023-11-29
*/
"""

from flask import Flask, request, make_response
from base64 import urlsafe_b64decode
import socket

app = Flask(__name__)
socket.setdefaulttimeout(5)

@app.route("/dns-query" ,methods=['GET', 'POST'])
def dns_query():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if request.headers.get('CONTENT_TYPE') == 'application/dns-message':
sock.sendto(request.stream.read(), ("127.0.0.1", 53))
rx_meesage, addr = sock.recvfrom(4096)
return make_response(rx_meesage, 200, {"Content-Type": "application/dns-message"})
if request.method == "GET" and request.args.get("dns"):
payload = urlsafe_b64decode(request.args.get("dns") + "===")
sock.sendto(payload, ("127.0.0.1", 53))
rx_meesage, addr = sock.recvfrom(4096)
return make_response(rx_meesage, 200, {"Content-Type": "application/dns-message"})
sock.close()
return make_response("", 400)

if __name__ == "__main__":
app.run('127.0.0.1', debug=False, port=5000)

完整代码

https://github.com/silenty4ng/DoH