/src/curl/lib/http_proxy.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | |
26 | | #include "http_proxy.h" |
27 | | |
28 | | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_PROXY) |
29 | | |
30 | | #include "curl_trc.h" |
31 | | #include "http.h" |
32 | | #include "url.h" |
33 | | #include "cfilters.h" |
34 | | #include "cf-h1-proxy.h" |
35 | | #include "cf-h2-proxy.h" |
36 | | #include "connect.h" |
37 | | #include "vauth/vauth.h" |
38 | | #include "vquic/vquic.h" |
39 | | #include "curlx/strparse.h" |
40 | | |
41 | | static CURLcode dynhds_add_custom(struct Curl_easy *data, |
42 | | bool is_connect, int httpversion, |
43 | | bool is_udp, struct dynhds *hds) |
44 | 0 | { |
45 | 0 | struct connectdata *conn = data->conn; |
46 | 0 | struct curl_slist *h[2]; |
47 | 0 | struct curl_slist *headers; |
48 | 0 | int numlists = 1; /* by default */ |
49 | 0 | int i; |
50 | |
|
51 | 0 | enum Curl_proxy_use proxy; |
52 | |
|
53 | 0 | if(is_connect && !is_udp) |
54 | 0 | proxy = HEADER_CONNECT; |
55 | 0 | else if(is_connect && is_udp) |
56 | 0 | proxy = HEADER_CONNECT_UDP; |
57 | 0 | else |
58 | 0 | proxy = conn->bits.origin_is_proxy ? HEADER_PROXY : HEADER_SERVER; |
59 | |
|
60 | 0 | switch(proxy) { |
61 | 0 | case HEADER_SERVER: |
62 | 0 | h[0] = data->set.headers; |
63 | 0 | break; |
64 | 0 | case HEADER_PROXY: |
65 | 0 | h[0] = data->set.headers; |
66 | 0 | if(data->set.sep_headers) { |
67 | 0 | h[1] = data->set.proxyheaders; |
68 | 0 | numlists++; |
69 | 0 | } |
70 | 0 | break; |
71 | 0 | case HEADER_CONNECT: |
72 | 0 | if(data->set.sep_headers) |
73 | 0 | h[0] = data->set.proxyheaders; |
74 | 0 | else |
75 | 0 | h[0] = data->set.headers; |
76 | 0 | break; |
77 | 0 | case HEADER_CONNECT_UDP: |
78 | 0 | if(data->set.sep_headers) |
79 | 0 | h[0] = data->set.proxyheaders; |
80 | 0 | else |
81 | 0 | h[0] = data->set.headers; |
82 | 0 | break; |
83 | 0 | } |
84 | | |
85 | | /* loop through one or two lists */ |
86 | 0 | for(i = 0; i < numlists; i++) { |
87 | 0 | for(headers = h[i]; headers; headers = headers->next) { |
88 | 0 | struct Curl_str name; |
89 | 0 | const char *value = NULL; |
90 | 0 | size_t valuelen = 0; |
91 | 0 | const char *ptr = headers->data; |
92 | | |
93 | | /* There are 2 quirks in place for custom headers: |
94 | | * 1. setting only 'name:' to suppress a header from being sent |
95 | | * 2. setting only 'name;' to send an empty (illegal) header |
96 | | */ |
97 | 0 | if(!curlx_str_cspn(&ptr, &name, ";:")) { |
98 | 0 | if(!curlx_str_single(&ptr, ':')) { |
99 | 0 | curlx_str_passblanks(&ptr); |
100 | 0 | if(*ptr) { |
101 | 0 | value = ptr; |
102 | 0 | valuelen = strlen(value); |
103 | 0 | } |
104 | 0 | else { |
105 | | /* quirk #1, suppress this header */ |
106 | 0 | continue; |
107 | 0 | } |
108 | 0 | } |
109 | 0 | else if(!curlx_str_single(&ptr, ';')) { |
110 | 0 | curlx_str_passblanks(&ptr); |
111 | 0 | if(!*ptr) { |
112 | | /* quirk #2, send an empty header */ |
113 | 0 | value = ""; |
114 | 0 | valuelen = 0; |
115 | 0 | } |
116 | 0 | else { |
117 | | /* this may be used for something else in the future, |
118 | | * ignore this for now */ |
119 | 0 | continue; |
120 | 0 | } |
121 | 0 | } |
122 | 0 | else |
123 | | /* neither : nor ; in provided header value. We ignore this |
124 | | * silently */ |
125 | 0 | continue; |
126 | 0 | } |
127 | 0 | else |
128 | | /* no name, move on */ |
129 | 0 | continue; |
130 | | |
131 | 0 | DEBUGASSERT(curlx_strlen(&name) && value); |
132 | | /* trim surrounding whitespace so a padded field name (e.g. |
133 | | `Authorization :`) cannot slip past the Authorization/Cookie check */ |
134 | 0 | curlx_str_trimblanks(&name); |
135 | 0 | if(data->state.aptr.host && |
136 | | /* a Host: header was sent already, do not pass on any custom Host: |
137 | | header as that will produce *two* in the same request! */ |
138 | 0 | curlx_str_casecompare(&name, "Host")) |
139 | 0 | ; |
140 | 0 | else if(data->state.httpreq == HTTPREQ_POST_FORM && |
141 | | /* this header (extended by formdata.c) is sent later */ |
142 | 0 | curlx_str_casecompare(&name, "Content-Type")) |
143 | 0 | ; |
144 | 0 | else if(data->state.httpreq == HTTPREQ_POST_MIME && |
145 | | /* this header is sent later */ |
146 | 0 | curlx_str_casecompare(&name, "Content-Type")) |
147 | 0 | ; |
148 | 0 | else if(data->req.authneg && |
149 | | /* while doing auth neg, do not allow the custom length since |
150 | | we will force length zero then */ |
151 | 0 | curlx_str_casecompare(&name, "Content-Length")) |
152 | 0 | ; |
153 | 0 | else if((httpversion >= 20) && |
154 | 0 | curlx_str_casecompare(&name, "Transfer-Encoding")) |
155 | 0 | ; |
156 | | /* HTTP/2 and HTTP/3 do not support chunked requests */ |
157 | 0 | else if((curlx_str_casecompare(&name, "Authorization") || |
158 | 0 | curlx_str_casecompare(&name, "Cookie")) && |
159 | | /* be careful of sending this potentially sensitive header to |
160 | | other hosts */ |
161 | 0 | !Curl_auth_allowed_to_host(data)) |
162 | 0 | ; |
163 | 0 | else { |
164 | 0 | CURLcode result = |
165 | 0 | Curl_dynhds_add(hds, curlx_str(&name), curlx_strlen(&name), |
166 | 0 | value, valuelen); |
167 | 0 | if(result) |
168 | 0 | return result; |
169 | 0 | } |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | 0 | return CURLE_OK; |
174 | 0 | } |
175 | | |
176 | | struct cf_proxy_ctx { |
177 | | struct Curl_peer *peer; /* proxy */ |
178 | | struct Curl_peer *tunnel_peer; /* tunnel destination */ |
179 | | uint8_t proxytype; |
180 | | uint8_t tunnel_transport; |
181 | | BIT(sub_filter_installed); |
182 | | }; |
183 | | |
184 | | static int proxy_http_ver_major(proxy_http_ver ver) |
185 | 0 | { |
186 | 0 | switch(ver) { |
187 | 0 | case PROXY_HTTP_V1: |
188 | 0 | return 11; |
189 | 0 | case PROXY_HTTP_V2: |
190 | 0 | return 20; |
191 | 0 | case PROXY_HTTP_V3: |
192 | 0 | return 30; |
193 | 0 | } |
194 | 0 | return 0; |
195 | 0 | } |
196 | | |
197 | | static CURLcode http_proxy_create_CONNECT(struct httpreq **preq, |
198 | | struct Curl_cfilter *cf, |
199 | | struct Curl_easy *data, |
200 | | struct Curl_peer *dest, |
201 | | proxy_http_ver ver) |
202 | 0 | { |
203 | 0 | char *authority = NULL; |
204 | 0 | int httpversion = proxy_http_ver_major(ver); |
205 | 0 | CURLcode result; |
206 | 0 | struct httpreq *req = NULL; |
207 | |
|
208 | 0 | authority = curl_maprintf("%s%s%s:%u", |
209 | 0 | dest->ipv6 ? "[" : "", |
210 | 0 | dest->hostname, |
211 | 0 | dest->ipv6 ? "]" : "", |
212 | 0 | dest->port); |
213 | 0 | if(!authority) { |
214 | 0 | result = CURLE_OUT_OF_MEMORY; |
215 | 0 | goto out; |
216 | 0 | } |
217 | | |
218 | 0 | result = Curl_http_req_make(&req, "CONNECT", CURL_CSTRLEN("CONNECT"), |
219 | 0 | NULL, 0, authority, strlen(authority), |
220 | 0 | NULL, 0); |
221 | 0 | if(result) |
222 | 0 | goto out; |
223 | | |
224 | | /* Setup the proxy-authorization header, if any */ |
225 | 0 | result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET, |
226 | 0 | req->authority, NULL, TRUE); |
227 | 0 | if(result) |
228 | 0 | goto out; |
229 | | |
230 | | /* If user is not overriding Host: header, we add for HTTP/1.x */ |
231 | 0 | if(ver == PROXY_HTTP_V1 && |
232 | 0 | !Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) { |
233 | 0 | result = Curl_dynhds_cadd(&req->headers, "Host", authority); |
234 | 0 | if(result) |
235 | 0 | goto out; |
236 | 0 | } |
237 | | |
238 | 0 | if(data->req.hd_proxy_auth) { |
239 | 0 | result = Curl_dynhds_h1_cadd_line(&req->headers, |
240 | 0 | data->req.hd_proxy_auth); |
241 | 0 | if(result) |
242 | 0 | goto out; |
243 | 0 | } |
244 | | |
245 | 0 | if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) && |
246 | 0 | data->set.str[STRING_USERAGENT] && *data->set.str[STRING_USERAGENT]) { |
247 | 0 | result = Curl_dynhds_cadd(&req->headers, "User-Agent", |
248 | 0 | data->set.str[STRING_USERAGENT]); |
249 | 0 | if(result) |
250 | 0 | goto out; |
251 | 0 | } |
252 | | |
253 | 0 | if(ver == PROXY_HTTP_V1 && |
254 | 0 | !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) { |
255 | 0 | result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive"); |
256 | 0 | if(result) |
257 | 0 | goto out; |
258 | 0 | } |
259 | | |
260 | 0 | result = dynhds_add_custom(data, TRUE, httpversion, |
261 | 0 | FALSE, &req->headers); |
262 | |
|
263 | 0 | out: |
264 | 0 | if(result && req) { |
265 | 0 | Curl_http_req_free(req); |
266 | 0 | req = NULL; |
267 | 0 | } |
268 | 0 | curlx_free(authority); |
269 | 0 | *preq = req; |
270 | 0 | return result; |
271 | 0 | } |
272 | | |
273 | | static CURLcode http_proxy_create_CONNECTUDP(struct httpreq **preq, |
274 | | struct Curl_cfilter *cf, |
275 | | struct Curl_easy *data, |
276 | | struct Curl_peer *dest, |
277 | | proxy_http_ver ver) |
278 | 0 | { |
279 | 0 | const char *proxy_scheme = "http"; |
280 | 0 | const char *proxy_host = cf->conn->http_proxy.peer->hostname; |
281 | 0 | int httpversion = proxy_http_ver_major(ver); |
282 | 0 | char *authority = NULL; |
283 | 0 | char *path = NULL; |
284 | 0 | char *encoded_host = NULL; |
285 | 0 | struct httpreq *req = NULL; |
286 | 0 | bool proxy_ipv6_ip; |
287 | 0 | CURLcode result; |
288 | |
|
289 | 0 | if(cf->conn->http_proxy.proxytype == CURLPROXY_HTTPS || |
290 | 0 | cf->conn->http_proxy.proxytype == CURLPROXY_HTTPS2 || |
291 | 0 | cf->conn->http_proxy.proxytype == CURLPROXY_HTTPS3) |
292 | 0 | proxy_scheme = "https"; |
293 | |
|
294 | 0 | proxy_ipv6_ip = cf->conn->http_proxy.peer->ipv6 != 0; |
295 | |
|
296 | 0 | authority = curl_maprintf("%s%s%s:%d", |
297 | 0 | proxy_ipv6_ip ? "[" : "", |
298 | 0 | proxy_host, |
299 | 0 | proxy_ipv6_ip ? "]" : "", |
300 | 0 | cf->conn->http_proxy.peer->port); |
301 | 0 | if(!authority) { |
302 | 0 | result = CURLE_OUT_OF_MEMORY; |
303 | 0 | goto out; |
304 | 0 | } |
305 | | |
306 | 0 | if(dest->ipv6) { |
307 | | /* RFC 9298: colons in IPv6 addresses MUST be percent-encoded |
308 | | * in the URI template (e.g. "2001:db8::1" -> "2001%3Adb8%3A%3A1") */ |
309 | 0 | const char *s = dest->hostname; |
310 | 0 | char *d; |
311 | 0 | size_t hlen = strlen(s); |
312 | 0 | encoded_host = curlx_malloc(hlen * 3 + 1); |
313 | 0 | if(!encoded_host) { |
314 | 0 | result = CURLE_OUT_OF_MEMORY; |
315 | 0 | goto out; |
316 | 0 | } |
317 | 0 | d = encoded_host; |
318 | 0 | while(*s) { |
319 | 0 | if(*s == ':') { |
320 | 0 | *d++ = '%'; |
321 | 0 | *d++ = '3'; |
322 | 0 | *d++ = 'A'; |
323 | 0 | } |
324 | 0 | else |
325 | 0 | *d++ = *s; |
326 | 0 | s++; |
327 | 0 | } |
328 | 0 | *d = '\0'; |
329 | 0 | path = curl_maprintf("/.well-known/masque/udp/%s/%u/", |
330 | 0 | encoded_host, (unsigned int)dest->port); |
331 | 0 | } |
332 | 0 | else { |
333 | 0 | path = curl_maprintf("/.well-known/masque/udp/%s/%u/", |
334 | 0 | dest->hostname, (unsigned int)dest->port); |
335 | 0 | } |
336 | | |
337 | 0 | if(!path) { |
338 | 0 | result = CURLE_OUT_OF_MEMORY; |
339 | 0 | goto out; |
340 | 0 | } |
341 | | |
342 | 0 | if(ver == PROXY_HTTP_V1) { |
343 | 0 | result = Curl_http_req_make(&req, "GET", CURL_CSTRLEN("GET"), |
344 | 0 | proxy_scheme, strlen(proxy_scheme), |
345 | 0 | authority, strlen(authority), |
346 | 0 | path, strlen(path)); |
347 | 0 | if(result) |
348 | 0 | goto out; |
349 | 0 | } |
350 | 0 | else if(ver == PROXY_HTTP_V2 || ver == PROXY_HTTP_V3) { |
351 | 0 | result = Curl_http_req_make(&req, "CONNECT", CURL_CSTRLEN("CONNECT"), |
352 | 0 | proxy_scheme, strlen(proxy_scheme), |
353 | 0 | authority, strlen(authority), |
354 | 0 | path, strlen(path)); |
355 | 0 | if(result) |
356 | 0 | goto out; |
357 | 0 | } |
358 | 0 | else { |
359 | 0 | result = CURLE_FAILED_INIT; |
360 | 0 | goto out; |
361 | 0 | } |
362 | | |
363 | | /* Setup the proxy-authorization header, if any */ |
364 | 0 | result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET, |
365 | 0 | req->authority, NULL, TRUE); |
366 | 0 | if(result) |
367 | 0 | goto out; |
368 | | |
369 | | /* If user is not overriding Host: header, we add for HTTP/1.x */ |
370 | 0 | if(ver == PROXY_HTTP_V1 && |
371 | 0 | !Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) { |
372 | 0 | result = Curl_dynhds_cadd(&req->headers, "Host", authority); |
373 | 0 | if(result) |
374 | 0 | goto out; |
375 | 0 | } |
376 | | |
377 | 0 | if(data->req.hd_proxy_auth) { |
378 | 0 | result = Curl_dynhds_h1_cadd_line(&req->headers, |
379 | 0 | data->req.hd_proxy_auth); |
380 | 0 | if(result) |
381 | 0 | goto out; |
382 | 0 | } |
383 | | |
384 | 0 | if(ver == PROXY_HTTP_V1 && |
385 | 0 | !Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) && |
386 | 0 | data->set.str[STRING_USERAGENT] && *data->set.str[STRING_USERAGENT]) { |
387 | 0 | result = Curl_dynhds_cadd(&req->headers, "User-Agent", |
388 | 0 | data->set.str[STRING_USERAGENT]); |
389 | 0 | if(result) |
390 | 0 | goto out; |
391 | 0 | } |
392 | | |
393 | 0 | if(ver == PROXY_HTTP_V1 && |
394 | 0 | !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) { |
395 | 0 | result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive"); |
396 | 0 | if(result) |
397 | 0 | goto out; |
398 | 0 | } |
399 | | |
400 | 0 | if(ver == PROXY_HTTP_V1) { |
401 | 0 | result = Curl_dynhds_cadd(&req->headers, "Connection", "Upgrade"); |
402 | 0 | if(result) |
403 | 0 | goto out; |
404 | | |
405 | 0 | result = Curl_dynhds_cadd(&req->headers, "Upgrade", "connect-udp"); |
406 | 0 | if(result) |
407 | 0 | goto out; |
408 | | |
409 | 0 | result = Curl_dynhds_cadd(&req->headers, "Capsule-Protocol", "?1"); |
410 | 0 | if(result) |
411 | 0 | goto out; |
412 | 0 | } |
413 | 0 | else { |
414 | 0 | result = Curl_dynhds_cadd(&req->headers, ":Protocol", "connect-udp"); |
415 | 0 | if(result) |
416 | 0 | goto out; |
417 | | |
418 | 0 | if(ver >= PROXY_HTTP_V2) { |
419 | 0 | result = Curl_dynhds_cadd(&req->headers, "Capsule-Protocol", "?1"); |
420 | 0 | if(result) |
421 | 0 | goto out; |
422 | 0 | } |
423 | 0 | } |
424 | | |
425 | 0 | result = dynhds_add_custom(data, TRUE, httpversion, |
426 | 0 | TRUE, &req->headers); |
427 | |
|
428 | 0 | out: |
429 | 0 | if(result && req) { |
430 | 0 | Curl_http_req_free(req); |
431 | 0 | req = NULL; |
432 | 0 | } |
433 | 0 | curlx_free(authority); |
434 | 0 | curlx_free(path); |
435 | 0 | curlx_free(encoded_host); |
436 | 0 | *preq = req; |
437 | 0 | return result; |
438 | 0 | } |
439 | | |
440 | | CURLcode Curl_http_proxy_create_tunnel_request( |
441 | | struct httpreq **preq, struct Curl_cfilter *cf, |
442 | | struct Curl_easy *data, struct Curl_peer *dest, |
443 | | proxy_http_ver ver, bool udp_tunnel) |
444 | 0 | { |
445 | 0 | CURLcode result; |
446 | |
|
447 | 0 | if(udp_tunnel) |
448 | 0 | result = http_proxy_create_CONNECTUDP(preq, cf, data, dest, ver); |
449 | 0 | else |
450 | 0 | result = http_proxy_create_CONNECT(preq, cf, data, dest, ver); |
451 | 0 | if(result) |
452 | 0 | return result; |
453 | | |
454 | 0 | if(udp_tunnel) |
455 | 0 | infof(data, "Establishing %s proxy UDP tunnel to %s:%u", |
456 | 0 | (ver == PROXY_HTTP_V2) ? "HTTP/2" : |
457 | 0 | (ver == PROXY_HTTP_V3) ? "HTTP/3" : "HTTP", |
458 | 0 | dest->user_hostname, dest->port); |
459 | 0 | else |
460 | 0 | infof(data, "Establishing %s proxy tunnel to %s", |
461 | 0 | (ver == PROXY_HTTP_V2) ? "HTTP/2" : |
462 | 0 | (ver == PROXY_HTTP_V3) ? "HTTP/3" : "HTTP", |
463 | 0 | (*preq)->authority); |
464 | 0 | return CURLE_OK; |
465 | 0 | } |
466 | | |
467 | | CURLcode Curl_http_proxy_inspect_tunnel_response( |
468 | | struct Curl_cfilter *cf, struct Curl_easy *data, |
469 | | struct http_resp *resp, bool udp_tunnel, |
470 | | proxy_inspect_result *presult) |
471 | 0 | { |
472 | 0 | struct dynhds_entry *capsule_protocol = NULL; |
473 | 0 | struct dynhds_entry *auth_reply = NULL; |
474 | 0 | size_t i, header_count; |
475 | 0 | CURLcode result = CURLE_OK; |
476 | |
|
477 | 0 | DEBUGASSERT(resp); |
478 | |
|
479 | 0 | header_count = Curl_dynhds_count(&resp->headers); |
480 | 0 | if(udp_tunnel) |
481 | 0 | infof(data, "CONNECT-UDP Response Status %d", resp->status); |
482 | 0 | else |
483 | 0 | infof(data, "CONNECT Response Status %d", resp->status); |
484 | 0 | infof(data, "Response Headers (%zu total):", header_count); |
485 | 0 | for(i = 0; i < header_count; i++) { |
486 | 0 | struct dynhds_entry *entry = Curl_dynhds_getn(&resp->headers, i); |
487 | 0 | if(entry) |
488 | 0 | infof(data, " %s: %s", entry->name, entry->value); |
489 | 0 | } |
490 | |
|
491 | 0 | if(resp->status == 401) { |
492 | 0 | auth_reply = Curl_dynhds_cget(&resp->headers, "WWW-Authenticate"); |
493 | 0 | } |
494 | 0 | else if(resp->status == 407) { |
495 | 0 | auth_reply = Curl_dynhds_cget(&resp->headers, "Proxy-Authenticate"); |
496 | 0 | } |
497 | |
|
498 | 0 | if(auth_reply) { |
499 | 0 | CURL_TRC_CF(data, cf, "[0] CONNECT%s: fwd auth header '%s'", |
500 | 0 | udp_tunnel ? "-UDP" : "", auth_reply->value); |
501 | 0 | result = Curl_http_input_auth(data, resp->status == 407, |
502 | 0 | auth_reply->value); |
503 | 0 | if(result) |
504 | 0 | return result; |
505 | 0 | if(data->req.newurl) { |
506 | 0 | curlx_safefree(data->req.newurl); |
507 | 0 | *presult = PROXY_INSPECT_AUTH_RETRY; |
508 | 0 | return CURLE_OK; |
509 | 0 | } |
510 | 0 | } |
511 | | |
512 | 0 | if(udp_tunnel) { |
513 | 0 | if(resp->status / 100 == 2) { |
514 | 0 | capsule_protocol = Curl_dynhds_cget(&resp->headers, |
515 | 0 | "capsule-protocol"); |
516 | 0 | if(capsule_protocol) { |
517 | 0 | if(!strncmp(capsule_protocol->value, "?1", 2) && |
518 | 0 | !capsule_protocol->value[2]) { |
519 | 0 | infof(data, "CONNECT-UDP tunnel established, response %d", |
520 | 0 | resp->status); |
521 | 0 | *presult = PROXY_INSPECT_OK; |
522 | 0 | return CURLE_OK; |
523 | 0 | } |
524 | 0 | failf(data, "Failed to establish CONNECT-UDP tunnel, response %d, " |
525 | 0 | "unsupported capsule-protocol value '%s'", |
526 | 0 | resp->status, capsule_protocol->value); |
527 | 0 | *presult = PROXY_INSPECT_FAILED; |
528 | 0 | return CURLE_COULDNT_CONNECT; |
529 | 0 | } |
530 | 0 | else { |
531 | | /* NOTE proxies may not set capsule protocol in the headers */ |
532 | 0 | infof(data, "CONNECT-UDP tunnel established, response %d " |
533 | 0 | "but no capsule-protocol header found", resp->status); |
534 | 0 | *presult = PROXY_INSPECT_OK; |
535 | 0 | return CURLE_OK; |
536 | 0 | } |
537 | 0 | } |
538 | 0 | else { |
539 | 0 | failf(data, "Failed to establish CONNECT-UDP tunnel, " |
540 | 0 | "response %d", resp->status); |
541 | 0 | *presult = PROXY_INSPECT_FAILED; |
542 | 0 | return CURLE_COULDNT_CONNECT; |
543 | 0 | } |
544 | 0 | } |
545 | | |
546 | 0 | if(resp->status / 100 == 2) { |
547 | 0 | infof(data, "CONNECT tunnel established, response %d", resp->status); |
548 | 0 | *presult = PROXY_INSPECT_OK; |
549 | 0 | return CURLE_OK; |
550 | 0 | } |
551 | | |
552 | 0 | *presult = PROXY_INSPECT_FAILED; |
553 | 0 | return CURLE_COULDNT_CONNECT; |
554 | 0 | } |
555 | | |
556 | | static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf, |
557 | | struct Curl_easy *data, |
558 | | bool *done) |
559 | 0 | { |
560 | 0 | struct cf_proxy_ctx *ctx = cf->ctx; |
561 | 0 | CURLcode result; |
562 | 0 | bool udp_tunnel = TRNSPRT_IS_DGRAM(ctx->tunnel_transport); |
563 | 0 | const char *tunnel_type = udp_tunnel ? "CONNECT-UDP" : "CONNECT"; |
564 | |
|
565 | 0 | if(cf->connected) { |
566 | 0 | *done = TRUE; |
567 | 0 | return CURLE_OK; |
568 | 0 | } |
569 | | |
570 | 0 | CURL_TRC_CF(data, cf, "%s", tunnel_type); |
571 | 0 | connect_sub: |
572 | | /* in case of h3_proxy, cf->next will be NULL initially */ |
573 | 0 | if(cf->next) { |
574 | 0 | result = cf->next->cft->do_connect(cf->next, data, done); |
575 | 0 | if(result || !*done) |
576 | 0 | return result; |
577 | 0 | } |
578 | | |
579 | 0 | *done = FALSE; |
580 | 0 | if(!ctx->sub_filter_installed) { |
581 | 0 | const char *alpn = NULL; |
582 | | |
583 | | /* in case of h3_proxy, cf->next will be NULL initially */ |
584 | 0 | if(cf->next) { |
585 | 0 | alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data); |
586 | 0 | } |
587 | |
|
588 | 0 | if(alpn) |
589 | 0 | infof(data, "%s: '%s' negotiated", tunnel_type, alpn); |
590 | 0 | else if(!alpn) { |
591 | | /* No ALPN, proxytype rules. Fake ALPN */ |
592 | 0 | infof(data, "%s: no ALPN negotiated", tunnel_type); |
593 | 0 | switch(ctx->proxytype) { |
594 | 0 | case CURLPROXY_HTTP_1_0: |
595 | 0 | alpn = "http/1.0"; |
596 | 0 | break; |
597 | 0 | case CURLPROXY_HTTPS2: |
598 | 0 | alpn = "h2"; |
599 | 0 | break; |
600 | 0 | case CURLPROXY_HTTPS3: |
601 | 0 | alpn = "h3"; |
602 | 0 | break; |
603 | 0 | default: |
604 | 0 | alpn = "http/1.1"; |
605 | 0 | break; |
606 | 0 | } |
607 | 0 | } |
608 | | |
609 | 0 | if(!strcmp(alpn, "http/1.0")) { |
610 | 0 | CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.0"); |
611 | 0 | result = Curl_cf_h1_proxy_insert_after(cf, data, ctx->tunnel_peer, 10, |
612 | 0 | udp_tunnel); |
613 | 0 | if(result) |
614 | 0 | goto out; |
615 | 0 | } |
616 | 0 | else if(!strcmp(alpn, "http/1.1")) { |
617 | 0 | int httpversion = (ctx->proxytype == CURLPROXY_HTTP_1_0) ? 10 : 11; |
618 | 0 | CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.%d", |
619 | 0 | httpversion % 10); |
620 | 0 | result = Curl_cf_h1_proxy_insert_after(cf, data, ctx->tunnel_peer, |
621 | 0 | httpversion, udp_tunnel); |
622 | 0 | if(result) |
623 | 0 | goto out; |
624 | 0 | } |
625 | 0 | #ifdef USE_NGHTTP2 |
626 | 0 | else if(!strcmp(alpn, "h2")) { |
627 | 0 | CURL_TRC_CF(data, cf, "installing subfilter for HTTP/2"); |
628 | 0 | result = Curl_cf_h2_proxy_insert_after(cf, data, ctx->tunnel_peer, |
629 | 0 | udp_tunnel); |
630 | 0 | if(result) |
631 | 0 | goto out; |
632 | 0 | } |
633 | 0 | #endif /* USE_NGHTTP2 */ |
634 | | #if defined(USE_PROXY_HTTP3) && defined(USE_NGHTTP3) && \ |
635 | | defined(USE_NGTCP2) && defined(USE_OPENSSL) |
636 | | else if(!strcmp(alpn, "h3")) { |
637 | | CURL_TRC_CF(data, cf, "installing subfilter for HTTP/3"); |
638 | | result = Curl_cf_h3_proxy_insert_after(cf, data, ctx->peer, ctx->peer, |
639 | | ctx->tunnel_peer, |
640 | | ctx->tunnel_transport); |
641 | | if(result) |
642 | | goto out; |
643 | | } |
644 | | #endif /* USE_PROXY_HTTP3 && USE_NGHTTP3 && USE_NGTCP2 && USE_OPENSSL */ |
645 | 0 | else { |
646 | 0 | failf(data, "%s: negotiated ALPN '%s' not supported", tunnel_type, alpn); |
647 | 0 | result = CURLE_COULDNT_CONNECT; |
648 | 0 | goto out; |
649 | 0 | } |
650 | | |
651 | 0 | ctx->sub_filter_installed = TRUE; |
652 | | /* after we installed the filter "below" us, we call connect |
653 | | * on out sub-chain again. |
654 | | */ |
655 | 0 | goto connect_sub; |
656 | 0 | } |
657 | 0 | else { |
658 | | /* subchain connected and we had already installed the protocol filter. |
659 | | * This means the protocol tunnel is established, we are done. */ |
660 | 0 | DEBUGASSERT(ctx->sub_filter_installed); |
661 | 0 | result = CURLE_OK; |
662 | 0 | } |
663 | | |
664 | 0 | out: |
665 | 0 | if(!result) { |
666 | 0 | cf->connected = TRUE; |
667 | 0 | *done = TRUE; |
668 | 0 | } |
669 | 0 | return result; |
670 | 0 | } |
671 | | |
672 | | static CURLcode cf_http_proxy_query(struct Curl_cfilter *cf, |
673 | | struct Curl_easy *data, |
674 | | int query, int *pres1, void *pres2) |
675 | 0 | { |
676 | 0 | struct cf_proxy_ctx *ctx = cf->ctx; |
677 | 0 | switch(query) { |
678 | 0 | case CF_QUERY_HOST_PORT: |
679 | 0 | *pres1 = (int)ctx->tunnel_peer->port; |
680 | 0 | *((const char **)pres2) = ctx->tunnel_peer->hostname; |
681 | 0 | return CURLE_OK; |
682 | 0 | case CF_QUERY_ALPN_NEGOTIATED: { |
683 | 0 | const char **palpn = pres2; |
684 | 0 | DEBUGASSERT(palpn); |
685 | 0 | *palpn = NULL; |
686 | 0 | return CURLE_OK; |
687 | 0 | } |
688 | 0 | default: |
689 | 0 | break; |
690 | 0 | } |
691 | 0 | return cf->next ? |
692 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
693 | 0 | CURLE_UNKNOWN_OPTION; |
694 | 0 | } |
695 | | |
696 | | static void cf_https_proxy_ctx_free(struct cf_proxy_ctx *ctx) |
697 | 0 | { |
698 | 0 | if(ctx) { |
699 | 0 | Curl_peer_unlink(&ctx->peer); |
700 | 0 | Curl_peer_unlink(&ctx->tunnel_peer); |
701 | 0 | curlx_free(ctx); |
702 | 0 | } |
703 | 0 | } |
704 | | |
705 | | static void http_proxy_cf_destroy(struct Curl_cfilter *cf, |
706 | | struct Curl_easy *data) |
707 | 0 | { |
708 | 0 | struct cf_proxy_ctx *ctx = cf->ctx; |
709 | 0 | if(ctx) { |
710 | 0 | CURL_TRC_CF(data, cf, "destroy"); |
711 | 0 | cf_https_proxy_ctx_free(ctx); |
712 | 0 | } |
713 | 0 | } |
714 | | |
715 | | struct Curl_cftype Curl_cft_http_proxy = { |
716 | | "HTTP-PROXY", |
717 | | CF_TYPE_IP_CONNECT | CF_TYPE_PROXY | CF_TYPE_SETUP, |
718 | | 0, |
719 | | http_proxy_cf_destroy, |
720 | | http_proxy_cf_connect, |
721 | | Curl_cf_def_shutdown, |
722 | | Curl_cf_def_adjust_pollset, |
723 | | Curl_cf_def_data_pending, |
724 | | Curl_cf_def_send, |
725 | | Curl_cf_def_recv, |
726 | | Curl_cf_def_cntrl, |
727 | | Curl_cf_def_conn_is_alive, |
728 | | Curl_cf_def_conn_keep_alive, |
729 | | cf_http_proxy_query, |
730 | | }; |
731 | | |
732 | | CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at, |
733 | | struct Curl_easy *data, |
734 | | struct Curl_peer *peer, |
735 | | struct Curl_peer *tunnel_peer, |
736 | | uint8_t tunnel_transport, |
737 | | uint8_t proxytype) |
738 | 0 | { |
739 | 0 | struct Curl_cfilter *cf; |
740 | 0 | struct cf_proxy_ctx *ctx = NULL; |
741 | 0 | CURLcode result; |
742 | |
|
743 | 0 | (void)data; |
744 | 0 | if(!peer || !tunnel_peer) |
745 | 0 | return CURLE_FAILED_INIT; |
746 | | |
747 | 0 | ctx = curlx_calloc(1, sizeof(*ctx)); |
748 | 0 | if(!ctx) { |
749 | 0 | result = CURLE_OUT_OF_MEMORY; |
750 | 0 | goto out; |
751 | 0 | } |
752 | 0 | Curl_peer_link(&ctx->peer, peer); |
753 | 0 | Curl_peer_link(&ctx->tunnel_peer, tunnel_peer); |
754 | 0 | ctx->proxytype = proxytype; |
755 | 0 | ctx->tunnel_transport = tunnel_transport; |
756 | |
|
757 | 0 | result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx); |
758 | 0 | if(result) |
759 | 0 | goto out; |
760 | 0 | ctx = NULL; |
761 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
762 | |
|
763 | 0 | out: |
764 | 0 | cf_https_proxy_ctx_free(ctx); |
765 | 0 | return result; |
766 | 0 | } |
767 | | |
768 | | uint8_t Curl_http_proxy_transport(uint8_t proxytype) |
769 | 0 | { |
770 | 0 | switch(proxytype) { |
771 | 0 | case CURLPROXY_HTTPS3: |
772 | 0 | return TRNSPRT_QUIC; |
773 | 0 | default: |
774 | 0 | return TRNSPRT_TCP; |
775 | 0 | } |
776 | 0 | } |
777 | | |
778 | | #endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */ |