2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2021-04-06 04:33:43 -04:00
|
|
|
use super::utils::into_string;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2021-04-06 04:33:43 -04:00
|
|
|
use deno_core::error::{type_error, AnyError};
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::Extension;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2021-04-05 12:40:24 -04:00
|
|
|
use serde::Serialize;
|
2019-08-23 01:30:14 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::env;
|
2021-12-11 09:56:45 -05:00
|
|
|
use std::sync::atomic::AtomicI32;
|
2021-11-27 18:45:38 -05:00
|
|
|
use std::sync::atomic::Ordering::Relaxed;
|
2021-12-11 09:56:45 -05:00
|
|
|
use std::sync::Arc;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2021-12-11 09:56:45 -05:00
|
|
|
pub fn init(maybe_exit_code: Option<Arc<AtomicI32>>) -> Extension {
|
2021-05-02 19:22:57 -04:00
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
2022-03-14 13:44:15 -04:00
|
|
|
op_env::decl(),
|
|
|
|
op_exec_path::decl(),
|
|
|
|
op_exit::decl(),
|
|
|
|
op_delete_env::decl(),
|
|
|
|
op_get_env::decl(),
|
|
|
|
op_getuid::decl(),
|
|
|
|
op_hostname::decl(),
|
|
|
|
op_loadavg::decl(),
|
|
|
|
op_network_interfaces::decl(),
|
|
|
|
op_os_release::decl(),
|
|
|
|
op_set_env::decl(),
|
|
|
|
op_set_exit_code::decl(),
|
|
|
|
op_system_memory_info::decl(),
|
2021-05-02 19:22:57 -04:00
|
|
|
])
|
2021-12-11 09:56:45 -05:00
|
|
|
.state(move |state| {
|
|
|
|
let exit_code = maybe_exit_code.clone().unwrap_or_default();
|
|
|
|
state.put::<Arc<AtomicI32>>(exit_code);
|
|
|
|
Ok(())
|
|
|
|
})
|
2021-05-02 19:22:57 -04:00
|
|
|
.build()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_exec_path(state: &mut OpState) -> Result<String, AnyError> {
|
2019-08-23 01:30:14 -04:00
|
|
|
let current_exe = env::current_exe().unwrap();
|
2020-09-19 19:17:35 -04:00
|
|
|
state
|
2021-04-11 22:15:43 -04:00
|
|
|
.borrow_mut::<Permissions>()
|
2021-03-17 17:45:12 -04:00
|
|
|
.read
|
|
|
|
.check_blind(¤t_exe, "exec_path")?;
|
2019-08-23 01:30:14 -04:00
|
|
|
// Now apply URL parser to current exe to get fully resolved path, otherwise
|
|
|
|
// we might get `./` and `../` bits in `exec_path`
|
2019-08-14 11:03:02 -04:00
|
|
|
let exe_url = Url::from_file_path(current_exe).unwrap();
|
2019-08-23 01:30:14 -04:00
|
|
|
let path = exe_url.to_file_path().unwrap();
|
2021-04-05 12:40:24 -04:00
|
|
|
|
|
|
|
into_string(path.into_os_string())
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_set_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-10-05 16:38:27 -04:00
|
|
|
key: String,
|
|
|
|
value: String,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-10-05 16:38:27 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check(&key)?;
|
|
|
|
let invalid_key = key.is_empty() || key.contains(&['=', '\0'] as &[char]);
|
|
|
|
let invalid_value = value.contains('\0');
|
2021-02-23 04:24:59 -05:00
|
|
|
if invalid_key || invalid_value {
|
|
|
|
return Err(type_error("Key or value contains invalid characters."));
|
|
|
|
}
|
2021-10-05 16:38:27 -04:00
|
|
|
env::set_var(key, value);
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_env(state: &mut OpState) -> Result<HashMap<String, String>, AnyError> {
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(env::vars().collect())
|
2019-10-02 11:55:28 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_get_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
key: String,
|
|
|
|
) -> Result<Option<String>, AnyError> {
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check(&key)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
if key.is_empty() || key.contains(&['=', '\0'] as &[char]) {
|
2021-02-23 04:24:59 -05:00
|
|
|
return Err(type_error("Key contains invalid characters."));
|
|
|
|
}
|
2021-04-05 12:40:24 -04:00
|
|
|
let r = match env::var(key) {
|
|
|
|
Err(env::VarError::NotPresent) => None,
|
|
|
|
v => Some(v?),
|
2019-10-02 11:55:28 -04:00
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(r)
|
2019-10-02 11:55:28 -04:00
|
|
|
}
|
2021-04-13 07:25:21 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_delete_env(state: &mut OpState, key: String) -> Result<(), AnyError> {
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check(&key)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
if key.is_empty() || key.contains(&['=', '\0'] as &[char]) {
|
2021-02-23 04:24:59 -05:00
|
|
|
return Err(type_error("Key contains invalid characters."));
|
|
|
|
}
|
2021-04-05 12:40:24 -04:00
|
|
|
env::remove_var(key);
|
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_set_exit_code(state: &mut OpState, code: i32) -> Result<(), AnyError> {
|
2021-12-11 09:56:45 -05:00
|
|
|
state.borrow_mut::<Arc<AtomicI32>>().store(code, Relaxed);
|
2021-11-27 18:45:38 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_exit(state: &mut OpState) -> Result<(), AnyError> {
|
2021-12-11 09:56:45 -05:00
|
|
|
let code = state.borrow::<Arc<AtomicI32>>().load(Relaxed);
|
2021-04-05 12:40:24 -04:00
|
|
|
std::process::exit(code)
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_loadavg(state: &mut OpState) -> Result<(f64, f64, f64), AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.loadavg");
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
2020-02-22 18:46:52 -05:00
|
|
|
match sys_info::loadavg() {
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(loadavg) => Ok((loadavg.one, loadavg.five, loadavg.fifteen)),
|
|
|
|
Err(_) => Ok((0.0, 0.0, 0.0)),
|
2020-02-22 18:46:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_hostname(state: &mut OpState) -> Result<String, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.hostname");
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
2020-02-24 08:35:45 -05:00
|
|
|
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_string());
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(hostname)
|
2019-09-27 19:09:42 -04:00
|
|
|
}
|
2020-02-24 08:35:45 -05:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_os_release(state: &mut OpState) -> Result<String, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.osRelease");
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
2020-02-24 08:35:45 -05:00
|
|
|
let release = sys_info::os_release().unwrap_or_else(|_| "".to_string());
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(release)
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-12-22 18:00:43 -05:00
|
|
|
fn op_network_interfaces(
|
|
|
|
state: &mut OpState,
|
|
|
|
) -> Result<Vec<NetworkInterface>, AnyError> {
|
|
|
|
super::check_unstable(state, "Deno.networkInterfaces");
|
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
|
|
|
Ok(netif::up()?.map(NetworkInterface::from).collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(serde::Serialize)]
|
|
|
|
struct NetworkInterface {
|
|
|
|
family: &'static str,
|
|
|
|
name: String,
|
|
|
|
address: String,
|
|
|
|
netmask: String,
|
|
|
|
scopeid: Option<u32>,
|
|
|
|
cidr: String,
|
|
|
|
mac: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<netif::Interface> for NetworkInterface {
|
|
|
|
fn from(ifa: netif::Interface) -> Self {
|
|
|
|
let family = match ifa.address() {
|
|
|
|
std::net::IpAddr::V4(_) => "IPv4",
|
|
|
|
std::net::IpAddr::V6(_) => "IPv6",
|
|
|
|
};
|
|
|
|
|
|
|
|
let (address, range) = ifa.cidr();
|
|
|
|
let cidr = format!("{:?}/{}", address, range);
|
|
|
|
|
|
|
|
let name = ifa.name().to_owned();
|
|
|
|
let address = format!("{:?}", ifa.address());
|
|
|
|
let netmask = format!("{:?}", ifa.netmask());
|
|
|
|
let scopeid = ifa.scope_id();
|
|
|
|
|
|
|
|
let [b0, b1, b2, b3, b4, b5] = ifa.mac();
|
|
|
|
let mac = format!(
|
|
|
|
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
|
|
|
|
b0, b1, b2, b3, b4, b5
|
|
|
|
);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
family,
|
|
|
|
name,
|
|
|
|
address,
|
|
|
|
netmask,
|
|
|
|
scopeid,
|
|
|
|
cidr,
|
|
|
|
mac,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
// Copied from sys-info/lib.rs (then tweaked)
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct MemInfo {
|
|
|
|
pub total: u64,
|
|
|
|
pub free: u64,
|
|
|
|
pub available: u64,
|
|
|
|
pub buffers: u64,
|
|
|
|
pub cached: u64,
|
|
|
|
pub swap_total: u64,
|
|
|
|
pub swap_free: u64,
|
2020-02-24 08:35:45 -05:00
|
|
|
}
|
2020-09-10 04:38:17 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-09-10 04:38:17 -04:00
|
|
|
fn op_system_memory_info(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<Option<MemInfo>, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.systemMemoryInfo");
|
2021-04-13 07:25:21 -04:00
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
2020-09-10 04:38:17 -04:00
|
|
|
match sys_info::mem_info() {
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(info) => Ok(Some(MemInfo {
|
|
|
|
total: info.total,
|
|
|
|
free: info.free,
|
|
|
|
available: info.avail,
|
|
|
|
buffers: info.buffers,
|
|
|
|
cached: info.cached,
|
|
|
|
swap_total: info.swap_total,
|
|
|
|
swap_free: info.swap_free,
|
2020-09-10 04:38:17 -04:00
|
|
|
})),
|
2021-04-05 12:40:24 -04:00
|
|
|
Err(_) => Ok(None),
|
2020-09-10 04:38:17 -04:00
|
|
|
}
|
|
|
|
}
|
2022-01-31 00:44:19 -05:00
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_getuid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
|
2022-01-31 00:44:19 -05:00
|
|
|
super::check_unstable(state, "Deno.getUid");
|
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
|
|
|
unsafe { Ok(Some(libc::getuid())) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_getuid(state: &mut OpState) -> Result<Option<u32>, AnyError> {
|
2022-01-31 00:44:19 -05:00
|
|
|
super::check_unstable(state, "Deno.getUid");
|
|
|
|
state.borrow_mut::<Permissions>().env.check_all()?;
|
|
|
|
Ok(None)
|
|
|
|
}
|