From e078302d1d61ca9c8e95beeaa85a12fa9e2bd4e5 Mon Sep 17 00:00:00 2001 From: Silent YANG Date: Tue, 26 Dec 2023 22:51:38 +0800 Subject: [PATCH] new posts --- source/_posts/doh-server.md | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 source/_posts/doh-server.md diff --git a/source/_posts/doh-server.md b/source/_posts/doh-server.md new file mode 100644 index 0000000..2e6fdfd --- /dev/null +++ b/source/_posts/doh-server.md @@ -0,0 +1,50 @@ +--- +title: DoH 服务器的 Python 实现 +categories: 散文杂记 +tags: + - python + - doh +date: 2023/12/26 22:25 +updated: 2023/12/26 22:25 +--- + +## 实现 +看到有 Go、PHP 语言实现的 DoH 服务器,于是弄了个 Python 实现的 DoH 服务器(兼容 RFC8484),可以使用 Nginx、Caddy 反向代理 `/dns-query` 接口完成部署。 + +``` +""" +/* + * @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](https://github.com/silenty4ng/DoH)