From 374b399631d76125c7c668f1397bacaf9eca4701 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 4 Aug 2023 16:33:39 +0700 Subject: [PATCH 01/12] Fix simultaneous discovery subscription --- .../core/src/bc_protocol/connection/bcconn.rs | 1 + .../src/bc_protocol/connection/discovery.rs | 57 ++++++++++++------- crates/core/src/bcudp/ser.rs | 2 +- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index a6756e9..29b549d 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -295,6 +295,7 @@ impl Poller { if occ_entry.get().is_closed() { occ_entry.insert(tx); } else { + // log::error!("Failed to subscribe in bcconn to {:?} for {:?}", msg_num, msg_id); let _ = tx .send(Err(Error::SimultaneousSubscription { msg_num })) .await; diff --git a/crates/core/src/bc_protocol/connection/discovery.rs b/crates/core/src/bc_protocol/connection/discovery.rs index e015480..0709724 100644 --- a/crates/core/src/bc_protocol/connection/discovery.rs +++ b/crates/core/src/bc_protocol/connection/discovery.rs @@ -78,12 +78,14 @@ lazy_static! { } type Subscriber = Arc>>>>; +type Handlers = Arc>>>>; type ArcFramedSocket = UdpFramed>; pub(crate) struct Discoverer { socket: Arc, handle: RwLock>, writer: Sender>, subsribers: Subscriber, + handlers: Handlers, local_addr: SocketAddr, } @@ -96,26 +98,27 @@ impl Discoverer { let (mut writer, mut reader) = inner.split(); let mut set = JoinSet::new(); let subsribers: Subscriber = Default::default(); + let handlers: Handlers = Default::default(); let thread_subscriber = subsribers.clone(); + let thread_handlers = handlers.clone(); set.spawn(async move { loop { tokio::task::yield_now().await; match reader.next().await { Some(Ok((BcUdp::Discovery(bcudp), addr))) => { - let mut tid = bcudp.tid; + let tid = bcudp.tid; let mut needs_removal = false; - if let Some(sender) = thread_subscriber.read().await.get(&tid) { + if let (Some(sender), true) = + (thread_subscriber.read().await.get(&tid), tid > 0) + { if sender.send(Ok((bcudp, addr))).await.is_err() { needs_removal = true; } - } else if let Some(any_sender) = thread_subscriber.read().await.get(&0) { - if any_sender.send(Ok((bcudp, addr))).await.is_err() { - tid = 0; // To make is remove 0 - needs_removal = true; - } } else { - trace!("Udp Discovery got this unexpected BcUdp {:?}", bcudp); + for sender in thread_handlers.write().await.iter_mut() { + let _ = sender.send(Ok((bcudp.clone(), addr))).await; + } } if needs_removal { thread_subscriber.write().await.remove(&tid); @@ -148,6 +151,7 @@ impl Discoverer { handle: RwLock::new(set), writer: sinker, subsribers, + handlers, local_addr, }) } @@ -157,24 +161,33 @@ impl Discoverer { } async fn subscribe(&self, tid: u32) -> Result>> { - let mut subs = self.subsribers.write().await; - match subs.entry(tid) { - Entry::Vacant(vacant) => { - let (tx, rx) = channel(10); - vacant.insert(tx); - Ok(rx) - } - Entry::Occupied(mut occ) => { - if occ.get().is_closed() { + if tid > 0 { + let mut subs = self.subsribers.write().await; + match subs.entry(tid) { + Entry::Vacant(vacant) => { let (tx, rx) = channel(10); - occ.insert(tx); + vacant.insert(tx); Ok(rx) - } else { - Err(Error::SimultaneousSubscription { - msg_num: Some(tid as u16), - }) + } + Entry::Occupied(mut occ) => { + if occ.get().is_closed() { + let (tx, rx) = channel(10); + occ.insert(tx); + Ok(rx) + } else { + // log::error!("Failed to subscribe in discovery to {:?}", tid); + Err(Error::SimultaneousSubscription { + msg_num: Some(tid as u16), + }) + } } } + } else { + // If tid is zero we listen to all! + let mut handlers = self.handlers.write().await; + let (tx, rx) = channel(10); + handlers.push(tx); + Ok(rx) } } diff --git a/crates/core/src/bcudp/ser.rs b/crates/core/src/bcudp/ser.rs index e055e2e..b566d8a 100644 --- a/crates/core/src/bcudp/ser.rs +++ b/crates/core/src/bcudp/ser.rs @@ -52,7 +52,7 @@ fn bcudp_ack<'a, W: 'a + Write>( le_u32(0), le_u32(payload.group_id), le_u32(payload.packet_id), - le_u32(0xd7160000), + le_u32(0), le_u32(binary_payload.len() as u32), slice(binary_payload), )) From 810dd3cdc71921668c90f09348d303b4d4fafc11 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 4 Aug 2023 18:53:08 +0700 Subject: [PATCH 02/12] Fix early exit on discovery --- crates/core/src/bc_protocol.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 3447228..713eaf0 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -186,7 +186,7 @@ impl BcCamera { }; let res = tokio::select! { - v = async { + Ok(v) = async { let uid_local = uid.clone(); info!("{}: Trying local discovery", options.name); let result = discovery.local(&uid_local, Some(sockets)).await; @@ -202,8 +202,9 @@ impl BcCamera { }, Err(e) => Err(e) } - }, if allow_local => v, - v = async { + }, if allow_local => Ok(v), + Ok(v) = async { + let mut discovery = Discovery::new().await?; let reg_result; // Registration is looped as it seems that reolink // only updates the registration lazily when someone attempts @@ -220,9 +221,11 @@ impl BcCamera { } log::debug!("Registration failed. Retrying"); tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + // New discovery to get new client IDs + discovery = Discovery::new().await?; }; tokio::select! { - v = async { + Ok(v) = async { let uid_remote = uid.clone(); info!("{}: Trying remote discovery", options.name); let result = discovery @@ -240,8 +243,8 @@ impl BcCamera { }, Err(e) => Err(e) } - }, if allow_remote => v, - v = async { + }, if allow_remote => Ok(v), + Ok(v) = async { let uid_map = uid.clone(); info!("{}: Trying map discovery", options.name); let result = discovery.map(®_result).await; @@ -257,8 +260,8 @@ impl BcCamera { }, Err(e) => Err(e), } - }, if allow_map => v, - v = async { + }, if allow_map => Ok(v), + Ok(v) = async { let uid_relay = uid.clone(); info!("{}: Trying relay discovery", options.name); let result = discovery.relay(®_result).await; @@ -274,9 +277,11 @@ impl BcCamera { }, Err(e) => Err(e), } - }, if allow_relay => v, + }, if allow_relay => Ok(v), + else => Err(Error::DiscoveryTimeout), } - }, if allow_remote || allow_map || allow_relay => v, + }, if allow_remote || allow_map || allow_relay => Ok(v), + else => Err(Error::DiscoveryTimeout), }?; return Ok(res); From 6b60ee6bccf373756dccf3fa732017756b4051cc Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 4 Aug 2023 18:59:14 +0700 Subject: [PATCH 03/12] Don't send -1 ack packets --- crates/core/src/bc_protocol/connection/udpsource.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/core/src/bc_protocol/connection/udpsource.rs b/crates/core/src/bc_protocol/connection/udpsource.rs index 1c9d030..166c44f 100644 --- a/crates/core/src/bc_protocol/connection/udpsource.rs +++ b/crates/core/src/bc_protocol/connection/udpsource.rs @@ -282,7 +282,7 @@ impl UdpPayloadSource { self.send_buffer.push_back(BcUdp::Data(resend.clone())); } } - if self.ack_interval.poll_tick(cx).is_ready() { + if self.ack_interval.poll_tick(cx).is_ready() && self.packets_want > 0 { let ack = BcUdp::Ack(self.build_send_ack()); self.send_buffer.push_back(ack); self.state = State::Flushing; From af899d3cfdaa3870cc984a26c107bb8500c9fb53 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 4 Aug 2023 19:53:02 +0700 Subject: [PATCH 04/12] Empty legacy packet --- crates/core/src/bc/model.rs | 2 ++ crates/core/src/bc/ser.rs | 4 ++++ .../src/bc_protocol/connection/udpsource.rs | 4 +++- crates/core/src/bc_protocol/login.rs | 19 ++++++++----------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 54b5cb6..d65650d 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -128,6 +128,8 @@ pub enum LegacyMsg { /// Password for a legacy login password: String, }, + /// Sent to upgrade to modern and not exposed the MD5 username/password + LoginUpgrade, /// Any other type of legacy message will be collected here UnknownMsg, } diff --git a/crates/core/src/bc/ser.rs b/crates/core/src/bc/ser.rs index 60d24d8..9860834 100644 --- a/crates/core/src/bc/ser.rs +++ b/crates/core/src/bc/ser.rs @@ -130,6 +130,10 @@ fn bc_legacy(legacy: &'_ LegacyMsg) -> impl SerializeFn + '_ { slice(&[0u8; 1772][..]), ))(out) } + LoginUpgrade => { + // Write nothing as it is header only + slice(&[])(out) + } UnknownMsg => { panic!("Cannot serialize an unknown message!"); } diff --git a/crates/core/src/bc_protocol/connection/udpsource.rs b/crates/core/src/bc_protocol/connection/udpsource.rs index 166c44f..0b56d43 100644 --- a/crates/core/src/bc_protocol/connection/udpsource.rs +++ b/crates/core/src/bc_protocol/connection/udpsource.rs @@ -282,7 +282,9 @@ impl UdpPayloadSource { self.send_buffer.push_back(BcUdp::Data(resend.clone())); } } - if self.ack_interval.poll_tick(cx).is_ready() && self.packets_want > 0 { + if self.ack_interval.poll_tick(cx).is_ready() + // && self.packets_want > 0 + { let ack = BcUdp::Ack(self.build_send_ack()); self.send_buffer.push_back(ack); self.state = State::Flushing; diff --git a/crates/core/src/bc_protocol/login.rs b/crates/core/src/bc_protocol/login.rs index 96d8e56..50dbaa3 100644 --- a/crates/core/src/bc_protocol/login.rs +++ b/crates/core/src/bc_protocol/login.rs @@ -1,4 +1,4 @@ -use super::{md5_string, BcCamera, Error, Result, Truncate, ZeroLast}; +use super::{md5_string, BcCamera, Error, Result, Truncate}; use crate::bc::{model::*, xml::*}; use std::sync::atomic::Ordering; @@ -48,12 +48,12 @@ impl BcCamera { // Note: I suspect there may be a buffer overflow opportunity in the firmware since in the // Baichuan library, these strings are capped at 32 bytes with a null terminator. This // could also be a mistake in the library, the effect being it only compares 31 chars, not 32. - let md5_username = md5_string(&credentials.username, ZeroLast); - let md5_password = credentials - .password - .as_ref() - .map(|p| md5_string(p, ZeroLast)) - .unwrap_or_else(|| EMPTY_LEGACY_PASSWORD.to_owned()); + // let md5_username = md5_string(&credentials.username, ZeroLast); + // let md5_password = credentials + // .password + // .as_ref() + // .map(|p| md5_string(p, ZeroLast)) + // .unwrap_or_else(|| EMPTY_LEGACY_PASSWORD.to_owned()); let enc_byte = match max_encryption { MaxEncryption::None => 0xdc00, @@ -69,10 +69,7 @@ impl BcCamera { response_code: enc_byte, class: 0x6514, }, - body: BcBody::LegacyMsg(LegacyMsg::LoginMsg { - username: md5_username, - password: md5_password, - }), + body: BcBody::LegacyMsg(LegacyMsg::LoginUpgrade), }; sub_login.send(legacy_login).await?; From e93dc2fe9a3159e709fabc805a18a10d1e5435fd Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 4 Aug 2023 21:09:56 +0700 Subject: [PATCH 05/12] Don't send -1 ack packets again --- crates/core/src/bc_protocol/connection/udpsource.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/core/src/bc_protocol/connection/udpsource.rs b/crates/core/src/bc_protocol/connection/udpsource.rs index 0b56d43..166c44f 100644 --- a/crates/core/src/bc_protocol/connection/udpsource.rs +++ b/crates/core/src/bc_protocol/connection/udpsource.rs @@ -282,9 +282,7 @@ impl UdpPayloadSource { self.send_buffer.push_back(BcUdp::Data(resend.clone())); } } - if self.ack_interval.poll_tick(cx).is_ready() - // && self.packets_want > 0 - { + if self.ack_interval.poll_tick(cx).is_ready() && self.packets_want > 0 { let ack = BcUdp::Ack(self.build_send_ack()); self.send_buffer.push_back(ack); self.state = State::Flushing; From c3921cbb6c1aca0e806aac1a961016268ced43b0 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 4 Aug 2023 21:46:03 +0700 Subject: [PATCH 06/12] Try with a 0x12 for the login kind --- crates/core/src/bc_protocol/login.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/core/src/bc_protocol/login.rs b/crates/core/src/bc_protocol/login.rs index 50dbaa3..c87ab72 100644 --- a/crates/core/src/bc_protocol/login.rs +++ b/crates/core/src/bc_protocol/login.rs @@ -58,7 +58,7 @@ impl BcCamera { let enc_byte = match max_encryption { MaxEncryption::None => 0xdc00, MaxEncryption::BcEncrypt => 0xdc01, - MaxEncryption::Aes => 0xdc02, + MaxEncryption::Aes => 0xdc12, }; let legacy_login = Bc { meta: BcMeta { From 816797a32bd87eac47d5f60bae17a1f640fd5da7 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 4 Aug 2023 22:27:28 +0700 Subject: [PATCH 07/12] Match both 0x02, 0x12 as the basic AES --- crates/core/src/bc/codex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/core/src/bc/codex.rs b/crates/core/src/bc/codex.rs index 9a44438..e137808 100644 --- a/crates/core/src/bc/codex.rs +++ b/crates/core/src/bc/codex.rs @@ -110,7 +110,7 @@ impl Decoder for BcCodex { match encryption_protocol_byte { 0x00 => self.context.set_encrypted(EncryptionProtocol::Unencrypted), 0x01 => self.context.set_encrypted(EncryptionProtocol::BCEncrypt), - 0x02 => self.context.set_encrypted(EncryptionProtocol::Aes( + 0x02 | 0x12 => self.context.set_encrypted(EncryptionProtocol::Aes( self.context.credentials.make_aeskey(nonce), )), _ => { From 971c1cddf8bf2d0f69266d7c62097c11be0431e7 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sat, 5 Aug 2023 10:10:22 +0700 Subject: [PATCH 08/12] Match on 0x12 inside de.rs too --- crates/core/src/bc/de.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index 4bf895b..931524a 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -159,7 +159,9 @@ fn bc_modern_msg<'a>( msg_id: 1, response_code, .. - } if (response_code & 0xff) == 0x02 => EncryptionProtocol::BCEncrypt, // This is AES but the first packet with the NONCE is BCEcrypt, since the NONCE in this packet is required to build the AES key + } if (response_code & 0xff) == 0x02 || (response_code & 0xff) == 0x12 => { + EncryptionProtocol::BCEncrypt + } // This is AES but the first packet with the NONCE is BCEcrypt, since the NONCE in this packet is required to build the AES key BcHeader { msg_id: 1, .. } if matches!(context.get_encrypted(), EncryptionProtocol::Aes(_)) => { From b90d6d7eed7a3bdec7aa9d677d07aa741de30041 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sun, 6 Aug 2023 17:31:08 +0700 Subject: [PATCH 09/12] Try AES on binary payloads --- crates/core/src/bc/codex.rs | 9 +++++-- crates/core/src/bc/de.rs | 42 +++++++++++++++++--------------- crates/core/src/bc/model.rs | 3 +++ crates/core/src/bc/xml.rs | 8 ++++++ crates/core/src/bc/xml_crypto.rs | 4 +-- 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/crates/core/src/bc/codex.rs b/crates/core/src/bc/codex.rs index e137808..8c0d3aa 100644 --- a/crates/core/src/bc/codex.rs +++ b/crates/core/src/bc/codex.rs @@ -29,7 +29,9 @@ impl Encoder for BcCodex { // let context = self.context.read().unwrap(); let buf: Vec = Default::default(); let enc_protocol: EncryptionProtocol = match self.context.get_encrypted() { - EncryptionProtocol::Aes(_) if item.meta.msg_id == 1 => { + EncryptionProtocol::Aes(_) | EncryptionProtocol::FullAes(_) + if item.meta.msg_id == 1 => + { // During login the encyption protocol cannot go higher than BCEncrypt // even if we support AES. (BUt it can go lower i.e. None) EncryptionProtocol::BCEncrypt @@ -110,7 +112,10 @@ impl Decoder for BcCodex { match encryption_protocol_byte { 0x00 => self.context.set_encrypted(EncryptionProtocol::Unencrypted), 0x01 => self.context.set_encrypted(EncryptionProtocol::BCEncrypt), - 0x02 | 0x12 => self.context.set_encrypted(EncryptionProtocol::Aes( + 0x02 => self.context.set_encrypted(EncryptionProtocol::Aes( + self.context.credentials.make_aeskey(nonce), + )), + 0x12 => self.context.set_encrypted(EncryptionProtocol::FullAes( self.context.credentials.make_aeskey(nonce), )), _ => { diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index 931524a..acb67bd 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -149,24 +149,23 @@ fn bc_modern_msg<'a>( msg_id: 1, response_code, .. - } if (response_code & 0xff) == 0x00 => EncryptionProtocol::Unencrypted, - BcHeader { - msg_id: 1, - response_code, - .. - } if (response_code & 0xff) == 0x01 => EncryptionProtocol::BCEncrypt, - BcHeader { - msg_id: 1, - response_code, - .. - } if (response_code & 0xff) == 0x02 || (response_code & 0xff) == 0x12 => { - EncryptionProtocol::BCEncrypt - } // This is AES but the first packet with the NONCE is BCEcrypt, since the NONCE in this packet is required to build the AES key - BcHeader { msg_id: 1, .. } - if matches!(context.get_encrypted(), EncryptionProtocol::Aes(_)) => - { - // Maximum encryption level is BCEncrypt during login... Not sure why Reolink did it like that - EncryptionProtocol::BCEncrypt + } if (response_code >> 8) & 0xff == 0xdd => { + // 0xdd means we are setting the encryption method + // Durig login, the max encryption is BcEncrypt since + // the nonce has not been exchanged yet + match response_code & 0xff { + 0x00 => EncryptionProtocol::Unencrypted, + _ => EncryptionProtocol::BCEncrypt, + } + } + BcHeader { msg_id: 1, .. } => { + match *context.get_encrypted() { + EncryptionProtocol::Aes(_) | EncryptionProtocol::FullAes(_) => { + // During login max is BcEncrypt + EncryptionProtocol::BCEncrypt + } + n => n, + } } _ => *context.get_encrypted(), }; @@ -174,7 +173,12 @@ fn bc_modern_msg<'a>( let processed_payload_buf = xml_crypto::decrypt(header.channel_id as u32, payload_buf, &encryption_protocol); if context.in_bin_mode.contains(&(header.msg_num)) || in_binary { - payload = Some(BcPayloads::Binary(payload_buf.to_vec())); + payload = match context.get_encrypted() { + EncryptionProtocol::FullAes(_) => { + Some(BcPayloads::Binary(processed_payload_buf.to_vec())) + } + _ => Some(BcPayloads::Binary(payload_buf.to_vec())), + }; } else { let xml = BcXml::try_parse(processed_payload_buf.as_slice()).map_err(|_| { error!("header.msg_id: {}", header.msg_id); diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index d65650d..50e9483 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -200,6 +200,9 @@ pub enum EncryptionProtocol { /// Latest cameras/firmwares use Aes with the key derived from /// the camera's password and the negotiated NONCE Aes([u8; 16]), + /// Same as Aes but the media stream is also encrypted and not just + /// the control commands + FullAes([u8; 16]), } #[derive(Debug)] diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index 7c15cb2..0772660 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -259,6 +259,12 @@ pub struct Extension { /// The rfID used in the PIR #[yaserde(rename = "rfId")] pub rf_id: Option, + /// Encrypted binary has this to verify successful decryption + #[yaserde(rename = "checkPos")] + pub check_pos: Option, + /// Encrypted binary has this to verify successful decryption + #[yaserde(rename = "checkValue")] + pub check_value: Option, } impl Default for Extension { @@ -270,6 +276,8 @@ impl Default for Extension { token: None, channel_id: None, rf_id: None, + check_pos: None, + check_value: None, } } } diff --git a/crates/core/src/bc/xml_crypto.rs b/crates/core/src/bc/xml_crypto.rs index 0c3063b..bf0f086 100644 --- a/crates/core/src/bc/xml_crypto.rs +++ b/crates/core/src/bc/xml_crypto.rs @@ -21,7 +21,7 @@ pub fn decrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol .map(|(key, i)| *i ^ key ^ (offset as u8)) .collect() } - EncryptionProtocol::Aes(aeskey) => { + EncryptionProtocol::Aes(aeskey) | EncryptionProtocol::FullAes(aeskey) => { // AES decryption let mut decrypted = buf.to_vec(); @@ -41,7 +41,7 @@ pub fn encrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol // Encrypt is the same as decrypt decrypt(offset, buf, encryption_protocol) } - EncryptionProtocol::Aes(aeskey) => { + EncryptionProtocol::Aes(aeskey) | EncryptionProtocol::FullAes(aeskey) => { // AES encryption let mut encrypted = buf.to_vec(); Aes128CfbEnc::new(aeskey.into(), IV.into()).encrypt(&mut encrypted); From 5109dbba9e5d9919668f901896f34f985a5f7aad Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sun, 6 Aug 2023 19:05:04 +0700 Subject: [PATCH 10/12] Only decrypt bcmecia packets where encrypt len is set --- crates/core/src/bc/de.rs | 21 ++++++++++++++++++--- crates/core/src/bc/xml.rs | 4 ++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index acb67bd..6014485 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -113,8 +113,13 @@ fn bc_modern_msg<'a>( }; let mut in_binary = false; + let mut encrypted_len = None; // Now we'll take the buffer that Nom gave a ref to and parse it. let extension = if ext_len > 0 { + log::trace!( + "Extension Txt: {:?}", + String::from_utf8(processed_ext_buf.to_vec()).unwrap_or("Not Text".to_string()) + ); // Apply the XML parse function, but throw away the reference to decrypted in the Ok and // Err case. This error-error-error thing is the same idiom Nom uses internally. let parsed = Extension::try_parse(processed_ext_buf).map_err(|_| { @@ -126,11 +131,13 @@ fn bc_modern_msg<'a>( })?; if let Extension { binary_data: Some(1), + encrypt_len, .. } = parsed { // In binary so tell the current context that we need to treat the payload as binary in_binary = true; + encrypted_len = encrypt_len; } Some(parsed) } else { @@ -173,13 +180,21 @@ fn bc_modern_msg<'a>( let processed_payload_buf = xml_crypto::decrypt(header.channel_id as u32, payload_buf, &encryption_protocol); if context.in_bin_mode.contains(&(header.msg_num)) || in_binary { - payload = match context.get_encrypted() { - EncryptionProtocol::FullAes(_) => { - Some(BcPayloads::Binary(processed_payload_buf.to_vec())) + payload = match (context.get_encrypted(), encrypted_len) { + (EncryptionProtocol::FullAes(_), Some(encrypted_len)) => { + log::trace!("Binary: {:X?}", &processed_payload_buf[0..30]); + + Some(BcPayloads::Binary( + processed_payload_buf[0..(encrypted_len as usize)].to_vec(), + )) } _ => Some(BcPayloads::Binary(payload_buf.to_vec())), }; } else { + log::trace!( + "Payload Txt: {:?}", + String::from_utf8(processed_payload_buf.to_vec()).unwrap_or("Not Text".to_string()) + ); let xml = BcXml::try_parse(processed_payload_buf.as_slice()).map_err(|_| { error!("header.msg_id: {}", header.msg_id); error!( diff --git a/crates/core/src/bc/xml.rs b/crates/core/src/bc/xml.rs index 0772660..47cd275 100644 --- a/crates/core/src/bc/xml.rs +++ b/crates/core/src/bc/xml.rs @@ -265,6 +265,9 @@ pub struct Extension { /// Encrypted binary has this to verify successful decryption #[yaserde(rename = "checkValue")] pub check_value: Option, + /// Used in newer encrypted payload packets + #[yaserde(rename = "encryptLen")] + pub encrypt_len: Option, } impl Default for Extension { @@ -278,6 +281,7 @@ impl Default for Extension { rf_id: None, check_pos: None, check_value: None, + encrypt_len: None, } } } From c198f21c92d6294365e0ab9d5af42c371e32748d Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 7 Aug 2023 09:01:13 +0700 Subject: [PATCH 11/12] Add a latency field to the ack packet --- .../src/bc_protocol/connection/udpsource.rs | 61 ++++++++++++++++++- crates/core/src/bcudp/de.rs | 3 +- crates/core/src/bcudp/model.rs | 8 ++- crates/core/src/bcudp/ser.rs | 2 +- 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/crates/core/src/bc_protocol/connection/udpsource.rs b/crates/core/src/bc_protocol/connection/udpsource.rs index 166c44f..d87ecb7 100644 --- a/crates/core/src/bc_protocol/connection/udpsource.rs +++ b/crates/core/src/bc_protocol/connection/udpsource.rs @@ -20,7 +20,7 @@ use std::sync::Arc; use std::task::{Context, Poll}; use tokio::{ net::UdpSocket, - time::{interval, Duration, Interval}, + time::{interval, Duration, Instant, Interval}, }; use tokio_util::compat::{Compat, FuturesAsyncReadCompatExt}; use tokio_util::{ @@ -164,6 +164,7 @@ impl BcUdpSource { send_buffer: Default::default(), ack_interval: interval(Duration::from_millis(10)), // Offical Client does ack every 10ms resend_interval: interval(Duration::from_millis(500)), // Offical Client does resend every 500ms + ack_latency: Default::default(), } } } @@ -205,6 +206,57 @@ enum State { YieldNow, // Used to ensure we rest between polling packets so as to not starve the runtime } +#[derive(Default)] +struct AckLatency { + current_values: Vec, + last_recieve_time: Option, + display_value: u32, + last_display_time: Option, +} + +impl AckLatency { + /// Used to get the current latency, in thd way that the official + /// client does. This is a value that seems to be updated only every second + /// Observed values are `0`, `54785`, `55062`, `2528`, + fn get_value(&self) -> u32 { + self.display_value + } + + /// Used to updaet the average latency calculation + fn feed_ack(&mut self) { + // Update the last recieve time + let now = Instant::now(); + if let Some(last_recieve_time) = self.last_recieve_time { + let diff = (now - last_recieve_time).as_micros(); + self.current_values.push(diff as u32); + self.last_recieve_time = Some(now); + } else { + self.last_recieve_time = Some(now); + } + + // Update the display_value + // this is done only ever 1s + if let Some(last_display_time) = self.last_display_time { + if now - last_display_time > Duration::from_secs(1) { + // A second has passed update this + self.last_display_time = Some(now); + let current_values_count = self.current_values.len() as u32; + let current_value = self + .current_values + .iter() + .fold(0u32, |acc, value| acc + *value / current_values_count); + self.current_values = vec![]; // Reset the average vec + + self.display_value = current_value; + } + } else { + // First 1s is a zero value + self.last_display_time = Some(now); + self.display_value = 0; + } + } +} + pub(crate) struct UdpPayloadSource { inner: BcUdpSource, client_id: i32, @@ -222,6 +274,7 @@ pub(crate) struct UdpPayloadSource { /// Offical Client does resend every 500ms /// This `resend_interval` controls how ofen we do this resend_interval: Interval, + ack_latency: AckLatency, } impl UdpPayloadSource { @@ -253,6 +306,7 @@ impl UdpPayloadSource { connection_id: self.camera_id, packet_id: first_missing - 1, // Last we actually have is first_missing - 1 group_id: 0, + maybe_latency: self.ack_latency.get_value(), payload: missing_ids, } } else { @@ -273,6 +327,7 @@ impl UdpPayloadSource { } } } + self.ack_latency.feed_ack(); } fn maintanence(&mut self, cx: &mut Context<'_>) { @@ -282,7 +337,9 @@ impl UdpPayloadSource { self.send_buffer.push_back(BcUdp::Data(resend.clone())); } } - if self.ack_interval.poll_tick(cx).is_ready() && self.packets_want > 0 { + if self.ack_interval.poll_tick(cx).is_ready() + // && self.packets_want > 0 + { let ack = BcUdp::Ack(self.build_send_ack()); self.send_buffer.push_back(ack); self.state = State::Flushing; diff --git a/crates/core/src/bcudp/de.rs b/crates/core/src/bcudp/de.rs index 3bcd9a7..f01f71d 100644 --- a/crates/core/src/bcudp/de.rs +++ b/crates/core/src/bcudp/de.rs @@ -96,7 +96,7 @@ fn udp_ack(buf: &[u8]) -> IResult<&[u8], UdpAck> { )(buf)?; let (buf, packet_id) = error_context("Missing packet_id", le_u32)(buf)?; // This is the point at which the camera has contigious // packets to - let (buf, _unknown) = error_context("ACK: Missing unknown", le_u32)(buf)?; + let (buf, maybe_latency) = error_context("ACK: Missing Maybe Latency", le_u32)(buf)?; let (buf, payload_size) = error_context("ACK: Missing payload_size", le_u32)(buf)?; let (buf, payload) = if payload_size > 0 { let (buf, t_payload) = take(payload_size)(buf)?; // It is a binary payload of @@ -113,6 +113,7 @@ fn udp_ack(buf: &[u8]) -> IResult<&[u8], UdpAck> { connection_id, packet_id, group_id, + maybe_latency, payload, }; Ok((buf, data)) diff --git a/crates/core/src/bcudp/model.rs b/crates/core/src/bcudp/model.rs index 53f03a2..f66f837 100644 --- a/crates/core/src/bcudp/model.rs +++ b/crates/core/src/bcudp/model.rs @@ -68,8 +68,11 @@ pub struct UdpAck { pub group_id: u32, /// The ID of the last data packet [`UdpData`] pub packet_id: u32, - // 2 Bytes Unknown: Observed values `00000000`, `d6010000`, `d7160000` `09e00000` - // Unknown but seems to change randomly every second + /// 4 Bytes Unknown: Observed values `00000000`, `d6010000`, `d7160000`, `09e00000`, + /// `0`, `54785`, `55062`, `2528`, + /// Unknown but seems to change randomly every second + /// Might be a latency for the recieved acknoledge packets + pub maybe_latency: u32, // 2 Bytes size of a payload // /// Payload of `00 01 01 01 01` where `01` is added after every repeat @@ -86,6 +89,7 @@ impl UdpAck { connection_id, group_id: 0xffffffff, packet_id: 0xffffffff, + maybe_latency: 0x0, payload: vec![], } } diff --git a/crates/core/src/bcudp/ser.rs b/crates/core/src/bcudp/ser.rs index b566d8a..b911a6c 100644 --- a/crates/core/src/bcudp/ser.rs +++ b/crates/core/src/bcudp/ser.rs @@ -52,7 +52,7 @@ fn bcudp_ack<'a, W: 'a + Write>( le_u32(0), le_u32(payload.group_id), le_u32(payload.packet_id), - le_u32(0), + le_u32(payload.maybe_latency), le_u32(binary_payload.len() as u32), slice(binary_payload), )) From a45bcf876157bf350db97efca15073fb9570abc2 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 7 Aug 2023 10:52:16 +0700 Subject: [PATCH 12/12] Added enable config option --- crates/core/src/bc_protocol.rs | 8 +- .../src/bc_protocol/connection/discovery.rs | 146 +++++++++++------- src/config.rs | 3 + src/mqtt/mod.rs | 1 + src/rtsp/mod.rs | 2 +- src/utils.rs | 1 + 6 files changed, 103 insertions(+), 58 deletions(-) diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 713eaf0..db6c118 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -213,13 +213,19 @@ impl BcCamera { // // We loop infinitly and allow the caller to timeout at the // interval they desire + let mut retry = 0; + const MAX_RETRY: usize = 10; loop { tokio::task::yield_now().await; if let Ok(result) = discovery.get_registration(uid).await { reg_result = result; break; } - log::debug!("Registration failed. Retrying"); + if retry >= MAX_RETRY { + return Err(Error::DiscoveryTimeout); + } + log::info!("{}: Registration with reolink servers failed. Retrying: {}/{}", options.name, retry + 1, MAX_RETRY); + retry += 1; tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; // New discovery to get new client IDs discovery = Discovery::new().await?; diff --git a/crates/core/src/bc_protocol/connection/discovery.rs b/crates/core/src/bc_protocol/connection/discovery.rs index 0709724..8e6d220 100644 --- a/crates/core/src/bc_protocol/connection/discovery.rs +++ b/crates/core/src/bc_protocol/connection/discovery.rs @@ -442,69 +442,103 @@ impl Discoverer { // Send and await acceptance let (sid, dev, dmap, relay) = self - .retry_send(msg, lookup.reg, |bc, _| match bc { - UdpDiscovery { - tid: _, - payload: - UdpXml { - r2c_c_r: - Some(R2cCr { - dmap, - dev, - relay, - sid, - rsp, - .. - }), - .. - }, - } if (dev - .as_ref() - .map(|d| !d.ip.is_empty() && d.port > 0) - .unwrap_or(false) - || dmap + .retry_send(msg, lookup.reg, |bc, socket| { + trace!("{}", socket.ip()); + match bc { + UdpDiscovery { + tid: _, + payload: + UdpXml { + r2c_c_r: + Some(R2cCr { + dmap, + dev, + relay, + sid, + rsp, + .. + }), + .. + }, + } if (dev .as_ref() .map(|d| !d.ip.is_empty() && d.port > 0) .unwrap_or(false) - || relay - .as_ref() - .map(|d| !d.ip.is_empty() && d.port > 0) - .unwrap_or(false)) - && rsp != -1 => - { - Some(Ok((sid, dev, dmap, relay))) - } - UdpDiscovery { - tid: _, - payload: - UdpXml { - r2c_c_r: - Some(R2cCr { - dev, - dmap, - relay, - rsp, - .. - }), - .. - }, - } if (dev - .as_ref() - .map(|d| !d.ip.is_empty() && d.port > 0) - .unwrap_or(false) - || dmap + || dmap + .as_ref() + .map(|d| !d.ip.is_empty() && d.port > 0) + .unwrap_or(false) + || relay + .as_ref() + .map(|d| !d.ip.is_empty() && d.port > 0) + .unwrap_or(false)) + && rsp != -1 => + { + Some(Ok((sid, dev, dmap, relay))) + } + // UdpDiscovery { + // tid: _, + // payload: + // UdpXml { + // r2c_c_r: + // Some(R2cCr { + // dmap, + // dev, + // relay: Some(mut relay), + // sid, + // rsp, + // .. + // }), + // .. + // }, + // } if (dev + // .as_ref() + // .map(|d| !d.ip.is_empty() && d.port > 0) + // .unwrap_or(false) + // || dmap + // .as_ref() + // .map(|d| !d.ip.is_empty() && d.port > 0) + // .unwrap_or(false) + // || (relay.ip == format!("{}", socket.ip()) && relay.port == 0)) + // && rsp != -1 => + // { + // // For a relay connection if port is 0 and the ip is the current socker addr + // // we use the current port + // relay.port = socket.port(); + // Some(Ok((sid, dev, dmap, Some(relay)))) + // } + UdpDiscovery { + tid: _, + payload: + UdpXml { + r2c_c_r: + Some(R2cCr { + dev, + dmap, + relay, + rsp, + .. + }), + .. + }, + } if (dev .as_ref() .map(|d| !d.ip.is_empty() && d.port > 0) .unwrap_or(false) - || relay - .as_ref() - .map(|d| !d.ip.is_empty() && d.port > 0) - .unwrap_or(false)) - && rsp == -1 => - { - Some(Err(Error::RegisterError)) + || dmap + .as_ref() + .map(|d| !d.ip.is_empty() && d.port > 0) + .unwrap_or(false) + || relay + .as_ref() + .map(|d| !d.ip.is_empty() && d.port > 0) + .unwrap_or(false)) + && rsp == -1 => + { + Some(Err(Error::RegisterError)) + } + _ => None, } - _ => None, }) .await??; diff --git a/src/config.rs b/src/config.rs index 8393245..aa8d075 100644 --- a/src/config.rs +++ b/src/config.rs @@ -123,6 +123,9 @@ pub(crate) struct CameraConfig { #[serde(default = "default_true", alias = "splash")] pub(crate) use_splash: bool, + + #[serde(default = "default_true", alias = "enable")] + pub(crate) enabled: bool, } #[derive(Debug, Deserialize, Validate, Clone)] diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index e61c71f..681e1c7 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -95,6 +95,7 @@ pub(crate) async fn main(_: Opt, config: Config) -> Result<()> { for camera_config in config .cameras .iter() + .filter(|c| c.enabled) .map(|a| Arc::new(a.clone())) .collect::>() { diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 3e46350..be9caee 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -88,7 +88,7 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> { ) } let mut cameras = vec![]; - for camera_config in config.cameras.drain(..) { + for camera_config in config.cameras.drain(..).filter(|c| c.enabled) { cameras .push(Camera::::new(camera_config, &config.users, rtsp.clone()).await?); } diff --git a/src/utils.rs b/src/utils.rs index 91a15f2..ffbabaa 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -153,6 +153,7 @@ pub(crate) fn find_camera_by_name<'a>(config: &'a Config, name: &str) -> Result< config .cameras .iter() + .filter(|c| c.enabled) .find(|c| c.name == name) .ok_or_else(|| anyhow!("Camera {} not found in the config file", name)) }