wconnect 0.8.2

Wispers Connect connectivity test and sidecar utility
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! SOCKS5 proxy for accessing services on remote nodes.
//!
//! This module implements a SOCKS5 proxy (RFC 1928) that allows clients
//! to access services running on nodes in the connectivity group using
//! hostnames like `3.wispers.link`.

use anyhow::{Context, Result};
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use wispers_connect::{Node, NodeState};

use tracing::{debug, error, info, warn};

use crate::proxy_common::{
    CLEANUP_INTERVAL, ConnectionPool, ProxyError, REQUEST_TIMEOUT, parse_wispers_host, send_command,
};

// SOCKS5 constants
const SOCKS_VERSION: u8 = 0x05;
const AUTH_NOAUTH: u8 = 0x00;
const CMD_CONNECT: u8 = 0x01;
const ATYP_IPV4: u8 = 0x01;
const ATYP_DOMAIN: u8 = 0x03;
const ATYP_IPV6: u8 = 0x04;

// SOCKS5 reply codes
const REP_SUCCESS: u8 = 0x00;
const REP_GENERAL_FAILURE: u8 = 0x01;
const REP_NOT_ALLOWED: u8 = 0x02;
const REP_HOST_UNREACHABLE: u8 = 0x04;
const REP_CONNECTION_REFUSED: u8 = 0x05;
const REP_TTL_EXPIRED: u8 = 0x06;
const REP_COMMAND_NOT_SUPPORTED: u8 = 0x07;
const REP_ADDRESS_TYPE_NOT_SUPPORTED: u8 = 0x08;

/// Run the SOCKS5 proxy server.
pub async fn run(
    hub_override: Option<&str>,
    profile: &str,
    bind_addr: &str,
    egress_node: Option<i32>,
) -> Result<()> {
    let storage = super::get_storage(hub_override, profile)?;
    let node = super::load_node(&storage).await?;

    if node.state() != NodeState::Activated {
        anyhow::bail!(
            "Node must be activated to use SOCKS5 proxy. Current state: {:?}",
            node.state()
        );
    }

    let listener = TcpListener::bind(bind_addr)
        .await
        .with_context(|| format!("failed to bind to {}", bind_addr))?;

    println!("SOCKS5 proxy listening on {}", bind_addr);
    if let Some(egress) = egress_node {
        println!("  Internet egress: enabled via node {}", egress);
        println!(
            "Example: curl --proxy socks5h://{} https://example.com/",
            bind_addr
        );
    } else {
        println!("  Internet egress: disabled (wispers.link only)");
        println!(
            "Example: curl --proxy socks5h://{} http://3.wispers.link/",
            bind_addr
        );
    }
    println!("  (Use socks5h:// so the proxy resolves hostnames, not curl)");

    let node = Arc::new(node);
    let pool = ConnectionPool::new();

    // Start background cleanup task
    let cleanup_pool = pool.clone();
    tokio::spawn(async move {
        loop {
            tokio::time::sleep(CLEANUP_INTERVAL).await;
            cleanup_pool.cleanup_idle().await;
        }
    });

    loop {
        match listener.accept().await {
            Ok((stream, addr)) => {
                debug!(%addr, "Accepted connection");
                let node = Arc::clone(&node);
                let pool = pool.clone();
                tokio::spawn(async move {
                    if let Err(e) = handle_client_connection(stream, node, pool, egress_node).await
                    {
                        warn!(error = %e, "Connection error");
                    }
                });
            }
            Err(e) => {
                error!(error = %e, "Accept error");
            }
        }
    }
}

/// Parsed SOCKS5 connect request.
#[derive(Debug)]
struct ConnectRequest {
    /// Target hostname or IP address
    host: String,
    /// Target port
    port: u16,
}

