proxy-twister 1.1.4

A flexible HTTP proxy switcher that routes traffic through different proxies (SOCKS5 or HTTP) based on target host patterns.
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use crate::config::Config;
use crate::protocols::{http, socks};
use std::sync::{Arc, Mutex};
use tokio::io::AsyncWriteExt;
use tokio::net::TcpListener;
use tokio::sync::RwLock;
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, info, trace};

fn select_profile(config: &Config, target_host: &str) -> String {
    let mut selected = config.switch.default.clone();
    for rule in config.switch.rules.iter() {
        let pattern = &rule.pattern;
        if crate::utils::matches_pattern(target_host, pattern) {
            selected = rule.profile.clone();
            break;
        }
    }
    selected
}

async fn extract_host_and_port(
    client: &mut tokio::net::TcpStream,
    request: &http::HttpRequest,
) -> tokio::io::Result<(String, u16)> {
    trace!(
        "extract_host_and_port: method={}, target={}",
        request.method, request.target
    );
    trace!("extract_host_and_port: headers={:?}", request.headers);

    if request.method == "CONNECT" {
        return http::handle_connect(client, request.clone()).await;
    }
    let host = request
        .headers
        .get("host")
        .cloned()
        .or_else(|| {
            let uri = request.target.clone();
            trace!(
                "extract_host_and_port: trying to extract host from URI: {}",
                uri
            );
            if let Some(uri) = uri.strip_prefix("http://") {
                uri.split('/').next().map(|h| h.to_string())
            } else {
                None
            }
        })
        .ok_or_else(|| {
            tokio::io::Error::new(
                tokio::io::ErrorKind::InvalidData,
                "No host header in request",
            )
        })?;

    trace!("extract_host_and_port: extracted host string: '{}'", host);

    let parts: Vec<&str> = host.split(':').collect();
    let host_without_port = parts[0].to_string();
    let port = if parts.len() > 1 {
        parts[1].parse().unwrap_or(80)
    } else {
        80
    };

    trace!(
        "extract_host_and_port: final result - host: '{}', port: {}",
        host_without_port, port
    );
    Ok((host_without_port, port))
}

