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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
//! Module that define stream IO that could be use by a ProSA processor
use std::{
    fmt, io,
    net::{Ipv4Addr, SocketAddrV4},
    os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd},
    path::Path,
    pin::Pin,
    task::{Context, Poll},
    time::Duration,
};

#[cfg(feature = "openssl")]
use prosa_utils::config::ssl::SslConfigContext;
use prosa_utils::config::{ssl::SslConfig, url_authentication};

use serde::{Deserialize, Serialize};
use tokio::{
    io::{AsyncRead, AsyncWrite, ReadBuf},
    net::{TcpStream, ToSocketAddrs},
    time::timeout,
};
use url::Url;

use super::{SocketAddr, url_is_ssl};

/// ProSA socket object to handle TCP/SSL socket with or without proxy
#[derive(Debug)]
pub enum Stream {
    #[cfg(target_family = "unix")]
    /// Unix socket (only on unix systems)
    Unix(tokio::net::UnixStream),
    /// TCP socket
    Tcp(TcpStream),
    #[cfg(feature = "openssl")]
    /// SSL socket
    OpenSsl(tokio_openssl::SslStream<TcpStream>),
    #[cfg(feature = "http-proxy")]
    /// TCP socket using Http proxy
    TcpHttpProxy(TcpStream),
    #[cfg(all(feature = "openssl", feature = "http-proxy"))]
    /// SSL socket using Http proxy
    OpenSslHttpProxy(tokio_openssl::SslStream<TcpStream>),
}

impl Stream {
    /// Returns the socket address of the remote peer of this TCP connection.
    ///
    /// ```
    /// use tokio::io;
    /// use url::Url;
    /// use prosa::io::stream::Stream;
    /// use prosa::io::SocketAddr;
    /// use std::net::{Ipv4Addr, SocketAddrV4};
    ///
    /// async fn accepting() -> Result<(), io::Error> {
    ///     let stream: Stream = Stream::connect_tcp("127.0.0.1:8080").await?;
    ///
    ///     assert_eq!(stream.peer_addr()?,
    ///                SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
    ///
    ///     Ok(())
    /// }
    /// ```
    pub fn peer_addr(&self) -> Result<SocketAddr, io::Error> {
        match self {
            #[cfg(target_family = "unix")]
            Stream::Unix(s) => s.peer_addr().map(|addr| addr.into()),
            Stream::Tcp(s) => s.peer_addr().map(|addr| addr.into()),
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => s.get_ref().peer_addr().map(|addr| addr.into()),
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => s.peer_addr().map(|addr| addr.into()),
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => s.get_ref().peer_addr().map(|addr| addr.into()),
        }
    }

    /// Returns the local address that this stream is bound to.
    ///
    /// ```
    /// use tokio::io;
    /// use url::Url;
    /// use prosa::io::stream::Stream;
    /// use prosa::io::SocketAddr;
    /// use std::net::{IpAddr, Ipv4Addr, SocketAddrV4};
    ///
    /// async fn accepting() -> Result<(), io::Error> {
    ///     let stream: Stream = Stream::connect_tcp("127.0.0.1:8080").await?;
    ///
    ///     assert_eq!(stream.local_addr()?.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
    ///
    ///     Ok(())
    /// }
    /// ```
    pub fn local_addr(&self) -> Result<SocketAddr, io::Error> {
        match self {
            #[cfg(target_family = "unix")]
            Stream::Unix(s) => s.local_addr().map(|addr| addr.into()),
            Stream::Tcp(s) => s.local_addr().map(|addr| addr.into()),
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => s.get_ref().local_addr().map(|addr| addr.into()),
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => s.local_addr().map(|addr| addr.into()),
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => s.get_ref().local_addr().map(|addr| addr.into()),
        }
    }

