summaryrefslogtreecommitdiff
path: root/internal/server/doh.go
blob: 0feb09455fd9409d1761225ea5a31510e6160286 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package server

import (
	"encoding/base64"
	"io"
	"log/slog"
	"net/http"

	"codeberg.org/miekg/dns"
	"linum/internal/cache"
)

func (s *Server) dohHandler(w http.ResponseWriter, r *http.Request) {
	clientIP := parseHTTPClientIP(r.RemoteAddr)
	if !s.isAllowed(clientIP) {
		slog.Warn("doh query denied by ACL", "client", clientIP)
		http.Error(w, "refused", http.StatusForbidden)
		return
	}
	if !s.rateLimit(clientIP.String()) {
		slog.Warn("doh query rate limited", "client", clientIP)
		http.Error(w, "too many requests", http.StatusTooManyRequests)
		return
	}

	var raw []byte

	switch r.Method {
	case http.MethodPost:
		ct := r.Header.Get("Content-Type")
		if ct != "application/dns-message" {
			http.Error(w, "unsupported content type", http.StatusUnsupportedMediaType)
			return
		}
		body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 65535))
		if err != nil {
			http.Error(w, "read body", http.StatusBadRequest)
			return
		}
		raw = body
	case http.MethodGet:
		param := r.URL.Query().Get("dns")
		if param == "" {
			http.Error(w, "missing dns param", http.StatusBadRequest)
			return
		}
		decoded, err := base64.RawURLEncoding.DecodeString(param)
		if err != nil {
			http.Error(w, "invalid base64url", http.StatusBadRequest)
			return
		}
		raw = decoded
	default:
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}

	msg := new(dns.Msg)
	msg.Data = raw
	if err := msg.Unpack(); err != nil {
		http.Error(w, "invalid dns message", http.StatusBadRequest)
		return
	}

	if len(msg.Question) == 0 {
		http.Error(w, "no question", http.StatusBadRequest)
		return
	}

	q := msg.Question[0]
	qname := q.Header().Name
	qtype := dns.RRToType(q)
	qclass := q.Header().Class

	if s.blocklist != nil && s.blocklist.IsBlocked(qname) {
		httpWriteMsg(w, s.blockedResponse(msg))
		slog.Debug("doh query served",
			"qname", qname,
			"qtype", dns.TypeToString[qtype],
			"blocked", true,
		)
		return
	}

	if s.cache != nil {
		key := cache.Key{Name: qname, Qtype: qtype, Class: qclass}
		if packed, ok := s.cache.Get(key); ok {
			packed[0] = byte(msg.ID >> 8)
			packed[1] = byte(msg.ID)
			w.Header().Set("Content-Type", "application/dns-message")
			w.Header().Set("Cache-Control", "no-cache, max-age=0")
			w.Write(packed)
			slog.Debug("doh query served",
				"qname", qname,
				"qtype", dns.TypeToString[qtype],
				"cached", true,
			)
			return
		}
	}

	resp, _ := s.buildResponse(msg)
	httpWriteMsg(w, resp)
	slog.Debug("doh query served",
		"qname", qname,
		"qtype", dns.TypeToString[qtype],
		"rcode", dns.RcodeToString[resp.Rcode],
	)
}

func httpWriteMsg(w http.ResponseWriter, msg *dns.Msg) {
	if err := msg.Pack(); err != nil {
		http.Error(w, "pack response", http.StatusInternalServerError)
		return
	}
	w.Header().Set("Content-Type", "application/dns-message")
	w.Header().Set("Cache-Control", "no-cache, max-age=0")
	if _, err := w.Write(msg.Data); err != nil {
		slog.Error("doh write failed", "err", err)
	}
}