use std::sync::Arc;
use serde_json::{Map, Value, json};
use crate::envelope::Envelope;
use crate::error::Error;
use crate::links;
use crate::params::{py_list, py_quote};
use crate::routes;
use crate::transport::{RequestBody, Transport};
pub const DHCP_MODE_AUTOMATIC: &str = "automatic";
pub const DHCP_MODE_MANUAL: &str = "manual";
pub const DHCP_MODE_CUSTOM: &str = "custom";
pub const DHCP_MODES: &[&str] = &[DHCP_MODE_AUTOMATIC, DHCP_MODE_MANUAL, DHCP_MODE_CUSTOM];
const DHCP_MODE_WIRE_VALUES: &[(&str, &str)] = &[
(DHCP_MODE_AUTOMATIC, DHCP_MODE_AUTOMATIC),
(DHCP_MODE_MANUAL, DHCP_MODE_CUSTOM),
(DHCP_MODE_CUSTOM, DHCP_MODE_CUSTOM),
];
pub const CONNECTION_MODES: &[&str] = &["BRIDGE", "NAT"];
const CUSTOM_LEASE_FIELDS: &[&str] = &["start_ip", "end_ip", "subnet_ip", "subnet_mask"];
const CUSTOM_V2_FIELDS: &[&str] = &["main", "guest", "subnetA", "subnetB", "supernet"];
fn wire_mode(mode: &str) -> Option<&'static str> {
DHCP_MODE_WIRE_VALUES
.iter()
.find(|(accepted, _)| *accepted == mode)
.map(|&(_, wire)| wire)
}
#[derive(Debug)]
pub struct DhcpApi {
transport: Arc<Transport>,
}
impl DhcpApi {
#[must_use]
pub fn new(transport: Arc<Transport>) -> Self {
Self { transport }
}
pub async fn set_dhcp(
&self,
network_id: &str,
mode: Option<&str>,
custom: Option<&Map<String, Value>>,
custom_v2: Option<&Map<String, Value>>,
parent: Option<&Value>,
) -> Result<Envelope, Error> {
let mut dhcp = Map::new();
if let Some(mode) = mode {
let Some(wire) = wire_mode(mode) else {
return Err(Error::validation(
"mode",
format!(
"must be one of {}, got {}",
py_list(DHCP_MODES),
py_quote(mode)
),
));
};
dhcp.insert("mode".to_owned(), Value::String(wire.to_owned()));
}
if let Some(custom) = custom {
dhcp.insert(
"custom".to_owned(),
Value::Object(filter_given_keys(custom, CUSTOM_LEASE_FIELDS)?),
);
}
if let Some(custom_v2) = custom_v2 {
dhcp.insert(
"custom_v2".to_owned(),
Value::Object(filter_given_keys(custom_v2, CUSTOM_V2_FIELDS)?),
);
}
if dhcp.is_empty() {
return Err(Error::validation(
"dhcp",
"at least one of mode, custom, custom_v2 must be supplied",
));
}
let url =
routes::dhcp::DHCP_SET_DHCP.resolve(self.transport.api_host(), network_id, parent)?;
links::warn_uncharacterised_write(
"set DHCP configuration for network -- may reboot every eero, like the confirmed \
DNS write path",
);
self.transport
.request(
routes::dhcp::DHCP_SET_DHCP.method.clone(),
url,
&[],
RequestBody::Json(json!({ "dhcp": dhcp })),
)
.await
}
pub async fn set_connection_mode(
&self,
network_id: &str,
mode: &str,
parent: Option<&Value>,
) -> Result<Envelope, Error> {
if !CONNECTION_MODES.contains(&mode) {
return Err(Error::validation(
"mode",
format!(
"must be one of {}, got {}",
py_list(CONNECTION_MODES),
py_quote(mode)
),
));
}
let url = routes::dhcp::DHCP_SET_CONNECTION_MODE.resolve(
self.transport.api_host(),
network_id,
parent,
)?;
links::warn_uncharacterised_write(
"set connection mode for network -- may reboot every eero, like the confirmed DNS \
write path",
);
self.transport
.request(
routes::dhcp::DHCP_SET_CONNECTION_MODE.method.clone(),
url,
&[],
RequestBody::Json(json!({ "connection": { "mode": mode } })),
)
.await
}
pub async fn set_nat_port_randomization(
&self,
network_id: &str,
enabled: bool,
parent: Option<&Value>,
) -> Result<Envelope, Error> {
let url = routes::dhcp::DHCP_SET_NAT_PORT_RANDOMIZATION.resolve(
self.transport.api_host(),
network_id,
parent,
)?;
links::warn_uncharacterised_write(
"set NAT port randomization for network -- may reboot every eero, like the \
confirmed DNS write path",
);
self.transport
.request(
routes::dhcp::DHCP_SET_NAT_PORT_RANDOMIZATION.method.clone(),
url,
&[],
RequestBody::Json(json!({ "nat_port_randomization": enabled })),
)
.await
}
pub async fn set_pppoe(
&self,
eero_serial_or_id: &str,
username: &str,
password: &str,
) -> Result<Envelope, Error> {
let url = routes::dhcp::DHCP_SET_PPPOE.resolve(
self.transport.api_host(),
eero_serial_or_id,
None,
)?;
links::warn_uncharacterised_write("encrypt PPPoE credentials for eero");
self.transport
.request(
routes::dhcp::DHCP_SET_PPPOE.method.clone(),
url,
&[],
RequestBody::Json(json!({
"pppoe": { "username": username, "password": password }
})),
)
.await
}
}
fn filter_given_keys(
mapping: &Map<String, Value>,
allowed: &[&str],
) -> Result<Map<String, Value>, Error> {
let unknown: Vec<&str> = mapping
.keys()
.map(String::as_str)
.filter(|key| !allowed.contains(key))
.collect();
if !unknown.is_empty() {
return Err(Error::validation(
"field",
format!(
"unrecognised field(s): {}; expected one of {}",
py_list(&unknown),
py_list(allowed)
),
));
}
Ok(mapping.clone())
}