    #[cfg(target_family = "unix")]
    #[cfg_attr(doc, aquamarine::aquamarine)]
    /// Connect a UNIX socket on a path
    ///
    /// ```mermaid
    /// graph LR
    ///     client[Client]
    ///     server[Server]
    ///
    ///     client -- UNIX --> server
    /// ```
    ///
    /// ```
    /// use tokio::io;
    /// use url::Url;
    /// use prosa::io::stream::Stream;
    ///
    /// async fn connecting() -> Result<(), io::Error> {
    ///     let stream: Stream = Stream::connect_unix("/var/run/prosa.socket").await?;
    ///
    ///     // Handle the stream like any tokio stream
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn connect_unix<P>(path: P) -> Result<Stream, io::Error>
    where
        P: AsRef<Path>,
    {
        Ok(Stream::Unix(tokio::net::UnixStream::connect(path).await?))
    }

    #[cfg_attr(doc, aquamarine::aquamarine)]
    /// Connect a TCP socket to a distant
    ///
    /// ```mermaid
    /// graph LR
    ///     client[Client]
    ///     server[Server]
    ///
    ///     client -- TCP --> server
    /// ```
    ///
    /// ```
    /// use tokio::io;
    /// use url::Url;
    /// use prosa::io::stream::Stream;
    ///
    /// async fn connecting() -> Result<(), io::Error> {
    ///     let stream: Stream = Stream::connect_tcp("worldline.com:80").await?;
    ///
    ///     // Handle the stream like any tokio stream
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn connect_tcp<A>(addr: A) -> Result<Stream, io::Error>
    where
        A: ToSocketAddrs,
    {
        Ok(Stream::Tcp(TcpStream::connect(addr).await?))
    }

    #[cfg(feature = "openssl")]
    /// Method to create an SSL stream from a TCP stream
    async fn create_openssl<S>(
        tcp_stream: S,
        ssl_connector: &openssl::ssl::SslConnector,
        domain: &str,
    ) -> Result<tokio_openssl::SslStream<S>, io::Error>
    where
        S: AsyncRead + AsyncWrite + std::marker::Unpin,
    {
        let ssl = ssl_connector.configure()?.into_ssl(domain)?;
        let mut stream = tokio_openssl::SslStream::new(ssl, tcp_stream).unwrap();
        if let Err(e) = Pin::new(&mut stream).connect().await
            && e.code() != openssl::ssl::ErrorCode::ZERO_RETURN
        {
            return Err(io::Error::new(
                io::ErrorKind::Interrupted,
                format!("Can't connect the SSL socket `{e}`"),
            ));
        }

        Ok(stream)
    }

