trojan-client 0.9.0

Trojan protocol client with SOCKS5 proxy
Documentation
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
//! Connection handlers for TCP CONNECT and UDP ASSOCIATE.

use std::io::ErrorKind;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

use bytes::{Buf, BytesMut};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpStream, UdpSocket};
use tracing::debug;
use trojan_core::defaults::{
    DEFAULT_RELAY_BUFFER_SIZE, DEFAULT_TCP_TIMEOUT_SECS, DEFAULT_UDP_TIMEOUT_SECS,
};
use trojan_core::io::{NoOpMetrics, relay_bidirectional};
use trojan_proto::{
    AddressRef, CMD_CONNECT, CMD_UDP_ASSOCIATE, ParseResult, parse_udp_packet,
    write_request_header, write_udp_packet,
};

use crate::connector::ClientState;
use crate::error::ClientError;
use crate::socks5::handshake::{
    self, REPLY_ADDRESS_TYPE_NOT_SUPPORTED, REPLY_COMMAND_NOT_SUPPORTED,
    REPLY_CONNECTION_NOT_ALLOWED, REPLY_CONNECTION_REFUSED, REPLY_GENERAL_FAILURE,
    REPLY_HOST_UNREACHABLE, REPLY_NETWORK_UNREACHABLE, REPLY_SUCCEEDED, REPLY_TTL_EXPIRED,
    Socks5Request, send_reply, send_reply_unspecified,
};
use crate::socks5::udp::{parse_socks5_udp, write_socks5_udp};

/// Handle a single SOCKS5 client connection.
pub async fn handle_socks5_conn(mut stream: TcpStream, peer: SocketAddr, state: Arc<ClientState>) {
    if let Err(e) = handle_socks5_conn_inner(&mut stream, peer, &state).await {
        debug!(peer = %peer, error = %e, "connection error");
    }
}

async fn handle_socks5_conn_inner(
    stream: &mut TcpStream,
    peer: SocketAddr,
    state: &ClientState,
) -> Result<(), ClientError> {
    // SOCKS5 method negotiation
    handshake::negotiate_method(stream).await?;

    // Read SOCKS5 request
    let request = match handshake::read_request(stream).await {
        Ok(req) => req,
        Err(crate::error::Socks5Error::UnsupportedAddressType(atyp)) => {
            let _ = send_reply_unspecified(stream, REPLY_ADDRESS_TYPE_NOT_SUPPORTED).await;
            return Err(crate::error::Socks5Error::UnsupportedAddressType(atyp).into());
        }
        Err(e) => return Err(e.into()),
    };

    match request.command {
        handshake::CMD_CONNECT => handle_connect(stream, &request, state).await,
        handshake::CMD_UDP_ASSOCIATE => handle_udp_associate(stream, peer, state).await,
        cmd => {
            let _ = send_reply_unspecified(stream, REPLY_COMMAND_NOT_SUPPORTED).await;
            Err(crate::error::Socks5Error::UnsupportedCommand(cmd).into())
        }
    }
}