/// Handle a single SOCKS5 client connection.
async fn handle_client_connection(
    mut stream: TcpStream,
    node: Arc<Node>,
    pool: ConnectionPool,
    egress_node: Option<i32>,
) -> Result<()> {
    let peer = stream.peer_addr()?;

    // Step 1: Handle authentication negotiation
    if let Err(e) = handle_auth(&mut stream).await {
        debug!(%peer, error = %e, "Auth failed");
        return Ok(());
    }

    // Step 2: Handle connect request
    let request = match handle_connect_request(&mut stream).await {
        Ok(req) => req,
        Err(e) => {
            debug!(%peer, error = %e, "Connect request failed");
            return Ok(());
        }
    };

    info!(host = %request.host, port = request.port, "CONNECT");

    // Step 3: Route based on destination
    match route_connection(&mut stream, &node, &pool, &request, egress_node).await {
        Ok(()) => {}
        Err(e) => {
            info!(host = %request.host, error = %e, "Routing failed");
        }
    }

    debug!(%peer, "Connection closed");
    Ok(())
}

/// Handle SOCKS5 authentication negotiation.
async fn handle_auth(stream: &mut TcpStream) -> Result<(), ProxyError> {
    // Read client greeting: VER | NMETHODS | METHODS...
    let mut buf = [0u8; 258]; // max: 1 + 1 + 256 methods
    let n = stream
        .read(&mut buf)
        .await
        .map_err(|e| ProxyError::BadRequest(format!("failed to read auth request: {}", e)))?;

    if n < 2 {
        return Err(ProxyError::BadRequest("auth request too short".to_string()));
    }

    let version = buf[0];
    if version != SOCKS_VERSION {
        return Err(ProxyError::BadRequest(format!(
            "unsupported SOCKS version: {}",
            version
        )));
    }

    let nmethods = buf[1] as usize;
    if n < 2 + nmethods {
        return Err(ProxyError::BadRequest("auth request truncated".to_string()));
    }

    // Check if NOAUTH (0x00) is offered
    let methods = &buf[2..2 + nmethods];
    if !methods.contains(&AUTH_NOAUTH) {
        // Send "no acceptable methods" response
        let _ = stream.write_all(&[SOCKS_VERSION, 0xFF]).await;
        return Err(ProxyError::BadRequest(
            "client does not support NOAUTH".to_string(),
        ));
    }

    // Accept NOAUTH
    stream
        .write_all(&[SOCKS_VERSION, AUTH_NOAUTH])
        .await
        .map_err(|e| ProxyError::BadRequest(format!("failed to send auth response: {}", e)))?;

    Ok(())
}

/// Handle SOCKS5 connect request.
async fn handle_connect_request(stream: &mut TcpStream) -> Result<ConnectRequest, ProxyError> {
    // Read request header: VER | CMD | RSV | ATYP
    let mut header = [0u8; 4];
    stream
        .read_exact(&mut header)
        .await
        .map_err(|e| ProxyError::BadRequest(format!("failed to read request header: {}", e)))?;

    let version = header[0];
    let cmd = header[1];
    // header[2] is reserved
    let atyp = header[3];

    if version != SOCKS_VERSION {
        return Err(ProxyError::BadRequest(format!(
            "unsupported SOCKS version: {}",
            version
        )));
    }

    // Only support CONNECT command
    if cmd != CMD_CONNECT {
        send_reply(stream, REP_COMMAND_NOT_SUPPORTED).await;
        return Err(ProxyError::BadRequest(format!(
            "unsupported command: {}",
            cmd
        )));
    }

    // Parse destination address based on address type
    let host = match atyp {
        ATYP_IPV4 => {
            let mut addr = [0u8; 4];
            stream.read_exact(&mut addr).await.map_err(|e| {
                ProxyError::BadRequest(format!("failed to read IPv4 address: {}", e))
            })?;
            format!("{}.{}.{}.{}", addr[0], addr[1], addr[2], addr[3])
        }
        ATYP_DOMAIN => {
            let mut len_buf = [0u8; 1];
            stream.read_exact(&mut len_buf).await.map_err(|e| {
                ProxyError::BadRequest(format!("failed to read domain length: {}", e))
            })?;
            let len = len_buf[0] as usize;
            let mut domain = vec![0u8; len];
            stream
                .read_exact(&mut domain)
                .await
                .map_err(|e| ProxyError::BadRequest(format!("failed to read domain: {}", e)))?;
            String::from_utf8(domain)
                .map_err(|_| ProxyError::BadRequest("invalid domain name encoding".to_string()))?
        }
        ATYP_IPV6 => {
            let mut addr = [0u8; 16];
            stream.read_exact(&mut addr).await.map_err(|e| {
                ProxyError::BadRequest(format!("failed to read IPv6 address: {}", e))
            })?;
            std::net::Ipv6Addr::from(addr).to_string()
        }
        _ => {
            send_reply(stream, REP_ADDRESS_TYPE_NOT_SUPPORTED).await;
            return Err(ProxyError::BadRequest(format!(
                "unsupported address type: {}",
                atyp
            )));
        }
    };

    // Read port (2 bytes, big-endian)
    let mut port_buf = [0u8; 2];
    stream
        .read_exact(&mut port_buf)
        .await
        .map_err(|e| ProxyError::BadRequest(format!("failed to read port: {}", e)))?;
    let port = u16::from_be_bytes(port_buf);

    Ok(ConnectRequest { host, port })
}