    #[cfg(feature = "openssl")]
    #[cfg_attr(doc, aquamarine::aquamarine)]
    /// Connect an OpenSSL socket to a distant
    ///
    /// ```mermaid
    /// graph LR
    ///     client[Client]
    ///     server[Server]
    ///
    ///     client -- TCP+TLS --> server
    /// ```
    ///
    /// ```
    /// use tokio::io;
    /// use url::Url;
    /// use prosa_utils::config::ssl::{SslConfig, SslConfigContext};
    /// use prosa::io::stream::Stream;
    ///
    /// async fn connecting() -> Result<(), io::Error> {
    ///     let ssl_config = SslConfig::default();
    ///     if let Ok(ssl_context_builder) = ssl_config.init_tls_client_context() {
    ///         let ssl_context = ssl_context_builder.build();
    ///         let stream: Stream = Stream::connect_openssl(&Url::parse("worldline.com:443").unwrap(), &ssl_context).await?;
    ///
    ///         // Handle the stream like any tokio stream
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn connect_openssl(
        url: &Url,
        ssl_context: &openssl::ssl::SslConnector,
    ) -> Result<Stream, io::Error> {
        let addrs = url.socket_addrs(|| url.port_or_known_default())?;
        Ok(Stream::OpenSsl(
            Self::create_openssl(
                TcpStream::connect(&*addrs).await?,
                ssl_context,
                url.host_str().ok_or(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    format!("Can't retrieve host from url `{url}` for ssl"),
                ))?,
            )
            .await?,
        ))
    }

    #[cfg(feature = "http-proxy")]
    /// Method to connect a TCP stream through an HTTP proxy
    async fn connect_http_proxy(
        host: &str,
        port: u16,
        proxy: &Url,
    ) -> Result<TcpStream, io::Error> {
        let proxy_addrs = proxy.socket_addrs(|| proxy.port_or_known_default())?;
        let mut tcp_stream = TcpStream::connect(&*proxy_addrs).await?;
        if let (username, Some(password)) = (proxy.username(), proxy.password()) {
            if let Err(e) = async_http_proxy::http_connect_tokio_with_basic_auth(
                &mut tcp_stream,
                host,
                port,
                username,
                password,
            )
            .await
            {
                return Err(io::Error::new(
                    io::ErrorKind::ConnectionAborted,
                    format!("Can't connect to the http proxy with basic_auth `{e}`"),
                ));
            }
        } else if let Err(e) =
            async_http_proxy::http_connect_tokio(&mut tcp_stream, host, port).await
        {
            return Err(io::Error::new(
                io::ErrorKind::ConnectionAborted,
                format!("Can't connect to the http proxy `{e}`"),
            ));
        }

        Ok(tcp_stream)
    }

    #[cfg(feature = "http-proxy")]
    #[cfg_attr(doc, aquamarine::aquamarine)]
    /// Connect a TCP socket to a distant through an HTTP proxy
    ///
    /// ```mermaid
    /// graph LR
    ///     client[Client]
    ///     server[Server]
    ///     proxy[Proxy]
    ///
    ///     client -- TCP --> proxy
    ///     proxy --> server
    /// ```
    ///
    /// ```
    /// use tokio::io;
    /// use url::Url;
    /// use prosa::io::stream::Stream;
    ///
    /// async fn connecting() -> Result<(), io::Error> {
    ///     let proxy_url = Url::parse("http://user:pwd@proxy:3128").unwrap();
    ///     let stream: Stream = Stream::connect_tcp_with_http_proxy("worldline.com", 443, &proxy_url).await?;
    ///
    ///     // Handle the stream like any tokio stream
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn connect_tcp_with_http_proxy(
        host: &str,
        port: u16,
        proxy: &Url,
    ) -> Result<Stream, io::Error> {
        Ok(Stream::TcpHttpProxy(
            Self::connect_http_proxy(host, port, proxy).await?,
        ))
    }

    #[cfg(all(feature = "openssl", feature = "http-proxy"))]
    #[cfg_attr(doc, aquamarine::aquamarine)]
    /// Connect an OpenSSL socket to a distant through an HTTP proxy
    ///
    /// ```mermaid
    /// graph LR
    ///     client[Client]
    ///     server[Server]
    ///     proxy[Proxy]
    ///
    ///     client -- TCP+TLS --> proxy
    ///     proxy --> server
    /// ```
    ///
    /// ```
    /// use tokio::io;
    /// use url::Url;
    /// use prosa_utils::config::ssl::{SslConfig, SslConfigContext};
    /// use prosa::io::stream::Stream;
    ///
    /// async fn connecting() -> Result<(), io::Error> {
    ///     let proxy_url = Url::parse("http://user:pwd@proxy:3128").unwrap();
    ///     let ssl_config = SslConfig::default();
    ///     if let Ok(ssl_context_builder) = ssl_config.init_tls_client_context() {
    ///         let ssl_context = ssl_context_builder.build();
    ///         let stream: Stream = Stream::connect_openssl_with_http_proxy("worldline.com", 443, &ssl_context, &proxy_url).await?;
    ///
    ///         // Handle the stream like any tokio stream
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn connect_openssl_with_http_proxy(
        host: &str,
        port: u16,
        ssl_connector: &openssl::ssl::SslConnector,
        proxy: &Url,
    ) -> Result<Stream, io::Error> {
        Ok(Stream::OpenSslHttpProxy(
            Self::create_openssl(
                Self::connect_http_proxy(host, port, proxy).await?,
                ssl_connector,
                host,
            )
            .await?,
        ))
    }

    /// Sets the value of the TCP_NODELAY option on the ProSA socket
    pub fn set_nodelay(&self, nodelay: bool) -> Result<(), io::Error> {
        match self {
            #[cfg(target_family = "unix")]
            Stream::Unix(_) => Ok(()),
            Stream::Tcp(s) => s.set_nodelay(nodelay),
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => s.get_ref().set_nodelay(nodelay),
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => s.set_nodelay(nodelay),
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => s.get_ref().set_nodelay(nodelay),
        }
    }

    /// Gets the value of the TCP_NODELAY option for the ProSA socket
    pub fn nodelay(&self) -> Result<bool, io::Error> {
        match self {
            #[cfg(target_family = "unix")]
            Stream::Unix(_) => Ok(true),
            Stream::Tcp(s) => s.nodelay(),
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => s.get_ref().nodelay(),
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => s.nodelay(),
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => s.get_ref().nodelay(),
        }
    }

    /// Sets the value for the IP_TTL option on the ProSA socket
    pub fn set_ttl(&self, ttl: u32) -> Result<(), io::Error> {
        match self {
            #[cfg(target_family = "unix")]
            Stream::Unix(_) => Ok(()),
            Stream::Tcp(s) => s.set_ttl(ttl),
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => s.get_ref().set_ttl(ttl),
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => s.set_ttl(ttl),
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => s.get_ref().set_ttl(ttl),
        }
    }

    /// Gets the value of the IP_TTL option for the ProSA socket
    pub fn ttl(&self) -> Result<u32, io::Error> {
        match self {
            #[cfg(target_family = "unix")]
            Stream::Unix(_) => Ok(0),
            Stream::Tcp(s) => s.ttl(),
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => s.get_ref().ttl(),
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => s.ttl(),
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => s.get_ref().ttl(),
        }
    }

    /// Method to know if the stream is SSL
    pub fn is_ssl(&self) -> bool {
        match self {
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(_) => true,
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(_) => true,
            _ => false,
        }
    }

    /// Method to check the protocol selected via Application Layer Protocol Negotiation (ALPN)
    ///
    /// ```
    /// use prosa::io::stream::Stream;
    ///
    /// async fn processing(stream: Stream) {
    ///     let is_http2 = stream.selected_alpn_check(|alpn| alpn == b"h2");
    ///     // is_http2 is true if the server sent the HTTP/2 ALPN value `h2`
    /// }
    /// ```
    pub fn selected_alpn_check<F>(&self, _f: F) -> bool
    where
        F: Fn(&[u8]) -> bool,
    {
        match self {
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => {
                if let Some(alpn) = s.ssl().selected_alpn_protocol() {
                    _f(alpn)
                } else {
                    false
                }
            }
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => {
                if let Some(alpn) = s.ssl().selected_alpn_protocol() {
                    _f(alpn)
                } else {
                    false
                }
            }
            _ => false,
        }
    }
}

