Refactor pa_volume_interface to not poll inefficiently
This commit is contained in:
parent
4d476c5e42
commit
e2f4aac438
11 changed files with 212 additions and 329 deletions
|
@ -18,8 +18,4 @@ tokio-tungstenite = { version = "0.21.0", features = ["rustls-tls-native-roots"]
|
|||
tokio-stream = "0.1.14"
|
||||
futures-util = "0.3.30"
|
||||
parse-display = "0.9.0"
|
||||
serde_with = "3.6.1"
|
||||
|
||||
# same as tokio-tungstenite
|
||||
rustls = "0.22.0"
|
||||
rustls-native-certs = "0.7.0"
|
||||
serde_with = "3.6.1"
|
|
@ -7,8 +7,6 @@ use url::Url;
|
|||
pub struct GlobalConfig {
|
||||
pub base_url: Url,
|
||||
pub token: Box<str>,
|
||||
#[serde(default)]
|
||||
pub accept_invalid_certs: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use futures_util::SinkExt;
|
||||
use parse_display::{Display, FromStr, IntoResult};
|
||||
use parse_display::{Display, FromStr};
|
||||
use reqwest::header::{HeaderMap, HeaderValue};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
@ -10,7 +10,7 @@ use std::sync::Arc;
|
|||
use std::time::Duration;
|
||||
use tokio::sync::{broadcast, RwLock};
|
||||
use tokio_stream::StreamExt;
|
||||
use tokio_tungstenite::{tungstenite, Connector};
|
||||
use tokio_tungstenite::tungstenite;
|
||||
use url::Url;
|
||||
|
||||
#[derive(Debug, Clone, FromStr, Display, SerializeDisplay, DeserializeFromStr, Eq, PartialEq, Hash)]
|
||||
|
@ -42,7 +42,7 @@ pub struct HaClient {
|
|||
}
|
||||
|
||||
impl HaClient {
|
||||
pub async fn new(base_url: Url, token: Box<str>, accept_invalid_certs: bool, subscribed_entity_ids: Vec<EntityId>) -> Self {
|
||||
pub async fn new(base_url: Url, token: Box<str>, subscribed_entity_ids: Vec<EntityId>) -> Self {
|
||||
let http_client = reqwest::ClientBuilder::new()
|
||||
.connect_timeout(Duration::from_secs(10))
|
||||
.default_headers({
|
||||
|
@ -53,7 +53,6 @@ impl HaClient {
|
|||
);
|
||||
map
|
||||
})
|
||||
.danger_accept_invalid_certs(accept_invalid_certs)
|
||||
.user_agent(format!("home_assistant deckster handler (v{})", env!("CARGO_PKG_VERSION")))
|
||||
.build()
|
||||
.unwrap(); // The HTTP client being available is essential.
|
||||
|
@ -61,12 +60,9 @@ impl HaClient {
|
|||
let state_updates_sender = broadcast::Sender::<StateUpdate>::new(min(subscribed_entity_ids.len(), 16));
|
||||
let state_timestamp_by_entity_id = subscribed_entity_ids.iter().map(|i| (i.clone(), "".to_owned().into_boxed_str())).collect();
|
||||
|
||||
let rustls_config = rustls::ClientConfig::builder().with_root_certificates();
|
||||
|
||||
tokio::spawn(do_work(
|
||||
base_url.clone(),
|
||||
token,
|
||||
Arc::new(rustls_config),
|
||||
state_updates_sender.clone(),
|
||||
http_client.clone(),
|
||||
state_timestamp_by_entity_id,
|
||||
|
@ -123,7 +119,6 @@ impl HaClient {
|
|||
async fn do_work(
|
||||
base_url: Url,
|
||||
token: Box<str>,
|
||||
rustls_config: Arc<rustls::ClientConfig>,
|
||||
state_updates_sender: broadcast::Sender<StateUpdate>,
|
||||
http_client: reqwest::Client,
|
||||
state_timestamp_by_entity_id: HashMap<EntityId, Box<str>>,
|
||||
|
@ -140,8 +135,7 @@ async fn do_work(
|
|||
let state_timestamp_by_entity_id = Arc::new(RwLock::new(state_timestamp_by_entity_id));
|
||||
|
||||
loop {
|
||||
let connection_result =
|
||||
tokio_tungstenite::connect_async_tls_with_config(&websocket_url, None, false, Some(Connector::Rustls(Arc::clone(&rustls_config)))).await;
|
||||
let connection_result = tokio_tungstenite::connect_async(&websocket_url).await;
|
||||
|
||||
match connection_result {
|
||||
Err(tungstenite::Error::Io(error)) => {
|
||||
|
|
|
@ -40,12 +40,7 @@ impl Handler {
|
|||
|
||||
let ha_client = task_set.block_on(
|
||||
&runtime,
|
||||
HaClient::new(
|
||||
data.global_config.base_url,
|
||||
data.global_config.token,
|
||||
data.global_config.accept_invalid_certs,
|
||||
subscribed_entity_ids,
|
||||
),
|
||||
HaClient::new(data.global_config.base_url, data.global_config.token, subscribed_entity_ids),
|
||||
);
|
||||
|
||||
for (path, config) in data.key_configs {
|
||||
|
|
|
@ -6,7 +6,6 @@ use crate::handler::Handler;
|
|||
mod config;
|
||||
mod ha_client;
|
||||
mod handler;
|
||||
mod tls;
|
||||
mod util;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
use rustls::client::danger::ServerCertVerifier;
|
||||
use rustls::{ClientConfig, RootCertStore, SignatureScheme};
|
||||
use std::sync::Arc;
|
||||
|
||||
// tokio-tungstenite does not provide a way to allow invalid certs.
|
||||
// Because of that, we need to build our own rustls config.
|
||||
pub fn get_rustls_client_config() -> Arc<ClientConfig> {
|
||||
let mut root_store = RootCertStore::empty();
|
||||
let native_certs = rustls_native_certs::load_native_certs().unwrap();
|
||||
_ = root_store.add_parsable_certificates(native_certs);
|
||||
|
||||
let mut config = ClientConfig::builder().with_root_certificates(root_store).with_no_client_auth();
|
||||
|
||||
config.dangerous().set_certificate_verifier(Arc::new(NoVerifier));
|
||||
|
||||
Arc::new(config)
|
||||
}
|
||||
|
||||
pub struct NoVerifier;
|
||||
|
||||
impl ServerCertVerifier for NoVerifier {
|
||||
fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
|
||||
todo!()
|
||||
}
|
||||
fn verify_server_cert(
|
||||
&self,
|
||||
_end_entity: &rustls::Certificate,
|
||||
_intermediates: &[rustls::Certificate],
|
||||
_server_name: &ServerName,
|
||||
_scts: &mut dyn Iterator<Item = &[u8]>,
|
||||
_ocsp_response: &[u8],
|
||||
_now: std::time::SystemTime,
|
||||
) -> Result<ServerCertVerified, TLSError> {
|
||||
Ok(ServerCertVerified::assertion())
|
||||
}
|
||||
|
||||
fn verify_tls12_signature(&self, _message: &[u8], _cert: &rustls::Certificate, _dss: &DigitallySignedStruct) -> Result<HandshakeSignatureValid, TLSError> {
|
||||
Ok(HandshakeSignatureValid::assertion())
|
||||
}
|
||||
|
||||
fn verify_tls13_signature(&self, _message: &[u8], _cert: &rustls::Certificate, _dss: &DigitallySignedStruct) -> Result<HandshakeSignatureValid, TLSError> {
|
||||
Ok(HandshakeSignatureValid::assertion())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue