2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
use super::io::TcpStreamResource;
|
2021-01-14 23:32:27 -05:00
|
|
|
use super::io::TlsClientStreamResource;
|
|
|
|
use super::io::TlsServerStreamResource;
|
2021-04-06 04:33:43 -04:00
|
|
|
use super::net::IpAddr;
|
|
|
|
use super::net::OpAddr;
|
|
|
|
use super::net::OpConn;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2019-09-23 14:40:38 -04:00
|
|
|
use crate::resolve_addr::resolve_addr;
|
2020-12-15 07:02:26 -05:00
|
|
|
use crate::resolve_addr::resolve_addr_sync;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::bad_resource;
|
|
|
|
use deno_core::error::bad_resource_id;
|
|
|
|
use deno_core::error::custom_error;
|
2020-12-29 20:19:28 -05:00
|
|
|
use deno_core::error::generic_error;
|
2021-04-26 15:39:55 -04:00
|
|
|
use deno_core::error::invalid_hostname;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
|
|
|
use deno_core::CancelHandle;
|
|
|
|
use deno_core::CancelTryFuture;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::RcRef;
|
|
|
|
use deno_core::Resource;
|
2021-03-19 13:25:37 -04:00
|
|
|
use deno_core::ResourceId;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2021-01-12 19:22:33 -05:00
|
|
|
use std::collections::HashMap;
|
2019-09-23 14:40:38 -04:00
|
|
|
use std::convert::From;
|
2019-10-21 14:38:28 -04:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::BufReader;
|
2020-01-20 09:45:44 -05:00
|
|
|
use std::path::Path;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2019-09-23 14:40:38 -04:00
|
|
|
use std::sync::Arc;
|
2021-01-12 19:22:33 -05:00
|
|
|
use std::sync::Mutex;
|
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},
|
2021-01-12 19:22:33 -05:00
|
|
|
Certificate, NoClientAuth, PrivateKey, ServerConfig, StoresClientSessions,
|
2019-10-21 14:38:28 -04:00
|
|
|
},
|
|
|
|
TlsAcceptor,
|
|
|
|
};
|
2019-09-23 14:40:38 -04:00
|
|
|
use webpki::DNSNameRef;
|
|
|
|
|
2021-01-12 19:22:33 -05:00
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref CLIENT_SESSION_MEMORY_CACHE: Arc<ClientSessionMemoryCache> =
|
|
|
|
Arc::new(ClientSessionMemoryCache::default());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct ClientSessionMemoryCache(Mutex<HashMap<Vec<u8>, Vec<u8>>>);
|
|
|
|
|
|
|
|
impl StoresClientSessions for ClientSessionMemoryCache {
|
|
|
|
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
|
|
|
|
self.0.lock().unwrap().get(key).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn put(&self, key: Vec<u8>, value: Vec<u8>) -> bool {
|
|
|
|
let mut sessions = self.0.lock().unwrap();
|
|
|
|
// TODO(bnoordhuis) Evict sessions LRU-style instead of arbitrarily.
|
|
|
|
while sessions.len() >= 1024 {
|
|
|
|
let key = sessions.keys().next().unwrap().clone();
|
|
|
|
sessions.remove(&key);
|
|
|
|
}
|
|
|
|
sessions.insert(key, value);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
2021-04-12 15:55:05 -04:00
|
|
|
super::reg_async(rt, "op_start_tls", op_start_tls);
|
|
|
|
super::reg_async(rt, "op_connect_tls", op_connect_tls);
|
|
|
|
super::reg_sync(rt, "op_listen_tls", op_listen_tls);
|
|
|
|
super::reg_async(rt, "op_accept_tls", op_accept_tls);
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
|
2019-09-23 14:40:38 -04:00
|
|
|
#[derive(Deserialize)]
|
2019-10-21 14:38:28 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-25 14:17:37 -04:00
|
|
|
pub struct ConnectTlsArgs {
|
2020-01-18 15:49:55 -05:00
|
|
|
transport: String,
|
2019-09-23 14:40:38 -04:00
|
|
|
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
|
|
|
|
2020-04-18 11:21:20 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-25 14:17:37 -04:00
|
|
|
struct StartTlsArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
rid: ResourceId,
|
2020-04-18 11:21:20 -04:00
|
|
|
cert_file: Option<String>,
|
|
|
|
hostname: String,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_start_tls(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-25 14:17:37 -04:00
|
|
|
args: StartTlsArgs,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-06 04:33:43 -04:00
|
|
|
) -> Result<OpConn, AnyError> {
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2020-04-18 11:21:20 -04:00
|
|
|
|
2021-02-07 09:50:05 -05:00
|
|
|
let mut domain = args.hostname.as_str();
|
2020-04-18 11:21:20 -04:00
|
|
|
if domain.is_empty() {
|
2021-02-07 09:50:05 -05:00
|
|
|
domain = "localhost";
|
2020-04-18 11:21:20 -04:00
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
{
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable2(&state, "Deno.startTls");
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut s = state.borrow_mut();
|
|
|
|
let permissions = s.borrow_mut::<Permissions>();
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.net.check(&(&domain, Some(0)))?;
|
2021-02-07 09:50:05 -05:00
|
|
|
if let Some(path) = &args.cert_file {
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check(Path::new(&path))?;
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2020-04-28 12:01:13 -04:00
|
|
|
}
|
2020-04-18 11:21:20 -04:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource_rc = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.take::<TcpStreamResource>(rid)
|
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
let resource = Rc::try_unwrap(resource_rc)
|
|
|
|
.expect("Only a single use of this resource should happen");
|
|
|
|
let (read_half, write_half) = resource.into_inner();
|
|
|
|
let tcp_stream = read_half.reunite(write_half)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
|
|
|
let mut config = ClientConfig::new();
|
2021-01-12 19:22:33 -05:00
|
|
|
config.set_persistence(CLIENT_SESSION_MEMORY_CACHE.clone());
|
2020-12-16 11:14:12 -05:00
|
|
|
config
|
|
|
|
.root_store
|
|
|
|
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
|
2021-02-07 09:50:05 -05:00
|
|
|
if let Some(path) = args.cert_file {
|
2020-12-16 11:14:12 -05:00
|
|
|
let key_file = File::open(path)?;
|
|
|
|
let reader = &mut BufReader::new(key_file);
|
|
|
|
config.root_store.add_pem_file(reader).unwrap();
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2020-12-16 11:14:12 -05:00
|
|
|
|
|
|
|
let tls_connector = TlsConnector::from(Arc::new(config));
|
2021-04-26 15:39:55 -04:00
|
|
|
let dnsname = DNSNameRef::try_from_ascii_str(domain)
|
|
|
|
.map_err(|_| invalid_hostname(domain))?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let tls_stream = tls_connector.connect(dnsname, tcp_stream).await?;
|
|
|
|
|
|
|
|
let rid = {
|
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
state_
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.add(TlsClientStreamResource::from(tls_stream))
|
2020-12-16 11:14:12 -05:00
|
|
|
};
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(OpConn {
|
|
|
|
rid,
|
|
|
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
|
|
|
hostname: local_addr.ip().to_string(),
|
|
|
|
port: local_addr.port(),
|
|
|
|
})),
|
|
|
|
remote_addr: Some(OpAddr::Tcp(IpAddr {
|
|
|
|
hostname: remote_addr.ip().to_string(),
|
|
|
|
port: remote_addr.port(),
|
|
|
|
})),
|
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_connect_tls(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-25 14:17:37 -04:00
|
|
|
args: ConnectTlsArgs,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-06 04:33:43 -04:00
|
|
|
) -> Result<OpConn, AnyError> {
|
|
|
|
assert_eq!(args.transport, "tcp");
|
|
|
|
|
2021-04-26 15:39:55 -04:00
|
|
|
let mut domain = args.hostname.as_str();
|
|
|
|
if domain.is_empty() {
|
|
|
|
domain = "localhost";
|
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut s = state.borrow_mut();
|
|
|
|
let permissions = s.borrow_mut::<Permissions>();
|
2021-04-26 15:39:55 -04:00
|
|
|
permissions.net.check(&(domain, Some(args.port)))?;
|
2021-02-07 09:50:05 -05:00
|
|
|
if let Some(path) = &args.cert_file {
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check(Path::new(&path))?;
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2021-04-26 15:39:55 -04:00
|
|
|
let dnsname = DNSNameRef::try_from_ascii_str(domain)
|
|
|
|
.map_err(|_| invalid_hostname(domain))?;
|
|
|
|
let addr = resolve_addr(domain, args.port)
|
2020-12-29 20:19:28 -05:00
|
|
|
.await?
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| generic_error("No resolved address found"))?;
|
2020-08-28 11:08:24 -04:00
|
|
|
let tcp_stream = TcpStream::connect(&addr).await?;
|
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
|
|
|
let mut config = ClientConfig::new();
|
2021-01-12 19:22:33 -05:00
|
|
|
config.set_persistence(CLIENT_SESSION_MEMORY_CACHE.clone());
|
2020-08-28 11:08:24 -04:00
|
|
|
config
|
|
|
|
.root_store
|
|
|
|
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
|
2021-02-07 09:50:05 -05:00
|
|
|
if let Some(path) = args.cert_file {
|
2020-08-28 11:08:24 -04:00
|
|
|
let key_file = File::open(path)?;
|
|
|
|
let reader = &mut BufReader::new(key_file);
|
|
|
|
config.root_store.add_pem_file(reader).unwrap();
|
|
|
|
}
|
|
|
|
let tls_connector = TlsConnector::from(Arc::new(config));
|
|
|
|
let tls_stream = tls_connector.connect(dnsname, tcp_stream).await?;
|
2020-09-10 09:57:45 -04:00
|
|
|
let rid = {
|
|
|
|
let mut state_ = state.borrow_mut();
|
2020-12-16 11:14:12 -05:00
|
|
|
state_
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.add(TlsClientStreamResource::from(tls_stream))
|
2020-09-10 09:57:45 -04:00
|
|
|
};
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(OpConn {
|
|
|
|
rid,
|
|
|
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
|
|
|
hostname: local_addr.ip().to_string(),
|
|
|
|
port: local_addr.port(),
|
|
|
|
})),
|
|
|
|
remote_addr: Some(OpAddr::Tcp(IpAddr {
|
|
|
|
hostname: remote_addr.ip().to_string(),
|
|
|
|
port: remote_addr.port(),
|
|
|
|
})),
|
|
|
|
})
|
2019-09-23 14:40:38 -04:00
|
|
|
}
|
2019-10-21 14:38:28 -04:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
fn load_certs(path: &str) -> Result<Vec<Certificate>, AnyError> {
|
2019-10-21 14:38:28 -04:00
|
|
|
let cert_file = File::open(path)?;
|
|
|
|
let reader = &mut BufReader::new(cert_file);
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
let certs = certs(reader)
|
2020-09-14 12:48:57 -04:00
|
|
|
.map_err(|_| custom_error("InvalidData", "Unable to decode certificate"))?;
|
2019-10-21 14:38:28 -04:00
|
|
|
|
|
|
|
if certs.is_empty() {
|
2020-09-14 12:48:57 -04:00
|
|
|
let e = custom_error("InvalidData", "No certificates found in cert file");
|
2020-02-23 14:51:29 -05:00
|
|
|
return Err(e);
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(certs)
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
fn key_decode_err() -> AnyError {
|
|
|
|
custom_error("InvalidData", "Unable to decode key")
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
fn key_not_found_err() -> AnyError {
|
|
|
|
custom_error("InvalidData", "No keys found in key file")
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts with -----BEGIN RSA PRIVATE KEY-----
|
2020-09-14 12:48:57 -04:00
|
|
|
fn load_rsa_keys(path: &str) -> Result<Vec<PrivateKey>, AnyError> {
|
2019-10-21 14:38:28 -04:00
|
|
|
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-----
|
2020-09-14 12:48:57 -04:00
|
|
|
fn load_pkcs8_keys(path: &str) -> Result<Vec<PrivateKey>, AnyError> {
|
2019-10-21 14:38:28 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
fn load_keys(path: &str) -> Result<Vec<PrivateKey>, AnyError> {
|
2019-10-21 14:38:28 -04:00
|
|
|
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() {
|
2020-02-23 14:51:29 -05:00
|
|
|
return Err(key_not_found_err());
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(keys)
|
|
|
|
}
|
|
|
|
|
2019-11-07 09:59:02 -05:00
|
|
|
pub struct TlsListenerResource {
|
2020-12-16 11:14:12 -05:00
|
|
|
listener: AsyncRefCell<TcpListener>,
|
2019-11-07 09:59:02 -05:00
|
|
|
tls_acceptor: TlsAcceptor,
|
2020-12-16 11:14:12 -05:00
|
|
|
cancel: CancelHandle,
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
impl Resource for TlsListenerResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"tlsListener".into()
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel();
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 14:38:28 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct ListenTlsArgs {
|
2019-10-21 14:38:28 -04:00
|
|
|
transport: String,
|
|
|
|
hostname: String,
|
|
|
|
port: u16,
|
|
|
|
cert_file: String,
|
|
|
|
key_file: String,
|
2021-04-10 16:04:44 -04:00
|
|
|
alpn_protocols: Option<Vec<String>>,
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn op_listen_tls(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: ListenTlsArgs,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-06 04:33:43 -04:00
|
|
|
) -> Result<OpConn, AnyError> {
|
2019-10-21 14:38:28 -04:00
|
|
|
assert_eq!(args.transport, "tcp");
|
|
|
|
|
|
|
|
let cert_file = args.cert_file;
|
|
|
|
let key_file = args.key_file;
|
2020-09-10 09:57:45 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.net.check(&(&args.hostname, Some(args.port)))?;
|
|
|
|
permissions.read.check(Path::new(&cert_file))?;
|
|
|
|
permissions.read.check(Path::new(&key_file))?;
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2019-10-21 14:38:28 -04:00
|
|
|
let mut config = ServerConfig::new(NoClientAuth::new());
|
2021-04-10 16:04:44 -04:00
|
|
|
if let Some(alpn_protocols) = args.alpn_protocols {
|
|
|
|
super::check_unstable(state, "Deno.listenTls#alpn_protocols");
|
|
|
|
config.alpn_protocols =
|
|
|
|
alpn_protocols.into_iter().map(|s| s.into_bytes()).collect();
|
|
|
|
}
|
2019-10-21 14:38:28 -04:00
|
|
|
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));
|
2020-12-29 20:19:28 -05:00
|
|
|
let addr = resolve_addr_sync(&args.hostname, args.port)?
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| generic_error("No resolved address found"))?;
|
2020-04-17 12:45:34 -04:00
|
|
|
let std_listener = std::net::TcpListener::bind(&addr)?;
|
2021-01-12 02:50:02 -05:00
|
|
|
std_listener.set_nonblocking(true)?;
|
2020-04-17 12:45:34 -04:00
|
|
|
let listener = TcpListener::from_std(std_listener)?;
|
2019-10-21 14:38:28 -04:00
|
|
|
let local_addr = listener.local_addr()?;
|
2019-11-07 09:59:02 -05:00
|
|
|
let tls_listener_resource = TlsListenerResource {
|
2020-12-16 11:14:12 -05:00
|
|
|
listener: AsyncRefCell::new(listener),
|
2019-11-07 09:59:02 -05:00
|
|
|
tls_acceptor,
|
2020-12-16 11:14:12 -05:00
|
|
|
cancel: Default::default(),
|
2019-11-07 09:59:02 -05:00
|
|
|
};
|
2020-04-21 09:48:44 -04:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.resource_table.add(tls_listener_resource);
|
2019-10-21 14:38:28 -04:00
|
|
|
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(OpConn {
|
|
|
|
rid,
|
|
|
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
|
|
|
hostname: local_addr.ip().to_string(),
|
|
|
|
port: local_addr.port(),
|
|
|
|
})),
|
|
|
|
remote_addr: None,
|
|
|
|
})
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_accept_tls(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2021-04-06 04:33:43 -04:00
|
|
|
) -> Result<OpConn, AnyError> {
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table
|
|
|
|
.get::<TlsListenerResource>(rid)
|
|
|
|
.ok_or_else(|| bad_resource("Listener has been closed"))?;
|
2021-01-12 02:50:02 -05:00
|
|
|
let listener = RcRef::map(&resource, |r| &r.listener)
|
2020-12-16 11:14:12 -05:00
|
|
|
.try_borrow_mut()
|
|
|
|
.ok_or_else(|| custom_error("Busy", "Another accept task is ongoing"))?;
|
|
|
|
let cancel = RcRef::map(resource, |r| &r.cancel);
|
|
|
|
let (tcp_stream, _socket_addr) =
|
|
|
|
listener.accept().try_or_cancel(cancel).await.map_err(|e| {
|
|
|
|
// FIXME(bartlomieju): compatibility with current JS implementation
|
|
|
|
if let std::io::ErrorKind::Interrupted = e.kind() {
|
|
|
|
bad_resource("Listener has been closed")
|
|
|
|
} else {
|
|
|
|
e.into()
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2020-12-16 11:14:12 -05:00
|
|
|
})?;
|
2020-08-28 11:08:24 -04:00
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table
|
|
|
|
.get::<TlsListenerResource>(rid)
|
|
|
|
.ok_or_else(|| bad_resource("Listener has been closed"))?;
|
|
|
|
let cancel = RcRef::map(&resource, |r| &r.cancel);
|
|
|
|
let tls_acceptor = resource.tls_acceptor.clone();
|
|
|
|
let tls_stream = tls_acceptor
|
|
|
|
.accept(tcp_stream)
|
|
|
|
.try_or_cancel(cancel)
|
|
|
|
.await?;
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
let rid = {
|
2020-09-10 09:57:45 -04:00
|
|
|
let mut state_ = state.borrow_mut();
|
2020-12-16 11:14:12 -05:00
|
|
|
state_
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.add(TlsServerStreamResource::from(tls_stream))
|
2020-08-28 11:08:24 -04:00
|
|
|
};
|
2020-12-16 11:14:12 -05:00
|
|
|
|
2021-04-06 04:33:43 -04:00
|
|
|
Ok(OpConn {
|
|
|
|
rid,
|
|
|
|
local_addr: Some(OpAddr::Tcp(IpAddr {
|
|
|
|
hostname: local_addr.ip().to_string(),
|
|
|
|
port: local_addr.port(),
|
|
|
|
})),
|
|
|
|
remote_addr: Some(OpAddr::Tcp(IpAddr {
|
|
|
|
hostname: remote_addr.ip().to_string(),
|
|
|
|
port: remote_addr.port(),
|
|
|
|
})),
|
|
|
|
})
|
2019-10-21 14:38:28 -04:00
|
|
|
}
|