]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/doh.hh
Merge pull request #8469 from omoerbeek/auth-illegal-to-invalid
[thirdparty/pdns.git] / pdns / doh.hh
1 #pragma once
2 #include "iputils.hh"
3 #include "libssl.hh"
4
5 struct DOHServerConfig;
6
7 class DOHResponseMapEntry
8 {
9 public:
10 DOHResponseMapEntry(const std::string& regex, uint16_t status, const std::string& content, const boost::optional<std::vector<std::pair<std::string, std::string>>>& headers): d_regex(regex), d_customHeaders(headers), d_content(content), d_status(status)
11 {
12 }
13
14 bool matches(const std::string& path) const
15 {
16 return d_regex.match(path);
17 }
18
19 uint16_t getStatusCode() const
20 {
21 return d_status;
22 }
23
24 const std::string& getContent() const
25 {
26 return d_content;
27 }
28
29 const boost::optional<std::vector<std::pair<std::string, std::string>>>& getHeaders() const
30 {
31 return d_customHeaders;
32 }
33
34 private:
35 Regex d_regex;
36 boost::optional<std::vector<std::pair<std::string, std::string>>> d_customHeaders;
37 std::string d_content;
38 uint16_t d_status;
39 };
40
41 struct DOHFrontend
42 {
43 DOHFrontend()
44 {
45 }
46
47 std::shared_ptr<DOHServerConfig> d_dsc{nullptr};
48 std::vector<std::shared_ptr<DOHResponseMapEntry>> d_responsesMap;
49 TLSConfig d_tlsConfig;
50 TLSErrorCounters d_tlsCounters;
51 std::string d_serverTokens{"h2o/dnsdist"};
52 std::vector<std::pair<std::string, std::string>> d_customResponseHeaders;
53 ComboAddress d_local;
54
55 uint32_t d_idleTimeout{30}; // HTTP idle timeout in seconds
56 std::vector<std::string> d_urls;
57
58 std::atomic<uint64_t> d_httpconnects{0}; // number of TCP/IP connections established
59 std::atomic<uint64_t> d_getqueries{0}; // valid DNS queries received via GET
60 std::atomic<uint64_t> d_postqueries{0}; // valid DNS queries received via POST
61 std::atomic<uint64_t> d_badrequests{0}; // request could not be converted to dns query
62 std::atomic<uint64_t> d_errorresponses{0}; // dnsdist set 'error' on response
63 std::atomic<uint64_t> d_redirectresponses{0}; // dnsdist set 'redirect' on response
64 std::atomic<uint64_t> d_validresponses{0}; // valid responses sent out
65
66 struct HTTPVersionStats
67 {
68 std::atomic<uint64_t> d_nbQueries{0}; // valid DNS queries received
69 std::atomic<uint64_t> d_nb200Responses{0};
70 std::atomic<uint64_t> d_nb400Responses{0};
71 std::atomic<uint64_t> d_nb403Responses{0};
72 std::atomic<uint64_t> d_nb500Responses{0};
73 std::atomic<uint64_t> d_nb502Responses{0};
74 std::atomic<uint64_t> d_nbOtherResponses{0};
75 };
76
77 HTTPVersionStats d_http1Stats;
78 HTTPVersionStats d_http2Stats;
79
80 time_t getTicketsKeyRotationDelay() const
81 {
82 return d_tlsConfig.d_ticketsKeyRotationDelay;
83 }
84
85 #ifndef HAVE_DNS_OVER_HTTPS
86 void setup()
87 {
88 }
89
90 void reloadCertificates()
91 {
92 }
93
94 void rotateTicketsKey(time_t now)
95 {
96 }
97
98 void loadTicketsKeys(const std::string& keyFile)
99 {
100 }
101
102 void handleTicketsKeyRotation()
103 {
104 }
105
106 time_t getNextTicketsKeyRotation() const
107 {
108 return 0;
109 }
110
111 size_t getTicketsKeysCount() const
112 {
113 size_t res = 0;
114 return res;
115 }
116
117 #else
118 void setup();
119 void reloadCertificates();
120
121 void rotateTicketsKey(time_t now);
122 void loadTicketsKeys(const std::string& keyFile);
123 void handleTicketsKeyRotation();
124 time_t getNextTicketsKeyRotation() const;
125 size_t getTicketsKeysCount() const;
126 #endif /* HAVE_DNS_OVER_HTTPS */
127 };
128
129 #ifndef HAVE_DNS_OVER_HTTPS
130 struct DOHUnit
131 {
132 };
133
134 #else /* HAVE_DNS_OVER_HTTPS */
135 #include <unordered_map>
136
137 struct st_h2o_req_t;
138
139 struct DOHUnit
140 {
141 DOHUnit()
142 {
143 }
144 DOHUnit(const DOHUnit&) = delete;
145 DOHUnit& operator=(const DOHUnit&) = delete;
146
147 void get()
148 {
149 ++d_refcnt;
150 }
151
152 void release()
153 {
154 if (--d_refcnt == 0) {
155 delete this;
156 }
157 }
158
159 std::string query;
160 std::string response;
161 ComboAddress remote;
162 ComboAddress dest;
163 st_h2o_req_t* req{nullptr};
164 DOHUnit** self{nullptr};
165 std::string contentType;
166 std::atomic<uint64_t> d_refcnt{1};
167 int rsock;
168 uint16_t qtype;
169 /* the status_code is set from
170 processDOHQuery() (which is executed in
171 the DOH client thread) so that the correct
172 response can be sent in on_dnsdist(),
173 after the DOHUnit has been passed back to
174 the main DoH thread.
175 */
176 uint16_t status_code{200};
177 bool ednsAdded{false};
178
179 std::string getHTTPPath() const;
180 std::string getHTTPHost() const;
181 std::string getHTTPScheme() const;
182 std::string getHTTPQueryString() const;
183 std::unordered_map<std::string, std::string> getHTTPHeaders() const;
184 void setHTTPResponse(uint16_t statusCode, const std::string& body, const std::string& contentType="");
185 };
186
187 #endif /* HAVE_DNS_OVER_HTTPS */
188
189 void handleDOHTimeout(DOHUnit* oldDU);