-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathhttps_client_with_sni.rs
More file actions
489 lines (445 loc) · 17.3 KB
/
https_client_with_sni.rs
File metadata and controls
489 lines (445 loc) · 17.3 KB
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
use crate::{
DnsResolver,
abortable_stream::{AbortableStream, AbortableStreamHandle},
proxy::{ApiConnection, ApiConnectionMode, ProxyConfig},
tls_stream::TlsStream,
};
use futures::{StreamExt, channel::mpsc, future, pin_mut};
#[cfg(target_os = "android")]
use futures::{channel::oneshot, sink::SinkExt};
use http::uri::Scheme;
use hyper::Uri;
use mullvad_encrypted_dns_proxy::{
Forwarder as EncryptedDNSForwarder, config::ProxyConfig as EncryptedDNSConfig,
};
use shadowsocks::{
ServerConfig,
config::{ServerConfigError, ServerType},
context::{Context as SsContext, SharedContext},
crypto::CipherKind,
relay::tcprelay::ProxyClientStream,
};
#[cfg(target_os = "android")]
use std::os::unix::io::{AsRawFd, RawFd};
use std::{
fmt,
future::Future,
io,
net::{IpAddr, SocketAddr},
str::{self, FromStr},
sync::{Arc, Mutex},
time::Duration,
};
use talpid_types::{ErrorExt, net::proxy};
use tokio::{
io::{AsyncRead, AsyncWrite},
net::{TcpSocket, TcpStream},
time::timeout,
};
#[cfg(any(feature = "api-override", test))]
use crate::proxy::ConnectionDecorator;
const CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
#[derive(Clone)]
pub struct HttpsConnectorWithSniHandle {
tx: mpsc::UnboundedSender<HttpsConnectorRequest>,
}
impl HttpsConnectorWithSniHandle {
/// Stop all streams produced by this connector
pub fn reset(&self) {
let _ = self.tx.unbounded_send(HttpsConnectorRequest::Reset);
}
/// Change the proxy settings for the connector
pub fn set_connection_mode(&self, proxy: ApiConnectionMode) {
let _ = self
.tx
.unbounded_send(HttpsConnectorRequest::SetConnectionMode(proxy));
}
}
enum HttpsConnectorRequest {
Reset,
SetConnectionMode(ApiConnectionMode),
}
#[derive(Clone)]
enum InnerConnectionMode {
/// Connect directly to the target.
Direct,
/// Connect to the destination via a Shadowsocks proxy.
Shadowsocks(ShadowsocksConfig),
/// Connect to the destination via a Socks proxy.
Socks5(SocksConfig),
/// Connect to the destination via Mullvad Encrypted DNS proxy.
/// See [`mullvad-encrypted-dns-proxy`] for how the proxy works.
EncryptedDnsProxy(EncryptedDNSConfig),
}
impl InnerConnectionMode {
async fn connect(
self,
hostname: &str,
addr: &SocketAddr,
#[cfg(target_os = "android")] socket_bypass_tx: Option<mpsc::Sender<SocketBypassRequest>>,
#[cfg(any(feature = "api-override", test))] disable_tls: bool,
) -> Result<ApiConnection, std::io::Error> {
match self {
// Set up a TCP-socket connection.
InnerConnectionMode::Direct => {
let first_hop = *addr;
let make_proxy_stream = |tcp_stream| async { Ok(tcp_stream) };
Self::connect_proxied(
first_hop,
hostname,
make_proxy_stream,
#[cfg(target_os = "android")]
socket_bypass_tx,
#[cfg(any(feature = "api-override", test))]
disable_tls,
)
.await
}
// Set up a Shadowsocks-connection.
InnerConnectionMode::Shadowsocks(shadowsocks) => {
let first_hop = shadowsocks.params.peer;
let make_proxy_stream = |tcp_stream| async {
Ok(ProxyClientStream::from_stream(
shadowsocks.proxy_context,
tcp_stream,
&ServerConfig::try_from(shadowsocks.params)
.map_err(|_| std::io::Error::other("Invalid shadowsocks config"))?,
*addr,
))
};
Self::connect_proxied(
first_hop,
hostname,
make_proxy_stream,
#[cfg(target_os = "android")]
socket_bypass_tx,
#[cfg(any(feature = "api-override", test))]
disable_tls,
)
.await
}
// Set up a SOCKS5-connection.
InnerConnectionMode::Socks5(socks) => {
let first_hop = socks.peer;
let make_proxy_stream = |tcp_stream| async {
match socks.authentication {
None => {
tokio_socks::tcp::Socks5Stream::connect_with_socket(tcp_stream, addr)
.await
}
Some(credentials) => {
tokio_socks::tcp::Socks5Stream::connect_with_password_and_socket(
tcp_stream,
addr,
credentials.username(),
credentials.password(),
)
.await
}
}
.map_err(|error| io::Error::other(format!("SOCKS error: {error}")))
};
Self::connect_proxied(
first_hop,
hostname,
make_proxy_stream,
#[cfg(target_os = "android")]
socket_bypass_tx,
#[cfg(any(feature = "api-override", test))]
disable_tls,
)
.await
}
InnerConnectionMode::EncryptedDnsProxy(proxy_config) => {
let first_hop = SocketAddr::V4(proxy_config.addr);
let make_proxy_stream = |tcp_stream| async {
let forwarder = EncryptedDNSForwarder::from_stream(&proxy_config, tcp_stream);
Ok(forwarder)
};
Self::connect_proxied(
first_hop,
hostname,
make_proxy_stream,
#[cfg(target_os = "android")]
socket_bypass_tx,
#[cfg(any(feature = "api-override", test))]
disable_tls,
)
.await
}
}
}
/// Create an [`ApiConnection`] from a [`TcpStream`].
///
/// The `make_proxy_stream` closure receives a [`TcpStream`] and produces a
/// stream which can send to and receive data from some server using any
/// proxy protocol. The only restriction is that this stream must implement
/// [`tokio::io::AsyncRead`] and [`tokio::io::AsyncWrite`], as well as
/// [`Unpin`] and [`Send`].
///
/// If a direct connection is to be established (i.e. the stream will not be
/// using any proxy protocol) `make_proxy_stream` may return the
/// [`TcpStream`] itself. See for example how a connection is established
/// from connection mode [`InnerConnectionMode::Direct`].
async fn connect_proxied<ProxyFactory, ProxyFuture, Proxy>(
first_hop: SocketAddr,
hostname: &str,
make_proxy_stream: ProxyFactory,
#[cfg(target_os = "android")] socket_bypass_tx: Option<mpsc::Sender<SocketBypassRequest>>,
#[cfg(any(feature = "api-override", test))] disable_tls: bool,
) -> Result<ApiConnection, io::Error>
where
ProxyFactory: FnOnce(TcpStream) -> ProxyFuture,
ProxyFuture: Future<Output = io::Result<Proxy>>,
Proxy: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
let socket = HttpsConnectorWithSni::open_socket(
first_hop,
#[cfg(target_os = "android")]
socket_bypass_tx,
)
.await?;
let proxy = make_proxy_stream(socket).await?;
#[cfg(any(feature = "api-override", test))]
if disable_tls {
return Ok(ApiConnection::new(Box::new(ConnectionDecorator(proxy))));
}
let tls_stream = TlsStream::connect_https(proxy, hostname).await?;
Ok(ApiConnection::new(Box::new(tls_stream)))
}
}
#[derive(Clone)]
struct ShadowsocksConfig {
proxy_context: SharedContext,
params: ParsedShadowsocksConfig,
}
#[derive(Clone)]
struct ParsedShadowsocksConfig {
peer: SocketAddr,
password: String,
cipher: CipherKind,
}
impl TryFrom<ParsedShadowsocksConfig> for ServerConfig {
type Error = ServerConfigError;
fn try_from(config: ParsedShadowsocksConfig) -> Result<Self, Self::Error> {
ServerConfig::new(config.peer, config.password, config.cipher)
}
}
#[derive(Clone)]
struct SocksConfig {
peer: SocketAddr,
authentication: Option<proxy::SocksAuth>,
}
#[derive(thiserror::Error, Debug)]
enum ProxyConfigError {
#[error("Unrecognized cipher selected: {0}")]
InvalidCipher(String),
}
impl TryFrom<ApiConnectionMode> for InnerConnectionMode {
type Error = ProxyConfigError;
fn try_from(config: ApiConnectionMode) -> Result<Self, Self::Error> {
use std::net::Ipv4Addr;
Ok(match config {
ApiConnectionMode::Direct => InnerConnectionMode::Direct,
ApiConnectionMode::Proxied(proxy_settings) => match proxy_settings {
ProxyConfig::Shadowsocks(config) => {
InnerConnectionMode::Shadowsocks(ShadowsocksConfig {
params: ParsedShadowsocksConfig {
peer: config.endpoint,
password: config.password,
cipher: CipherKind::from_str(&config.cipher)
.map_err(|_| ProxyConfigError::InvalidCipher(config.cipher))?,
},
proxy_context: SsContext::new_shared(ServerType::Local),
})
}
ProxyConfig::Socks5Local(config) => InnerConnectionMode::Socks5(SocksConfig {
peer: SocketAddr::new(IpAddr::from(Ipv4Addr::LOCALHOST), config.local_port),
authentication: None,
}),
ProxyConfig::Socks5Remote(config) => InnerConnectionMode::Socks5(SocksConfig {
peer: config.endpoint,
authentication: config.auth,
}),
ProxyConfig::EncryptedDnsProxy(config) => {
InnerConnectionMode::EncryptedDnsProxy(config)
}
},
})
}
}
/// A Connector for the `https` scheme.
#[derive(Clone)]
pub struct HttpsConnectorWithSni {
inner: Arc<Mutex<HttpsConnectorWithSniInner>>,
abort_notify: Arc<tokio::sync::Notify>,
dns_resolver: Arc<dyn DnsResolver>,
#[cfg(target_os = "android")]
socket_bypass_tx: Option<mpsc::Sender<SocketBypassRequest>>,
#[cfg(any(feature = "api-override", test))]
disable_tls: bool,
}
struct HttpsConnectorWithSniInner {
stream_handles: Vec<AbortableStreamHandle>,
proxy_config: InnerConnectionMode,
}
#[cfg(target_os = "android")]
pub type SocketBypassRequest = (RawFd, oneshot::Sender<()>);
impl HttpsConnectorWithSni {
pub fn new(
dns_resolver: Arc<dyn DnsResolver>,
#[cfg(target_os = "android")] socket_bypass_tx: Option<mpsc::Sender<SocketBypassRequest>>,
#[cfg(any(feature = "api-override", test))] disable_tls: bool,
) -> (Self, HttpsConnectorWithSniHandle) {
let (tx, mut rx) = mpsc::unbounded();
let abort_notify = Arc::new(tokio::sync::Notify::new());
let inner = Arc::new(Mutex::new(HttpsConnectorWithSniInner {
stream_handles: vec![],
proxy_config: InnerConnectionMode::Direct,
}));
let inner_copy = inner.clone();
let notify = abort_notify.clone();
tokio::spawn(async move {
// Handle requests by `HttpsConnectorWithSniHandle`s
while let Some(request) = rx.next().await {
let handles = {
let mut inner = inner_copy.lock().unwrap();
if let HttpsConnectorRequest::SetConnectionMode(config) = request {
match InnerConnectionMode::try_from(config) {
Ok(config) => {
inner.proxy_config = config;
}
Err(error) => {
log::error!(
"{}",
error.display_chain_with_msg(
"Failed to parse new API proxy config"
)
);
}
}
}
std::mem::take(&mut inner.stream_handles)
};
for handle in handles {
handle.close();
}
notify.notify_waiters();
}
});
(
HttpsConnectorWithSni {
inner,
abort_notify,
dns_resolver,
#[cfg(target_os = "android")]
socket_bypass_tx,
#[cfg(any(feature = "api-override", test))]
disable_tls,
},
HttpsConnectorWithSniHandle { tx },
)
}
/// Establishes a TCP connection with a peer at the specified socket address.
///
/// Will timeout after [`CONNECT_TIMEOUT`] seconds.
async fn open_socket(
addr: SocketAddr,
#[cfg(target_os = "android")] socket_bypass_tx: Option<mpsc::Sender<SocketBypassRequest>>,
) -> std::io::Result<TcpStream> {
let socket = match addr {
SocketAddr::V4(_) => TcpSocket::new_v4()?,
SocketAddr::V6(_) => TcpSocket::new_v6()?,
};
#[cfg(target_os = "android")]
if let Some(mut tx) = socket_bypass_tx {
let (done_tx, done_rx) = oneshot::channel();
let _ = tx.send((socket.as_raw_fd(), done_tx)).await;
if done_rx.await.is_err() {
log::error!("Failed to bypass socket, connection might fail");
}
}
timeout(CONNECT_TIMEOUT, socket.connect(addr))
.await
.map_err(|err| io::Error::new(io::ErrorKind::TimedOut, err))?
}
/// Resolve the provided `uri` to an IP and port. If the URI contains an IP, that IP will be used.
/// Otherwise `dns_resolver` will be used as a fallback.
/// If the URI contains a port, then that port will be used.
async fn resolve_address(dns_resolver: &dyn DnsResolver, uri: Uri) -> io::Result<SocketAddr> {
const DEFAULT_PORT: u16 = 443;
let hostname = uri.host().ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "invalid url, missing host")
})?;
let port = uri.port_u16();
if let Ok(addr) = hostname.parse::<IpAddr>() {
return Ok(SocketAddr::new(addr, port.unwrap_or(DEFAULT_PORT)));
}
let addrs = dns_resolver.resolve(hostname.to_owned()).await?;
let addr = addrs
.first()
.ok_or_else(|| io::Error::other("Empty DNS response"))?;
let port = match (addr.port(), port) {
(_, Some(port)) => port,
(0, None) => DEFAULT_PORT,
(addr_port, None) => addr_port,
};
Ok(SocketAddr::new(addr.ip(), port))
}
pub async fn get_stream(
&mut self,
uri: Uri,
) -> Result<AbortableStream<ApiConnection>, io::Error> {
let inner = self.inner.clone();
let abort_notify = self.abort_notify.clone();
#[cfg(target_os = "android")]
let socket_bypass_tx = self.socket_bypass_tx.clone();
let dns_resolver = self.dns_resolver.clone();
#[cfg(any(feature = "api-override", test))]
let disable_tls = self.disable_tls;
if uri.scheme() != Some(&Scheme::HTTPS) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"invalid url, not https",
));
}
let Some(hostname) = uri.host().map(str::to_owned) else {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"invalid url, missing host",
));
};
let addr = Self::resolve_address(&*dns_resolver, uri).await?;
// Loop until we have established a connection. This starts over if a new endpoint
// is selected while connecting.
let stream = loop {
let notify = abort_notify.notified();
let proxy_config = { inner.lock().unwrap().proxy_config.clone() };
let stream_fut = proxy_config.connect(
&hostname,
&addr,
#[cfg(target_os = "android")]
socket_bypass_tx.clone(),
#[cfg(any(feature = "api-override", test))]
disable_tls,
);
pin_mut!(stream_fut);
pin_mut!(notify);
// Wait for connection. Abort and retry if we switched to a different server.
if let future::Either::Left((stream, _)) = future::select(stream_fut, notify).await {
break stream?;
}
};
let (stream, socket_handle) = AbortableStream::new(stream);
{
let mut inner = inner.lock().unwrap();
inner.stream_handles.push(socket_handle);
}
Ok(stream)
}
}
impl fmt::Debug for HttpsConnectorWithSni {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("HttpsConnectorWithSni").finish()
}
}