ws2tcp-local 0.1.0

Local HTTP proxy client for ws2tcp-router.
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use anyhow::{Context, Result, anyhow, bail};
use tokio::{io::AsyncReadExt, net::TcpStream};
use url::Url;

const MAX_HEADER_BYTES: usize = 16 * 1024;

#[derive(Debug, PartialEq, Eq)]
pub(crate) enum ProxyRequest {
    Connect {
        authority: String,
        initial_client_bytes: Vec<u8>,
    },
    Http {
        authority: String,
        initial_client_bytes: Vec<u8>,
    },
}

impl ProxyRequest {
    pub(crate) fn authority(&self) -> &str {
        match self {
            Self::Connect { authority, .. } | Self::Http { authority, .. } => authority,
        }
    }

    pub(crate) fn initial_client_bytes(self) -> Vec<u8> {
        match self {
            Self::Connect {
                initial_client_bytes,
                ..
            }
            | Self::Http {
                initial_client_bytes,
                ..
            } => initial_client_bytes,
        }
    }

    pub(crate) fn is_connect(&self) -> bool {
        matches!(self, Self::Connect { .. })
    }

    pub(crate) fn log_kind(&self) -> &'static str {
        match self {
            Self::Connect { .. } => "connect",
            Self::Http { .. } => "http",
        }
    }
}

pub(crate) async fn read_proxy_request(client: &mut TcpStream) -> Result<ProxyRequest> {
    let mut buffer = Vec::with_capacity(1024);
    let header_end = loop {
        let mut chunk = [0_u8; 1024];
        let n = client
            .read(&mut chunk)
            .await
            .context("read client request failed")?;
        if n == 0 {
            bail!("client closed before sending proxy request");
        }
        buffer.extend_from_slice(&chunk[..n]);
        if buffer.len() > MAX_HEADER_BYTES {
            bail!("proxy request header exceeds {MAX_HEADER_BYTES} bytes");
        }
        if let Some(pos) = find_header_end(&buffer) {
            break pos;
        }
    };

    let header = std::str::from_utf8(&buffer[..header_end])
        .context("proxy request header is not valid UTF-8")?;
    let leftover = buffer[header_end + 4..].to_vec();

    parse_proxy_request(header, leftover)
}

fn find_header_end(buffer: &[u8]) -> Option<usize> {
    buffer.windows(4).position(|window| window == b"\r\n\r\n")
}

fn parse_proxy_request(header: &str, leftover: Vec<u8>) -> Result<ProxyRequest> {
    let request_line = header
        .lines()
        .next()
        .ok_or_else(|| anyhow!("proxy request is empty"))?;
    let (method, target, version) = parse_request_line(request_line)?;

    if !version.starts_with("HTTP/") {
        bail!("invalid HTTP version {version}");
    }

    if method == "CONNECT" {
        validate_authority(target)?;
        return Ok(ProxyRequest::Connect {
            authority: target.to_owned(),
            initial_client_bytes: leftover,
        });
    }

    parse_http_proxy_request(header, method, target, version, leftover)
}

fn parse_request_line(request_line: &str) -> Result<(&str, &str, &str)> {
    let mut parts = request_line.split_whitespace();
    let method = parts
        .next()
        .ok_or_else(|| anyhow!("proxy request has no method"))?;
    let target = parts
        .next()
        .ok_or_else(|| anyhow!("proxy request has no target"))?;
    let version = parts
        .next()
        .ok_or_else(|| anyhow!("proxy request has no HTTP version"))?;

    if parts.next().is_some() {
        bail!("proxy request line has too many fields");
    }

    Ok((method, target, version))
}

fn parse_http_proxy_request(
    header: &str,
    method: &str,
    target: &str,
    version: &str,
    leftover: Vec<u8>,
) -> Result<ProxyRequest> {
    let (authority, origin_form) = if target.starts_with("http://")
        || target.starts_with("https://")
    {
        parse_absolute_http_target(target)?
    } else {
        let host = find_header_value(header, "host")
            .ok_or_else(|| anyhow!("HTTP proxy request without absolute URI must include Host"))?;
        (normalize_http_authority(host)?, target.to_owned())
    };
    validate_authority(&authority)?;

    let rewritten = rewrite_http_proxy_header(header, method, &origin_form, version, &authority)?;
    let mut initial_client_bytes = rewritten.into_bytes();
    initial_client_bytes.extend_from_slice(&leftover);

    Ok(ProxyRequest::Http {
        authority,
        initial_client_bytes,
    })
}

