Skip to content

Commit d2347cc

Browse files
committed
build: Clean up "proxy" feature
Before, the `client` module was gated on the "proxy" feature, which caused unnecessary coupling since proxy is only required for the Socks5 client type. This change removes the client module's dependency on the "proxy" feature in favor of more precise feature gating of the Socks5 client type and related functions calling into the `socks` module. Note that `Client` still requires a TLS backend to be enabled by one of "use-openssl", "use-rustls" or "use-rustls-ring".
1 parent b185259 commit d2347cc

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

src/client.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ use std::convert::TryFrom;
1717
/// [`RawClient`](client/struct.RawClient.html) and provides a more user-friendly
1818
/// constructor that can choose the right backend based on the url prefix.
1919
///
20-
/// **This is available only with the `default` features, or if `proxy` and one ssl implementation are enabled**
20+
/// **Note the `Socks5` client type requires the "proxy" feature to be enabled.**
2121
pub enum ClientType {
2222
#[allow(missing_docs)]
2323
TCP(RawClient<ElectrumPlaintextStream>),
2424
#[allow(missing_docs)]
2525
SSL(RawClient<ElectrumSslStream>),
2626
#[allow(missing_docs)]
27+
#[cfg(feature = "proxy")]
2728
Socks5(RawClient<ElectrumProxyStream>),
2829
}
2930

@@ -44,6 +45,7 @@ macro_rules! impl_inner_call {
4445
let res = match &*read_client {
4546
ClientType::TCP(inner) => inner.$name( $($args, )* ),
4647
ClientType::SSL(inner) => inner.$name( $($args, )* ),
48+
#[cfg(feature = "proxy")]
4749
ClientType::Socks5(inner) => inner.$name( $($args, )* ),
4850
};
4951
drop(read_client);
@@ -110,6 +112,7 @@ impl ClientType {
110112
pub fn from_config(url: &str, config: &Config) -> Result<Self, Error> {
111113
if url.starts_with("ssl://") {
112114
let url = url.replacen("ssl://", "", 1);
115+
#[cfg(feature = "proxy")]
113116
let client = match config.socks5() {
114117
Some(socks5) => RawClient::new_proxy_ssl(
115118
url.as_str(),
@@ -121,19 +124,28 @@ impl ClientType {
121124
RawClient::new_ssl(url.as_str(), config.validate_domain(), config.timeout())?
122125
}
123126
};
127+
#[cfg(not(feature = "proxy"))]
128+
let client =
129+
RawClient::new_ssl(url.as_str(), config.validate_domain(), config.timeout())?;
124130

125131
Ok(ClientType::SSL(client))
126132
} else {
127133
let url = url.replacen("tcp://", "", 1);
128134

129-
Ok(match config.socks5().as_ref() {
130-
None => ClientType::TCP(RawClient::new(url.as_str(), config.timeout())?),
135+
#[cfg(feature = "proxy")]
136+
let client = match config.socks5() {
131137
Some(socks5) => ClientType::Socks5(RawClient::new_proxy(
132138
url.as_str(),
133139
socks5,
134140
config.timeout(),
135141
)?),
136-
})
142+
None => ClientType::TCP(RawClient::new(url.as_str(), config.timeout())?),
143+
};
144+
145+
#[cfg(not(feature = "proxy"))]
146+
let client = ClientType::TCP(RawClient::new(url.as_str(), config.timeout())?);
147+
148+
Ok(client)
137149
}
138150
}
139151
}

src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,24 @@ extern crate serde_json;
4343
))]
4444
extern crate webpki_roots;
4545

46-
#[cfg(any(feature = "default", feature = "proxy"))]
46+
#[cfg(feature = "proxy")]
4747
extern crate byteorder;
4848

49-
#[cfg(all(unix, any(feature = "default", feature = "proxy")))]
49+
#[cfg(all(unix, feature = "proxy"))]
5050
extern crate libc;
51-
#[cfg(all(windows, any(feature = "default", feature = "proxy")))]
51+
#[cfg(all(windows, feature = "proxy"))]
5252
extern crate winapi;
5353

54-
#[cfg(any(feature = "default", feature = "proxy"))]
54+
#[cfg(feature = "proxy")]
5555
pub mod socks;
5656

5757
mod api;
5858
mod batch;
5959

6060
#[cfg(any(
61-
all(feature = "proxy", feature = "use-openssl"),
62-
all(feature = "proxy", feature = "use-rustls"),
63-
all(feature = "proxy", feature = "use-rustls-ring")
61+
feature = "use-openssl",
62+
feature = "use-rustls",
63+
feature = "use-rustls-ring",
6464
))]
6565
pub mod client;
6666

@@ -74,9 +74,9 @@ pub mod utils;
7474
pub use api::ElectrumApi;
7575
pub use batch::Batch;
7676
#[cfg(any(
77-
all(feature = "proxy", feature = "use-openssl"),
78-
all(feature = "proxy", feature = "use-rustls"),
79-
all(feature = "proxy", feature = "use-rustls-ring")
77+
feature = "use-openssl",
78+
feature = "use-rustls",
79+
feature = "use-rustls-ring",
8080
))]
8181
pub use client::*;
8282
pub use config::{Config, ConfigBuilder, Socks5Config};

src/raw_client.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,10 @@ impl RawClient<ElectrumSslStream> {
470470
}
471471
}
472472

473-
#[cfg(any(feature = "default", feature = "proxy"))]
473+
#[cfg(feature = "proxy")]
474474
/// Transport type used to establish a connection to a server through a socks proxy
475475
pub type ElectrumProxyStream = Socks5Stream;
476-
#[cfg(any(feature = "default", feature = "proxy"))]
476+
#[cfg(feature = "proxy")]
477477
impl RawClient<ElectrumProxyStream> {
478478
/// Creates a new socks client and tries to connect to `target_addr` using `proxy_addr` as a
479479
/// socks proxy server. The DNS resolution of `target_addr`, if required, is done
@@ -499,10 +499,13 @@ impl RawClient<ElectrumProxyStream> {
499499
Ok(stream.into())
500500
}
501501

502-
#[cfg(any(
503-
feature = "use-openssl",
504-
feature = "use-rustls",
505-
feature = "use-rustls-ring"
502+
#[cfg(all(
503+
any(
504+
feature = "use-openssl",
505+
feature = "use-rustls",
506+
feature = "use-rustls-ring",
507+
),
508+
feature = "proxy",
506509
))]
507510
/// Creates a new TLS client that connects to `target_addr` using `proxy_addr` as a socks proxy
508511
/// server. The DNS resolution of `target_addr`, if required, is done through the proxy. This

0 commit comments

Comments
 (0)