socks-lib 0.1.6

A library compliant with the SOCKS protocol standard
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
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
use std::io;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::sync::LazyLock;

use bytes::{Buf, BufMut, Bytes, BytesMut};

use crate::io::{AsyncRead, AsyncReadExt};

/// # Address
///
/// ```text
///  +------+----------+----------+
///  | ATYP | DST.ADDR | DST.PORT |
///  +------+----------+----------+
///  |  1   | Variable |    2     |
///  +------+----------+----------+
/// ```
///
/// ## DST.ADDR BND.ADDR
///   In an address field (DST.ADDR, BND.ADDR), the ATYP field specifies
///   the type of address contained within the field:
///   
/// o ATYP: X'01'
///   the address is a version-4 IP address, with a length of 4 octets
///   
/// o ATYP: X'03'
///   the address field contains a fully-qualified domain name.  The first
///   octet of the address field contains the number of octets of name that
///   follow, there is no terminating NUL octet.
///   
/// o ATYP: X'04'  
///   the address is a version-6 IP address, with a length of 16 octets.
///
#[derive(Debug, Clone, PartialEq)]
pub enum Address {
    IPv4(SocketAddrV4),
    IPv6(SocketAddrV6),
    Domain(Domain, u16),
}

static UNSPECIFIED_ADDRESS: LazyLock<Address> =
    LazyLock::new(|| Address::IPv4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)));

#[rustfmt::skip]
impl Address {
    pub const PORT_LENGTH:         usize = 2;
    pub const IPV4_ADDRESS_LENGTH: usize = 4;
    pub const IPV6_ADDRESS_LENGTH: usize = 16;

    pub const SOCKS5_ADDRESS_TYPE_IPV4:        u8 = 0x01;
    pub const SOCKS5_ADDRESS_TYPE_DOMAIN_NAME: u8 = 0x03;
    pub const SOCKS5_ADDRESS_TYPE_IPV6:        u8 = 0x04;
}

