use std::future::IntoFuture as _;
use std::io;
use std::net::{Ipv4Addr, SocketAddr};
use std::pin::Pin;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::task::{Context, Poll};
use std::time::Duration;
use axum::Router;
use axum::body::{Body, Bytes, HttpBody};
use axum::extract::State;
use axum::http::header::{AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE, WWW_AUTHENTICATE};
use axum::http::{HeaderMap, Method, StatusCode, Uri};
use axum::response::{IntoResponse, Response};
use tokio::task::JoinHandle;
use tr064::{ClientBuilder, Fingerprint, SecretString, Service, Url};
mod auth;
mod callmonitor;
mod device;
pub mod dns;
mod fixture;
mod soap;
mod tls;
mod web;
pub use callmonitor::CallMonitor;
use device::{Device, Fault, Hooks, short_id};
pub use fixture::{Answer, Error, Fixture};
use tls::TlsListener;
use web::Web;
pub const USERNAME: &str = "fritz-sim";
pub const PASSWORD: &str = "fritz-sim-password";
const XML: &str = r#"text/xml; charset="utf-8""#;
#[derive(Debug, Clone)]
pub struct Options {
pub address: SocketAddr,
pub username: String,
pub password: String,
pub https: bool,
}
impl Default for Options {
fn default() -> Self {
Self {
address: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
username: USERNAME.to_owned(),
password: PASSWORD.to_owned(),
https: false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Injection {
Fault {
service: String,
action: String,
code: u16,
},
Delay(Duration),
DiscardWrites,
SideEffect {
service: String,
action: String,
variable: String,
value: String,
},
LockLogin,
Counter {
service: String,
variable: String,
step: u64,
wrap: Option<u64>,
},
Disconnect { service: String, action: String },
Truncate { service: String, action: String },
Reboot { down: u32, serial: Option<String> },
FailPage {
page: String,
field: String,
value: String,
},
}
impl Injection {
pub fn disconnect(service: impl Into<String>, action: impl Into<String>) -> Self {
Self::Disconnect {
service: service.into(),
action: action.into(),
}
}
pub fn truncate(service: impl Into<String>, action: impl Into<String>) -> Self {
Self::Truncate {
service: service.into(),
action: action.into(),
}
}
pub fn reboot(down: u32) -> Self {
Self::Reboot { down, serial: None }
}
pub fn fail_page(
page: impl Into<String>,
field: impl Into<String>,
value: impl Into<String>,
) -> Self {
Self::FailPage {
page: page.into(),
field: field.into(),
value: value.into(),
}
}
pub fn fault(service: impl Into<String>, action: impl Into<String>, code: u16) -> Self {
Self::Fault {
service: service.into(),
action: action.into(),
code,
}
}
pub fn side_effect(
service: impl Into<String>,
action: impl Into<String>,
variable: impl Into<String>,
value: impl Into<String>,
) -> Self {
Self::SideEffect {
service: service.into(),
action: action.into(),
variable: variable.into(),
value: value.into(),
}
}
pub fn counter(
service: impl Into<String>,
variable: impl Into<String>,
step: u64,
wrap: Option<u64>,
) -> Self {
Self::Counter {
service: service.into(),
variable: variable.into(),
step,
wrap,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Request {
pub method: String,
pub path: String,
pub action: Option<String>,
pub arguments: Vec<(String, String)>,
pub authorized: bool,
}
#[derive(Debug)]
pub struct Sim {
url: Url,
fingerprint: Option<Fingerprint>,
shared: Arc<Shared>,
server: JoinHandle<io::Result<()>>,
}
impl Sim {
pub async fn start(fixture: Fixture) -> io::Result<Self> {
Self::start_with(fixture, Options::default()).await
}
pub async fn start_with(fixture: Fixture, options: Options) -> io::Result<Self> {
let listener = tokio::net::TcpListener::bind(options.address).await?;
let scheme = if options.https { "https" } else { "http" };
let url = Url::parse(&format!("{scheme}://{}", listener.local_addr()?))
.map_err(io::Error::other)?;
let https = options.https;
let shared = Arc::new(Shared {
options,
inner: Mutex::new(Inner {
web: Web::new(&fixture.pages),
device: Device::new(fixture),
injections: Vec::new(),
requests: Vec::new(),
down: 0,
}),
});
let app = Router::new()
.fallback(handle)
.with_state(Arc::clone(&shared));
let (server, fingerprint) = if https {
let (listener, fingerprint) = TlsListener::new(listener);
let server = tokio::spawn(axum::serve(listener, app).into_future());
(server, Some(fingerprint))
} else {
(tokio::spawn(axum::serve(listener, app).into_future()), None)
};
Ok(Self {
url,
fingerprint,
shared,
server,
})
}
pub fn url(&self) -> &Url {
&self.url
}
pub fn fingerprint(&self) -> Option<Fingerprint> {
self.fingerprint
}
pub fn client(&self) -> ClientBuilder {
let options = &self.shared.options;
let builder = tr064::Client::builder(self.url.clone()).credentials(
options.username.clone(),
SecretString::from(options.password.clone()),
);
match self.fingerprint {
Some(fingerprint) => builder.pin(fingerprint),
None => builder,
}
}
pub fn inject(&self, injection: Injection) {
self.shared.lock().injections.push(injection);
}
pub fn clear_injections(&self) {
self.shared.lock().injections.clear();
}
pub fn requests(&self) -> Vec<Request> {
self.shared.lock().requests.clone()
}
pub fn log_filter(&self) -> Option<String> {
self.shared.lock().web.log_filter().map(str::to_owned)
}
pub fn state(&self, service: &str, variable: &str) -> Option<String> {
let inner = self.shared.lock();
let service = inner.device.fixture.description.service(service).ok()?;
inner
.device
.state(short_id(service), variable)
.map(str::to_owned)
}
}
impl Drop for Sim {
fn drop(&mut self) {
self.server.abort();
}
}
#[derive(Debug)]
struct Shared {
options: Options,
inner: Mutex<Inner>,
}
#[derive(Debug)]
struct Inner {
device: Device,
web: Web,
injections: Vec<Injection>,
requests: Vec<Request>,
down: u32,
}
impl Shared {
fn lock(&self) -> MutexGuard<'_, Inner> {
self.inner.lock().unwrap_or_else(PoisonError::into_inner)
}
fn respond(
&self,
method: &Method,
uri: &Uri,
headers: &HeaderMap,
body: &str,
) -> (Response, Duration) {
let mut inner = self.lock();
let path = uri.path();
let call = (method == Method::POST)
.then(|| soap::parse(body))
.flatten();
let locked = inner.injections.contains(&Injection::LockLogin);
let authorized = !locked
&& auth::verify(
headers.get(AUTHORIZATION).and_then(|v| v.to_str().ok()),
&auth::Expected {
username: &self.options.username,
password: &self.options.password,
nonce: NONCE,
method: method.as_str(),
uri: uri.path_and_query().map_or(path, |p| p.as_str()),
},
);
inner.requests.push(Request {
method: method.to_string(),
path: path.to_owned(),
action: call.as_ref().map(|c| c.action.clone()),
arguments: call.as_ref().map_or_default(|c| c.arguments.clone()),
authorized,
});
let delay = inner
.injections
.iter()
.map(|i| match i {
Injection::Delay(delay) => *delay,
_ => Duration::ZERO,
})
.sum();
let response = match *method {
_ if inner.down > 0 => {
inner.down -= 1;
broken()
}
_ if path == web::LOGIN => inner.web.login(method, body, &self.options, locked),
_ if path.starts_with(web::API) => {
let Inner { web, device, .. } = &*inner;
web.api(&device.fixture.api, method, path, headers)
}
_ if path == web::DATA => {
let Inner {
web,
device,
injections,
..
} = &mut *inner;
web.page(&device.fixture.pages, injections, method, body)
}
_ if path == web::HOMEAUTO => {
inner.web.stats(&inner.device.fixture.stats, method, body)
}
Method::GET => inner.document(path, uri.query()),
Method::POST if !authorized => (
StatusCode::UNAUTHORIZED,
[(WWW_AUTHENTICATE, auth::challenge(NONCE))],
)
.into_response(),
Method::POST => inner.control(path, headers, call),
_ => StatusCode::METHOD_NOT_ALLOWED.into_response(),
};
(response, delay)
}
}
const NONCE: &str = "F1A2B3C4D5E6F708";
const SESSION: &str = "5e55104d0c0ffee5";
impl Inner {
fn document(&self, path: &str, query: Option<&str>) -> Response {
let fixture = &self.device.fixture;
let name = path.trim_start_matches('/');
if let Some(list) = name
.strip_suffix(".lua")
.and_then(|stem| fixture.lists.get(stem))
{
let session = query
.into_iter()
.flat_map(|q| q.split('&'))
.any(|p| p == format!("sid={SESSION}"));
return if session {
list.clone().into_response()
} else {
StatusCode::FORBIDDEN.into_response()
};
}
let xml = match name {
"tr64desc.xml" => Some(&fixture.description_xml),
name => fixture.scpds.get(name).map(|(xml, _)| xml),
};
match xml {
Some(xml) => ([(CONTENT_TYPE, XML)], xml.clone()).into_response(),
None => StatusCode::NOT_FOUND.into_response(),
}
}
fn control(&mut self, path: &str, headers: &HeaderMap, call: Option<soap::Call>) -> Response {
let service = self
.device
.fixture
.description
.services()
.find(|s| s.control_url == path)
.cloned();
let Some(service) = service else {
return StatusCode::NOT_FOUND.into_response();
};
let Some(call) = call else {
return StatusCode::BAD_REQUEST.into_response();
};
let soap_action = headers
.get("SOAPAction")
.and_then(|v| v.to_str().ok())
.unwrap_or_default()
.trim_matches('"');
let expected = format!("{}#{}", service.service_type, call.action);
let result = if soap_action == expected && call.service_type == service.service_type {
if let Some(response) = self.breaks(&service, &call.action) {
return response;
}
let hooks = self.hooks(&service, &call.action);
let result = self
.device
.call(&service, &call.action, &call.arguments, &hooks);
if result.is_ok() {
self.reboot(&service, &call.action);
}
result
} else {
Err(Fault::new(401))
};
match result {
Ok(mut out) => {
for (_, value) in &mut out {
*value = value.replace("sid=***", &format!("sid={SESSION}"));
}
let xml = soap::response(&service.service_type, &call.action, &out);
([(CONTENT_TYPE, XML)], xml).into_response()
}
Err(fault) => (
StatusCode::INTERNAL_SERVER_ERROR,
[(CONTENT_TYPE, XML)],
soap::fault(&fault),
)
.into_response(),
}
}
fn breaks(&self, service: &Service, action: &str) -> Option<Response> {
self.injections.iter().find_map(|i| match i {
Injection::Disconnect {
service: reference,
action: name,
} if name == action && service.matches(reference) => Some(broken()),
Injection::Truncate {
service: reference,
action: name,
} if name == action && service.matches(reference) => Some(truncated()),
_ => None,
})
}
fn reboot(&mut self, service: &Service, action: &str) -> Option<()> {
(action == "Reboot" && service.matches("DeviceConfig1")).then_some(())?;
let reboot = self
.injections
.iter()
.find(|i| matches!(i, Injection::Reboot { .. }));
let Some(Injection::Reboot { down, serial }) = reboot.cloned() else {
return None;
};
self.down = down;
let device = &mut self.device;
device.set_output("DeviceInfo1", "GetInfo", "NewUpTime", "0")?;
device.set_output("DeviceInfo1", "GetInfo", "NewSerialNumber", &serial?)
}
fn hooks(&self, service: &Service, action: &str) -> Hooks {
let fault = self.injections.iter().find_map(|i| match i {
Injection::Fault {
service: reference,
action: name,
code,
} if name == action && service.matches(reference) => Some(Fault::new(*code)),
_ => None,
});
let side_effects = self
.injections
.iter()
.filter_map(|i| match i {
Injection::SideEffect {
service: reference,
action: name,
variable,
value,
} if name == action && service.matches(reference) => {
Some((variable.clone(), value.clone()))
}
_ => None,
})
.collect();
let counters = self
.injections
.iter()
.filter_map(|i| match i {
Injection::Counter {
service: reference,
variable,
step,
wrap,
} if service.matches(reference) => Some((variable.clone(), *step, *wrap)),
_ => None,
})
.collect();
Hooks {
fault,
discard_writes: self.injections.contains(&Injection::DiscardWrites),
side_effects,
counters,
}
}
}
fn broken() -> Response {
(StatusCode::OK, [(CONTENT_LENGTH, "1")], Body::new(Broken)).into_response()
}
fn truncated() -> Response {
(StatusCode::OK, "<?xml version=\"1.0\"?><s:Envelope").into_response()
}
struct Broken;
impl HttpBody for Broken {
type Data = Bytes;
type Error = io::Error;
fn poll_frame(
self: Pin<&mut Self>,
_: &mut Context<'_>,
) -> Poll<Option<Result<http_body::Frame<Bytes>, io::Error>>> {
Poll::Ready(Some(Err(io::ErrorKind::ConnectionReset.into())))
}
}
async fn handle(
State(shared): State<Arc<Shared>>,
method: Method,
uri: Uri,
headers: HeaderMap,
body: String,
) -> Response {
let (response, delay) = shared.respond(&method, &uri, &headers, &body);
tokio::time::sleep(delay).await;
response
}
#[cfg(test)]
mod tests {
use super::*;
const FIXTURE: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/fixtures/fritzbox-7690/8.25");
const WLAN: &str = "urn:dslforum-org:service:WLANConfiguration:1";
const CONTROL: &str = "/upnp/control/wlanconfig1";
fn inner() -> Inner {
Inner {
device: Device::new(Fixture::load(FIXTURE).unwrap()),
web: Web::default(),
injections: Vec::new(),
requests: Vec::new(),
down: 0,
}
}
fn call(service_type: &str) -> soap::Call {
soap::Call {
service_type: service_type.to_owned(),
action: "GetInfo".to_owned(),
arguments: Vec::new(),
}
}
fn soap_action(value: &str) -> HeaderMap {
let mut headers = HeaderMap::new();
headers.insert("SOAPAction", value.parse().unwrap());
headers
}
#[test]
fn answers_calls_that_match_the_service() {
let headers = soap_action(&format!("\"{WLAN}#GetInfo\""));
let response = inner().control(CONTROL, &headers, Some(call(WLAN)));
assert_eq!(response.status(), StatusCode::OK);
}
#[test]
fn rejects_calls_that_do_not_match_the_service() {
let mut inner = inner();
let right = soap_action(&format!("{WLAN}#GetInfo"));
let wrong = soap_action(&format!("{WLAN}#SetEnable"));
for (headers, service_type) in [
(&wrong, WLAN),
(&HeaderMap::new(), WLAN),
(&right, "urn:other"),
] {
let response = inner.control(CONTROL, headers, Some(call(service_type)));
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
let response = inner.control("/upnp/control/missing", &right, Some(call(WLAN)));
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let response = inner.control(CONTROL, &right, None);
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
#[test]
fn allows_only_get_and_post() {
let shared = Shared {
options: Options::default(),
inner: Mutex::new(inner()),
};
let uri = Uri::from_static("/tr64desc.xml");
let (response, delay) = shared.respond(&Method::PUT, &uri, &HeaderMap::new(), "");
assert_eq!(response.status(), StatusCode::METHOD_NOT_ALLOWED);
assert_eq!(delay, Duration::ZERO);
assert_eq!(shared.lock().requests[0].method, "PUT");
}
}