2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-06-28 19:43:03 -04:00
|
|
|
|
|
|
|
use crate::io::TcpStreamResource;
|
2019-08-21 20:42:48 -04:00
|
|
|
use crate::resolve_addr::resolve_addr;
|
2020-12-15 07:02:26 -05:00
|
|
|
use crate::resolve_addr::resolve_addr_sync;
|
2021-06-28 19:43:03 -04:00
|
|
|
use crate::NetPermissions;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::bad_resource;
|
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2023-02-11 09:14:02 -05:00
|
|
|
use deno_core::CancelFuture;
|
2022-03-14 13:44:15 -04:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
2021-11-26 13:59:53 -05:00
|
|
|
use deno_core::ByteString;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::CancelHandle;
|
|
|
|
use deno_core::CancelTryFuture;
|
2022-03-15 18:43:17 -04:00
|
|
|
use deno_core::OpDecl;
|
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;
|
2021-01-19 09:39:04 -05:00
|
|
|
use serde::Serialize;
|
2022-03-04 18:43:48 -05:00
|
|
|
use socket2::Domain;
|
2022-10-24 05:05:07 -04:00
|
|
|
use socket2::Protocol;
|
2022-03-04 18:43:48 -05:00
|
|
|
use socket2::Socket;
|
|
|
|
use socket2::Type;
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2019-11-07 09:59:02 -05:00
|
|
|
use std::net::SocketAddr;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2019-08-14 11:03:02 -04:00
|
|
|
use tokio::net::TcpListener;
|
|
|
|
use tokio::net::TcpStream;
|
2020-02-21 11:26:54 -05:00
|
|
|
use tokio::net::UdpSocket;
|
2022-05-16 05:20:41 -04:00
|
|
|
use trust_dns_proto::rr::rdata::caa::Value;
|
2021-01-19 09:39:04 -05:00
|
|
|
use trust_dns_proto::rr::record_data::RData;
|
|
|
|
use trust_dns_proto::rr::record_type::RecordType;
|
|
|
|
use trust_dns_resolver::config::NameServerConfigGroup;
|
|
|
|
use trust_dns_resolver::config::ResolverConfig;
|
|
|
|
use trust_dns_resolver::config::ResolverOpts;
|
2021-10-10 06:14:45 -04:00
|
|
|
use trust_dns_resolver::error::ResolveErrorKind;
|
2021-01-19 09:39:04 -05:00
|
|
|
use trust_dns_resolver::system_conf;
|
|
|
|
use trust_dns_resolver::AsyncResolver;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2022-03-15 18:43:17 -04:00
|
|
|
pub fn init<P: NetPermissions + 'static>() -> Vec<OpDecl> {
|
2021-06-28 19:43:03 -04:00
|
|
|
vec![
|
2022-10-25 16:50:55 -04:00
|
|
|
op_net_accept_tcp::decl(),
|
|
|
|
#[cfg(unix)]
|
|
|
|
crate::ops_unix::op_net_accept_unix::decl(),
|
|
|
|
op_net_connect_tcp::decl::<P>(),
|
|
|
|
#[cfg(unix)]
|
|
|
|
crate::ops_unix::op_net_connect_unix::decl::<P>(),
|
|
|
|
op_net_listen_tcp::decl::<P>(),
|
|
|
|
op_net_listen_udp::decl::<P>(),
|
2022-11-10 16:03:28 -05:00
|
|
|
op_node_unstable_net_listen_udp::decl::<P>(),
|
2022-10-25 16:50:55 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
crate::ops_unix::op_net_listen_unix::decl::<P>(),
|
|
|
|
#[cfg(unix)]
|
|
|
|
crate::ops_unix::op_net_listen_unixpacket::decl::<P>(),
|
2022-11-10 16:03:28 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
crate::ops_unix::op_node_unstable_net_listen_unixpacket::decl::<P>(),
|
2022-10-25 16:50:55 -04:00
|
|
|
op_net_recv_udp::decl(),
|
|
|
|
#[cfg(unix)]
|
|
|
|
crate::ops_unix::op_net_recv_unixpacket::decl(),
|
|
|
|
op_net_send_udp::decl::<P>(),
|
|
|
|
#[cfg(unix)]
|
|
|
|
crate::ops_unix::op_net_send_unixpacket::decl::<P>(),
|
2022-03-14 13:44:15 -04:00
|
|
|
op_dns_resolve::decl::<P>(),
|
2022-11-26 23:48:17 -05:00
|
|
|
op_set_nodelay::decl(),
|
|
|
|
op_set_keepalive::decl(),
|
2021-06-28 19:43:03 -04:00
|
|
|
]
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2021-11-26 13:59:53 -05:00
|
|
|
#[derive(Serialize, Clone, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct TlsHandshakeInfo {
|
|
|
|
pub alpn_protocol: Option<ByteString>,
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
#[derive(Deserialize, Serialize)]
|
2021-04-06 04:33:43 -04:00
|
|
|
pub struct IpAddr {
|
|
|
|
pub hostname: String,
|
|
|
|
pub port: u16,
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
impl From<SocketAddr> for IpAddr {
|
|
|
|
fn from(addr: SocketAddr) -> Self {
|
|
|
|
Self {
|
|
|
|
hostname: addr.ip().to_string(),
|
|
|
|
port: addr.port(),
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2021-12-14 17:27:04 -05:00
|
|
|
pub(crate) fn accept_err(e: std::io::Error) -> AnyError {
|
|
|
|
// FIXME(bartlomieju): compatibility with current JS implementation
|
|
|
|
if let std::io::ErrorKind::Interrupted = e.kind() {
|
|
|
|
bad_resource("Listener has been closed")
|
|
|
|
} else {
|
|
|
|
e.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
#[op]
|
|
|
|
async fn op_net_accept_tcp(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-10-25 16:50:55 -04:00
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(ResourceId, IpAddr, IpAddr), AnyError> {
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table
|
|
|
|
.get::<TcpListenerResource>(rid)
|
2021-08-15 07:29:19 -04:00
|
|
|
.map_err(|_| 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);
|
2021-12-14 17:27:04 -05:00
|
|
|
let (tcp_stream, _socket_addr) = listener
|
|
|
|
.accept()
|
|
|
|
.try_or_cancel(cancel)
|
|
|
|
.await
|
|
|
|
.map_err(accept_err)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
2020-09-10 09:57:45 -04:00
|
|
|
|
|
|
|
let mut state = state.borrow_mut();
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state
|
|
|
|
.resource_table
|
|
|
|
.add(TcpStreamResource::new(tcp_stream.into_split()));
|
2022-10-25 16:50:55 -04:00
|
|
|
Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr)))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-10-25 16:50:55 -04:00
|
|
|
async fn op_net_recv_udp(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-10-25 16:50:55 -04:00
|
|
|
rid: ResourceId,
|
|
|
|
mut buf: ZeroCopyBuf,
|
|
|
|
) -> Result<(usize, IpAddr), AnyError> {
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<UdpSocketResource>(rid)
|
2021-08-15 07:29:19 -04:00
|
|
|
.map_err(|_| bad_resource("Socket has been closed"))?;
|
2021-01-12 02:50:02 -05:00
|
|
|
let socket = RcRef::map(&resource, |r| &r.socket).borrow().await;
|
|
|
|
let cancel_handle = RcRef::map(&resource, |r| &r.cancel);
|
2022-10-25 16:50:55 -04:00
|
|
|
let (nread, remote_addr) = socket
|
|
|
|
.recv_from(&mut buf)
|
2021-01-12 02:50:02 -05:00
|
|
|
.try_or_cancel(cancel_handle)
|
2020-12-16 11:14:12 -05:00
|
|
|
.await?;
|
2022-10-25 16:50:55 -04:00
|
|
|
Ok((nread, IpAddr::from(remote_addr)))
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-10-25 16:50:55 -04:00
|
|
|
async fn op_net_send_udp<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-19 13:25:37 -04:00
|
|
|
rid: ResourceId,
|
2022-10-25 16:50:55 -04:00
|
|
|
addr: IpAddr,
|
2021-10-05 16:38:27 -04:00
|
|
|
zero_copy: ZeroCopyBuf,
|
2021-06-28 19:43:03 -04:00
|
|
|
) -> Result<usize, AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
2022-10-25 16:50:55 -04:00
|
|
|
{
|
|
|
|
let mut s = state.borrow_mut();
|
|
|
|
s.borrow_mut::<NP>().check_net(
|
|
|
|
&(&addr.hostname, Some(addr.port)),
|
|
|
|
"Deno.DatagramConn.send()",
|
|
|
|
)?;
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
2022-10-25 16:50:55 -04:00
|
|
|
let addr = resolve_addr(&addr.hostname, addr.port)
|
|
|
|
.await?
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| generic_error("No resolved address found"))?;
|
2022-10-24 08:55:39 -04:00
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<UdpSocketResource>(rid)
|
|
|
|
.map_err(|_| bad_resource("Socket has been closed"))?;
|
|
|
|
let socket = RcRef::map(&resource, |r| &r.socket).borrow().await;
|
|
|
|
let nwritten = socket.send_to(&zero_copy, &addr).await?;
|
|
|
|
|
|
|
|
Ok(nwritten)
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-10-25 16:50:55 -04:00
|
|
|
pub async fn op_net_connect_tcp<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-10-25 16:50:55 -04:00
|
|
|
addr: IpAddr,
|
|
|
|
) -> Result<(ResourceId, IpAddr, IpAddr), AnyError>
|
2021-06-28 19:43:03 -04:00
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
2022-10-25 16:50:55 -04:00
|
|
|
{
|
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
state_
|
|
|
|
.borrow_mut::<NP>()
|
|
|
|
.check_net(&(&addr.hostname, Some(addr.port)), "Deno.connect()")?;
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
2022-10-25 16:50:55 -04:00
|
|
|
|
|
|
|
let addr = resolve_addr(&addr.hostname, addr.port)
|
|
|
|
.await?
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| generic_error("No resolved address found"))?;
|
|
|
|
let tcp_stream = TcpStream::connect(&addr).await?;
|
|
|
|
let local_addr = tcp_stream.local_addr()?;
|
|
|
|
let remote_addr = tcp_stream.peer_addr()?;
|
|
|
|
|
|
|
|
let mut state_ = state.borrow_mut();
|
|
|
|
let rid = state_
|
|
|
|
.resource_table
|
|
|
|
.add(TcpStreamResource::new(tcp_stream.into_split()));
|
|
|
|
|
|
|
|
Ok((rid, IpAddr::from(local_addr), IpAddr::from(remote_addr)))
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2021-04-08 18:34:15 -04:00
|
|
|
pub struct TcpListenerResource {
|
|
|
|
pub listener: AsyncRefCell<TcpListener>,
|
|
|
|
pub cancel: CancelHandle,
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
impl Resource for TcpListenerResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"tcpListener".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
|
|
|
}
|
2020-12-16 11:14:12 -05:00
|
|
|
}
|
2019-11-07 09:59:02 -05:00
|
|
|
|
2021-01-12 02:50:02 -05:00
|
|
|
struct UdpSocketResource {
|
|
|
|
socket: AsyncRefCell<UdpSocket>,
|
|
|
|
cancel: CancelHandle,
|
|
|
|
}
|
2019-11-07 09:59:02 -05:00
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
impl Resource for UdpSocketResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"udpSocket".into()
|
2019-11-07 09:59:02 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
fn close(self: Rc<Self>) {
|
2021-01-12 02:50:02 -05:00
|
|
|
self.cancel.cancel()
|
2020-12-16 11:14:12 -05:00
|
|
|
}
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
#[op]
|
|
|
|
fn op_net_listen_tcp<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-10-25 16:50:55 -04:00
|
|
|
addr: IpAddr,
|
2022-10-26 15:04:27 -04:00
|
|
|
reuse_port: bool,
|
2022-10-25 16:50:55 -04:00
|
|
|
) -> Result<(ResourceId, IpAddr), AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
2022-10-26 15:04:27 -04:00
|
|
|
if reuse_port {
|
|
|
|
super::check_unstable(state, "Deno.listen({ reusePort: true })");
|
|
|
|
}
|
2022-10-25 16:50:55 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<NP>()
|
|
|
|
.check_net(&(&addr.hostname, Some(addr.port)), "Deno.listen()")?;
|
|
|
|
let addr = resolve_addr_sync(&addr.hostname, addr.port)?
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| generic_error("No resolved address found"))?;
|
2022-03-04 18:43:48 -05:00
|
|
|
let domain = if addr.is_ipv4() {
|
|
|
|
Domain::IPV4
|
|
|
|
} else {
|
|
|
|
Domain::IPV6
|
|
|
|
};
|
|
|
|
let socket = Socket::new(domain, Type::STREAM, None)?;
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
socket.set_reuse_address(true)?;
|
2022-10-26 15:04:27 -04:00
|
|
|
if reuse_port {
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
socket.set_reuse_port(true)?;
|
|
|
|
}
|
2022-03-04 18:43:48 -05:00
|
|
|
let socket_addr = socket2::SockAddr::from(addr);
|
|
|
|
socket.bind(&socket_addr)?;
|
|
|
|
socket.listen(128)?;
|
|
|
|
socket.set_nonblocking(true)?;
|
|
|
|
let std_listener: std::net::TcpListener = socket.into();
|
2020-04-17 12:45:34 -04:00
|
|
|
let listener = TcpListener::from_std(std_listener)?;
|
2020-02-21 11:26:54 -05:00
|
|
|
let local_addr = listener.local_addr()?;
|
|
|
|
let listener_resource = TcpListenerResource {
|
2020-12-16 11:14:12 -05:00
|
|
|
listener: AsyncRefCell::new(listener),
|
|
|
|
cancel: Default::default(),
|
2020-02-21 11:26:54 -05:00
|
|
|
};
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.resource_table.add(listener_resource);
|
2020-02-21 11:26:54 -05:00
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
Ok((rid, IpAddr::from(local_addr)))
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
fn net_listen_udp<NP>(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-10-25 16:50:55 -04:00
|
|
|
addr: IpAddr,
|
|
|
|
reuse_address: bool,
|
|
|
|
) -> Result<(ResourceId, IpAddr), AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
|
|
|
state
|
|
|
|
.borrow_mut::<NP>()
|
|
|
|
.check_net(&(&addr.hostname, Some(addr.port)), "Deno.listenDatagram()")?;
|
|
|
|
let addr = resolve_addr_sync(&addr.hostname, addr.port)?
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| generic_error("No resolved address found"))?;
|
|
|
|
|
2022-10-24 05:05:07 -04:00
|
|
|
let domain = if addr.is_ipv4() {
|
|
|
|
Domain::IPV4
|
|
|
|
} else {
|
|
|
|
Domain::IPV6
|
|
|
|
};
|
|
|
|
let socket_tmp = Socket::new(domain, Type::DGRAM, Some(Protocol::UDP))?;
|
2022-10-25 16:50:55 -04:00
|
|
|
if reuse_address {
|
2022-10-24 05:05:07 -04:00
|
|
|
// This logic is taken from libuv:
|
|
|
|
//
|
|
|
|
// On the BSDs, SO_REUSEPORT implies SO_REUSEADDR but with some additional
|
|
|
|
// refinements for programs that use multicast.
|
|
|
|
//
|
|
|
|
// Linux as of 3.9 has a SO_REUSEPORT socket option but with semantics that
|
|
|
|
// are different from the BSDs: it _shares_ the port rather than steal it
|
|
|
|
// from the current listener. While useful, it's not something we can
|
|
|
|
// emulate on other platforms so we don't enable it.
|
|
|
|
#[cfg(any(target_os = "windows", target_os = "linux"))]
|
|
|
|
socket_tmp.set_reuse_address(true)?;
|
|
|
|
#[cfg(all(unix, not(target_os = "linux")))]
|
|
|
|
socket_tmp.set_reuse_port(true)?;
|
|
|
|
}
|
|
|
|
let socket_addr = socket2::SockAddr::from(addr);
|
|
|
|
socket_tmp.bind(&socket_addr)?;
|
|
|
|
socket_tmp.set_nonblocking(true)?;
|
2021-11-29 05:14:46 -05:00
|
|
|
// Enable messages to be sent to the broadcast address (255.255.255.255) by default
|
2022-10-24 05:05:07 -04:00
|
|
|
socket_tmp.set_broadcast(true)?;
|
|
|
|
let std_socket: std::net::UdpSocket = socket_tmp.into();
|
2020-04-17 12:45:34 -04:00
|
|
|
let socket = UdpSocket::from_std(std_socket)?;
|
2020-02-21 11:26:54 -05:00
|
|
|
let local_addr = socket.local_addr()?;
|
2021-01-12 02:50:02 -05:00
|
|
|
let socket_resource = UdpSocketResource {
|
|
|
|
socket: AsyncRefCell::new(socket),
|
|
|
|
cancel: Default::default(),
|
|
|
|
};
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.resource_table.add(socket_resource);
|
2020-02-21 11:26:54 -05:00
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
Ok((rid, IpAddr::from(local_addr)))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2021-01-19 09:39:04 -05:00
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
#[op]
|
|
|
|
fn op_net_listen_udp<NP>(
|
|
|
|
state: &mut OpState,
|
|
|
|
addr: IpAddr,
|
|
|
|
reuse_address: bool,
|
|
|
|
) -> Result<(ResourceId, IpAddr), AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
|
|
|
super::check_unstable(state, "Deno.listenDatagram");
|
|
|
|
net_listen_udp::<NP>(state, addr, reuse_address)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
fn op_node_unstable_net_listen_udp<NP>(
|
|
|
|
state: &mut OpState,
|
|
|
|
addr: IpAddr,
|
|
|
|
reuse_address: bool,
|
|
|
|
) -> Result<(ResourceId, IpAddr), AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
|
|
|
net_listen_udp::<NP>(state, addr, reuse_address)
|
|
|
|
}
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
#[derive(Serialize, Eq, PartialEq, Debug)]
|
2021-01-19 09:39:04 -05:00
|
|
|
#[serde(untagged)]
|
2021-10-10 06:16:11 -04:00
|
|
|
pub enum DnsReturnRecord {
|
2021-01-19 09:39:04 -05:00
|
|
|
A(String),
|
2021-03-25 14:17:37 -04:00
|
|
|
Aaaa(String),
|
|
|
|
Aname(String),
|
2022-05-16 05:20:41 -04:00
|
|
|
Caa {
|
|
|
|
critical: bool,
|
|
|
|
tag: String,
|
|
|
|
value: String,
|
|
|
|
},
|
2021-03-25 14:17:37 -04:00
|
|
|
Cname(String),
|
|
|
|
Mx {
|
2021-01-19 09:39:04 -05:00
|
|
|
preference: u16,
|
|
|
|
exchange: String,
|
|
|
|
},
|
2022-05-15 11:42:02 -04:00
|
|
|
Naptr {
|
|
|
|
order: u16,
|
|
|
|
preference: u16,
|
|
|
|
flags: String,
|
|
|
|
services: String,
|
|
|
|
regexp: String,
|
|
|
|
replacement: String,
|
|
|
|
},
|
2022-05-03 14:04:20 -04:00
|
|
|
Ns(String),
|
2021-03-25 14:17:37 -04:00
|
|
|
Ptr(String),
|
2022-05-14 08:08:35 -04:00
|
|
|
Soa {
|
|
|
|
mname: String,
|
|
|
|
rname: String,
|
2022-05-15 10:43:08 -04:00
|
|
|
serial: u32,
|
|
|
|
refresh: i32,
|
|
|
|
retry: i32,
|
|
|
|
expire: i32,
|
|
|
|
minimum: u32,
|
2022-05-14 08:08:35 -04:00
|
|
|
},
|
2021-03-25 14:17:37 -04:00
|
|
|
Srv {
|
2021-01-19 09:39:04 -05:00
|
|
|
priority: u16,
|
|
|
|
weight: u16,
|
|
|
|
port: u16,
|
|
|
|
target: String,
|
|
|
|
},
|
2021-03-25 14:17:37 -04:00
|
|
|
Txt(Vec<String>),
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
2021-03-18 14:42:01 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ResolveAddrArgs {
|
2023-02-11 09:14:02 -05:00
|
|
|
cancel_rid: Option<ResourceId>,
|
2021-03-18 14:42:01 -04:00
|
|
|
query: String,
|
|
|
|
record_type: RecordType,
|
|
|
|
options: Option<ResolveDnsOption>,
|
|
|
|
}
|
2021-01-19 09:39:04 -05:00
|
|
|
|
2021-03-18 14:42:01 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ResolveDnsOption {
|
|
|
|
name_server: Option<NameServer>,
|
|
|
|
}
|
2021-01-19 09:39:04 -05:00
|
|
|
|
2021-03-18 14:42:01 -04:00
|
|
|
fn default_port() -> u16 {
|
|
|
|
53
|
|
|
|
}
|
2021-01-19 09:39:04 -05:00
|
|
|
|
2021-03-18 14:42:01 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct NameServer {
|
|
|
|
ip_addr: String,
|
|
|
|
#[serde(default = "default_port")]
|
|
|
|
port: u16,
|
|
|
|
}
|
2021-01-19 09:39:04 -05:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-10-10 06:16:11 -04:00
|
|
|
pub async fn op_dns_resolve<NP>(
|
2021-03-18 14:42:01 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
args: ResolveAddrArgs,
|
2021-06-28 19:43:03 -04:00
|
|
|
) -> Result<Vec<DnsReturnRecord>, AnyError>
|
|
|
|
where
|
|
|
|
NP: NetPermissions + 'static,
|
|
|
|
{
|
2021-01-19 09:39:04 -05:00
|
|
|
let ResolveAddrArgs {
|
|
|
|
query,
|
|
|
|
record_type,
|
|
|
|
options,
|
2023-02-11 09:14:02 -05:00
|
|
|
cancel_rid,
|
2021-03-18 14:42:01 -04:00
|
|
|
} = args;
|
2021-01-19 09:39:04 -05:00
|
|
|
|
|
|
|
let (config, opts) = if let Some(name_server) =
|
|
|
|
options.as_ref().and_then(|o| o.name_server.as_ref())
|
|
|
|
{
|
|
|
|
let group = NameServerConfigGroup::from_ips_clear(
|
|
|
|
&[name_server.ip_addr.parse()?],
|
|
|
|
name_server.port,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
(
|
|
|
|
ResolverConfig::from_parts(None, vec![], group),
|
|
|
|
ResolverOpts::default(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
system_conf::read_system_conf()?
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut s = state.borrow_mut();
|
2021-06-28 19:43:03 -04:00
|
|
|
let perm = s.borrow_mut::<NP>();
|
2021-01-19 09:39:04 -05:00
|
|
|
|
|
|
|
// Checks permission against the name servers which will be actually queried.
|
|
|
|
for ns in config.name_servers() {
|
|
|
|
let socker_addr = &ns.socket_addr;
|
|
|
|
let ip = socker_addr.ip().to_string();
|
|
|
|
let port = socker_addr.port();
|
2022-09-27 16:36:33 -04:00
|
|
|
perm.check_net(&(ip, Some(port)), "Deno.resolveDns()")?;
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let resolver = AsyncResolver::tokio(config, opts)?;
|
|
|
|
|
2023-02-11 09:14:02 -05:00
|
|
|
let lookup_fut = resolver.lookup(query, record_type);
|
|
|
|
|
|
|
|
let cancel_handle = cancel_rid.and_then(|rid| {
|
|
|
|
state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<CancelHandle>(rid)
|
|
|
|
.ok()
|
|
|
|
});
|
|
|
|
|
|
|
|
let lookup = if let Some(cancel_handle) = cancel_handle {
|
|
|
|
let lookup_rv = lookup_fut.or_cancel(cancel_handle).await;
|
|
|
|
|
|
|
|
if let Some(cancel_rid) = cancel_rid {
|
|
|
|
state.borrow_mut().resource_table.close(cancel_rid).ok();
|
|
|
|
};
|
|
|
|
|
|
|
|
lookup_rv?
|
|
|
|
} else {
|
|
|
|
lookup_fut.await
|
|
|
|
};
|
|
|
|
|
|
|
|
lookup
|
2021-10-10 06:14:45 -04:00
|
|
|
.map_err(|e| {
|
2023-01-27 10:43:16 -05:00
|
|
|
let message = format!("{e}");
|
2021-10-10 06:14:45 -04:00
|
|
|
match e.kind() {
|
|
|
|
ResolveErrorKind::NoRecordsFound { .. } => {
|
|
|
|
custom_error("NotFound", message)
|
|
|
|
}
|
|
|
|
ResolveErrorKind::Message("No connections available") => {
|
|
|
|
custom_error("NotConnected", message)
|
|
|
|
}
|
|
|
|
ResolveErrorKind::Timeout => custom_error("TimedOut", message),
|
|
|
|
_ => generic_error(message),
|
|
|
|
}
|
|
|
|
})?
|
2021-01-19 09:39:04 -05:00
|
|
|
.iter()
|
2023-01-11 15:31:14 -05:00
|
|
|
.filter_map(|rdata| rdata_to_return_record(record_type)(rdata).transpose())
|
|
|
|
.collect::<Result<Vec<DnsReturnRecord>, AnyError>>()
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-11-26 23:48:17 -05:00
|
|
|
pub fn op_set_nodelay(
|
2022-01-31 10:36:54 -05:00
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
|
|
|
nodelay: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let resource: Rc<TcpStreamResource> =
|
|
|
|
state.resource_table.get::<TcpStreamResource>(rid)?;
|
|
|
|
resource.set_nodelay(nodelay)
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-11-26 23:48:17 -05:00
|
|
|
pub fn op_set_keepalive(
|
2022-01-31 10:36:54 -05:00
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
|
|
|
keepalive: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let resource: Rc<TcpStreamResource> =
|
|
|
|
state.resource_table.get::<TcpStreamResource>(rid)?;
|
|
|
|
resource.set_keepalive(keepalive)
|
|
|
|
}
|
|
|
|
|
2021-01-19 09:39:04 -05:00
|
|
|
fn rdata_to_return_record(
|
|
|
|
ty: RecordType,
|
2023-01-11 15:31:14 -05:00
|
|
|
) -> impl Fn(&RData) -> Result<Option<DnsReturnRecord>, AnyError> {
|
2021-01-19 09:39:04 -05:00
|
|
|
use RecordType::*;
|
2023-01-11 15:31:14 -05:00
|
|
|
move |r: &RData| -> Result<Option<DnsReturnRecord>, AnyError> {
|
|
|
|
let record = match ty {
|
2021-01-19 09:39:04 -05:00
|
|
|
A => r.as_a().map(ToString::to_string).map(DnsReturnRecord::A),
|
|
|
|
AAAA => r
|
|
|
|
.as_aaaa()
|
|
|
|
.map(ToString::to_string)
|
2021-03-25 14:17:37 -04:00
|
|
|
.map(DnsReturnRecord::Aaaa),
|
2021-01-19 09:39:04 -05:00
|
|
|
ANAME => r
|
|
|
|
.as_aname()
|
|
|
|
.map(ToString::to_string)
|
2021-03-25 14:17:37 -04:00
|
|
|
.map(DnsReturnRecord::Aname),
|
2022-05-16 05:20:41 -04:00
|
|
|
CAA => r.as_caa().map(|caa| DnsReturnRecord::Caa {
|
|
|
|
critical: caa.issuer_critical(),
|
|
|
|
tag: caa.tag().to_string(),
|
|
|
|
value: match caa.value() {
|
|
|
|
Value::Issuer(name, key_values) => {
|
|
|
|
let mut s = String::new();
|
|
|
|
|
|
|
|
if let Some(name) = name {
|
2022-07-01 09:28:06 -04:00
|
|
|
s.push_str(&name.to_string());
|
2022-05-16 05:20:41 -04:00
|
|
|
} else if name.is_none() && key_values.is_empty() {
|
|
|
|
s.push(';');
|
|
|
|
}
|
|
|
|
|
|
|
|
for key_value in key_values {
|
2022-07-01 09:28:06 -04:00
|
|
|
s.push_str("; ");
|
|
|
|
s.push_str(&key_value.to_string());
|
2022-05-16 05:20:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
|
|
|
Value::Url(url) => url.to_string(),
|
|
|
|
Value::Unknown(data) => String::from_utf8(data.to_vec()).unwrap(),
|
|
|
|
},
|
|
|
|
}),
|
2021-01-19 09:39:04 -05:00
|
|
|
CNAME => r
|
|
|
|
.as_cname()
|
|
|
|
.map(ToString::to_string)
|
2021-03-25 14:17:37 -04:00
|
|
|
.map(DnsReturnRecord::Cname),
|
|
|
|
MX => r.as_mx().map(|mx| DnsReturnRecord::Mx {
|
2021-01-19 09:39:04 -05:00
|
|
|
preference: mx.preference(),
|
|
|
|
exchange: mx.exchange().to_string(),
|
|
|
|
}),
|
2022-05-15 11:42:02 -04:00
|
|
|
NAPTR => r.as_naptr().map(|naptr| DnsReturnRecord::Naptr {
|
|
|
|
order: naptr.order(),
|
|
|
|
preference: naptr.preference(),
|
|
|
|
flags: String::from_utf8(naptr.flags().to_vec()).unwrap(),
|
|
|
|
services: String::from_utf8(naptr.services().to_vec()).unwrap(),
|
|
|
|
regexp: String::from_utf8(naptr.regexp().to_vec()).unwrap(),
|
|
|
|
replacement: naptr.replacement().to_string(),
|
|
|
|
}),
|
2022-05-03 14:04:20 -04:00
|
|
|
NS => r.as_ns().map(ToString::to_string).map(DnsReturnRecord::Ns),
|
2021-01-19 09:39:04 -05:00
|
|
|
PTR => r
|
|
|
|
.as_ptr()
|
|
|
|
.map(ToString::to_string)
|
2021-03-25 14:17:37 -04:00
|
|
|
.map(DnsReturnRecord::Ptr),
|
2022-05-14 08:08:35 -04:00
|
|
|
SOA => r.as_soa().map(|soa| DnsReturnRecord::Soa {
|
|
|
|
mname: soa.mname().to_string(),
|
|
|
|
rname: soa.rname().to_string(),
|
2022-05-15 10:43:08 -04:00
|
|
|
serial: soa.serial(),
|
|
|
|
refresh: soa.refresh(),
|
|
|
|
retry: soa.retry(),
|
|
|
|
expire: soa.expire(),
|
|
|
|
minimum: soa.minimum(),
|
2022-05-14 08:08:35 -04:00
|
|
|
}),
|
2021-03-25 14:17:37 -04:00
|
|
|
SRV => r.as_srv().map(|srv| DnsReturnRecord::Srv {
|
2021-01-19 09:39:04 -05:00
|
|
|
priority: srv.priority(),
|
|
|
|
weight: srv.weight(),
|
|
|
|
port: srv.port(),
|
|
|
|
target: srv.target().to_string(),
|
|
|
|
}),
|
|
|
|
TXT => r.as_txt().map(|txt| {
|
|
|
|
let texts: Vec<String> = txt
|
|
|
|
.iter()
|
|
|
|
.map(|bytes| {
|
|
|
|
// Tries to parse these bytes as Latin-1
|
|
|
|
bytes.iter().map(|&b| b as char).collect::<String>()
|
|
|
|
})
|
|
|
|
.collect();
|
2021-03-25 14:17:37 -04:00
|
|
|
DnsReturnRecord::Txt(texts)
|
2021-01-19 09:39:04 -05:00
|
|
|
}),
|
2023-01-11 15:31:14 -05:00
|
|
|
_ => {
|
|
|
|
return Err(custom_error(
|
|
|
|
"NotSupported",
|
|
|
|
"Provided record type is not supported",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(record)
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-02-16 11:32:29 -05:00
|
|
|
use crate::UnstableChecker;
|
2022-01-31 10:36:54 -05:00
|
|
|
use deno_core::Extension;
|
|
|
|
use deno_core::JsRuntime;
|
|
|
|
use deno_core::RuntimeOptions;
|
|
|
|
use socket2::SockRef;
|
2021-01-19 09:39:04 -05:00
|
|
|
use std::net::Ipv4Addr;
|
|
|
|
use std::net::Ipv6Addr;
|
2022-01-31 10:36:54 -05:00
|
|
|
use std::path::Path;
|
2022-05-16 05:20:41 -04:00
|
|
|
use trust_dns_proto::rr::rdata::caa::KeyValue;
|
|
|
|
use trust_dns_proto::rr::rdata::caa::CAA;
|
2021-01-19 09:39:04 -05:00
|
|
|
use trust_dns_proto::rr::rdata::mx::MX;
|
2022-05-15 11:42:02 -04:00
|
|
|
use trust_dns_proto::rr::rdata::naptr::NAPTR;
|
2021-01-19 09:39:04 -05:00
|
|
|
use trust_dns_proto::rr::rdata::srv::SRV;
|
|
|
|
use trust_dns_proto::rr::rdata::txt::TXT;
|
2022-05-14 08:08:35 -04:00
|
|
|
use trust_dns_proto::rr::rdata::SOA;
|
2021-01-19 09:39:04 -05:00
|
|
|
use trust_dns_proto::rr::record_data::RData;
|
|
|
|
use trust_dns_proto::rr::Name;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_a() {
|
|
|
|
let func = rdata_to_return_record(RecordType::A);
|
|
|
|
let rdata = RData::A(Ipv4Addr::new(127, 0, 0, 1));
|
|
|
|
assert_eq!(
|
2023-01-11 15:31:14 -05:00
|
|
|
func(&rdata).unwrap(),
|
2021-01-19 09:39:04 -05:00
|
|
|
Some(DnsReturnRecord::A("127.0.0.1".to_string()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_aaaa() {
|
|
|
|
let func = rdata_to_return_record(RecordType::AAAA);
|
|
|
|
let rdata = RData::AAAA(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
|
2023-01-11 15:31:14 -05:00
|
|
|
assert_eq!(
|
|
|
|
func(&rdata).unwrap(),
|
|
|
|
Some(DnsReturnRecord::Aaaa("::1".to_string()))
|
|
|
|
);
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_aname() {
|
|
|
|
let func = rdata_to_return_record(RecordType::ANAME);
|
|
|
|
let rdata = RData::ANAME(Name::new());
|
2023-01-11 15:31:14 -05:00
|
|
|
assert_eq!(
|
|
|
|
func(&rdata).unwrap(),
|
|
|
|
Some(DnsReturnRecord::Aname("".to_string()))
|
|
|
|
);
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
2022-05-16 05:20:41 -04:00
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_caa() {
|
|
|
|
let func = rdata_to_return_record(RecordType::CAA);
|
|
|
|
let rdata = RData::CAA(CAA::new_issue(
|
|
|
|
false,
|
|
|
|
Some(Name::parse("example.com", None).unwrap()),
|
|
|
|
vec![KeyValue::new("account", "123456")],
|
|
|
|
));
|
|
|
|
assert_eq!(
|
2023-01-11 15:31:14 -05:00
|
|
|
func(&rdata).unwrap(),
|
2022-05-16 05:20:41 -04:00
|
|
|
Some(DnsReturnRecord::Caa {
|
|
|
|
critical: false,
|
|
|
|
tag: "issue".to_string(),
|
|
|
|
value: "example.com; account=123456".to_string(),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-19 09:39:04 -05:00
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_cname() {
|
|
|
|
let func = rdata_to_return_record(RecordType::CNAME);
|
|
|
|
let rdata = RData::CNAME(Name::new());
|
2023-01-11 15:31:14 -05:00
|
|
|
assert_eq!(
|
|
|
|
func(&rdata).unwrap(),
|
|
|
|
Some(DnsReturnRecord::Cname("".to_string()))
|
|
|
|
);
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_mx() {
|
|
|
|
let func = rdata_to_return_record(RecordType::MX);
|
|
|
|
let rdata = RData::MX(MX::new(10, Name::new()));
|
|
|
|
assert_eq!(
|
2023-01-11 15:31:14 -05:00
|
|
|
func(&rdata).unwrap(),
|
2021-03-25 14:17:37 -04:00
|
|
|
Some(DnsReturnRecord::Mx {
|
2021-01-19 09:39:04 -05:00
|
|
|
preference: 10,
|
|
|
|
exchange: "".to_string()
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-15 11:42:02 -04:00
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_naptr() {
|
|
|
|
let func = rdata_to_return_record(RecordType::NAPTR);
|
|
|
|
let rdata = RData::NAPTR(NAPTR::new(
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
<Box<[u8]>>::default(),
|
|
|
|
<Box<[u8]>>::default(),
|
|
|
|
<Box<[u8]>>::default(),
|
|
|
|
Name::new(),
|
|
|
|
));
|
|
|
|
assert_eq!(
|
2023-01-11 15:31:14 -05:00
|
|
|
func(&rdata).unwrap(),
|
2022-05-15 11:42:02 -04:00
|
|
|
Some(DnsReturnRecord::Naptr {
|
|
|
|
order: 1,
|
|
|
|
preference: 2,
|
|
|
|
flags: "".to_string(),
|
|
|
|
services: "".to_string(),
|
|
|
|
regexp: "".to_string(),
|
|
|
|
replacement: "".to_string()
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-03 14:04:20 -04:00
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_ns() {
|
|
|
|
let func = rdata_to_return_record(RecordType::NS);
|
|
|
|
let rdata = RData::NS(Name::new());
|
2023-01-11 15:31:14 -05:00
|
|
|
assert_eq!(
|
|
|
|
func(&rdata).unwrap(),
|
|
|
|
Some(DnsReturnRecord::Ns("".to_string()))
|
|
|
|
);
|
2022-05-03 14:04:20 -04:00
|
|
|
}
|
|
|
|
|
2021-01-19 09:39:04 -05:00
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_ptr() {
|
|
|
|
let func = rdata_to_return_record(RecordType::PTR);
|
|
|
|
let rdata = RData::PTR(Name::new());
|
2023-01-11 15:31:14 -05:00
|
|
|
assert_eq!(
|
|
|
|
func(&rdata).unwrap(),
|
|
|
|
Some(DnsReturnRecord::Ptr("".to_string()))
|
|
|
|
);
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
2022-05-14 08:08:35 -04:00
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_soa() {
|
|
|
|
let func = rdata_to_return_record(RecordType::SOA);
|
|
|
|
let rdata = RData::SOA(SOA::new(
|
|
|
|
Name::new(),
|
|
|
|
Name::new(),
|
|
|
|
0,
|
|
|
|
i32::MAX,
|
|
|
|
i32::MAX,
|
|
|
|
i32::MAX,
|
|
|
|
0,
|
|
|
|
));
|
|
|
|
assert_eq!(
|
2023-01-11 15:31:14 -05:00
|
|
|
func(&rdata).unwrap(),
|
2022-05-14 08:08:35 -04:00
|
|
|
Some(DnsReturnRecord::Soa {
|
|
|
|
mname: "".to_string(),
|
2022-05-15 10:43:08 -04:00
|
|
|
rname: "".to_string(),
|
|
|
|
serial: 0,
|
|
|
|
refresh: i32::MAX,
|
|
|
|
retry: i32::MAX,
|
|
|
|
expire: i32::MAX,
|
|
|
|
minimum: 0,
|
2022-05-14 08:08:35 -04:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-19 09:39:04 -05:00
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_srv() {
|
|
|
|
let func = rdata_to_return_record(RecordType::SRV);
|
|
|
|
let rdata = RData::SRV(SRV::new(1, 2, 3, Name::new()));
|
|
|
|
assert_eq!(
|
2023-01-11 15:31:14 -05:00
|
|
|
func(&rdata).unwrap(),
|
2021-03-25 14:17:37 -04:00
|
|
|
Some(DnsReturnRecord::Srv {
|
2021-01-19 09:39:04 -05:00
|
|
|
priority: 1,
|
|
|
|
weight: 2,
|
|
|
|
port: 3,
|
|
|
|
target: "".to_string()
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rdata_to_return_record_txt() {
|
|
|
|
let func = rdata_to_return_record(RecordType::TXT);
|
|
|
|
let rdata = RData::TXT(TXT::from_bytes(vec![
|
|
|
|
"foo".as_bytes(),
|
|
|
|
"bar".as_bytes(),
|
|
|
|
&[0xa3], // "£" in Latin-1
|
|
|
|
&[0xe3, 0x81, 0x82], // "あ" in UTF-8
|
|
|
|
]));
|
|
|
|
assert_eq!(
|
2023-01-11 15:31:14 -05:00
|
|
|
func(&rdata).unwrap(),
|
2021-03-25 14:17:37 -04:00
|
|
|
Some(DnsReturnRecord::Txt(vec![
|
2021-01-19 09:39:04 -05:00
|
|
|
"foo".to_string(),
|
|
|
|
"bar".to_string(),
|
|
|
|
"£".to_string(),
|
|
|
|
"ã\u{81}\u{82}".to_string(),
|
|
|
|
]))
|
|
|
|
);
|
|
|
|
}
|
2022-01-31 10:36:54 -05:00
|
|
|
|
|
|
|
struct TestPermission {}
|
|
|
|
|
|
|
|
impl NetPermissions for TestPermission {
|
|
|
|
fn check_net<T: AsRef<str>>(
|
|
|
|
&mut self,
|
|
|
|
_host: &(T, Option<u16>),
|
2022-09-27 16:36:33 -04:00
|
|
|
_api_name: &str,
|
2022-01-31 10:36:54 -05:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
fn check_read(
|
|
|
|
&mut self,
|
|
|
|
_p: &Path,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-01-31 10:36:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
fn check_write(
|
|
|
|
&mut self,
|
|
|
|
_p: &Path,
|
|
|
|
_api_name: &str,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-01-31 10:36:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
|
|
|
async fn tcp_set_no_delay() {
|
|
|
|
let set_nodelay = Box::new(|state: &mut OpState, rid| {
|
2022-11-26 23:48:17 -05:00
|
|
|
op_set_nodelay::call(state, rid, true).unwrap();
|
2022-01-31 10:36:54 -05:00
|
|
|
});
|
|
|
|
let test_fn = Box::new(|socket: SockRef| {
|
|
|
|
assert!(socket.nodelay().unwrap());
|
|
|
|
assert!(!socket.keepalive().unwrap());
|
|
|
|
});
|
|
|
|
check_sockopt(String::from("127.0.0.1:4245"), set_nodelay, test_fn).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
|
|
|
async fn tcp_set_keepalive() {
|
|
|
|
let set_keepalive = Box::new(|state: &mut OpState, rid| {
|
2022-11-26 23:48:17 -05:00
|
|
|
op_set_keepalive::call(state, rid, true).unwrap();
|
2022-01-31 10:36:54 -05:00
|
|
|
});
|
|
|
|
let test_fn = Box::new(|socket: SockRef| {
|
|
|
|
assert!(!socket.nodelay().unwrap());
|
|
|
|
assert!(socket.keepalive().unwrap());
|
|
|
|
});
|
|
|
|
check_sockopt(String::from("127.0.0.1:4246"), set_keepalive, test_fn).await;
|
|
|
|
}
|
|
|
|
|
2022-08-21 13:31:14 -04:00
|
|
|
#[allow(clippy::type_complexity)]
|
2022-01-31 10:36:54 -05:00
|
|
|
async fn check_sockopt(
|
|
|
|
addr: String,
|
|
|
|
set_sockopt_fn: Box<dyn Fn(&mut OpState, u32)>,
|
|
|
|
test_fn: Box<dyn FnOnce(SockRef)>,
|
|
|
|
) {
|
|
|
|
let clone_addr = addr.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let listener = TcpListener::bind(addr).await.unwrap();
|
|
|
|
let _ = listener.accept().await;
|
|
|
|
});
|
2023-01-08 17:48:46 -05:00
|
|
|
let my_ext = Extension::builder("test_ext")
|
2022-01-31 10:36:54 -05:00
|
|
|
.state(move |state| {
|
|
|
|
state.put(TestPermission {});
|
2022-02-16 11:32:29 -05:00
|
|
|
state.put(UnstableChecker { unstable: true });
|
2022-01-31 10:36:54 -05:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let mut runtime = JsRuntime::new(RuntimeOptions {
|
|
|
|
extensions: vec![my_ext],
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
let conn_state = runtime.op_state();
|
|
|
|
|
|
|
|
let server_addr: Vec<&str> = clone_addr.split(':').collect();
|
2022-10-25 16:50:55 -04:00
|
|
|
let ip_addr = IpAddr {
|
2022-01-31 10:36:54 -05:00
|
|
|
hostname: String::from(server_addr[0]),
|
|
|
|
port: server_addr[1].parse().unwrap(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let connect_fut =
|
2022-10-25 16:50:55 -04:00
|
|
|
op_net_connect_tcp::call::<TestPermission>(conn_state, ip_addr);
|
|
|
|
let (rid, _, _) = connect_fut.await.unwrap();
|
2022-01-31 10:36:54 -05:00
|
|
|
|
|
|
|
let state = runtime.op_state();
|
|
|
|
set_sockopt_fn(&mut state.borrow_mut(), rid);
|
|
|
|
|
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<TcpStreamResource>(rid)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let wr = resource.wr_borrow_mut().await;
|
|
|
|
let stream = wr.as_ref().as_ref();
|
|
|
|
let socket = socket2::SockRef::from(stream);
|
|
|
|
test_fn(socket);
|
|
|
|
}
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|