/// Handle TCP CONNECT command.
async fn handle_connect(
    stream: &mut TcpStream,
    request: &Socks5Request,
    state: &ClientState,
) -> Result<(), ClientError> {
    let address = match request.to_address_ref() {
        Some(addr) => addr,
        None => {
            let _ = send_reply_unspecified(stream, REPLY_ADDRESS_TYPE_NOT_SUPPORTED).await;
            return Err(crate::error::Socks5Error::UnsupportedAddressType(request.atyp).into());
        }
    };

    debug!(target = %format_address(&address), "CONNECT");

    // Connect to trojan server over TLS
    let mut tls_stream = match state.connect().await {
        Ok(s) => s,
        Err(e) => {
            let reply = reply_code_for_connect_error(&e);
            let _ = send_reply_unspecified(stream, reply).await;
            return Err(e);
        }
    };

    // Build and send Trojan request header (optionally coalesced with initial payload)
    let mut payload_buf = vec![0u8; DEFAULT_RELAY_BUFFER_SIZE];
    let mut payload_len = 0usize;
    match stream.try_read(&mut payload_buf) {
        Ok(0) => {}
        Ok(n) => payload_len = n,
        Err(e) if e.kind() == ErrorKind::WouldBlock => {}
        Err(e) => {
            let _ = send_reply_unspecified(stream, REPLY_GENERAL_FAILURE).await;
            return Err(e.into());
        }
    }

    let mut header_buf = BytesMut::with_capacity(128 + payload_len);
    if let Err(e) = write_request_header(
        &mut header_buf,
        state.hash_hex.as_bytes(),
        CMD_CONNECT,
        &address,
    ) {
        let _ = send_reply_unspecified(stream, REPLY_GENERAL_FAILURE).await;
        return Err(e.into());
    }
    if payload_len > 0 {
        header_buf.extend_from_slice(&payload_buf[..payload_len]);
    }
    if let Err(e) = tls_stream.write_all(&header_buf).await {
        let _ = send_reply_unspecified(stream, REPLY_GENERAL_FAILURE).await;
        return Err(e.into());
    }
    tls_stream.flush().await?;

    // Send SOCKS5 success reply
    send_reply_unspecified(stream, REPLY_SUCCEEDED).await?;

    // Bidirectional relay
    let idle_timeout = Duration::from_secs(DEFAULT_TCP_TIMEOUT_SECS);
    relay_bidirectional(
        stream,
        tls_stream,
        idle_timeout,
        DEFAULT_RELAY_BUFFER_SIZE,
        &NoOpMetrics,
    )
    .await?;

    Ok(())
}

fn reply_code_for_connect_error(error: &ClientError) -> u8 {
    match error {
        ClientError::Resolve(_) => REPLY_HOST_UNREACHABLE,
        ClientError::Io(err) => match err.kind() {
            ErrorKind::ConnectionRefused => REPLY_CONNECTION_REFUSED,
            ErrorKind::NetworkUnreachable => REPLY_NETWORK_UNREACHABLE,
            ErrorKind::HostUnreachable => REPLY_HOST_UNREACHABLE,
            ErrorKind::PermissionDenied => REPLY_CONNECTION_NOT_ALLOWED,
            ErrorKind::TimedOut => REPLY_TTL_EXPIRED,
            ErrorKind::AddrNotAvailable => REPLY_HOST_UNREACHABLE,
            _ => REPLY_GENERAL_FAILURE,
        },
        _ => REPLY_GENERAL_FAILURE,
    }
}

/// Handle UDP ASSOCIATE command.
async fn handle_udp_associate(
    stream: &mut TcpStream,
    peer: SocketAddr,
    state: &ClientState,
) -> Result<(), ClientError> {
    // Bind a local UDP socket for the client
    let udp_bind = match peer {
        SocketAddr::V4(v4) if v4.ip().is_loopback() => "127.0.0.1:0",
        SocketAddr::V6(v6) if v6.ip().is_loopback() => "[::1]:0",
        SocketAddr::V4(_) => "0.0.0.0:0",
        SocketAddr::V6(_) => "[::]:0",
    };
    let udp_socket = UdpSocket::bind(udp_bind).await?;
    let local_udp_addr = udp_socket.local_addr()?;

    debug!(udp_addr = %local_udp_addr, "UDP ASSOCIATE");

    // Send SOCKS5 reply with the UDP relay address
    send_reply(stream, REPLY_SUCCEEDED, &local_udp_addr).await?;

    // Connect to trojan server over TLS
    let mut tls_stream = state.connect().await?;

    // Send Trojan header with UDP_ASSOCIATE command
    // The address in the header is the address the client wants to communicate with.
    // For UDP ASSOCIATE, we use the peer address as a placeholder (per RFC 1928).
    let placeholder_addr = AddressRef {
        host: match peer {
            SocketAddr::V4(v4) => trojan_proto::HostRef::Ipv4(v4.ip().octets()),
            SocketAddr::V6(v6) => trojan_proto::HostRef::Ipv6(v6.ip().octets()),
        },
        port: peer.port(),
    };
    let mut header_buf = BytesMut::with_capacity(128);
    write_request_header(
        &mut header_buf,
        state.hash_hex.as_bytes(),
        CMD_UDP_ASSOCIATE,
        &placeholder_addr,
    )?;
    tls_stream.write_all(&header_buf).await?;
    tls_stream.flush().await?;

    // UDP relay loop
    let idle_timeout = Duration::from_secs(DEFAULT_UDP_TIMEOUT_SECS);
    let result = udp_relay_loop(stream, &udp_socket, &mut tls_stream, idle_timeout).await;

    if let Err(e) = &result {
        debug!(error = %e, "UDP relay ended");
    }

    result
}