fn parse_absolute_http_target(target: &str) -> Result<(String, String)> {
    let url = Url::parse(target).with_context(|| format!("invalid HTTP proxy target: {target}"))?;
    if url.scheme() != "http" {
        bail!("ordinary HTTP proxy requests only support http:// targets; use CONNECT for https");
    }
    if url.username() != "" || url.password().is_some() {
        bail!("HTTP proxy target must not include userinfo");
    }
    let host = url
        .host_str()
        .ok_or_else(|| anyhow!("HTTP proxy target must include host"))?;
    let port = url.port_or_known_default().unwrap_or(80);
    let authority = format_authority(host, port);
    let mut origin_form = url.path().to_owned();
    if origin_form.is_empty() {
        origin_form.push('/');
    }
    if let Some(query) = url.query() {
        origin_form.push('?');
        origin_form.push_str(query);
    }

    Ok((authority, origin_form))
}

fn rewrite_http_proxy_header(
    header: &str,
    method: &str,
    origin_form: &str,
    version: &str,
    authority: &str,
) -> Result<String> {
    let mut rewritten = String::new();
    rewritten.push_str(method);
    rewritten.push(' ');
    rewritten.push_str(origin_form);
    rewritten.push(' ');
    rewritten.push_str(version);
    rewritten.push_str("\r\n");

    for line in header.lines().skip(1) {
        if line.is_empty() {
            continue;
        }
        let Some((name, value)) = line.split_once(':') else {
            bail!("invalid HTTP header line");
        };
        let name = name.trim();
        if should_strip_http_proxy_header(name) {
            continue;
        }
        if name.eq_ignore_ascii_case("host") {
            rewritten.push_str("Host: ");
            rewritten.push_str(authority);
            rewritten.push_str("\r\n");
            continue;
        }
        rewritten.push_str(name);
        rewritten.push(':');
        rewritten.push_str(value);
        rewritten.push_str("\r\n");
    }

    if find_header_value(header, "host").is_none() {
        rewritten.push_str("Host: ");
        rewritten.push_str(authority);
        rewritten.push_str("\r\n");
    }
    rewritten.push_str("Connection: close\r\n\r\n");

    Ok(rewritten)
}

fn should_strip_http_proxy_header(name: &str) -> bool {
    name.eq_ignore_ascii_case("connection")
        || name.eq_ignore_ascii_case("keep-alive")
        || name.eq_ignore_ascii_case("proxy-authenticate")
        || name.eq_ignore_ascii_case("proxy-authorization")
        || name.eq_ignore_ascii_case("proxy-connection")
        || name.eq_ignore_ascii_case("te")
        || name.eq_ignore_ascii_case("trailer")
        || name.eq_ignore_ascii_case("upgrade")
}

fn find_header_value<'a>(header: &'a str, name: &str) -> Option<&'a str> {
    header.lines().skip(1).find_map(|line| {
        let (header_name, value) = line.split_once(':')?;
        header_name
            .trim()
            .eq_ignore_ascii_case(name)
            .then_some(value.trim())
    })
}

fn normalize_http_authority(host: &str) -> Result<String> {
    let host = host.trim();
    if host.is_empty() {
        bail!("HTTP Host header must not be empty");
    }
    if let Some(rest) = host.strip_prefix('[') {
        if let Some((ipv6_host, port)) = rest.split_once("]:") {
            parse_port(port)?;
            return Ok(format!("[{ipv6_host}]:{port}"));
        }
        let ipv6_host = rest
            .strip_suffix(']')
            .ok_or_else(|| anyhow!("invalid IPv6 HTTP Host header"))?;
        return Ok(format!("[{ipv6_host}]:80"));
    }
    if let Some((host_part, port)) = host.rsplit_once(':') {
        if host_part.contains(':') {
            bail!("IPv6 HTTP Host header must use [host]:port");
        }
        parse_port(port)?;
        return Ok(format!("{host_part}:{port}"));
    }
    if host.contains(':') {
        bail!("IPv6 HTTP Host header must use [host]:port");
    }

    Ok(format!("{host}:80"))
}

fn format_authority(host: &str, port: u16) -> String {
    if host.contains(':') && !host.starts_with('[') {
        format!("[{host}]:{port}")
    } else {
        format!("{host}:{port}")
    }
}