async fn handle_direct_connection(
    mut client: tokio::net::TcpStream,
    request: &http::HttpRequest,
    target_host: &str,
    port: u16,
) -> tokio::io::Result<()> {
    if request.method == "CONNECT" {
        debug!("Attempting direct CONNECT to {}:{}", target_host, port);
        match tokio::net::TcpStream::connect(format!("{}:{}", target_host, port)).await {
            Ok(target_stream) => {
                debug!("Successfully connected to {}:{}", target_host, port);

                // Set socket options for better performance
                if let Err(e) = target_stream.set_nodelay(true) {
                    trace!("Failed to set TCP_NODELAY on target stream: {}", e);
                }

                let (mut ri, mut wi) = client.into_split();
                let (mut ro, mut wo) = target_stream.into_split();
                tokio::try_join!(
                    tokio::io::copy(&mut ri, &mut wo),
                    tokio::io::copy(&mut ro, &mut wi)
                )?;
            }
            Err(e) => {
                error!(
                    "Could not connect directly to {}:{}: {} (error kind: {:?})",
                    target_host,
                    port,
                    e,
                    e.kind()
                );
                client.write_all(http::HTTP_SERVER_ERROR.as_bytes()).await?;
            }
        }
    } else {
        trace!(
            "Attempting direct HTTP connection to {}:{}",
            target_host, port
        );
        match tokio::net::TcpStream::connect(format!("{}:{}", target_host, port)).await {
            Ok(mut target_stream) => {
                trace!("Successfully connected to {}:{}", target_host, port);

                // Set socket options for better performance and stability
                if let Err(e) = target_stream.set_nodelay(true) {
                    trace!("Failed to set TCP_NODELAY on target stream: {}", e);
                }

                // Prepare the HTTP request for direct forwarding
                let mut modified_request = request.clone();

                // For direct connections, we need to convert absolute URLs to relative paths
                if modified_request.target.starts_with("http://") {
                    // Extract the path part from the full URL
                    if let Some(url_without_scheme) =
                        modified_request.target.strip_prefix("http://")
                    {
                        if let Some(slash_pos) = url_without_scheme.find('/') {
                            modified_request.target = url_without_scheme[slash_pos..].to_string();
                        } else {
                            modified_request.target = "/".to_string();
                        }
                    } else {
                        // Fallback if strip_prefix fails for some reason
                        modified_request.target = "/".to_string();
                    }
                } else if modified_request.target.starts_with("https://") {
                    // Handle HTTPS URLs (though they should typically use CONNECT)
                    if let Some(url_without_scheme) =
                        modified_request.target.strip_prefix("https://")
                    {
                        if let Some(slash_pos) = url_without_scheme.find('/') {
                            modified_request.target = url_without_scheme[slash_pos..].to_string();
                        } else {
                            modified_request.target = "/".to_string();
                        }
                    } else {
                        modified_request.target = "/".to_string();
                    }
                }

                // Ensure the target starts with '/' for proper HTTP request format
                if !modified_request.target.starts_with('/') {
                    modified_request.target = format!("/{}", modified_request.target);
                }

                // Build the HTTP request string - preserve original HTTP version if possible
                let http_version = "HTTP/1.1";
                let mut http_request = format!(
                    "{} {} {}\r\n",
                    modified_request.method, modified_request.target, http_version
                );

                // Add headers, ensuring Host header is present
                let mut has_host_header = false;
                let mut has_connection_header = false;
                for (key, value) in &modified_request.headers {
                    let key_lower = key.to_lowercase();
                    if key_lower == "host" {
                        has_host_header = true;
                    }
                    if key_lower == "connection" {
                        has_connection_header = true;
                    }
                    // Skip proxy-specific headers for direct connections
                    if !key_lower.starts_with("proxy-") {
                        http_request.push_str(&format!("{}: {}\r\n", key, value));
                    }
                }

                // Ensure Host header is present
                if !has_host_header {
                    if port == 80 {
                        http_request.push_str(&format!("Host: {}\r\n", target_host));
                    } else {
                        http_request.push_str(&format!("Host: {}:{}\r\n", target_host, port));
                    }
                }

                // For HTTP/1.1, ensure proper connection handling
                if !has_connection_header {
                    http_request.push_str("Connection: close\r\n");
                }

                // Add Content-Length if body is present
                if !modified_request.body.is_empty() {
                    http_request.push_str(&format!(
                        "Content-Length: {}\r\n",
                        modified_request.body.len()
                    ));
                }

                // End headers
                http_request.push_str("\r\n");

                // Send the HTTP request to the target server
                if let Err(e) = target_stream.write_all(http_request.as_bytes()).await {
                    error!(
                        "Failed to send HTTP request to {}:{}: {}",
                        target_host, port, e
                    );
                    return Err(e);
                }

                // Send body if present
                if !modified_request.body.is_empty() {
                    if let Err(e) = target_stream.write_all(&modified_request.body).await {
                        error!(
                            "Failed to send HTTP body to {}:{}: {}",
                            target_host, port, e
                        );
                        return Err(e);
                    }
                }

                // Flush the target stream to ensure data is sent
                if let Err(e) = target_stream.flush().await {
                    error!(
                        "Failed to flush target stream for {}:{}: {}",
                        target_host, port, e
                    );
                    return Err(e);
                }

                trace!("HTTP request sent successfully to {}:{}", target_host, port);

                // Now set up bidirectional forwarding
                let (mut client_read, mut client_write) = client.into_split();
                let (mut target_read, mut target_write) = target_stream.into_split();

                // Forward data in both directions concurrently
                match tokio::try_join!(
                    tokio::io::copy(&mut target_read, &mut client_write),
                    tokio::io::copy(&mut client_read, &mut target_write)
                ) {
                    Ok((bytes_to_client, bytes_to_target)) => {
                        trace!(
                            "Direct HTTP connection completed: {} bytes to client, {} bytes to target",
                            bytes_to_client, bytes_to_target
                        );
                    }
                    Err(e) => {
                        trace!("Direct HTTP connection ended with error: {}", e);
                        return Err(e);
                    }
                }
            }
            Err(e) => {
                error!(
                    "Could not connect directly to {}:{}: {} (error kind: {:?})",
                    target_host,
                    port,
                    e,
                    e.kind()
                );
                client.write_all(http::HTTP_SERVER_ERROR.as_bytes()).await?;
            }
        }
    }
    Ok(())
}