impl AsFd for Stream {
    fn as_fd(&self) -> BorrowedFd<'_> {
        match self {
            #[cfg(target_family = "unix")]
            Stream::Unix(s) => s.as_fd(),
            Stream::Tcp(s) => s.as_fd(),
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => s.get_ref().as_fd(),
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => s.as_fd(),
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => s.get_ref().as_fd(),
        }
    }
}

impl AsRawFd for Stream {
    fn as_raw_fd(&self) -> RawFd {
        match self {
            #[cfg(target_family = "unix")]
            Stream::Unix(s) => s.as_raw_fd(),
            Stream::Tcp(s) => s.as_raw_fd(),
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => s.get_ref().as_raw_fd(),
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => s.as_raw_fd(),
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => s.get_ref().as_raw_fd(),
        }
    }
}

impl AsyncRead for Stream {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        match self.get_mut() {
            #[cfg(target_family = "unix")]
            Stream::Unix(s) => {
                let stream = Pin::new(s);
                stream.poll_read(cx, buf)
            }
            Stream::Tcp(s) => {
                let stream = Pin::new(s);
                stream.poll_read(cx, buf)
            }
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => {
                let stream = Pin::new(s);
                stream.poll_read(cx, buf)
            }
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => {
                let stream = Pin::new(s);
                stream.poll_read(cx, buf)
            }
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => {
                let stream = Pin::new(s);
                stream.poll_read(cx, buf)
            }
        }
    }
}