/// Route the connection based on destination hostname.
async fn route_connection(
    stream: &mut TcpStream,
    node: &Node,
    pool: &ConnectionPool,
    request: &ConnectRequest,
    egress_node: Option<i32>,
) -> Result<()> {
    // Check if it's a wispers.link hostname
    match parse_wispers_host(&request.host) {
        Ok(wispers_host) => {
            // Wispers-local: connect to node via FORWARD
            forward_to_node(stream, node, pool, wispers_host.node_number, request.port).await
        }
        Err(None) => {
            // Not a wispers.link hostname - try egress if configured
            match egress_node {
                Some(egress) => {
                    debug!(egress_node = egress, "Routing via egress");
                    egress_to_node(stream, node, pool, egress, &request.host, request.port).await
                }
                None => {
                    warn!(host = %request.host, "Rejected: egress not enabled");
                    send_reply(stream, REP_NOT_ALLOWED).await;
                    Err(anyhow::anyhow!("egress not enabled"))
                }
            }
        }
        Err(Some(e)) => {
            // Invalid wispers.link hostname
            warn!(host = %request.host, error = %e, "Rejected: invalid wispers.link host");
            send_reply(stream, REP_GENERAL_FAILURE).await;
            Err(anyhow::anyhow!("{}", e))
        }
    }
}

/// Forward connection to a wispers node using FORWARD command.
async fn forward_to_node(
    stream: &mut TcpStream,
    node: &Node,
    pool: &ConnectionPool,
    target_node: i32,
    port: u16,
) -> Result<()> {
    let quic_stream =
        match tokio::time::timeout(REQUEST_TIMEOUT, pool.open_stream(node, target_node)).await {
            Ok(Ok(s)) => s,
            Ok(Err(e)) => {
                warn!(target_node, error = %e, "Failed to open stream");
                send_reply(stream, REP_HOST_UNREACHABLE).await;
                return Err(anyhow::anyhow!("{}", e));
            }
            Err(_) => {
                warn!(target_node, "Timeout opening stream");
                send_reply(stream, REP_TTL_EXPIRED).await;
                return Err(anyhow::anyhow!("open_stream timeout"));
            }
        };

    let command = format!("FORWARD {}\n", port);
    if let Err(e) = send_command(&quic_stream, &command).await {
        warn!(target_node, port, error = %e, "FORWARD failed");
        send_reply(stream, REP_CONNECTION_REFUSED).await;
        return Err(anyhow::anyhow!("{}", e));
    }

    send_reply(stream, REP_SUCCESS).await;
    relay(stream, quic_stream).await;
    Ok(())
}

/// Egress connection through a wispers node using CONNECT command.
async fn egress_to_node(
    stream: &mut TcpStream,
    node: &Node,
    pool: &ConnectionPool,
    egress_node: i32,
    host: &str,
    port: u16,
) -> Result<()> {
    let quic_stream =
        match tokio::time::timeout(REQUEST_TIMEOUT, pool.open_stream(node, egress_node)).await {
            Ok(Ok(s)) => s,
            Ok(Err(e)) => {
                warn!(egress_node, error = %e, "Failed to open stream to egress");
                send_reply(stream, REP_HOST_UNREACHABLE).await;
                return Err(anyhow::anyhow!("{}", e));
            }
            Err(_) => {
                warn!(egress_node, "Timeout opening stream to egress");
                send_reply(stream, REP_TTL_EXPIRED).await;
                return Err(anyhow::anyhow!("open_stream timeout"));
            }
        };

    let command = format!("CONNECT {}:{}\n", host, port);
    if let Err(e) = send_command(&quic_stream, &command).await {
        warn!(egress_node, host, port, error = %e, "CONNECT failed");
        send_reply(stream, REP_CONNECTION_REFUSED).await;
        return Err(anyhow::anyhow!("{}", e));
    }

    send_reply(stream, REP_SUCCESS).await;
    relay(stream, quic_stream).await;
    Ok(())
}