fn validate_authority(authority: &str) -> Result<()> {
    if authority.is_empty() {
        bail!("empty CONNECT authority");
    }
    if authority.contains('/') || authority.contains('@') || authority.contains(char::is_whitespace)
    {
        bail!("CONNECT authority must be host:port");
    }

    if let Some(rest) = authority.strip_prefix('[') {
        let (host, port) = rest
            .split_once("]:")
            .ok_or_else(|| anyhow!("IPv6 CONNECT authority must be [host]:port"))?;
        if host.is_empty() || port.is_empty() {
            bail!("CONNECT authority host and port must be non-empty");
        }
        parse_port(port)?;
        return Ok(());
    }

    let (host, port) = authority
        .rsplit_once(':')
        .ok_or_else(|| anyhow!("CONNECT authority must include :port"))?;
    if host.is_empty() || port.is_empty() {
        bail!("CONNECT authority host and port must be non-empty");
    }
    if host.contains(':') {
        bail!("IPv6 CONNECT authority must use [host]:port");
    }
    parse_port(port)?;

    Ok(())
}

fn parse_port(port: &str) -> Result<()> {
    port.parse::<u16>()
        .with_context(|| format!("invalid CONNECT authority port {port}"))?;
    Ok(())
}

#[cfg(test)]
fn parse_connect_authority(header: &str) -> Result<String> {
    match parse_proxy_request(header, Vec::new())? {
        ProxyRequest::Connect { authority, .. } => Ok(authority),
        ProxyRequest::Http { .. } => bail!("request is not CONNECT"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_connect_authority() {
        let header = "CONNECT www.google.com:443 HTTP/1.1\r\nHost: www.google.com:443\r\n";

        assert_eq!(
            parse_connect_authority(header).unwrap(),
            "www.google.com:443"
        );
    }

    #[test]
    fn parses_ipv6_connect_authority() {
        let header = "CONNECT [2001:db8::1]:443 HTTP/1.1\r\nHost: [2001:db8::1]:443\r\n";

        assert_eq!(
            parse_connect_authority(header).unwrap(),
            "[2001:db8::1]:443"
        );
    }

    #[test]
    fn parses_http_proxy_request() {
        let header = "GET http://example.com/path?q=1 HTTP/1.1\r\nHost: example.com\r\nProxy-Connection: keep-alive\r\n";

        assert_eq!(
            parse_proxy_request(header, Vec::new()).unwrap(),
            ProxyRequest::Http {
                authority: "example.com:80".to_owned(),
                initial_client_bytes:
                    b"GET /path?q=1 HTTP/1.1\r\nHost: example.com:80\r\nConnection: close\r\n\r\n"
                        .to_vec(),
            }
        );
    }

    #[test]
    fn parses_http_proxy_request_with_body_leftover() {
        let header =
            "POST http://example.com/upload HTTP/1.1\r\nHost: example.com\r\nContent-Length: 4\r\n";

        assert_eq!(
            parse_proxy_request(header, b"test".to_vec()).unwrap(),
            ProxyRequest::Http {
                authority: "example.com:80".to_owned(),
                initial_client_bytes: b"POST /upload HTTP/1.1\r\nHost: example.com:80\r\nContent-Length: 4\r\nConnection: close\r\n\r\ntest".to_vec(),
            }
        );
    }

    #[test]
    fn parses_origin_form_http_request_with_host_header() {
        let header = "GET /path HTTP/1.1\r\nHost: example.com:8080\r\n";

        assert_eq!(
            parse_proxy_request(header, Vec::new()).unwrap(),
            ProxyRequest::Http {
                authority: "example.com:8080".to_owned(),
                initial_client_bytes:
                    b"GET /path HTTP/1.1\r\nHost: example.com:8080\r\nConnection: close\r\n\r\n"
                        .to_vec(),
            }
        );
    }

    #[test]
    fn rejects_https_absolute_http_proxy_request() {
        let header = "GET https://example.com/ HTTP/1.1\r\nHost: example.com\r\n";

        assert!(parse_proxy_request(header, Vec::new()).is_err());
    }

    #[test]
    fn rejects_unbracketed_ipv6_authority() {
        let header = "CONNECT 2001:db8::1:443 HTTP/1.1\r\nHost: 2001:db8::1:443\r\n";

        assert!(parse_connect_authority(header).is_err());
    }
}