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
#[cfg(feature = "http-proxy")]
use super::try_http_proxy_connect;
use super::{try_http2_connect, try_tcp_connect, try_tls_connect};
#[cfg(feature = "socks5")]
use super::{try_socks5_connect, try_socks5h_connect};
use crate::DohResult;
use bytes::Bytes;
use h2::client::SendRequest;
use rustls::ClientConfig;
use std::fmt::{Display, Formatter, Result};
use std::sync::Arc;

pub enum Host {
    Direct(String, u16),
    #[cfg(feature = "socks5")]
    Socks5(
        String,
        u16,
        Option<(String, String)>,
        Vec<std::net::SocketAddr>,
    ),
    #[cfg(feature = "socks5")]
    Socks5h(String, u16, Option<(String, String)>, String, u16),
    #[cfg(feature = "http-proxy")]
    HttpProxy(String, u16, Option<(String, String)>, String, u16),
    #[cfg(feature = "http-proxy")]
    HttpsProxy(
        String,
        u16,
        Option<(String, String)>,
        String,
        u16,
        Arc<ClientConfig>,
        String,
    ),
}

impl Host {
    pub(super) async fn connect(
        &mut self,
        client_config: &Arc<ClientConfig>,
        domain: &str,
    ) -> DohResult<SendRequest<Bytes>> {
        match self {
            Host::Direct(remote_host, remote_port) => {
                let tcp_connection = try_tcp_connect(remote_host, *remote_port).await?;
                let tls_connection = try_tls_connect(tcp_connection, client_config, domain).await?;
                let http2_connection = try_http2_connect(tls_connection).await?;
                Ok(http2_connection)
            }
            #[cfg(feature = "socks5")]
            Host::Socks5(proxy_host, proxy_port, credentials, remote_addrs) => {
                let tcp_connection =
                    try_socks5_connect(proxy_host, *proxy_port, remote_addrs, credentials).await?;
                let tls_connection = try_tls_connect(tcp_connection, client_config, domain).await?;
                let http2_connection = try_http2_connect(tls_connection).await?;
                Ok(http2_connection)
            }
            #[cfg(feature = "socks5")]
            Host::Socks5h(proxy_host, proxy_port, credentials, remote_host, remote_port) => {
                let tcp_connection = try_socks5h_connect(
                    proxy_host,
                    *proxy_port,
                    remote_host,
                    *remote_port,
                    credentials,
                )
                .await?;
                let tls_connection = try_tls_connect(tcp_connection, client_config, domain).await?;
                let http2_connection = try_http2_connect(tls_connection).await?;
                Ok(http2_connection)
            }
            #[cfg(feature = "http-proxy")]
            Host::HttpProxy(proxy_host, proxy_port, credentials, remote_host, remote_port) => {
                let mut tcp_connection = try_tcp_connect(proxy_host, *proxy_port).await?;
                try_http_proxy_connect(&mut tcp_connection, remote_host, *remote_port, credentials)
                    .await?;
                let tls_connection = try_tls_connect(tcp_connection, client_config, domain).await?;
                let http2_connection = try_http2_connect(tls_connection).await?;
                Ok(http2_connection)
            }
            #[cfg(feature = "http-proxy")]
            Host::HttpsProxy(
                proxy_host,
                proxy_port,
                credentials,
                remote_host,
                remote_port,
                https_client_config,
                https_domain,
            ) => {
                let tcp_connection = try_tcp_connect(proxy_host, *proxy_port).await?;
                let mut tls_connection =
                    try_tls_connect(tcp_connection, https_client_config, https_domain).await?;
                try_http_proxy_connect(&mut tls_connection, remote_host, *remote_port, credentials)
                    .await?;
                let tls_connection = try_tls_connect(tls_connection, client_config, domain).await?;
                let http2_connection = try_http2_connect(tls_connection).await?;
                Ok(http2_connection)
            }
        }
    }
}

impl Display for Host {
    fn fmt(&self, f: &mut Formatter) -> Result {
        match self {
            Host::Direct(remote_host, remote_port) => write!(f, "{}:{}", remote_host, remote_port),
            #[cfg(feature = "socks5")]
            Host::Socks5(proxy_host, proxy_port, _, remote_addrs) => {
                write!(
                    f,
                    "{}:{} via socks5 {:?}",
                    proxy_host, proxy_port, remote_addrs
                )
            }
            #[cfg(feature = "socks5")]
            Host::Socks5h(proxy_host, proxy_port, _, remote_host, remote_port) => {
                write!(
                    f,
                    "{}:{} via socks5h {}:{}",
                    proxy_host, proxy_port, remote_host, remote_port
                )
            }
            #[cfg(feature = "http-proxy")]
            Host::HttpProxy(proxy_host, proxy_port, _, remote_host, remote_port) => {
                write!(
                    f,
                    "{}:{} via http {}:{}",
                    proxy_host, proxy_port, remote_host, remote_port
                )
            }
            #[cfg(feature = "http-proxy")]
            Host::HttpsProxy(proxy_host, proxy_port, _, remote_host, remote_port, _, _) => {
                write!(
                    f,
                    "{}:{} via https {}:{}",
                    proxy_host, proxy_port, remote_host, remote_port
                )
            }
        }
    }
}