impl AsyncWrite for Stream {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        match self.get_mut() {
            #[cfg(target_family = "unix")]
            Stream::Unix(s) => {
                let stream = Pin::new(s);
                stream.poll_write(cx, buf)
            }
            Stream::Tcp(s) => {
                let stream = Pin::new(s);
                stream.poll_write(cx, buf)
            }
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => {
                let stream = Pin::new(s);
                stream.poll_write(cx, buf)
            }
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => {
                let stream = Pin::new(s);
                stream.poll_write(cx, buf)
            }
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => {
                let stream = Pin::new(s);
                stream.poll_write(cx, buf)
            }
        }
    }

    fn poll_write_vectored(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        bufs: &[io::IoSlice<'_>],
    ) -> Poll<io::Result<usize>> {
        match self.get_mut() {
            #[cfg(target_family = "unix")]
            Stream::Unix(s) => {
                let stream = Pin::new(s);
                stream.poll_write_vectored(cx, bufs)
            }
            Stream::Tcp(s) => {
                let stream = Pin::new(s);
                stream.poll_write_vectored(cx, bufs)
            }
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => {
                let stream = Pin::new(s);
                stream.poll_write_vectored(cx, bufs)
            }
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => {
                let stream = Pin::new(s);
                stream.poll_write_vectored(cx, bufs)
            }
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => {
                let stream = Pin::new(s);
                stream.poll_write_vectored(cx, bufs)
            }
        }
    }

    fn is_write_vectored(&self) -> bool {
        match self {
            #[cfg(target_family = "unix")]
            Stream::Unix(s) => s.is_write_vectored(),
            Stream::Tcp(s) => s.is_write_vectored(),
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => s.is_write_vectored(),
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => s.is_write_vectored(),
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => s.is_write_vectored(),
        }
    }

    #[inline]
    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        match self.get_mut() {
            #[cfg(target_family = "unix")]
            Stream::Unix(s) => {
                let stream = Pin::new(s);
                stream.poll_flush(cx)
            }
            Stream::Tcp(s) => {
                let stream = Pin::new(s);
                stream.poll_flush(cx)
            }
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => {
                let stream = Pin::new(s);
                stream.poll_flush(cx)
            }
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => {
                let stream = Pin::new(s);
                stream.poll_flush(cx)
            }
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => {
                let stream = Pin::new(s);
                stream.poll_flush(cx)
            }
        }
    }

    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        match self.get_mut() {
            #[cfg(target_family = "unix")]
            Stream::Unix(s) => {
                let stream = Pin::new(s);
                stream.poll_shutdown(cx)
            }
            Stream::Tcp(s) => {
                let stream = Pin::new(s);
                stream.poll_shutdown(cx)
            }
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(s) => {
                let stream = Pin::new(s);
                stream.poll_shutdown(cx)
            }
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(s) => {
                let stream = Pin::new(s);
                stream.poll_shutdown(cx)
            }
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(s) => {
                let stream = Pin::new(s);
                stream.poll_shutdown(cx)
            }
        }
    }
}

impl fmt::Display for Stream {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let addr = self
            .local_addr()
            .unwrap_or(SocketAddr::V4(SocketAddrV4::new(
                Ipv4Addr::new(0, 0, 0, 0),
                0,
            )));
        match self {
            #[cfg(target_family = "unix")]
            Stream::Unix(_) => write!(f, "unix://{addr}"),
            Stream::Tcp(_) => write!(f, "tcp://{addr}"),
            #[cfg(feature = "openssl")]
            Stream::OpenSsl(_) => write!(f, "ssl://{addr}"),
            #[cfg(feature = "http-proxy")]
            Stream::TcpHttpProxy(_) => write!(f, "tcp+http_proxy://{addr}"),
            #[cfg(all(feature = "openssl", feature = "http-proxy"))]
            Stream::OpenSslHttpProxy(_) => write!(f, "ssl+http_proxy://{addr}"),
        }
    }
}

