proxychains_masq/proxy/
https.rs1use std::{net::IpAddr, sync::Arc};
9
10use anyhow::{Context, Result};
11use rustls::{
12 client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier},
13 pki_types::{CertificateDer, IpAddr as PkiIpAddr, ServerName, UnixTime},
14 DigitallySignedStruct, Error as TlsError, SignatureScheme,
15};
16use tokio_rustls::TlsConnector;
17
18use super::{http, BoxStream, Target};
19
20#[derive(Debug)]
28struct SkipCertVerify;
29
30impl ServerCertVerifier for SkipCertVerify {
31 fn verify_server_cert(
32 &self,
33 _end_entity: &CertificateDer<'_>,
34 _intermediates: &[CertificateDer<'_>],
35 _server_name: &ServerName<'_>,
36 _ocsp_response: &[u8],
37 _now: UnixTime,
38 ) -> Result<ServerCertVerified, TlsError> {
39 Ok(ServerCertVerified::assertion())
40 }
41
42 fn verify_tls12_signature(
43 &self,
44 _message: &[u8],
45 _cert: &CertificateDer<'_>,
46 _dss: &DigitallySignedStruct,
47 ) -> Result<HandshakeSignatureValid, TlsError> {
48 Ok(HandshakeSignatureValid::assertion())
49 }
50
51 fn verify_tls13_signature(
52 &self,
53 _message: &[u8],
54 _cert: &CertificateDer<'_>,
55 _dss: &DigitallySignedStruct,
56 ) -> Result<HandshakeSignatureValid, TlsError> {
57 Ok(HandshakeSignatureValid::assertion())
58 }
59
60 fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
61 rustls::crypto::ring::default_provider()
63 .signature_verification_algorithms
64 .supported_schemes()
65 }
66}
67
68pub async fn connect(
88 stream: BoxStream,
89 target: &Target,
90 username: Option<&str>,
91 password: Option<&str>,
92 proxy_addr: IpAddr,
93) -> Result<BoxStream> {
94 let tls_stream = tls_wrap(stream, proxy_addr).await?;
95 http::connect(Box::new(tls_stream), target, username, password).await
97}
98
99async fn tls_wrap(
102 stream: BoxStream,
103 proxy_addr: IpAddr,
104) -> Result<tokio_rustls::client::TlsStream<BoxStream>> {
105 let config = Arc::new(
106 rustls::ClientConfig::builder()
107 .dangerous()
108 .with_custom_certificate_verifier(Arc::new(SkipCertVerify))
109 .with_no_client_auth(),
110 );
111
112 let server_name: ServerName<'static> = match proxy_addr {
113 IpAddr::V4(v4) => ServerName::IpAddress(PkiIpAddr::V4(v4.octets().into())),
114 IpAddr::V6(v6) => ServerName::IpAddress(PkiIpAddr::V6(v6.into())),
115 };
116
117 TlsConnector::from(config)
118 .connect(server_name, stream)
119 .await
120 .context("HTTPS proxy: TLS handshake failed")
121}