/// Bidirectional UDP relay:
/// - Local UDP socket <-> SOCKS5 client
/// - TLS stream <-> trojan server
///
/// Also monitors the SOCKS5 TCP control connection — when it closes, the
/// UDP association ends (per RFC 1928).
async fn udp_relay_loop<S>(
    tcp_stream: &mut TcpStream,
    udp_socket: &UdpSocket,
    tls_stream: &mut S,
    idle_timeout: Duration,
) -> Result<(), ClientError>
where
    S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin,
{
    let mut udp_buf = vec![0u8; 65536];
    let mut tcp_buf = vec![0u8; 65536];
    let mut tcp_acc = BytesMut::with_capacity(65536);
    let mut tcp_read_buf = [0u8; 1]; // for detecting TCP close
    let mut client_addr: Option<SocketAddr> = None;

    let idle_sleep = tokio::time::sleep(idle_timeout);
    tokio::pin!(idle_sleep);

    loop {
        tokio::select! {
            // Client sends UDP data via local socket
            result = udp_socket.recv_from(&mut udp_buf) => {
                let (n, from) = result?;
                client_addr = Some(from);

                // Parse SOCKS5 UDP header
                match parse_socks5_udp(&udp_buf[..n]) {
                    Ok(header) => {
                        // Encode as Trojan UDP packet and send over TLS
                        let mut trojan_buf = BytesMut::with_capacity(header.payload.len() + 64);
                        if write_udp_packet(&mut trojan_buf, &header.address, header.payload).is_ok() {
                            tls_stream.write_all(&trojan_buf).await?;
                            tls_stream.flush().await?;
                        }
                    }
                    Err(e) => {
                        debug!(error = %e, "invalid SOCKS5 UDP packet, dropping");
                    }
                }
                idle_sleep.as_mut().reset(tokio::time::Instant::now() + idle_timeout);
            }

            // Trojan server sends UDP data over TLS
            result = tls_stream.read(&mut tcp_buf) => {
                let n = result?;
                if n == 0 {
                    // TLS stream closed
                    return Ok(());
                }

                tcp_acc.extend_from_slice(&tcp_buf[..n]);

                let mut pending = Vec::new();
                let res = drain_trojan_udp_packets(&mut tcp_acc, |pkt| {
                    if let Some(addr) = client_addr {
                        let socks5_pkt = write_socks5_udp(&pkt.address, pkt.payload);
                        pending.push((socks5_pkt, addr));
                    }
                });

                if let Err(e) = res {
                    debug!(error = ?e, "invalid trojan UDP packet");
                }

                for (pkt, addr) in pending {
                    let _ = udp_socket.send_to(&pkt, addr).await;
                }
                idle_sleep.as_mut().reset(tokio::time::Instant::now() + idle_timeout);
            }

            // Monitor TCP control connection (if it closes, end the association)
            result = tcp_stream.read(&mut tcp_read_buf) => {
                match result {
                    Ok(0) | Err(_) => {
                        debug!("SOCKS5 TCP control connection closed, ending UDP association");
                        return Ok(());
                    }
                    Ok(_) => {
                        // Unexpected data on TCP control channel, ignore
                    }
                }
            }

            // Idle timeout
            _ = &mut idle_sleep => {
                debug!("UDP relay idle timeout");
                return Ok(());
            }
        }
    }
}