#[cfg(target_family = "unix")]
impl From<tokio::net::UnixStream> for Stream {
    fn from(stream: tokio::net::UnixStream) -> Self {
        Stream::Unix(stream)
    }
}

impl From<TcpStream> for Stream {
    fn from(stream: TcpStream) -> Self {
        Stream::Tcp(stream)
    }
}

#[cfg(feature = "openssl")]
impl From<tokio_openssl::SslStream<TcpStream>> for Stream {
    fn from(openssl_stream: tokio_openssl::SslStream<TcpStream>) -> Self {
        Stream::OpenSsl(openssl_stream)
    }
}

/// Configuration struct of an network target
///
/// ```
/// use tokio::io;
/// use url::Url;
/// use prosa::io::stream::{TargetSetting, Stream};
///
/// async fn connecting() -> Result<(), io::Error> {
///     let wl_target = TargetSetting::new(Url::parse("https://worldline.com").unwrap(), None, None);
///     let stream: Stream = wl_target.connect().await?;
///
///     // Handle the stream like any tokio stream
///
///     Ok(())
/// }
/// ```
#[derive(Deserialize, Serialize, Clone)]
pub struct TargetSetting {
    /// Url of the target destination
    pub url: Url,
    /// SSL configuration for target destination
    pub ssl: Option<SslConfig>,
    /// Optional proxy use to reach the target
    pub proxy: Option<Url>,
    #[cfg(feature = "openssl")]
    #[serde(skip)]
    /// OpenSSL configuration for target destination
    openssl_context: Option<::openssl::ssl::SslConnector>,
    #[serde(skip_serializing)]
    #[serde(default = "TargetSetting::get_default_connect_timeout")]
    /// Timeout for socket connection in milliseconds
    pub connect_timeout: u64,
}

impl TargetSetting {
    fn get_default_connect_timeout() -> u64 {
        5000
    }

    /// Method to create manually a target
    pub fn new(url: Url, ssl: Option<SslConfig>, proxy: Option<Url>) -> TargetSetting {
        let mut target = TargetSetting {
            url,
            ssl,
            proxy,
            #[cfg(feature = "openssl")]
            openssl_context: None,
            connect_timeout: Self::get_default_connect_timeout(),
        };

        target.init_ssl_context();
        target
    }

    /// Method to know if the target will be connected with SSL
    pub fn is_ssl(&self) -> bool {
        #[cfg(feature = "openssl")]
        if self.openssl_context.is_some() {
            return true;
        }

        self.ssl.is_some() || url_is_ssl(&self.url)
    }

    /// Getter of the URL with masked inner credential
    pub fn get_safe_url(&self) -> Url {
        let mut url = self.url.clone();
        url.set_query(None);
        if !url.username().is_empty() {
            let _ = url.set_username("***");
        }
        if url.password().is_some() {
            let _ = url.set_password(Some("***"));
        }

        url
    }

    /// Method to get authentication value out of URL username/password
    ///
    /// - If user password is provided, it return *Basic* authentication with base64 encoded username:password
    /// - If only password is provided, it return *Bearer* authentication with the password as token
    ///
    /// ```
    /// use url::Url;
    /// use prosa::io::stream::TargetSetting;
    ///
    /// let basic_auth_target = TargetSetting::from(Url::parse("http://user:pass@localhost:8080").unwrap());
    /// assert_eq!(Some(String::from("Basic dXNlcjpwYXNz")), basic_auth_target.get_authentication());
    ///
    /// let bearer_auth_target = TargetSetting::from(Url::parse("http://:token@localhost:8080").unwrap());
    /// assert_eq!(Some(String::from("Bearer token")), bearer_auth_target.get_authentication());
    /// ```
    pub fn get_authentication(&self) -> Option<String> {
        url_authentication(&self.url)
    }