async fn handle_proxy_connection(
    mut client: tokio::net::TcpStream,
    request: &http::HttpRequest,
    target_host: &str,
    port: u16,
    proxy: &crate::config::Profile,
) -> tokio::io::Result<()> {
    match proxy {
        crate::config::Profile::Socks5 {
            host,
            port: proxy_port,
        } => {
            trace!(
                "Using Socks5 proxy {}:{} for {}:{}",
                host, proxy_port, target_host, port
            );
            let socks5_request = socks::Socks5Request {
                target: target_host.to_string(),
                port,
            };
            let proxy_stream_result =
                socks::forward_to_proxy(&socks5_request, host, *proxy_port).await;
            match proxy_stream_result {
                Ok(mut proxy_stream) => {
                    if request.method == "CONNECT" {
                        let (mut ci, mut co) = client.into_split();
                        let (mut pi, mut po) = proxy_stream.into_split();
                        tokio::try_join!(
                            tokio::io::copy(&mut ci, &mut po),
                            tokio::io::copy(&mut pi, &mut co)
                        )?;
                    } else {
                        let mut http_req =
                            format!("{} {} HTTP/1.1\r\n", request.method, request.target);
                        for (k, v) in &request.headers {
                            http_req.push_str(&format!("{}: {}\r\n", k, v));
                        }
                        http_req.push_str("\r\n");
                        proxy_stream.write_all(http_req.as_bytes()).await?;
                        if !request.body.is_empty() {
                            proxy_stream.write_all(&request.body).await?;
                        }
                        let (mut ci, mut co) = client.into_split();
                        let (mut pi, mut po) = proxy_stream.into_split();
                        tokio::try_join!(
                            tokio::io::copy(&mut pi, &mut co),
                            tokio::io::copy(&mut ci, &mut po)
                        )?;
                    }
                }
                Err(e) => {
                    error!("Could not connect through proxy: {}", e);
                    client.write_all(http::HTTP_SERVER_ERROR.as_bytes()).await?;
                }
            }
        }
        crate::config::Profile::Http {
            host,
            port: proxy_port,
        } => {
            debug!(
                "Using HTTP proxy {}:{} for {}:{}",
                host, proxy_port, target_host, port
            );
            let proxy_stream = if request.method == "CONNECT" {
                http::forward_to_proxy(target_host, port, host, *proxy_port, None).await
            } else {
                http::forward_http_request(request, target_host, port, host, *proxy_port, None)
                    .await
            };
            match proxy_stream {
                Ok(proxy_stream) => {
                    let (mut ci, mut co) = client.into_split();
                    let (mut pi, mut po) = proxy_stream.into_split();
                    tokio::try_join!(
                        tokio::io::copy(&mut ci, &mut po),
                        tokio::io::copy(&mut pi, &mut co)
                    )?;
                }
                Err(e) => {
                    error!("Could not connect through proxy: {}", e);
                    client.write_all(http::HTTP_SERVER_ERROR.as_bytes()).await?;
                }
            }
        }
        _ => {
            return Err(tokio::io::Error::new(
                tokio::io::ErrorKind::InvalidInput,
                "Invalid proxy type",
            ));
        }
    }
    Ok(())
}

async fn handle_client(
    mut client: tokio::net::TcpStream,
    config: Arc<RwLock<Config>>,
    cancel_token: CancellationToken,
) -> tokio::io::Result<()> {
    // Check for cancellation before starting
    if cancel_token.is_cancelled() {
        return Ok(());
    }

    let request = http::parse_request(&mut client).await?;
    let (target_host, port) = extract_host_and_port(&mut client, &request).await?;

    trace!(
        "Extracted target_host: '{}', port: {}, method: '{}'",
        target_host, port, request.method
    );

    // IMPORTANT: Scope the read lock to ensure it's released as soon as we extract what we need
    let proxy_config = {
        let config_guard = config.read().await;
        let profile_name = select_profile(&config_guard, &target_host);
        debug!(
            "Target is '{}', using '{}' profile",
            target_host, profile_name
        );

        // Clone what we need from the config to avoid holding the lock

        match config_guard.profiles.get(&profile_name) {
            Some(p) => p.clone(),
            None => {
                error!("Profile {} not found in configuration", profile_name);
                client.write_all(http::HTTP_SERVER_ERROR.as_bytes()).await?;
                return Ok(());
            }
        }
    }; // read lock is released here

    // Process the request with our cloned data, without holding the lock
    match proxy_config {
        crate::config::Profile::Direct => {
            handle_direct_connection(client, &request, &target_host, port).await?;
        }
        crate::config::Profile::Socks5 { .. } | crate::config::Profile::Http { .. } => {
            handle_proxy_connection(client, &request, &target_host, port, &proxy_config).await?;
        }
    }

    Ok(())
}

pub async fn run_listener(
    addr: String,
    config: Arc<RwLock<Config>>,
    connections_token: Arc<Mutex<CancellationToken>>,
    shutdown_token: CancellationToken,
) {
    let listener = match TcpListener::bind(&addr).await {
        Ok(l) => l,
        Err(e) => {
            error!("Failed to bind to {}: {}", addr, e);
            return;
        }
    };
    info!("Listening on {}", addr);
    loop {
        tokio::select! {
            _ = shutdown_token.cancelled() => {
                info!("Listener on {} received shutdown signal", addr);
                break;
            }
            accept_result = listener.accept() => {
                match accept_result {
                    Ok((client_socket, _addr)) => {
                        let config = config.clone();
                        let token = connections_token.clone();
                        tokio::spawn(async move {
                            // Get the current token for this connection
                            let current_token = { token.lock().unwrap().clone() };
                            let _ = handle_client(client_socket, config, current_token).await;
                        });
                    }
                    Err(e) => {
                        error!("Accept error on {}: {:?}", addr, e);
                    }
                }
            }
        }
    }
}