2019-09-23 14:40:38 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2019-11-14 12:10:25 -05:00
|
|
|
use super::io::StreamResource;
|
2019-11-07 09:59:02 -05:00
|
|
|
use crate::deno_error::bad_resource;
|
2019-10-21 14:38:28 -04:00
|
|
|
use crate::deno_error::DenoError;
|
|
|
|
use crate::deno_error::ErrorKind;
|
2019-10-13 10:37:37 -04:00
|
|
|
use crate::ops::json_op;
|
2019-09-23 14:40:38 -04:00
|
|
|
use crate::resolve_addr::resolve_addr;
|
|
|
|
use crate::state::ThreadSafeState;
|
2019-11-14 12:10:25 -05:00
|
|
|
use deno::Resource;
|
2019-09-23 14:40:38 -04:00
|
|
|
use deno::*;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
|
|
|
use futures::future::TryFutureExt;
|
|
|
|
use futures::stream::StreamExt;
|
|
|
|
use futures::stream::TryStreamExt;
|
2019-09-23 14:40:38 -04:00
|
|
|
use std;
|
|
|
|
use std::convert::From;
|
2019-10-21 14:38:28 -04:00
|
|
|
use std::fs::File;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::future::Future;
|
2019-10-21 14:38:28 -04:00
|
|
|
use std::io::BufReader;
|
2019-11-07 09:59:02 -05:00
|
|
|
use std::net::SocketAddr;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2019-09-23 14:40:38 -04:00
|
|
|
use std::sync::Arc;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2019-09-23 14:40:38 -04:00
|
|
|
use tokio;
|
2019-11-16 19:17:47 -05:00
|
|
|
use tokio::net::tcp::Incoming;
|
2019-10-21 14:38:28 -04:00
|
|
|
use tokio::net::TcpListener;
|
2019-09-23 14:40:38 -04:00
|
|
|
use tokio::net::TcpStream;
|
|
|
|
use tokio_rustls::{rustls::ClientConfig, TlsConnector};
|
2019-10-21 14:38:28 -04:00
|
|
|
use tokio_rustls::{
|
|
|
|
rustls::{
|
|
|
|
internal::pemfile::{certs, pkcs8_private_keys, rsa_private_keys},
|
|
|
|
Certificate, NoClientAuth, PrivateKey, ServerConfig,
|
|
|
|
},
|
|
|
|
TlsAcceptor,
|
|
|
|
};
|
2019-09-23 14:40:38 -04:00
|
|
|
use webpki;
|
|
|
|
use webpki::DNSNameRef;
|
|
|
|
use webpki_roots;
|
|
|
|
|
2019-10-21 14:38:28 -04:00
|
|
|
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
|
|
|
i.register_op("dial_tls", s.core_op(json_op(s.stateful_op(op_dial_tls))));
|
|
|
|
i.register_op(
|
|
|
|
"listen_tls",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_listen_tls))),
|
|
|
|
);
|
|
|
|
i.register_op(
|
|
|
|
"accept_tls",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_accept_tls))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-23 14:40:38 -04:00
|
|
|
#[derive(Deserialize)]
|
2019-10-21 14:38:28 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-09-23 14:40:38 -04:00
|
|
|
struct DialTLSArgs {
|
|
|
|
hostname: String,
|
|
|
|
port: u16,
|
2019-10-21 14:38:28 -04:00
|
|
|
cert_file: Option<String>,
|
2019-10-13 10:37:37 -04:00
|
|
|
}
|
2019-09-23 14:40:38 -04:00
|
|
|
|
|
|
|
pub fn op_dial_tls(
|
|
|
|
state: &ThreadSafeState,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: DialTLSArgs = serde_json::from_value(args)?;
|
2019-10-21 14:38:28 -04:00
|
|
|
let cert_file = args.cert_file;
|
2019-11-14 12:10:25 -05:00
|
|
|
let state_ = state.clone();
|
2019-10-23 10:19:27 -04:00
|
|
|
state.check_net(&args.hostname, args.port)?;
|
2019-10-21 14:38:28 -04:00
|
|
|
if let Some(path) = cert_file.clone() {
|
|
|
|
state.check_read(&path)?;
|
|
|
|
}
|
2019-09-23 14:40:38 -04:00
|
|
|
|
2019-10-23 10:19:27 -04:00
|
|
|
let mut domain = args.hostname.clone();
|
2019-09-23 14:40:38 -04:00
|
|
|
if domain.is_empty() {
|
|
|
|
domain.push_str("localhost");
|
|
|
|
}
|
|
|
|
|
2019-10-23 10:19:27 -04:00
|
|
|
let op = resolve_addr(&args.hostname, args.port).and_then(move |addr| {
|
2019-11-16 19:17:47 -05:00
|
|
|
futures::compat::Compat01As03::new(TcpStream::connect(&addr))
|
2019-09-23 14:40:38 -04:00
|
|
|
.and_then(move |tcp_stream| {
|
2019-11-16 19:17:47 -05:00
|
|
|
let local_addr = match tcp_stream.local_addr() {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => return futures::future::err(e),
|
|
|
|
};
|
|
|
|
let remote_addr = match tcp_stream.peer_addr() {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => return futures::future::err(e),
|
|
|
|
};
|
2019-09-23 14:40:38 -04:00
|
|
|
let mut config = ClientConfig::new();
|
|
|
|
config
|
|
|
|
.root_store
|
|
|
|
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
|
|
|
|
|
2019-10-21 14:38:28 -04:00
|
|
|
if let Some(path) = cert_file {
|
2019-11-16 19:17:47 -05:00
|
|
|
let key_file = match File::open(path) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => return futures::future::err(e),
|
|
|
|
};
|
2019-10-21 14:38:28 -04:00
|
|
|
let reader = &mut BufReader::new(key_file);
|
|
|
|
config.root_store.add_pem_file(reader).unwrap();
|
|
|
|
}
|
2019-09-23 14:40:38 -04:00
|
|
|
let tls_connector = TlsConnector::from(Arc::new(config));
|
2019-11-16 19:17:47 -05:00
|
|
|
futures::future::ok((
|
|
|
|
tls_connector,
|
|
|
|
tcp_stream,
|
|
|
|
local_addr,
|
|
|
|
remote_addr,
|
|
|
|
))
|
2019-09-23 14:40:38 -04:00
|
|
|
})
|
|
|
|
.map_err(ErrBox::from)
|
|
|
|
.and_then(
|
|
|
|
move |(tls_connector, tcp_stream, local_addr, remote_addr)| {
|
|
|
|
let dnsname = DNSNameRef::try_from_ascii_str(&domain)
|
|
|
|
.expect("Invalid DNS lookup");
|
2019-11-16 19:17:47 -05:00
|
|
|
futures::compat::Compat01As03::new(
|
|
|
|
tls_connector.connect(dnsname, tcp_stream),
|
|
|
|
)
|
|
|
|
.map_err(ErrBox::from)
|
|
|
|
.and_then(move |tls_stream| {
|
|
|
|
let mut table = state_.lock_resource_table();
|
|
|
|
let rid = table.add(
|
|
|
|
"clientTlsStream",
|
|
|
|
Box::new(StreamResource::ClientTlsStream(Box::new(tls_stream))),
|
|
|
|
);
|
|
|
|
futures::future::ok(json!({
|
|
|
|
"rid": rid,
|
|
|
|
"localAddr": local_addr.to_string(),
|
|
|
|
"remoteAddr": remote_addr.to_string(),
|
|
|
|
}))
|
|
|
|
})
|
2019-09-23 14:40:38 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
Ok(JsonOp::Async(op.boxed()))
|
2019-09-23 14:40:38 -04:00
|
|
|
}
|
2019-10-21 14:38:28 -04:00
|
|
|
|
|
|
|
fn load_certs(path: &str) -> Result<Vec<Certificate>, ErrBox> {
|
|
|
|
let cert_file = File::open(path)?;
|
|
|
|
let reader = &mut BufReader::new(cert_file);
|
|
|
|
|
|
|
|
let certs = certs(reader).map_err(|_| {
|
|
|
|
DenoError::new(ErrorKind::Other, "Unable to decode certificate".to_string())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if certs.is_empty() {
|
|
|
|
let e = DenoError::new(
|
|
|
|
ErrorKind::Other,
|
|
|
|
"No certificates found in cert file".to_string(),
|
|
|
|
);
|
|
|
|
return Err(ErrBox::from(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(certs)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn key_decode_err() -> DenoError {
|
|
|
|
DenoError::new(ErrorKind::Other, "Unable to decode key".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn key_not_found_err() -> DenoError {
|
|
|
|
DenoError::new(ErrorKind::Other, "No keys found in key file".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts with -----BEGIN RSA PRIVATE KEY-----
|
|
|
|
fn load_rsa_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> {
|
|
|
|
let key_file = File::open(path)?;
|
|
|
|
let reader = &mut BufReader::new(key_file);
|
|
|
|
let keys = rsa_private_keys(reader).map_err(|_| key_decode_err())?;
|
|
|
|
Ok(keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts with -----BEGIN PRIVATE KEY-----
|
|
|
|
fn load_pkcs8_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> {
|
|
|
|
let key_file = File::open(path)?;
|
|
|
|
let reader = &mut BufReader::new(key_file);
|
|
|
|
let keys = pkcs8_private_keys(reader).map_err(|_| key_decode_err())?;
|
|
|
|
Ok(keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> {
|
|
|
|
let path = path.to_string();
|
|
|
|
let mut keys = load_rsa_keys(&path)?;
|
|
|
|
|
|
|
|
if keys.is_empty() {
|
|
|
|
keys = load_pkcs8_keys(&path)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if keys.is_empty() {
|
|
|
|
return Err(ErrBox::from(key_not_found_err()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(keys)
|
|
|
|
}
|
|
|
|
|
2019-11-07 09:59:02 -05:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct TlsListenerResource {
|
2019-11-16 19:17:47 -05:00
|
|
|
listener: Incoming,
|
2019-11-07 09:59:02 -05:00
|
|
|
tls_acceptor: TlsAcceptor,
|
2019-11-16 19:17:47 -05:00
|
|
|
waker: Option<futures::task::AtomicWaker>,
|
2019-11-07 09:59:02 -05:00
|
|
|
local_addr: SocketAddr,
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:11:15 -05:00
|
|
|
impl Resource for TlsListenerResource {}
|
2019-11-07 09:59:02 -05:00
|
|
|
|
|
|
|
impl Drop for TlsListenerResource {
|
|
|
|
fn drop(&mut self) {
|
2019-11-16 19:17:47 -05:00
|
|
|
self.wake_task();
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TlsListenerResource {
|
|
|
|
/// Track the current task so future awaiting for connection
|
|
|
|
/// can be notified when listener is closed.
|
|
|
|
///
|
|
|
|
/// Throws an error if another task is already tracked.
|
2019-11-16 19:17:47 -05:00
|
|
|
pub fn track_task(&mut self, cx: &Context) -> Result<(), ErrBox> {
|
2019-11-07 09:59:02 -05:00
|
|
|
// Currently, we only allow tracking a single accept task for a listener.
|
|
|
|
// This might be changed in the future with multiple workers.
|
|
|
|
// Caveat: TcpListener by itself also only tracks an accept task at a time.
|
|
|
|
// See https://github.com/tokio-rs/tokio/issues/846#issuecomment-454208883
|
2019-11-16 19:17:47 -05:00
|
|
|
if self.waker.is_some() {
|
2019-11-07 09:59:02 -05:00
|
|
|
let e = std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Another accept task is ongoing",
|
|
|
|
);
|
|
|
|
return Err(ErrBox::from(e));
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
let waker = futures::task::AtomicWaker::new();
|
|
|
|
waker.register(cx.waker());
|
|
|
|
self.waker.replace(waker);
|
2019-11-07 09:59:02 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Notifies a task when listener is closed so accept future can resolve.
|
2019-11-16 19:17:47 -05:00
|
|
|
pub fn wake_task(&mut self) {
|
|
|
|
if let Some(waker) = self.waker.as_ref() {
|
|
|
|
waker.wake();
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop tracking a task.
|
|
|
|
/// Happens when the task is done and thus no further tracking is needed.
|
|
|
|
pub fn untrack_task(&mut self) {
|
2019-11-16 19:17:47 -05:00
|
|
|
if self.waker.is_some() {
|
|
|
|
self.waker.take();
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 14:38:28 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct ListenTlsArgs {
|
|
|
|
transport: String,
|
|
|
|
hostname: String,
|
|
|
|
port: u16,
|
|
|
|
cert_file: String,
|
|
|
|
key_file: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_listen_tls(
|
|
|
|
state: &ThreadSafeState,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: ListenTlsArgs = serde_json::from_value(args)?;
|
|
|
|
assert_eq!(args.transport, "tcp");
|
|
|
|
|
|
|
|
let cert_file = args.cert_file;
|
|
|
|
let key_file = args.key_file;
|
|
|
|
|
2019-10-23 10:19:27 -04:00
|
|
|
state.check_net(&args.hostname, args.port)?;
|
2019-10-21 14:38:28 -04:00
|
|
|
state.check_read(&cert_file)?;
|
|
|
|
state.check_read(&key_file)?;
|
|
|
|
|
|
|
|
let mut config = ServerConfig::new(NoClientAuth::new());
|
|
|
|
config
|
|
|
|
.set_single_cert(load_certs(&cert_file)?, load_keys(&key_file)?.remove(0))
|
|
|
|
.expect("invalid key or certificate");
|
2019-11-07 09:59:02 -05:00
|
|
|
let tls_acceptor = TlsAcceptor::from(Arc::new(config));
|
2019-11-16 19:17:47 -05:00
|
|
|
let addr =
|
|
|
|
futures::executor::block_on(resolve_addr(&args.hostname, args.port))?;
|
2019-10-21 14:38:28 -04:00
|
|
|
let listener = TcpListener::bind(&addr)?;
|
|
|
|
let local_addr = listener.local_addr()?;
|
2019-11-07 09:59:02 -05:00
|
|
|
let local_addr_str = local_addr.to_string();
|
|
|
|
let tls_listener_resource = TlsListenerResource {
|
2019-11-16 19:17:47 -05:00
|
|
|
listener: listener.incoming(),
|
2019-11-07 09:59:02 -05:00
|
|
|
tls_acceptor,
|
2019-11-16 19:17:47 -05:00
|
|
|
waker: None,
|
2019-11-07 09:59:02 -05:00
|
|
|
local_addr,
|
|
|
|
};
|
2019-11-14 12:10:25 -05:00
|
|
|
let mut table = state.lock_resource_table();
|
2019-11-07 09:59:02 -05:00
|
|
|
let rid = table.add("tlsListener", Box::new(tls_listener_resource));
|
2019-10-21 14:38:28 -04:00
|
|
|
|
|
|
|
Ok(JsonOp::Sync(json!({
|
2019-11-07 09:59:02 -05:00
|
|
|
"rid": rid,
|
|
|
|
"localAddr": local_addr_str
|
2019-10-21 14:38:28 -04:00
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2019-11-07 09:59:02 -05:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum AcceptTlsState {
|
|
|
|
Eager,
|
|
|
|
Pending,
|
|
|
|
Done,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simply accepts a TLS connection.
|
2019-11-14 12:10:25 -05:00
|
|
|
pub fn accept_tls(state: &ThreadSafeState, rid: ResourceId) -> AcceptTls {
|
2019-11-07 09:59:02 -05:00
|
|
|
AcceptTls {
|
2019-11-14 12:10:25 -05:00
|
|
|
accept_state: AcceptTlsState::Eager,
|
2019-11-07 09:59:02 -05:00
|
|
|
rid,
|
2019-11-14 12:10:25 -05:00
|
|
|
state: state.clone(),
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A future representing state of accepting a TLS connection.
|
|
|
|
pub struct AcceptTls {
|
2019-11-14 12:10:25 -05:00
|
|
|
accept_state: AcceptTlsState,
|
2019-11-07 09:59:02 -05:00
|
|
|
rid: ResourceId,
|
2019-11-14 12:10:25 -05:00
|
|
|
state: ThreadSafeState,
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for AcceptTls {
|
2019-11-16 19:17:47 -05:00
|
|
|
type Output = Result<(TcpStream, SocketAddr), ErrBox>;
|
2019-11-07 09:59:02 -05:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let inner = self.get_mut();
|
|
|
|
if inner.accept_state == AcceptTlsState::Done {
|
2019-11-07 09:59:02 -05:00
|
|
|
panic!("poll AcceptTls after it's done");
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
let mut table = inner.state.lock_resource_table();
|
2019-11-07 09:59:02 -05:00
|
|
|
let listener_resource = table
|
2019-11-16 19:17:47 -05:00
|
|
|
.get_mut::<TlsListenerResource>(inner.rid)
|
2019-11-07 09:59:02 -05:00
|
|
|
.ok_or_else(|| {
|
|
|
|
let e = std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"Listener has been closed",
|
|
|
|
);
|
|
|
|
ErrBox::from(e)
|
|
|
|
})?;
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
let mut listener =
|
|
|
|
futures::compat::Compat01As03::new(&mut listener_resource.listener)
|
|
|
|
.map_err(ErrBox::from);
|
2019-11-07 09:59:02 -05:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
if inner.accept_state == AcceptTlsState::Eager {
|
2019-11-07 09:59:02 -05:00
|
|
|
// Similar to try_ready!, but also track/untrack accept task
|
|
|
|
// in TcpListener resource.
|
|
|
|
// In this way, when the listener is closed, the task can be
|
|
|
|
// notified to error out (instead of stuck forever).
|
2019-11-16 19:17:47 -05:00
|
|
|
match listener.poll_next_unpin(cx) {
|
|
|
|
Poll::Ready(Some(Ok(stream))) => {
|
|
|
|
inner.accept_state = AcceptTlsState::Done;
|
|
|
|
let addr = stream.peer_addr().unwrap();
|
|
|
|
return Poll::Ready(Ok((stream, addr)));
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Pending => {
|
|
|
|
inner.accept_state = AcceptTlsState::Pending;
|
|
|
|
return Poll::Pending;
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Ready(Some(Err(e))) => {
|
|
|
|
inner.accept_state = AcceptTlsState::Done;
|
|
|
|
return Poll::Ready(Err(e));
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
_ => unreachable!(),
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
match listener.poll_next_unpin(cx) {
|
|
|
|
Poll::Ready(Some(Ok(stream))) => {
|
2019-11-07 09:59:02 -05:00
|
|
|
listener_resource.untrack_task();
|
2019-11-16 19:17:47 -05:00
|
|
|
inner.accept_state = AcceptTlsState::Done;
|
|
|
|
let addr = stream.peer_addr().unwrap();
|
|
|
|
Poll::Ready(Ok((stream, addr)))
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Pending => {
|
|
|
|
listener_resource.track_task(cx)?;
|
|
|
|
Poll::Pending
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
Poll::Ready(Some(Err(e))) => {
|
2019-11-07 09:59:02 -05:00
|
|
|
listener_resource.untrack_task();
|
2019-11-16 19:17:47 -05:00
|
|
|
inner.accept_state = AcceptTlsState::Done;
|
|
|
|
Poll::Ready(Err(e))
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
_ => unreachable!(),
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 14:38:28 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct AcceptTlsArgs {
|
|
|
|
rid: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_accept_tls(
|
2019-11-14 12:10:25 -05:00
|
|
|
state: &ThreadSafeState,
|
2019-10-21 14:38:28 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: AcceptTlsArgs = serde_json::from_value(args)?;
|
2019-11-07 09:59:02 -05:00
|
|
|
let rid = args.rid as u32;
|
2019-11-14 12:10:25 -05:00
|
|
|
let state1 = state.clone();
|
|
|
|
let state2 = state.clone();
|
|
|
|
let op = accept_tls(state, rid)
|
2019-10-21 14:38:28 -04:00
|
|
|
.and_then(move |(tcp_stream, _socket_addr)| {
|
2019-11-16 19:17:47 -05:00
|
|
|
let local_addr = match tcp_stream.local_addr() {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => return futures::future::err(ErrBox::from(e)),
|
|
|
|
};
|
|
|
|
let remote_addr = match tcp_stream.peer_addr() {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => return futures::future::err(ErrBox::from(e)),
|
|
|
|
};
|
|
|
|
futures::future::ok((tcp_stream, local_addr, remote_addr))
|
2019-10-21 14:38:28 -04:00
|
|
|
})
|
|
|
|
.and_then(move |(tcp_stream, local_addr, remote_addr)| {
|
2019-11-14 12:10:25 -05:00
|
|
|
let table = state1.lock_resource_table();
|
2019-11-07 09:59:02 -05:00
|
|
|
let resource = table
|
|
|
|
.get::<TlsListenerResource>(rid)
|
|
|
|
.ok_or_else(bad_resource)
|
|
|
|
.expect("Can't find tls listener");
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
futures::compat::Compat01As03::new(
|
|
|
|
resource.tls_acceptor.accept(tcp_stream),
|
|
|
|
)
|
|
|
|
.map_err(ErrBox::from)
|
|
|
|
.and_then(move |tls_stream| {
|
|
|
|
let mut table = state2.lock_resource_table();
|
|
|
|
let rid = table.add(
|
|
|
|
"serverTlsStream",
|
|
|
|
Box::new(StreamResource::ServerTlsStream(Box::new(tls_stream))),
|
|
|
|
);
|
|
|
|
futures::future::ok((rid, local_addr, remote_addr))
|
|
|
|
})
|
2019-10-21 14:38:28 -04:00
|
|
|
})
|
2019-11-07 11:11:15 -05:00
|
|
|
.and_then(move |(rid, local_addr, remote_addr)| {
|
2019-10-21 14:38:28 -04:00
|
|
|
futures::future::ok(json!({
|
2019-11-07 11:11:15 -05:00
|
|
|
"rid": rid,
|
2019-10-21 14:38:28 -04:00
|
|
|
"localAddr": local_addr.to_string(),
|
|
|
|
"remoteAddr": remote_addr.to_string(),
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
Ok(JsonOp::Async(op.boxed()))
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|