    /// Method to init the ssl context out of the ssl target configuration.
    /// Must be call when the configuration is retrieved
    pub fn init_ssl_context(&mut self) {
        #[cfg(feature = "openssl")]
        if let Some(ssl_config) = self.ssl.as_ref() {
            // Init OpenSSL context by default
            let ssl_context_builder: Option<openssl::ssl::SslConnectorBuilder> =
                SslConfigContext::init_tls_client_context(ssl_config).ok();
            self.openssl_context = ssl_context_builder.map(|c| c.build());
        }
    }

    /// Method to connect a ProSA stream to the remote target using the configuration
    pub async fn connect(&self) -> Result<Stream, io::Error> {
        #[cfg(target_family = "unix")]
        if self.url.scheme() == "unix" || self.url.scheme() == "file" {
            return timeout(
                Duration::from_millis(self.connect_timeout),
                Stream::connect_unix(self.url.path()),
            )
            .await
            .map_err(|e| {
                io::Error::new(
                    io::ErrorKind::TimedOut,
                    format!("unix timeout after {e} for {}", self.get_safe_url()),
                )
            })?;
        }

        #[cfg(feature = "openssl")]
        let openssl_context = if self.openssl_context.is_some() {
            self.openssl_context.clone()
        } else if let Some(ssl_config) = &self.ssl {
            let ssl_context_builder: Option<openssl::ssl::SslConnectorBuilder> =
                SslConfigContext::init_tls_client_context(ssl_config).ok();
            ssl_context_builder.map(|c| c.build())
        } else if url_is_ssl(&self.url) {
            let ssl_config = SslConfig::default();
            let ssl_context_builder: Option<openssl::ssl::SslConnectorBuilder> =
                SslConfigContext::init_tls_client_context(&ssl_config).ok();
            ssl_context_builder.map(|c| c.build())
        } else {
            None
        };

        if let Some(proxy_url) = &self.proxy {
            if proxy_url.scheme() == "http" {
                #[cfg(feature = "http-proxy")]
                {
                    #[cfg(feature = "openssl")]
                    if let Some(ssl_cx) = openssl_context {
                        return timeout(
                            Duration::from_millis(self.connect_timeout),
                            Stream::connect_openssl_with_http_proxy(
                                self.url.host_str().unwrap_or_default(),
                                self.url.port_or_known_default().unwrap_or_default(),
                                &ssl_cx,
                                proxy_url,
                            ),
                        )
                        .await
                        .map_err(|e| {
                            io::Error::new(
                                io::ErrorKind::TimedOut,
                                format!(
                                    "openssl with proxy timeout after {e} for {}",
                                    self.get_safe_url()
                                ),
                            )
                        })?;
                    }

                    return timeout(
                        Duration::from_millis(self.connect_timeout),
                        Stream::connect_tcp_with_http_proxy(
                            self.url.host_str().unwrap_or_default(),
                            self.url.port_or_known_default().unwrap_or_default(),
                            proxy_url,
                        ),
                    )
                    .await
                    .map_err(|e| {
                        io::Error::new(
                            io::ErrorKind::TimedOut,
                            format!(
                                "tcp with proxy timeout after {e} for {}",
                                self.get_safe_url()
                            ),
                        )
                    })?;
                }

                #[cfg(not(feature = "http-proxy"))]
                return Err(io::Error::new(
                    io::ErrorKind::Unsupported,
                    "http-proxy feature is disable in ProSA",
                ));
            } else {
                return Err(io::Error::new(
                    io::ErrorKind::Unsupported,
                    format!("proxy type {}", proxy_url.scheme()),
                ));
            }
        }

        #[cfg(feature = "openssl")]
        if let Some(ssl_cx) = openssl_context {
            return timeout(
                Duration::from_millis(self.connect_timeout),
                Stream::connect_openssl(&self.url, &ssl_cx),
            )
            .await
            .map_err(|e| {
                io::Error::new(
                    io::ErrorKind::TimedOut,
                    format!("openssl timeout after {e} for {}", self.get_safe_url()),
                )
            })?;
        }

        let addrs = self.url.socket_addrs(|| self.url.port_or_known_default())?;
        timeout(
            Duration::from_millis(self.connect_timeout),
            Stream::connect_tcp(&*addrs),
        )
        .await
        .map_err(|e| {
            io::Error::new(
                io::ErrorKind::TimedOut,
                format!("tcp timeout after {e} for {}", self.get_safe_url()),
            )
        })?
    }
}