impl Address {
    #[inline]
    pub fn unspecified() -> &'static Self {
        &UNSPECIFIED_ADDRESS
    }

    pub async fn from_async_read<R: AsyncRead + Unpin>(reader: &mut R) -> io::Result<Self> {
        let address_type = reader.read_u8().await?;

        match address_type {
            Self::SOCKS5_ADDRESS_TYPE_IPV4 => {
                let mut buf = [0u8; Self::IPV4_ADDRESS_LENGTH + Self::PORT_LENGTH];
                reader.read_exact(&mut buf).await?;

                let ip = Ipv4Addr::new(buf[0], buf[1], buf[2], buf[3]);
                let port = u16::from_be_bytes([buf[4], buf[5]]);

                Ok(Address::IPv4(SocketAddrV4::new(ip, port)))
            }

            Self::SOCKS5_ADDRESS_TYPE_IPV6 => {
                let mut buf = [0u8; Self::IPV6_ADDRESS_LENGTH + Self::PORT_LENGTH];
                reader.read_exact(&mut buf).await?;

                let ip = Ipv6Addr::from([
                    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9],
                    buf[10], buf[11], buf[12], buf[13], buf[14], buf[15],
                ]);
                let port = u16::from_be_bytes([buf[16], buf[17]]);

                Ok(Address::IPv6(SocketAddrV6::new(ip, port, 0, 0)))
            }

            Self::SOCKS5_ADDRESS_TYPE_DOMAIN_NAME => {
                let domain_len = reader.read_u8().await? as usize;

                let mut buf = vec![0u8; domain_len + Self::PORT_LENGTH];
                reader.read_exact(&mut buf).await?;

                let domain = Bytes::copy_from_slice(&buf[..domain_len]);
                let port = u16::from_be_bytes([buf[domain_len], buf[domain_len + 1]]);

                Ok(Address::Domain(Domain(domain), port))
            }

            n => Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Invalid address type: {}", n),
            )),
        }
    }

    pub fn from_bytes<B: Buf>(buf: &mut B) -> io::Result<Self> {
        if buf.remaining() < 1 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "Insufficient data for address",
            ));
        }

        let address_type = buf.get_u8();

        match address_type {
            Self::SOCKS5_ADDRESS_TYPE_IPV4 => {
                if buf.remaining() < Self::IPV4_ADDRESS_LENGTH + Self::PORT_LENGTH {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "Insufficient data for IPv4 address",
                    ));
                }

                let mut ip = [0u8; Self::IPV4_ADDRESS_LENGTH];
                buf.copy_to_slice(&mut ip);

                let port = buf.get_u16();

                Ok(Address::IPv4(SocketAddrV4::new(Ipv4Addr::from(ip), port)))
            }

            Self::SOCKS5_ADDRESS_TYPE_IPV6 => {
                if buf.remaining() < Self::IPV6_ADDRESS_LENGTH + Self::PORT_LENGTH {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "Insufficient data for IPv6 address",
                    ));
                }

                let mut ip = [0u8; Self::IPV6_ADDRESS_LENGTH];
                buf.copy_to_slice(&mut ip);

                let port = buf.get_u16();

                Ok(Address::IPv6(SocketAddrV6::new(
                    Ipv6Addr::from(ip),
                    port,
                    0,
                    0,
                )))
            }

            Self::SOCKS5_ADDRESS_TYPE_DOMAIN_NAME => {
                if buf.remaining() < 1 {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "Insufficient data for domain length",
                    ));
                }

                let domain_len = buf.get_u8() as usize;

                if buf.remaining() < domain_len + Self::PORT_LENGTH {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "Insufficient data for domain name",
                    ));
                }

                let mut domain = vec![0u8; domain_len];
                buf.copy_to_slice(&mut domain);

                let port = buf.get_u16();

                Ok(Address::Domain(Domain(Bytes::from(domain)), port))
            }

            n => Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Invalid address type: {}", n),
            )),
        }
    }

    #[inline]
    pub fn to_bytes(&self) -> Bytes {
        let mut bytes = BytesMut::new();

        match self {
            Self::Domain(domain, port) => {
                let domain_bytes = domain.as_bytes();
                bytes.put_u8(Self::SOCKS5_ADDRESS_TYPE_DOMAIN_NAME);
                bytes.put_u8(domain_bytes.len() as u8);
                bytes.extend_from_slice(domain_bytes);
                bytes.extend_from_slice(&port.to_be_bytes());
            }
            Self::IPv4(addr) => {
                bytes.put_u8(Self::SOCKS5_ADDRESS_TYPE_IPV4);
                bytes.extend_from_slice(&addr.ip().octets());
                bytes.extend_from_slice(&addr.port().to_be_bytes());
            }
            Self::IPv6(addr) => {
                bytes.put_u8(Self::SOCKS5_ADDRESS_TYPE_IPV6);
                bytes.extend_from_slice(&addr.ip().octets());
                bytes.extend_from_slice(&addr.port().to_be_bytes());
            }
        }

        bytes.freeze()
    }

    #[inline]
    pub fn port(&self) -> u16 {
        match self {
            Self::IPv4(addr) => addr.port(),
            Self::IPv6(addr) => addr.port(),
            Self::Domain(_, port) => *port,
        }
    }

    pub async fn to_socket_addr(self) -> io::Result<SocketAddr> {
        use tokio::net::lookup_host;

        match self {
            Address::IPv4(addr) => Ok(SocketAddr::V4(addr)),
            Address::IPv6(addr) => Ok(SocketAddr::V6(addr)),
            Address::Domain(domain, port) => {
                let domain = domain.format_as_str();

                lookup_host((domain, port))
                    .await?
                    .next()
                    .ok_or(io::Error::other(format!(
                        "Failed to resolve domain {}",
                        domain
                    )))
            }
        }
    }
}

impl From<SocketAddr> for Address {
    #[inline]
    fn from(value: SocketAddr) -> Self {
        match value {
            SocketAddr::V4(addr) => Self::IPv4(addr),
            SocketAddr::V6(addr) => Self::IPv6(addr),
        }
    }
}

impl std::fmt::Display for Address {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let value = match self {
            Self::Domain(domain, port) => format!("{}:{}", domain.format_as_str(), port),
            Self::IPv4(addr) => addr.to_string(),
            Self::IPv6(addr) => addr.to_string(),
        };

        write!(f, "{value}")
    }
}

impl TryFrom<&str> for Address {
    type Error = io::Error;

    #[inline]
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        use std::str::FromStr;

        if let Ok(ipv4_addr) = SocketAddrV4::from_str(value) {
            return Ok(Address::IPv4(ipv4_addr));
        }

        if let Ok(addr) = SocketAddrV6::from_str(value) {
            return Ok(Address::IPv6(addr));
        }

        if let Some((domain, port_str)) = value.rsplit_once(':') {
            if let Ok(port) = port_str.parse::<u16>() {
                if !domain.is_empty() {
                    return Ok(Address::Domain(Domain::try_from(domain)?, port));
                }
            }
        }

        Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            format!("Invalid address format: {}", value),
        ))
    }
}

impl TryFrom<String> for Address {
    type Error = io::Error;