fn drain_trojan_udp_packets<F>(
    buffer: &mut BytesMut,
    mut on_packet: F,
) -> Result<(), trojan_proto::ParseError>
where
    F: FnMut(&trojan_proto::UdpPacket<'_>),
{
    let mut offset = 0;
    while offset < buffer.len() {
        match parse_udp_packet(&buffer[offset..]) {
            ParseResult::Complete(pkt) => {
                on_packet(&pkt);
                offset += pkt.packet_len;
            }
            ParseResult::Incomplete(_) => break,
            ParseResult::Invalid(e) => {
                buffer.clear();
                return Err(e);
            }
        }
    }

    if offset > 0 {
        buffer.advance(offset);
    }

    Ok(())
}

/// Format an AddressRef for logging.
fn format_address(addr: &AddressRef<'_>) -> String {
    match &addr.host {
        trojan_proto::HostRef::Ipv4(ip) => {
            format!("{}.{}.{}.{}:{}", ip[0], ip[1], ip[2], ip[3], addr.port)
        }
        trojan_proto::HostRef::Ipv6(ip) => {
            let ipv6 = std::net::Ipv6Addr::from(*ip);
            format!("[{ipv6}]:{}", addr.port)
        }
        trojan_proto::HostRef::Domain(d) => {
            let s = std::str::from_utf8(d).unwrap_or("<invalid>");
            format!("{s}:{}", addr.port)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{drain_trojan_udp_packets, reply_code_for_connect_error};
    use crate::error::ClientError;
    use crate::socks5::handshake::{
        REPLY_CONNECTION_NOT_ALLOWED, REPLY_CONNECTION_REFUSED, REPLY_GENERAL_FAILURE,
        REPLY_HOST_UNREACHABLE, REPLY_TTL_EXPIRED,
    };
    use bytes::BytesMut;
    use std::io::ErrorKind;
    use trojan_proto::{AddressRef, HostRef, write_udp_packet};

    #[derive(Debug, PartialEq, Eq)]
    enum OwnedHost {
        Ipv4([u8; 4]),
        Ipv6([u8; 16]),
        Domain(Vec<u8>),
    }

    #[derive(Debug, PartialEq, Eq)]
    struct OwnedPacket {
        host: OwnedHost,
        port: u16,
        payload: Vec<u8>,
    }

    fn capture(pkt: &trojan_proto::UdpPacket<'_>) -> OwnedPacket {
        let host = match pkt.address.host {
            HostRef::Ipv4(ip) => OwnedHost::Ipv4(ip),
            HostRef::Ipv6(ip) => OwnedHost::Ipv6(ip),
            HostRef::Domain(d) => OwnedHost::Domain(d.to_vec()),
        };

        OwnedPacket {
            host,
            port: pkt.address.port,
            payload: pkt.payload.to_vec(),
        }
    }

    #[test]
    fn drain_preserves_incomplete_frames() {
        let address = AddressRef {
            host: HostRef::Ipv4([1, 2, 3, 4]),
            port: 53,
        };

        let mut packet = BytesMut::new();
        write_udp_packet(&mut packet, &address, b"hello").unwrap();

        let split = 3;
        let mut acc = BytesMut::new();
        acc.extend_from_slice(&packet[..split]);

        let mut parsed = Vec::new();
        drain_trojan_udp_packets(&mut acc, |pkt| parsed.push(capture(pkt))).unwrap();
        assert!(parsed.is_empty());
        assert_eq!(acc.len(), split);

        acc.extend_from_slice(&packet[split..]);
        drain_trojan_udp_packets(&mut acc, |pkt| parsed.push(capture(pkt))).unwrap();

        assert!(acc.is_empty());
        assert_eq!(parsed.len(), 1);
        assert_eq!(parsed[0].payload, b"hello");
        assert_eq!(parsed[0].port, 53);
        assert_eq!(parsed[0].host, OwnedHost::Ipv4([1, 2, 3, 4]));
    }

    #[test]
    fn reply_code_maps_common_errors() {
        let err = ClientError::Resolve("example.com".into());
        assert_eq!(reply_code_for_connect_error(&err), REPLY_HOST_UNREACHABLE);

        let err = ClientError::Io(std::io::Error::new(ErrorKind::ConnectionRefused, "refused"));
        assert_eq!(reply_code_for_connect_error(&err), REPLY_CONNECTION_REFUSED);

        let err = ClientError::Io(std::io::Error::new(ErrorKind::PermissionDenied, "denied"));
        assert_eq!(
            reply_code_for_connect_error(&err),
            REPLY_CONNECTION_NOT_ALLOWED
        );

        let err = ClientError::Io(std::io::Error::new(ErrorKind::TimedOut, "timeout"));
        assert_eq!(reply_code_for_connect_error(&err), REPLY_TTL_EXPIRED);

        let err = ClientError::Io(std::io::Error::other("other"));
        assert_eq!(reply_code_for_connect_error(&err), REPLY_GENERAL_FAILURE);
    }
}