impl From<Url> for TargetSetting {
    fn from(url: Url) -> Self {
        TargetSetting {
            url,
            ssl: None,
            proxy: None,
            #[cfg(feature = "openssl")]
            openssl_context: None,
            connect_timeout: Self::get_default_connect_timeout(),
        }
    }
}

impl fmt::Debug for TargetSetting {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut binding = f.debug_struct("TargetSetting");
        binding
            .field("url", &self.get_safe_url())
            .field("ssl", &self.ssl)
            .field("connect_timeout", &self.connect_timeout);
        if let Some(proxy_url) = &self.proxy {
            binding.field("proxy", proxy_url);
        }
        binding.finish()
    }
}

impl fmt::Display for TargetSetting {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut url = self.get_safe_url();
        if self.ssl.is_some() {
            let url_scheme = url.scheme();
            if url_scheme.is_empty() {
                let _ = url.set_scheme("ssl");
            } else if !url_scheme.ends_with("ssl")
                && !url_scheme.ends_with("tls")
                && !url_scheme.ends_with("https")
                && !url_scheme.ends_with("wss")
            {
                let _ = url.set_scheme(format!("{url_scheme}+ssl").as_str());
            }
        }

        if f.alternate() {
            if let Some(proxy_url) = &self.proxy {
                write!(f, "{url} -proxy {proxy_url}")
            } else {
                write!(f, "{url}")
            }
        } else {
            // Remove username and password for more visibility
            let _ = url.set_username("");
            let _ = url.set_password(None);
            write!(f, "{url}")
        }
    }
}

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

    #[test]
    fn target_settings_test() {
        let target_without_credential = TargetSetting::new(
            Url::parse("https://localhost:4443/v1?var=1").unwrap(),
            None,
            None,
        );
        assert_eq!(
            "https://localhost:4443/v1",
            target_without_credential.to_string()
        );
        assert_eq!(
            "https://localhost:4443/v1",
            format!("{target_without_credential:#}")
        );

        let target_with_user_password = TargetSetting::new(
            Url::parse("https://admin:admin@localhost:4443/v1?user=admin&password=admin").unwrap(),
            None,
            None,
        );
        assert_eq!(
            "https://localhost:4443/v1",
            target_with_user_password.to_string()
        );
        assert_eq!(
            "https://***:***@localhost:4443/v1",
            format!("{target_with_user_password:#}")
        );
        assert_eq!(
            "TargetSetting { url: Url { scheme: \"https\", cannot_be_a_base: false, username: \"***\", password: Some(\"***\"), host: Some(Domain(\"localhost\")), port: Some(4443), path: \"/v1\", query: None, fragment: None }, ssl: None, connect_timeout: 5000 }",
            format!("{target_with_user_password:?}")
        );

        let target_with_token = TargetSetting::new(
            Url::parse("https://:token@localhost:4443/v1").unwrap(),
            None,
            None,
        );
        assert_eq!("https://localhost:4443/v1", target_with_token.to_string());
        assert_eq!(
            "https://:***@localhost:4443/v1",
            format!("{target_with_token:#}")
        );
        assert_eq!(
            "TargetSetting { url: Url { scheme: \"https\", cannot_be_a_base: false, username: \"\", password: Some(\"***\"), host: Some(Domain(\"localhost\")), port: Some(4443), path: \"/v1\", query: None, fragment: None }, ssl: None, connect_timeout: 5000 }",
            format!("{target_with_token:?}")
        );
    }
}