/// Bidirectional relay between TCP stream and QUIC stream.
async fn relay(tcp_stream: &mut TcpStream, quic_stream: wispers_connect::QuicStream) {
    let quic_stream = Arc::new(quic_stream);
    let (mut tcp_read, mut tcp_write) = tcp_stream.split();

    let quic_read = Arc::clone(&quic_stream);
    let quic_write = Arc::clone(&quic_stream);

    // TCP -> QUIC
    let tcp_to_quic = async move {
        let mut buf = [0u8; 8192];
        loop {
            match tcp_read.read(&mut buf).await {
                Ok(0) => break,
                Ok(n) => {
                    if let Err(e) = quic_write.write_all(&buf[..n]).await {
                        debug!(error = %e, "QUIC write error");
                        break;
                    }
                }
                Err(e) => {
                    debug!(error = %e, "TCP read error");
                    break;
                }
            }
        }
        let _ = quic_write.finish().await;
    };

    // QUIC -> TCP
    let quic_to_tcp = async move {
        let mut buf = [0u8; 8192];
        loop {
            match quic_read.read(&mut buf).await {
                Ok(0) => break,
                Ok(n) => {
                    if let Err(e) = tcp_write.write_all(&buf[..n]).await {
                        debug!(error = %e, "TCP write error");
                        break;
                    }
                }
                Err(e) => {
                    debug!(error = %e, "QUIC read error");
                    break;
                }
            }
        }
        let _ = tcp_write.shutdown().await;
    };

    tokio::join!(tcp_to_quic, quic_to_tcp);
}