    #[inline]
    fn try_from(value: String) -> Result<Self, Self::Error> {
        Address::try_from(value.as_str())
    }
}

// ===== Domain =====
#[derive(Debug, Clone, PartialEq)]
pub struct Domain(Bytes);

impl Domain {
    const MAX_LENGTH: usize = 254;

    #[inline]
    pub fn from_bytes(bytes: Bytes) -> io::Result<Self> {
        if bytes.is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "Domain is empty",
            ));
        }

        let domain_str = std::str::from_utf8(&bytes)
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;

        if domain_str.len() > Self::MAX_LENGTH {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "Punycode domain exceeds maximum length",
            ));
        }

        Ok(Self(bytes))
    }

    #[inline]
    pub fn from_string(value: String) -> io::Result<Self> {
        Self::from_bytes(value.into())
    }

    #[inline]
    pub fn format_as_str(&self) -> &str {
        use std::str::from_utf8;

        from_utf8(&self.0).expect("Invalid UTF-8")
    }

    #[inline]
    pub fn as_bytes(&self) -> &Bytes {
        &self.0
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl From<Domain> for Bytes {
    #[inline]
    fn from(value: Domain) -> Self {
        value.0
    }
}

impl From<Domain> for Vec<u8> {
    #[inline]
    fn from(value: Domain) -> Self {
        value.0.to_vec()
    }
}

impl TryFrom<&[u8]> for Domain {
    type Error = io::Error;

    #[inline]
    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        Self::from_bytes(Bytes::copy_from_slice(value))
    }
}

impl TryFrom<&str> for Domain {
    type Error = io::Error;

    #[inline]
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::from_bytes(Bytes::copy_from_slice(value.as_bytes()))
    }
}

impl TryFrom<String> for Domain {
    type Error = io::Error;

    #[inline]
    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::from_string(value)
    }
}

impl TryFrom<Bytes> for Domain {
    type Error = io::Error;

    #[inline]
    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
        Self::from_bytes(value)
    }
}

impl AsRef<[u8]> for Domain {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

#[cfg(test)]
mod tests {
    use tokio::io::BufReader;

    use super::*;

    use std::{
        io::Cursor,
        net::{Ipv4Addr, Ipv6Addr},
    };

    #[test]
    fn test_ipv4_serialization() {
        let addr = Address::IPv4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080));
        let bytes = addr.to_bytes();
        let mut buf = &bytes[..];
        let parsed = Address::from_bytes(&mut buf).unwrap();
        assert_eq!(addr, parsed);
    }

    #[test]
    fn test_ipv6_serialization() {
        let addr = Address::IPv6(SocketAddrV6::new(Ipv6Addr::LOCALHOST, 8080, 0, 0));
        let bytes = addr.to_bytes();
        let mut buf = &bytes[..];
        let parsed = Address::from_bytes(&mut buf).unwrap();
        assert_eq!(addr, parsed);
    }

    #[test]
    fn test_domain_serialization() {
        let domain = Domain::try_from("example.com").unwrap();
        let addr = Address::Domain(domain, 8080);
        let bytes = addr.to_bytes();
        let mut buf = &bytes[..];

        let parsed = Address::from_bytes(&mut buf).unwrap();

        if let Address::Domain(d, p) = parsed {
            assert_eq!(d.format_as_str(), "example.com");
            assert_eq!(p, 8080);
        } else {
            panic!("Parsed address is not Domain type");
        }
    }

    #[test]
    fn test_invalid_atyp() {
        let mut buf = bytes::BytesMut::new();
        buf.put_u8(0x04);
        let mut buf = buf.freeze();
        let result = Address::from_bytes(&mut buf);
        assert!(result.is_err());
    }

    #[test]
    fn test_domain_too_long() {
        let result = Domain::try_from(vec![b'a'; 255].as_slice());
        assert!(result.is_err())
    }

    #[tokio::test]
    async fn test_domain_resolution() {
        let domain = Domain::try_from("localhost").unwrap();
        let addr = Address::Domain(domain, 8080);
        let socket_addr = addr.to_socket_addr().await.unwrap();
        assert!(socket_addr.port() == 8080);
    }

    #[test]
    fn test_domain_utf8_error() {
        let result = Domain::from_bytes(Bytes::copy_from_slice(vec![0xff, 0xfe].as_slice()));
        assert!(result.is_err())
    }

    #[test]
    fn test_socket_addr_conversion() {
        let socket_v4 = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 8080);
        let addr: Address = SocketAddr::V4(socket_v4).into();
        assert!(matches!(addr, Address::IPv4(_)));

        let socket_v6 = SocketAddrV6::new(Ipv6Addr::LOCALHOST, 8080, 0, 0);
        let addr: Address = SocketAddr::V6(socket_v6).into();
        assert!(matches!(addr, Address::IPv6(_)));
    }

    #[tokio::test]
    async fn test_address_unspecified() {
        let unspecified = Address::unspecified();
        match unspecified {
            Address::IPv4(addr) => {
                assert_eq!(addr.ip(), &Ipv4Addr::UNSPECIFIED);
                assert_eq!(addr.port(), 0);
            }
            _ => panic!("Unspecified address should be IPv4"),
        }
    }

    #[tokio::test]
    async fn test_address_from_socket_addr_ipv4() {
        let socket = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080));
        let address = Address::from(socket);

        match address {
            Address::IPv4(addr) => {
                assert_eq!(addr.ip().octets(), [127, 0, 0, 1]);
                assert_eq!(addr.port(), 8080);
            }
            _ => panic!("Should be IPv4 address"),
        }
    }

    #[tokio::test]
    async fn test_address_from_socket_addr_ipv6() {
        let socket = SocketAddr::V6(SocketAddrV6::new(
            Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1),
            8080,
            0,
            0,
        ));
        let address = Address::from(socket);

        match address {
            Address::IPv6(addr) => {
                assert_eq!(
                    addr.ip().octets(),
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
                );
                assert_eq!(addr.port(), 8080);
            }
            _ => panic!("Should be IPv6 address"),
        }
    }

    #[tokio::test]
    async fn test_address_to_bytes_ipv4() {
        let addr = Address::IPv4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 1, 1), 80));
        let bytes = addr.to_bytes();

        assert_eq!(bytes[0], Address::SOCKS5_ADDRESS_TYPE_IPV4);
        assert_eq!(bytes[1..5], [192, 168, 1, 1]);
        assert_eq!(bytes[5..7], [0, 80]); // Port 80 in big-endian
    }

    #[tokio::test]
    async fn test_address_to_bytes_ipv6() {
        let addr = Address::IPv6(SocketAddrV6::new(
            Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1),
            443,
            0,
            0,
        ));
        let bytes = addr.to_bytes();

        assert_eq!(bytes[0], Address::SOCKS5_ADDRESS_TYPE_IPV6);
        assert_eq!(
            bytes[1..17],
            [0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
        );
        assert_eq!(bytes[17..19], [1, 187]); // Port 443 in big-endian
    }

    #[tokio::test]
    async fn test_address_to_bytes_domain() {
        let domain = Domain(Bytes::from("example.com"));
        let addr = Address::Domain(domain, 8080);
        let bytes = addr.to_bytes();

        assert_eq!(bytes[0], Address::SOCKS5_ADDRESS_TYPE_DOMAIN_NAME);
        assert_eq!(bytes[1], 11); // Length of "example.com"
        assert_eq!(&bytes[2..13], b"example.com");
        assert_eq!(bytes[13..15], [31, 144]); // Port 8080 in big-endian
    }

    #[tokio::test]
    async fn test_address_from_bytes_ipv4() {
        let mut buffer = BytesMut::new();
        buffer.put_u8(Address::SOCKS5_ADDRESS_TYPE_IPV4);
        buffer.put_slice(&[192, 168, 1, 1]); // IP
        buffer.put_u16(80); // Port

        let mut bytes = buffer.freeze();
        let addr = Address::from_bytes(&mut bytes).unwrap();

        match addr {
            Address::IPv4(socket_addr) => {
                assert_eq!(socket_addr.ip().octets(), [192, 168, 1, 1]);
                assert_eq!(socket_addr.port(), 80);
            }
            _ => panic!("Should be IPv4 address"),
        }
    }

    #[tokio::test]
    async fn test_address_from_bytes_ipv6() {
        let mut buffer = BytesMut::new();
        buffer.put_u8(Address::SOCKS5_ADDRESS_TYPE_IPV6);
        buffer.put_slice(&[0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); // IPv6
        buffer.put_u16(443); // Port

        let mut bytes = buffer.freeze();
        let addr = Address::from_bytes(&mut bytes).unwrap();

        match addr {
            Address::IPv6(socket_addr) => {
                assert_eq!(
                    socket_addr.ip().octets(),
                    [0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
                );
                assert_eq!(socket_addr.port(), 443);
            }
            _ => panic!("Should be IPv6 address"),
        }
    }

    #[tokio::test]
    async fn test_address_from_bytes_domain() {
        let mut buffer = BytesMut::new();
        buffer.put_u8(Address::SOCKS5_ADDRESS_TYPE_DOMAIN_NAME);
        buffer.put_u8(11); // Length of domain name
        buffer.put_slice(b"example.com"); // Domain name
        buffer.put_u16(8080); // Port

        let mut bytes = buffer.freeze();
        let addr = Address::from_bytes(&mut bytes).unwrap();

        match addr {
            Address::Domain(domain, port) => {
                assert_eq!(**domain.as_bytes(), *b"example.com");
                assert_eq!(port, 8080);
            }
            _ => panic!("Should be domain address"),
        }
    }

    #[tokio::test]
    async fn test_address_from_async_read_ipv4() {
        let mut buffer = BytesMut::new();
        buffer.put_u8(Address::SOCKS5_ADDRESS_TYPE_IPV4);
        buffer.put_slice(&[192, 168, 1, 1]); // IP
        buffer.put_u16(80); // Port

        let bytes = buffer.freeze();
        let mut cursor = Cursor::new(bytes);
        let mut reader = BufReader::new(&mut cursor);

        let addr = Address::from_async_read(&mut reader).await.unwrap();

        match addr {
            Address::IPv4(socket_addr) => {
                assert_eq!(socket_addr.ip().octets(), [192, 168, 1, 1]);
                assert_eq!(socket_addr.port(), 80);
            }
            _ => panic!("Should be IPv4 address"),
        }
    }

    #[tokio::test]
    async fn test_address_from_async_read_ipv6() {
        let mut buffer = BytesMut::new();
        buffer.put_u8(Address::SOCKS5_ADDRESS_TYPE_IPV6);
        buffer.put_slice(&[0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); // IPv6
        buffer.put_u16(443); // Port

        let bytes = buffer.freeze();
        let mut cursor = Cursor::new(bytes);
        let mut reader = BufReader::new(&mut cursor);

        let addr = Address::from_async_read(&mut reader).await.unwrap();

        match addr {
            Address::IPv6(socket_addr) => {
                assert_eq!(
                    socket_addr.ip().octets(),
                    [0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
                );
                assert_eq!(socket_addr.port(), 443);
            }
            _ => panic!("Should be IPv6 address"),
        }
    }

    #[tokio::test]
    async fn test_address_from_async_read_domain() {
        let mut buffer = BytesMut::new();
        buffer.put_u8(Address::SOCKS5_ADDRESS_TYPE_DOMAIN_NAME);
        buffer.put_u8(11); // Length of domain name
        buffer.put_slice(b"example.com"); // Domain name
        buffer.put_u16(8080); // Port

        let bytes = buffer.freeze();
        let mut cursor = Cursor::new(bytes);
        let mut reader = BufReader::new(&mut cursor);

        let addr = Address::from_async_read(&mut reader).await.unwrap();

        match addr {
            Address::Domain(domain, port) => {
                assert_eq!(**domain.as_bytes(), *b"example.com");
                assert_eq!(port, 8080);
            }
            _ => panic!("Should be domain address"),
        }
    }

    #[tokio::test]
    async fn test_address_from_bytes_invalid_type() {
        let mut buffer = BytesMut::new();
        buffer.put_u8(0xFF); // Invalid address type

        let mut bytes = buffer.freeze();
        let result = Address::from_bytes(&mut bytes);

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_address_from_bytes_insufficient_data() {
        // IPv4 with incomplete data
        let mut buffer = BytesMut::new();
        buffer.put_u8(Address::SOCKS5_ADDRESS_TYPE_IPV4);
        buffer.put_slice(&[192, 168]); // Incomplete IP

        let mut bytes = buffer.freeze();
        let result = Address::from_bytes(&mut bytes);

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_address_port() {
        let addr1 = Address::IPv4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080));
        assert_eq!(addr1.port(), 8080);

        let addr2 = Address::IPv6(SocketAddrV6::new(
            Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1),
            443,
            0,
            0,
        ));
        assert_eq!(addr2.port(), 443);

        let addr3 = Address::Domain(Domain(Bytes::from("example.com")), 80);
        assert_eq!(addr3.port(), 80);
    }

    #[tokio::test]
    async fn test_address_format_as_string() {
        let addr1 = Address::IPv4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080));
        assert_eq!(addr1.to_string(), "127.0.0.1:8080");

        let addr2 = Address::IPv6(SocketAddrV6::new(
            Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1),
            443,
            0,
            0,
        ));
        assert_eq!(addr2.to_string(), "[::1]:443");

        // This test assumes Domain::domain_str() returns Ok with the domain string
        let addr3 = Address::Domain(Domain(Bytes::from("example.com")), 80);
        assert_eq!(addr3.to_string(), "example.com:80");
    }
}