/// Send a SOCKS5 reply to the client.
async fn send_reply(stream: &mut TcpStream, reply_code: u8) {
    // Reply format: VER | REP | RSV | ATYP | BND.ADDR | BND.PORT
    // We use IPv4 address type with 0.0.0.0:0 as bind address
    let reply = [
        SOCKS_VERSION, // VER
        reply_code,    // REP
        0x00,          // RSV
        ATYP_IPV4,     // ATYP
        0,
        0,
        0,
        0, // BND.ADDR (0.0.0.0)
        0,
        0, // BND.PORT (0)
    ];
    let _ = stream.write_all(&reply).await;
}

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

    #[test]
    fn test_socks5_constants() {
        assert_eq!(SOCKS_VERSION, 0x05);
        assert_eq!(AUTH_NOAUTH, 0x00);
        assert_eq!(CMD_CONNECT, 0x01);
        assert_eq!(ATYP_IPV4, 0x01);
        assert_eq!(ATYP_DOMAIN, 0x03);
        assert_eq!(ATYP_IPV6, 0x04);
    }

    #[test]
    fn test_reply_codes() {
        assert_eq!(REP_SUCCESS, 0x00);
        assert_eq!(REP_GENERAL_FAILURE, 0x01);
        assert_eq!(REP_NOT_ALLOWED, 0x02);
        assert_eq!(REP_CONNECTION_REFUSED, 0x05);
        assert_eq!(REP_TTL_EXPIRED, 0x06);
        assert_eq!(REP_COMMAND_NOT_SUPPORTED, 0x07);
        assert_eq!(REP_ADDRESS_TYPE_NOT_SUPPORTED, 0x08);
    }

    // Helper to build auth request
    fn build_auth_request(methods: &[u8]) -> Vec<u8> {
        let mut data = vec![SOCKS_VERSION, methods.len() as u8];
        data.extend_from_slice(methods);
        data
    }

    // Helper to build connect request with IPv4
    fn build_connect_ipv4(ip: [u8; 4], port: u16) -> Vec<u8> {
        let mut data = vec![SOCKS_VERSION, CMD_CONNECT, 0x00, ATYP_IPV4];
        data.extend_from_slice(&ip);
        data.extend_from_slice(&port.to_be_bytes());
        data
    }

    // Helper to build connect request with domain
    fn build_connect_domain(domain: &str, port: u16) -> Vec<u8> {
        let mut data = vec![SOCKS_VERSION, CMD_CONNECT, 0x00, ATYP_DOMAIN];
        data.push(domain.len() as u8);
        data.extend_from_slice(domain.as_bytes());
        data.extend_from_slice(&port.to_be_bytes());
        data
    }

    // Helper to build connect request with IPv6
    fn build_connect_ipv6(ip: [u8; 16], port: u16) -> Vec<u8> {
        let mut data = vec![SOCKS_VERSION, CMD_CONNECT, 0x00, ATYP_IPV6];
        data.extend_from_slice(&ip);
        data.extend_from_slice(&port.to_be_bytes());
        data
    }

    // ===== Auth parsing tests =====

    #[test]
    fn test_auth_response_noauth() {
        // Expected response when NOAUTH is accepted
        let expected_response = [SOCKS_VERSION, AUTH_NOAUTH];
        assert_eq!(expected_response, [0x05, 0x00]);
    }

    #[test]
    fn test_auth_request_multiple_methods() {
        // Client offers multiple methods including NOAUTH
        let data = build_auth_request(&[0x02, AUTH_NOAUTH, 0x01]); // GSSAPI, NOAUTH, USERNAME
        assert_eq!(data, [0x05, 0x03, 0x02, 0x00, 0x01]);
    }

    #[test]
    fn test_auth_request_structure() {
        // Verify auth request format: VER | NMETHODS | METHODS...
        let req = build_auth_request(&[AUTH_NOAUTH]);
        assert_eq!(req[0], SOCKS_VERSION);
        assert_eq!(req[1], 1); // nmethods
        assert_eq!(req[2], AUTH_NOAUTH);
    }

    // ===== Connect request parsing tests =====

    #[test]
    fn test_connect_request_ipv4_structure() {
        let req = build_connect_ipv4([192, 168, 1, 1], 8080);
        assert_eq!(req[0], SOCKS_VERSION);
        assert_eq!(req[1], CMD_CONNECT);
        assert_eq!(req[2], 0x00); // reserved
        assert_eq!(req[3], ATYP_IPV4);
        assert_eq!(&req[4..8], &[192, 168, 1, 1]);
        assert_eq!(&req[8..10], &8080u16.to_be_bytes());
    }

    #[test]
    fn test_connect_request_domain_structure() {
        let req = build_connect_domain("example.com", 443);
        assert_eq!(req[0], SOCKS_VERSION);
        assert_eq!(req[1], CMD_CONNECT);
        assert_eq!(req[2], 0x00); // reserved
        assert_eq!(req[3], ATYP_DOMAIN);
        assert_eq!(req[4], 11); // "example.com".len()
        assert_eq!(&req[5..16], b"example.com");
        assert_eq!(&req[16..18], &443u16.to_be_bytes());
    }

    #[test]
    fn test_connect_request_wispers_domain() {
        let req = build_connect_domain("3.wispers.link", 80);
        assert_eq!(req[4], 14); // "3.wispers.link".len()
        assert_eq!(&req[5..19], b"3.wispers.link");
    }

    #[test]
    fn test_connect_request_ipv6_structure() {
        // IPv6 ::1
        let ip = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
        let req = build_connect_ipv6(ip, 8080);
        assert_eq!(req[0], SOCKS_VERSION);
        assert_eq!(req[1], CMD_CONNECT);
        assert_eq!(req[2], 0x00); // reserved
        assert_eq!(req[3], ATYP_IPV6);
        assert_eq!(&req[4..20], &ip);
        assert_eq!(&req[20..22], &8080u16.to_be_bytes());
    }

    // ===== Reply structure tests =====

    #[test]
    fn test_reply_structure() {
        // Reply format: VER | REP | RSV | ATYP | BND.ADDR | BND.PORT
        let expected_success = [
            SOCKS_VERSION, // VER
            REP_SUCCESS,   // REP
            0x00,          // RSV
            ATYP_IPV4,     // ATYP
            0,
            0,
            0,
            0, // BND.ADDR
            0,
            0, // BND.PORT
        ];
        assert_eq!(expected_success.len(), 10);
        assert_eq!(expected_success[0], 0x05);
        assert_eq!(expected_success[1], 0x00);
    }

    #[test]
    fn test_reply_error_codes() {
        // Verify error reply has same structure
        let error_reply = [
            SOCKS_VERSION,
            REP_CONNECTION_REFUSED,
            0x00,
            ATYP_IPV4,
            0,
            0,
            0,
            0,
            0,
            0,
        ];
        assert_eq!(error_reply[1], 0x05); // connection refused
    }

    // ===== Port encoding tests =====

    #[test]
    fn test_port_big_endian() {
        // Port 8080 = 0x1F90
        let port: u16 = 8080;
        let bytes = port.to_be_bytes();
        assert_eq!(bytes, [0x1F, 0x90]);

        // Port 443 = 0x01BB
        let port: u16 = 443;
        let bytes = port.to_be_bytes();
        assert_eq!(bytes, [0x01, 0xBB]);

        // Port 80 = 0x0050
        let port: u16 = 80;
        let bytes = port.to_be_bytes();
        assert_eq!(bytes, [0x00, 0x50]);
    }

    // ===== Command code tests =====

    #[test]
    fn test_unsupported_commands() {
        // BIND command
        const CMD_BIND: u8 = 0x02;
        // UDP ASSOCIATE command
        const CMD_UDP_ASSOCIATE: u8 = 0x03;

        // These should not be confused with CONNECT
        assert_ne!(CMD_BIND, CMD_CONNECT);
        assert_ne!(CMD_UDP_ASSOCIATE, CMD_CONNECT);
    }

    // ===== IPv4 formatting tests =====

    #[test]
    fn test_ipv4_address_formatting() {
        let addr = [192u8, 168, 1, 1];
        let formatted = format!("{}.{}.{}.{}", addr[0], addr[1], addr[2], addr[3]);
        assert_eq!(formatted, "192.168.1.1");

        let addr = [127u8, 0, 0, 1];
        let formatted = format!("{}.{}.{}.{}", addr[0], addr[1], addr[2], addr[3]);
        assert_eq!(formatted, "127.0.0.1");
    }

    // ===== IPv6 formatting tests =====

    #[test]
    fn test_ipv6_address_formatting() {
        use std::net::Ipv6Addr;

        // ::1 (loopback)
        let addr = [0u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
        let formatted = Ipv6Addr::from(addr).to_string();
        assert_eq!(formatted, "::1");

        // 2001:db8::1
        let addr = [0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
        let formatted = Ipv6Addr::from(addr).to_string();
        assert_eq!(formatted, "2001:db8::1");

        // fe80::1 (link-local)
        let addr = [0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
        let formatted = Ipv6Addr::from(addr).to_string();
        assert_eq!(formatted, "fe80::1");

        // Full address: 2001:db8:85a3:0:0:8a2e:370:7334
        let addr = [
            0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70,
            0x73, 0x34,
        ];
        let formatted = Ipv6Addr::from(addr).to_string();
        assert_eq!(formatted, "2001:db8:85a3::8a2e:370:7334");
    }

    // ===== Domain validation tests =====

    #[test]
    fn test_wispers_domain_parsing() {
        // These should be recognized as wispers domains
        let result = parse_wispers_host("3.wispers.link");
        assert!(result.is_ok());
        assert_eq!(result.unwrap().node_number, 3);

        let result = parse_wispers_host("123.wispers.link");
        assert!(result.is_ok());
        assert_eq!(result.unwrap().node_number, 123);
    }

    #[test]
    fn test_non_wispers_domain() {
        // These should NOT be recognized as wispers domains
        let result = parse_wispers_host("example.com");
        assert!(result.is_err());
        assert!(result.unwrap_err().is_none()); // None means not a wispers domain

        let result = parse_wispers_host("google.com");
        assert!(result.is_err());
        assert!(result.unwrap_err().is_none());
    }

    #[test]
    fn test_invalid_wispers_domain() {
        // Invalid node number
        let result = parse_wispers_host("abc.wispers.link");
        assert!(result.is_err());
        assert!(result.unwrap_err().is_some()); // Some error means malformed wispers domain
    }
}