From c8be843ebcb07ac32c4af29ef63f436ac8771b55 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sat, 18 Feb 2023 09:12:22 +0700 Subject: [PATCH] More changes to run in tokio --- Cargo.lock | 37 +- Cargo.toml | 1 + crates/core/src/bc/codex.rs | 61 +- crates/core/src/bc/de.rs | 2 +- crates/core/src/bc/model.rs | 9 +- crates/core/src/bc/ser.rs | 3 +- crates/core/src/bc/xml_crypto.rs | 29 +- crates/core/src/bc_protocol.rs | 123 +- .../core/src/bc_protocol/connection/.DS_Store | Bin 0 -> 6148 bytes .../core/src/bc_protocol/connection/bcconn.rs | 48 +- .../src/bc_protocol/connection/discovery.rs | 1104 +++++++++++------ crates/core/src/bc_protocol/connection/mod.rs | 4 +- .../core/src/bc_protocol/connection/source.rs | 23 +- .../src/bc_protocol/connection/tcpsource.rs | 32 +- .../connection/udpconn/aborthandle.rs | 25 - .../connection/udpconn/discover.rs | 570 --------- .../src/bc_protocol/connection/udpconn/mod.rs | 326 ----- .../connection/udpconn/transmit.rs | 346 ------ .../src/bc_protocol/connection/udpsource.rs | 57 +- crates/core/src/bc_protocol/errors.rs | 24 + crates/core/src/bc_protocol/login.rs | 28 +- crates/core/src/bc_protocol/logout.rs | 56 +- crates/core/src/bc_protocol/stream.rs | 51 +- crates/core/src/bcudp/xml.rs | 21 + crates/core/src/lib.rs | 50 + src/mqtt/event_cam.rs | 8 +- src/rtsp/gst.rs | 2 + src/rtsp/mod.rs | 8 +- src/rtsp/states/loggedin.rs | 5 +- src/rtsp/states/mod.rs | 32 +- src/rtsp/states/paused.rs | 45 +- src/rtsp/states/shared.rs | 2 +- src/rtsp/states/streaming.rs | 40 +- src/utils.rs | 23 +- 34 files changed, 1193 insertions(+), 2002 deletions(-) create mode 100644 crates/core/src/bc_protocol/connection/.DS_Store delete mode 100644 crates/core/src/bc_protocol/connection/udpconn/aborthandle.rs delete mode 100644 crates/core/src/bc_protocol/connection/udpconn/discover.rs delete mode 100644 crates/core/src/bc_protocol/connection/udpconn/mod.rs delete mode 100644 crates/core/src/bc_protocol/connection/udpconn/transmit.rs diff --git a/Cargo.lock b/Cargo.lock index bec672b..2e92209 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -429,9 +429,9 @@ checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7" [[package]] name = "futures" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38390104763dc37a5145a53c29c63c1290b5d316d6086ec32c293f6736051bb0" +checksum = "13e2792b0ff0340399d58445b88fd9770e3489eff258a4cbc1523418f12abf84" dependencies = [ "futures-channel", "futures-core", @@ -444,9 +444,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52ba265a92256105f45b719605a571ffe2d1f0fea3807304b522c1d778f79eed" +checksum = "2e5317663a9089767a1ec00a487df42e0ca174b61b4483213ac24448e4664df5" dependencies = [ "futures-core", "futures-sink", @@ -454,15 +454,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04909a7a7e4633ae6c4a9ab280aeb86da1236243a77b694a49eacd659a4bd3ac" +checksum = "ec90ff4d0fe1f57d600049061dc6bb68ed03c7d2fbd697274c41805dcb3f8608" [[package]] name = "futures-executor" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7acc85df6714c176ab5edf386123fafe217be88c0840ec11f199441134a074e2" +checksum = "e8de0a35a6ab97ec8869e32a2473f4b1324459e14c29275d14b10cb1fd19b50e" dependencies = [ "futures-core", "futures-task", @@ -471,15 +471,15 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00f5fb52a06bdcadeb54e8d3671f8888a39697dcb0b81b23b55174030427f4eb" +checksum = "bfb8371b6fb2aeb2d280374607aeabfc99d95c72edfe51692e42d3d7f0d08531" [[package]] name = "futures-macro" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdfb8ce053d86b91919aad980c220b1fb8401a9394410e1c289ed7e66b61835d" +checksum = "95a73af87da33b5acf53acfebdc339fe592ecf5357ac7c0a7734ab9d8c876a70" dependencies = [ "proc-macro2", "quote", @@ -488,21 +488,21 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39c15cf1a4aa79df40f1bb462fb39676d0ad9e366c2a33b590d7c66f4f81fcf9" +checksum = "f310820bb3e8cfd46c80db4d7fb8353e15dfff853a127158425f31e0be6c8364" [[package]] name = "futures-task" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ffb393ac5d9a6eaa9d3fdf37ae2776656b706e200c8e16b1bdb227f5198e6ea" +checksum = "dcf79a1bf610b10f42aea489289c5a2c478a786509693b80cd39c44ccd936366" [[package]] name = "futures-util" -version = "0.3.25" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "197676987abd2f9cadff84926f410af1c183608d36641465df73ae8211dc65d6" +checksum = "9c1d6de3acfef38d2be4b1f543f553131788603495be83da675e180c8d6b7bd1" dependencies = [ "futures-channel", "futures-core", @@ -1132,6 +1132,7 @@ dependencies = [ "crossbeam-channel", "env_logger", "err-derive", + "futures", "gstreamer", "gstreamer-app", "gstreamer-rtsp", diff --git a/Cargo.toml b/Cargo.toml index 7bbebd1..12db80d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,3 +44,4 @@ rumqttc = "0.8.0" tokio = { version = "1.0", features = ["rt", "macros", "io-util"] } anyhow = "1.0.42" async-trait = "0.1.64" +futures = "0.3.26" diff --git a/crates/core/src/bc/codex.rs b/crates/core/src/bc/codex.rs index 462fc96..a83a224 100644 --- a/crates/core/src/bc/codex.rs +++ b/crates/core/src/bc/codex.rs @@ -4,8 +4,10 @@ //! BcCodex is used with a `[tokio_util::codec::Framed]` to form complete packets //! use crate::bc::model::*; -use crate::{Error, Result}; +use crate::bc::xml::*; +use crate::{Credentials, Error, Result}; use bytes::BytesMut; +use log::*; use tokio_util::codec::{Decoder, Encoder}; pub(crate) struct BcCodex { @@ -13,17 +15,11 @@ pub(crate) struct BcCodex { } impl BcCodex { - pub(crate) fn new() -> Self { + pub(crate) fn new(credentials: Credentials) -> Self { Self { - context: BcContext::new(), + context: BcContext::new(credentials), } } - pub(crate) fn get_encrypted(&self) -> &EncryptionProtocol { - self.context.get_encrypted() - } - pub(crate) fn set_encrypted(&mut self, protocol: EncryptionProtocol) { - self.context.set_encrypted(protocol); - } } impl Encoder for BcCodex { @@ -31,7 +27,6 @@ impl Encoder for BcCodex { fn encode(&mut self, item: Bc, dst: &mut BytesMut) -> Result<()> { // let context = self.context.read().unwrap(); - let buf: Vec = Default::default(); let buf = item.serialize(buf, self.context.get_encrypted())?; dst.reserve(buf.len()); @@ -44,22 +39,46 @@ impl Decoder for BcCodex { type Item = Bc; type Error = Error; fn decode(&mut self, src: &mut BytesMut) -> Result> { - let bc = match { Bc::deserialize(&self.context, src) } { + trace!("Decoding: {:X?}", src); + let bc = Bc::deserialize(&self.context, src); + trace!("As: {:?}", bc); + let bc = match bc { Ok(bc) => bc, Err(Error::NomIncomplete(_)) => return Ok(None), Err(e) => return Err(e), }; // Update context - if bc.meta.msg_id == 1 && (bc.meta.response_code >> 8) == 0xdd { - // Login reply has the encryption info - // Set that the encryption type now - let encryption_protocol_byte = (bc.meta.response_code & 0xff) as usize; - 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(None)), - _ => { - return Err(Error::UnknownEncryption(encryption_protocol_byte)); + if let Bc { + meta: + BcMeta { + msg_id: 1, + response_code, + .. + }, + body: + BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + encryption: Some(Encryption { nonce, .. }), + .. + })), + .. + }), + } = &bc + { + if response_code >> 8 == 0xdd { + // Login reply has the encryption info + // Set that the encryption type now + let encryption_protocol_byte = (response_code & 0xff) as usize; + 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( + self.context.credentials.make_aeskey(nonce), + )), + _ => { + return Err(Error::UnknownEncryption(encryption_protocol_byte)); + } } } } diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index cdbed67..cc23e5f 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -149,7 +149,7 @@ fn bc_modern_msg<'a>( msg_id: 1, response_code, .. - } if (response_code & 0xff) == 0x02 => EncryptionProtocol::Aes(None), + } 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 _ => context.get_encrypted().clone(), }; let processed_payload_buf = diff --git a/crates/core/src/bc/model.rs b/crates/core/src/bc/model.rs index 81c3a98..2ade671 100644 --- a/crates/core/src/bc/model.rs +++ b/crates/core/src/bc/model.rs @@ -1,3 +1,5 @@ +use crate::Credentials; + pub use super::xml::{BcPayloads, BcXml, Extension}; use std::collections::HashSet; @@ -175,11 +177,12 @@ pub enum EncryptionProtocol { BCEncrypt, /// Latest cameras/firmwares use Aes with the key derived from /// the camera's password and the negotiated NONCE - Aes(Option<[u8; 16]>), + Aes([u8; 16]), } #[derive(Debug)] pub(crate) struct BcContext { + pub(crate) credentials: Credentials, pub(crate) in_bin_mode: HashSet, pub(crate) encryption_protocol: EncryptionProtocol, } @@ -218,8 +221,9 @@ impl Bc { } impl BcContext { - pub(crate) fn new() -> BcContext { + pub(crate) fn new(credentials: Credentials) -> BcContext { BcContext { + credentials, in_bin_mode: HashSet::new(), encryption_protocol: EncryptionProtocol::Unencrypted, } @@ -228,6 +232,7 @@ impl BcContext { #[allow(unused)] // Used in tests pub(crate) fn new_with_encryption(encryption_protocol: EncryptionProtocol) -> BcContext { BcContext { + credentials: Default::default(), in_bin_mode: HashSet::new(), encryption_protocol, } diff --git a/crates/core/src/bc/ser.rs b/crates/core/src/bc/ser.rs index 22984c7..60d24d8 100644 --- a/crates/core/src/bc/ser.rs +++ b/crates/core/src/bc/ser.rs @@ -191,8 +191,7 @@ fn test_legacy_login_roundtrip() { #[test] fn test_modern_login_roundtrip() { - let mut context = BcContext::new(); - context.set_encrypted(EncryptionProtocol::BCEncrypt); + let context = BcContext::new_with_encryption(EncryptionProtocol::BCEncrypt); // I don't want to make up a sample message; just load it let sample = include_bytes!("samples/model_sample_modern_login.bin"); diff --git a/crates/core/src/bc/xml_crypto.rs b/crates/core/src/bc/xml_crypto.rs index e784a59..bd0a499 100644 --- a/crates/core/src/bc/xml_crypto.rs +++ b/crates/core/src/bc/xml_crypto.rs @@ -16,17 +16,12 @@ pub fn decrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol .map(|(key, i)| *i ^ key ^ (offset as u8)) .collect() } - EncryptionProtocol::Aes(key) => { + EncryptionProtocol::Aes(aeskey) => { // AES decryption - if let Some(aeskey) = key { - let mut decrypted = buf.to_vec(); - Cfb::::new(aeskey.into(), IV.into()).decrypt(&mut decrypted); - decrypted - } else { - // Not yet ready to decrypt (still in login phase) - // Use BCEncrypt - decrypt(offset, buf, &EncryptionProtocol::BCEncrypt) - } + + let mut decrypted = buf.to_vec(); + Cfb::::new(aeskey.into(), IV.into()).decrypt(&mut decrypted); + decrypted } } } @@ -41,17 +36,11 @@ pub fn encrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol // Encrypt is the same as decrypt decrypt(offset, buf, encryption_protocol) } - EncryptionProtocol::Aes(key) => { + EncryptionProtocol::Aes(aeskey) => { // AES encryption - if let Some(aeskey) = key { - let mut encrypted = buf.to_vec(); - Cfb::::new(aeskey.into(), IV.into()).encrypt(&mut encrypted); - encrypted - } else { - // Not yet ready to decrypt (still in login phase) - // Use BCEncrypt - encrypt(offset, buf, &EncryptionProtocol::BCEncrypt) - } + let mut encrypted = buf.to_vec(); + Cfb::::new(aeskey.into(), IV.into()).encrypt(&mut encrypted); + encrypted } } } diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 888acc2..8f5afd5 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -1,11 +1,8 @@ -use crate::bc; +use crate::{bc, Credentials}; +use futures::stream::StreamExt; use log::*; -use std::convert::TryInto; use std::net::ToSocketAddrs; -use std::sync::{ - atomic::{AtomicBool, AtomicU16, Ordering}, - Mutex, -}; +use std::sync::atomic::{AtomicBool, AtomicU16, Ordering}; use Md5Trunc::*; @@ -33,7 +30,7 @@ pub use pirstate::PirState; pub use ptz::Direction; pub use resolution::*; use std::sync::Arc; -pub use stream::{Stream, StreamData}; +pub use stream::{StreamData, StreamKind}; pub(crate) type Result = std::result::Result; @@ -55,20 +52,7 @@ pub struct BcCamera { logged_in: AtomicBool, message_num: AtomicU16, // Certain commands such as logout require the username/pass in plain text.... why....??? - credentials: Mutex>, -} - -// Used for caching the credentials -#[derive(Clone)] -struct Credentials { - username: String, - password: Option, -} - -impl Credentials { - fn new(username: String, password: Option) -> Self { - Self { username, password } - } + credentials: Credentials, } impl Drop for BcCamera { @@ -92,13 +76,27 @@ impl BcCamera { /// /// returns either an error or the camera /// - pub async fn new_with_addr(host: T, channel_id: u8) -> Result { + pub async fn new_with_addr, W: Into>( + host: U, + channel_id: u8, + username: V, + passwd: Option, + ) -> Result { + let username: String = username.into(); + let passwd: Option = passwd.map(|t| t.into()); let addr_iter = match host.to_socket_addrs() { Ok(iter) => iter, Err(_) => return Err(Error::AddrResolutionError), }; for addr in addr_iter { - if let Ok(cam) = Self::new(SocketAddrOrUid::SocketAddr(addr), channel_id).await { + if let Ok(cam) = Self::new( + SocketAddrOrUid::SocketAddr(addr), + channel_id, + &username, + passwd.as_ref(), + ) + .await + { return Ok(cam); } } @@ -119,8 +117,19 @@ impl BcCamera { /// /// returns either an error or the camera /// - pub async fn new_with_uid(uid: &str, channel_id: u8) -> Result { - Self::new(SocketAddrOrUid::Uid(uid.to_string()), channel_id).await + pub async fn new_with_uid, V: Into>( + uid: &str, + channel_id: u8, + username: U, + passwd: Option, + ) -> Result { + Self::new( + SocketAddrOrUid::Uid(uid.to_string()), + channel_id, + username, + passwd, + ) + .await } /// @@ -143,16 +152,20 @@ impl BcCamera { /// /// returns either an error or the camera /// - pub async fn new_with_addr_or_uid( - host: T, + pub async fn new_with_addr_or_uid, W: Into>( + host: U, channel_id: u8, + username: V, + passwd: Option, ) -> Result { let addr_iter = match host.to_socket_addrs_or_uid() { Ok(iter) => iter, Err(_) => return Err(Error::AddrResolutionError), }; + let username: String = username.into(); + let passwd: Option = passwd.map(|t| t.into()); for addr_or_uid in addr_iter { - if let Ok(cam) = Self::new(addr_or_uid, channel_id).await { + if let Ok(cam) = Self::new(addr_or_uid, channel_id, &username, passwd.as_ref()).await { return Ok(cam); } } @@ -169,15 +182,30 @@ impl BcCamera { /// /// * `channel_id` - The channel ID this is usually zero unless using a NVR /// + /// * `username` - The username to login with + /// + /// * `passed` - The password to login with required for AES encrypted camera + /// /// # Returns /// /// returns either an error or the camera /// - pub async fn new(addr: SocketAddrOrUid, channel_id: u8) -> Result { - let source: Box = match addr { + pub async fn new, V: Into>( + addr: SocketAddrOrUid, + channel_id: u8, + username: U, + passwd: Option, + ) -> Result { + let username: String = username.into(); + let passwd: Option = passwd.map(|t| t.into()); + + let (sink, source): (BcConnSink, BcConnSource) = match addr { SocketAddrOrUid::SocketAddr(addr) => { debug!("Trying address {}", addr); - Box::new(TcpSource::new(addr).await?) + let (x, r) = TcpSource::new(addr, &username, passwd.as_ref()) + .await? + .split(); + (Box::new(x), Box::new(r)) } SocketAddrOrUid::Uid(uid) => { debug!("Trying uid {}", uid); @@ -255,11 +283,14 @@ impl BcCamera { } last_result }?; - Box::new(UdpSource::new_from_discovery(discovery).await?) + let (x, r) = UdpSource::new_from_discovery(discovery, &username, passwd.as_ref()) + .await? + .split(); + (Box::new(x), Box::new(r)) } }; - let conn = BcConnection::new(source).await?; + let conn = BcConnection::new(sink, source).await?; debug!("Success"); let me = Self { @@ -267,7 +298,7 @@ impl BcCamera { message_num: AtomicU16::new(0), channel_id, logged_in: AtomicBool::new(false), - credentials: Mutex::new(None), + credentials: Credentials::new(username, passwd), }; Ok(me) } @@ -291,18 +322,10 @@ impl BcCamera { } // Certains commands like logout need the username and password - // this command will return it as a tuple of (Username, Option) + // this command will return // This will only work after login - fn get_credentials(&self) -> Option { - self.credentials.lock().unwrap().clone() - } - // This is used to store the credentials it is called during login. - fn set_credentials(&self, username: String, password: Option) { - *(self.credentials.lock().unwrap()) = Some(Credentials::new(username, password)); - } - // This is used to clear the stored credentials it is called during logout. - fn clear_credentials(&self) { - *(self.credentials.lock().unwrap()) = None; + fn get_credentials(&self) -> &Credentials { + &self.credentials } } @@ -326,16 +349,6 @@ fn md5_string(input: &str, trunc: Md5Trunc) -> String { md5 } -/// This is a convience function to make an AES key from the login password and the NONCE -/// negotiated during login -pub fn make_aes_key(nonce: &str, passwd: &str) -> [u8; 16] { - let key_phrase = format!("{}-{}", nonce, passwd); - let key_phrase_hash = format!("{:X}\0", md5::compute(key_phrase)) - .to_uppercase() - .into_bytes(); - key_phrase_hash[0..16].try_into().unwrap() -} - #[test] fn test_md5_string() { // Note that these literals are only 31 characters long - see explanation above. diff --git a/crates/core/src/bc_protocol/connection/.DS_Store b/crates/core/src/bc_protocol/connection/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0>>>; +pub(crate) type BcConnSink = Box + Send + Sync + Unpin>; +pub(crate) type BcConnSource = Box> + Send + Sync + Unpin>; /// A shareable connection to a camera. Handles serialization of messages. To send/receive, call /// .[subscribe()] with a message number. You can use the BcSubscription to send or receive only @@ -20,28 +24,29 @@ type Subscriber = Arc>>>; /// /// There can be only one subscriber per kind of message at a time. pub struct BcConnection { - sink: Arc>>, + sink: Arc>, subscribers: Subscriber, rx_thread: Mutex>>, } impl BcConnection { - pub async fn new(source: Box) -> Result { + pub async fn new(sink: BcConnSink, mut source: BcConnSource) -> Result { let subscribers: Subscriber = Default::default(); let subs = subscribers.clone(); - let arc_source = Arc::new(RwLock::new(source)); - let conn = arc_source.clone(); - let rx_thread = task::spawn(async move { loop { - let bc = match conn.write().await.recv().await { - Ok(bc) => bc, - Err(e) => { + trace!("packet Wait"); + let packet = source.next().await; + trace!("packet: {:?}", packet); + let bc = match packet { + Some(Ok(bc)) => bc, + Some(Err(e)) => { error!("Deserialization error: {:?}", e); break; } + None => continue, }; if let Err(e) = Self::poll(bc, &subs).await { @@ -52,20 +57,22 @@ impl BcConnection { }); Ok(BcConnection { - sink: arc_source, + sink: Arc::new(Mutex::new(sink)), subscribers, rx_thread: Mutex::new(Some(rx_thread)), }) } pub fn stop_polling(&self) { - if let Some(t) = self.rx_thread.lock().unwrap().take() { + if let Some(t) = self.rx_thread.blocking_lock().take() { t.abort() }; } pub(super) async fn send(&self, bc: Bc) -> crate::Result<()> { - self.sink.write().await.send(bc).await?; + trace!("send Wait: {:?}", bc); + self.sink.lock().await.send(bc).await?; + trace!("send Complete"); Ok(()) } @@ -79,7 +86,11 @@ impl BcConnection { } pub fn unsubscribe(&self, msg_num: u16) -> Result<()> { - self.subscribers.blocking_write().remove(&msg_num); + let subs = self.subscribers.clone(); + tokio::task::spawn(async move { + subs.write().await.remove(&msg_num); + }); + Ok(()) } @@ -109,19 +120,12 @@ impl BcConnection { Ok(()) } - - pub(crate) async fn get_encrypted(&self) -> EncryptionProtocol { - self.sink.read().await.get_encrypted().clone() - } - pub(crate) async fn set_encrypted(&self, protocol: EncryptionProtocol) { - self.sink.write().await.set_encrypted(protocol) - } } impl Drop for BcConnection { fn drop(&mut self) { debug!("Shutting down BcConnection..."); - if let Some(t) = self.rx_thread.get_mut().unwrap().take() { + if let Some(t) = self.rx_thread.get_mut().take() { t.abort() }; } diff --git a/crates/core/src/bc_protocol/connection/discovery.rs b/crates/core/src/bc_protocol/connection/discovery.rs index 7fbc350..b3dedf5 100644 --- a/crates/core/src/bc_protocol/connection/discovery.rs +++ b/crates/core/src/bc_protocol/connection/discovery.rs @@ -16,7 +16,7 @@ use local_ip_address::local_ip; use log::*; use rand::{seq::SliceRandom, thread_rng, Rng}; use std::collections::{btree_map::Entry, BTreeMap}; -use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs}; +use std::net::{Ipv4Addr, SocketAddr, ToSocketAddrs}; use std::sync::Arc; use tokio::{ net::UdpSocket, @@ -30,6 +30,25 @@ use tokio::{ use tokio_stream::wrappers::ReceiverStream; use tokio_util::udp::UdpFramed; +#[derive(Debug)] +struct RegisterResult { + dmap: Option, + dev: Option, + sid: u32, +} + +#[derive(Debug)] +struct ConnectResult { + addr: SocketAddr, + camera_id: i32, +} + +#[derive(Debug)] +struct UidLookupResults { + reg: SocketAddr, + relay: SocketAddr, +} + pub(crate) struct Discovery {} const MTU: u32 = 1030; @@ -73,14 +92,19 @@ impl Discoverer { loop { match reader.next().await { Some(Ok((BcUdp::Discovery(bcudp), addr))) => { - let tid = bcudp.tid; + let mut tid = bcudp.tid; let mut needs_removal = false; if let Some(sender) = thread_subscriber.read().await.get(&tid) { 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 { - debug!("Udp Discovery got this unexpected BcUdp {:?}", bcudp); + trace!("Udp Discovery got this unexpected BcUdp {:?}", bcudp); } if needs_removal { thread_subscriber.write().await.remove(&tid); @@ -88,8 +112,7 @@ impl Discoverer { } Some(Ok(bcudp)) => { // Only discovery packets should be possible atm - error!("Got non Discovery during discovery: {:?}", bcudp); - unreachable!() + trace!("Got non Discovery during discovery: {:?}", bcudp); } Some(Err(e)) => { let mut locked_sub = thread_subscriber.write().await; @@ -201,6 +224,7 @@ impl Discoverer { } else { disc.tid }; + let mut reply = ReceiverStream::new(self.subscribe(target_tid).await?); let msg = BcUdp::Discovery(disc); @@ -221,11 +245,402 @@ impl Discoverer { } Err(Error::DiscoveryTimeout) } + + // This function will contact the p2p relay servers + // + // It will ask each of the servers for details on a specific UID + // + // On success it returns the M2cQr that the p2p relay + // server has about the UID + async fn uid_loopup(&self, uid: &str) -> Result { + let mut addrs = vec![]; + for p2p_relay in P2P_RELAY_HOSTNAMES.iter() { + addrs.append( + &mut format!("{}:9999", p2p_relay) + .to_socket_addrs() + .map(|i| i.collect::>()) + .unwrap_or_else(|_| vec![]), + ); + } + let msg = UdpDiscovery { + tid: 0, + payload: UdpXml { + c2m_q: Some(C2mQ { + uid: uid.to_string(), + os: "MAC".to_string(), + }), + ..Default::default() + }, + }; + let (packet, _) = self + .retry_send_multi(msg, addrs.as_slice(), |bc, addr| match bc { + UdpDiscovery { + tid: _, + payload: + UdpXml { + m2c_q_r: Some(m2c_q_r), + .. + }, + } if m2c_q_r.reg.port != 0 || !m2c_q_r.reg.ip.is_empty() => Some((m2c_q_r, addr)), + _ => None, + }) + .await?; + + Ok(UidLookupResults { + reg: SocketAddr::new(packet.reg.ip.parse()?, packet.reg.port), + relay: SocketAddr::new(packet.relay.ip.parse()?, packet.relay.port), + }) + } + + async fn send_and_forget(&self, mut disc: UdpDiscovery, dest: SocketAddr) -> Result<()> { + if disc.tid == 0 { + // If 0 make a random one + let target_tid = generate_tid(); + disc.tid = target_tid; + } + let mut inter = interval(Duration::from_millis(50)); + let msg = BcUdp::Discovery(disc); + + for _i in 0..5 { + inter.tick().await; + + self.send(msg.clone(), dest).await?; + } + + Ok(()) + } + + async fn device_initiated_connect( + &self, + client_id: i32, + register_result: &RegisterResult, + ) -> Result { + let (addr, local_tid, local_did) = self + .handle_incoming(|bc, addr| { + trace!("bc: {:?}", bc); + match (bc, register_result) { + ( + UdpDiscovery { + tid, + payload: + UdpXml { + d2c_t: Some(D2cT { sid, cid, did, .. }), + .. + }, + }, + RegisterResult { + dmap: Some(register_dmap), + sid: register_sid, + .. + }, + ) if cid == client_id && &sid == register_sid && &addr == register_dmap => { + Some((addr, tid, did)) + } + ( + UdpDiscovery { + tid, + payload: + UdpXml { + d2c_t: Some(D2cT { sid, cid, did, .. }), + .. + }, + }, + RegisterResult { + dev: Some(register_dev), + sid: register_sid, + .. + }, + ) if cid == client_id && &sid == register_sid && &addr == register_dev => { + Some((addr, tid, did)) + } + _ => None, + } + }) + .await?; + + let msg = UdpDiscovery { + tid: local_tid, + payload: UdpXml { + c2d_a: Some(C2dA { + sid: register_result.sid, + conn: "map".to_string(), + cid: client_id, + did: local_did, + mtu: MTU, + }), + ..Default::default() + }, + }; + + // Send and await confirm + self.retry_send(msg, addr, |bc, _| { + trace!("msg: {:?}", &bc); + match bc { + UdpDiscovery { + tid: _, + payload: + UdpXml { + d2c_cfm: Some(D2cCfm { sid, cid, did, .. }), + .. + }, + } if sid == register_result.sid && did == local_did && cid == client_id => Some(()), + _ => None, + } + }) + .await?; + + Ok(ConnectResult { + addr, + camera_id: local_did, + }) + } + + async fn client_initiated_connect( + &self, + local_sid: u32, + client_id: i32, + dev_addr: SocketAddr, + ) -> Result { + let tid = generate_tid(); + + let msg = UdpDiscovery { + tid, + payload: UdpXml { + c2d_t: Some(C2dT { + sid: local_sid, + cid: client_id, + mtu: MTU, + conn: "local".to_string(), + }), + ..Default::default() + }, + }; + + let (final_addr, local_did) = self + .retry_send(msg, dev_addr, |bc, addr| match bc { + UdpDiscovery { + tid: _, + payload: + UdpXml { + d2c_cfm: Some(D2cCfm { cid, did, sid, .. }), + .. + }, + } if cid == client_id && sid == local_sid => Some((addr, did)), + _ => None, + }) + .await?; + + Ok(ConnectResult { + addr: final_addr, + camera_id: local_did, + }) + } + + /// Register our local ip address with the reolink servers + /// This will be used for the device to contact us + async fn register_address( + &self, + uid: &str, + client_id: i32, + reg_addr: SocketAddr, + relay_addr: SocketAddr, + ) -> Result { + let tid = generate_tid(); + let local_addr = SocketAddr::new(local_ip()?, self.local_addr().port()); + let local_ip = local_addr.ip(); + let local_port = local_addr.port(); + let local_family = if local_addr.ip().is_ipv4() { 4 } else { 6 }; + + let msg = UdpDiscovery { + tid, + payload: UdpXml { + c2r_c: Some(C2rC { + uid: uid.to_string(), + cli: IpPort { + ip: local_ip.to_string(), + port: local_port, + }, + relay: IpPort { + ip: relay_addr.ip().to_string(), + port: relay_addr.port(), + }, + cid: client_id, + family: local_family, + debug: false, + os: "MAC".to_string(), + revision: Some(3), + }), + ..Default::default() + }, + }; + + // Send and await acceptance + let (sid, dmap, dev) = self + .retry_send(msg, reg_addr, |bc, _| match bc { + UdpDiscovery { + tid: _, + payload: + UdpXml { + r2c_t: + Some(R2cT { + dmap: Some(dmap), + sid, + cid, + dev: Some(dev), + }), + .. + }, + } if !dmap.ip.is_empty() + && dmap.port > 0 + && !dev.ip.is_empty() + && dev.port > 0 + && cid == client_id => + { + Some(Ok((sid, Some(dmap), Some(dev)))) + } + UdpDiscovery { + tid: _, + payload: + UdpXml { + r2c_t: + Some(R2cT { + dmap: Some(dmap), + sid, + cid, + dev: None, + }), + .. + }, + } if !dmap.ip.is_empty() && dmap.port > 0 && cid == client_id => { + Some(Ok((sid, Some(dmap), None))) + } + UdpDiscovery { + tid: _, + payload: + UdpXml { + r2c_t: + Some(R2cT { + dmap: None, + sid, + cid, + dev: Some(dev), + }), + .. + }, + } if !dev.ip.is_empty() && dev.port > 0 && cid == client_id => { + Some(Ok((sid, None, Some(dev)))) + } + UdpDiscovery { + tid: _, + payload: + UdpXml { + r2c_c_r: Some(R2cCr { dmap, sid, rsp, .. }), + .. + }, + } if !dmap.ip.is_empty() && dmap.port > 0 && rsp != -1 => { + Some(Ok((sid, Some(dmap), None))) + } + UdpDiscovery { + tid: _, + payload: + UdpXml { + r2c_c_r: Some(R2cCr { dmap, rsp, .. }), + .. + }, + } if !dmap.ip.is_empty() && dmap.port > 0 && rsp == -1 => { + Some(Err(Error::RegisterError)) + } + _ => None, + }) + .await??; + + Ok(RegisterResult { + sid, + dmap: dmap.map(|dmap| { + SocketAddr::new(dmap.ip.parse().expect("Could not read ip addr"), dmap.port) + }), + dev: dev.map(|dev| { + SocketAddr::new(dev.ip.parse().expect("Could not read ip addr"), dev.port) + }), + }) + } + + /// Confirm connection with relay + /// This is sent once all data has been found + async fn confirm_relay( + &self, + client_id: i32, + device_id: i32, + sid: u32, + reg_addr: SocketAddr, + ) -> Result<()> { + let msg = UdpDiscovery { + tid: 0, + payload: UdpXml { + c2r_cfm: Some(C2rCfm { + sid, + cid: client_id, + did: device_id, + rsp: 0, + conn: "map".to_string(), + }), + ..Default::default() + }, + }; + + self.send_and_forget(msg, reg_addr).await?; + + Ok(()) + } + + /// Confirm Map. This is sent if we learned the IP via the relay + /// then connected locally. We tell the relay they we are establishing + /// a local then tell the camera to connect to us via map + async fn confirm_map( + &self, + client_id: i32, + device_id: i32, + sid: u32, + reg_addr: SocketAddr, + dev_addr: SocketAddr, + ) -> Result<()> { + let msg = UdpDiscovery { + tid: 0, + payload: UdpXml { + c2r_cfm: Some(C2rCfm { + sid, + cid: client_id, + did: device_id, + conn: "local".to_string(), + rsp: 0, + }), + ..Default::default() + }, + }; + + self.send_and_forget(msg, reg_addr).await?; + + let msg = UdpDiscovery { + tid: 0, + payload: UdpXml { + c2d_t: Some(C2dT { + sid, + cid: client_id, + mtu: MTU, + conn: "map".to_string(), + }), + ..Default::default() + }, + }; + + self.send_and_forget(msg, dev_addr).await?; + + Ok(()) + } } impl Discovery { pub(crate) async fn local(uid: &str) -> Result { - trace!("Local"); let discoverer = Discoverer::new().await?; let client_id = generate_cid(); @@ -248,7 +663,6 @@ impl Discovery { }, }; - trace!("Local: Sending Broadcast"); let dests = get_broadcasts(&[2015, 2018])?; let (camera_address, camera_id) = discoverer .retry_send_multi(msg, &dests, |bc, addr| match bc { @@ -264,7 +678,6 @@ impl Discovery { }) .await?; - trace!("Local: Success"); Ok(DiscoveryResult { socket: discoverer.into_socket().await, addr: camera_address, @@ -285,196 +698,38 @@ impl Discovery { // - Send a message to the camera that we could connect remotely #[allow(unused)] pub(crate) async fn remote(uid: &str) -> Result { - trace!("Remote"); let mut discoverer = Discoverer::new().await?; let client_id = generate_cid(); - let local_addr = *discoverer.local_addr(); - let local_port = local_addr.port(); + let lookup = discoverer.uid_loopup(uid).await?; + let reg_addr = lookup.reg; + let relay_addr = lookup.relay; - trace!("Remote: Finding Register"); - let reg_packet = get_register(&discoverer, uid).await?; - trace!("Remote: Register Found"); + let reg_result = discoverer + .register_address(uid, client_id, lookup.reg, relay_addr) + .await?; + let dev_addr = reg_result.dev.ok_or(Error::NoDev)?; - let register_address = reg_packet.reg; - let relay_address = reg_packet.relay; - let log_address = reg_packet.log; + let connect_result = discoverer + .client_initiated_connect(reg_result.sid, client_id, dev_addr) + .await?; - let default_local_address = local_ip().expect("There to be a local ip"); - debug!("Register address found: {:?}", register_address); - debug!("Registering this address: {:?}", default_local_address); - - trace!("Remote: Registering local IP"); - let local_family = if local_addr.ip().is_ipv4() { 4 } else { 6 }; - let msg = UdpDiscovery { - tid: 0, - payload: UdpXml { - c2r_c: Some(C2rC { - uid: uid.to_string(), - cli: IpPort { - ip: default_local_address.to_string(), - port: local_port, - }, - relay: relay_address, - cid: client_id, - family: local_family, - debug: false, - os: "MAC".to_string(), - revision: None, - }), - ..Default::default() - }, - }; - - let register_address_ip: IpAddr = register_address.ip.parse()?; - let (device_sid, dev_loc) = discoverer - .retry_send_multi( - msg, - &[SocketAddr::new(register_address_ip, register_address.port)], - |bc, _| match bc { - UdpDiscovery { - tid: _, - payload: - UdpXml { - r2c_t: - Some(R2cT { - dev: Some(dev), - cid, - sid, - .. - }), - .. - }, - } if cid == client_id && !dev.ip.is_empty() && dev.port > 0 => Some((sid, dev)), - _ => None, - }, + discoverer + .confirm_map( + client_id, + connect_result.camera_id, + reg_result.sid, + reg_addr, + dev_addr, ) .await?; - debug!("Register revealed address as SID: {:?}", device_sid); - debug!("Register revealed address as IP: {:?}", dev_loc); - - trace!("Remote: Aquiring device ID"); - let msg = UdpDiscovery { - tid: 0, - payload: UdpXml { - c2d_t: Some(C2dT { - sid: device_sid, - cid: client_id, - mtu: MTU, - conn: "local".to_string(), - }), - ..Default::default() - }, - }; - - let mut device_addr = SocketAddr::new(dev_loc.ip.parse()?, dev_loc.port); - let device_id = tokio::select! { - v = discoverer - .retry_send( - msg, - device_addr, - |bc, _| match bc { - UdpDiscovery { - tid: _, - payload: - UdpXml { - d2c_disc: Some(D2cDisc { cid, did, .. }), - .. - }, - } if cid == client_id => Some(did), - UdpDiscovery { - tid: _, - payload: - UdpXml { - d2c_t: Some(D2cT { cid, did, .. }), - .. - }, - } if cid == client_id => Some(did), - _ => None, - }, - ) => {v?}, - // The camera can observe the registered details above and - // use them to establish it's own connection (hold punching) - // This branch handles those new camera connections - v = - discoverer - .handle_incoming(|bc, addr| match bc { - UdpDiscovery { - tid: _, - payload: - UdpXml { - d2c_t: - Some(D2cT { - sid, - cid, - did, - conn, - .. - }), - .. - }, - } if cid == client_id && sid == device_sid => { - Some((did, addr)) - } - _ => None, - }) => { - let (result, addr) = v?; - device_addr = addr; - result - } - }; - - debug!("Got device ID as: {:?}", device_id); - - trace!("Remote: Declare local"); - let msg = UdpDiscovery { - tid: 0, - payload: UdpXml { - c2r_cfm: Some(C2rCfm { - sid: device_sid, - cid: client_id, - did: device_id, - conn: "local".to_string(), - rsp: 0, - }), - ..Default::default() - }, - }; - - discoverer - .retry_send( - msg, - SocketAddr::new(log_address.ip.parse()?, log_address.port), - |_, _| Some(()), - ) - .await?; - - trace!("Remote: Declare map"); - let msg = UdpDiscovery { - tid: generate_tid(), - payload: UdpXml { - c2d_t: Some(C2dT { - sid: device_sid, - cid: client_id, - mtu: MTU, - conn: "map".to_string(), - }), - ..Default::default() - }, - }; - - discoverer - .retry_send(msg, device_addr, |_, _| Some(())) - .await?; - - trace!("Remote: Success"); Ok(DiscoveryResult { socket: discoverer.into_socket().await, - addr: device_addr, + addr: connect_result.addr, client_id, - camera_id: device_id, + camera_id: connect_result.camera_id, }) } @@ -485,238 +740,49 @@ impl Discovery { // #[allow(unused)] pub(crate) async fn relay(uid: &str) -> Result { - trace!("Relay"); let mut discoverer = Discoverer::new().await?; - let client_id = generate_cid(); - let local_addr = *discoverer.local_addr(); - let local_port = local_addr.port(); + let client_id = generate_cid(); + trace!("client_id: {}", client_id); - trace!("Relay: Finding Register"); - let reg_packet = get_register(&discoverer, uid).await?; - trace!("Relay: Register Found"); + let lookup = discoverer.uid_loopup(uid).await?; + trace!("lookup: {:?}", lookup); + let reg_addr = lookup.reg; + let relay_addr = lookup.relay; - let register_address = reg_packet.reg; - let relay_address = reg_packet.relay; - let log_address = reg_packet.log; - - let register_socket = SocketAddr::new(register_address.ip.parse()?, register_address.port); - - let default_local_address = local_ip().expect("There to be a local ip"); - debug!("Register address found: {:?}", register_address); - debug!("Registering this address: {:?}", default_local_address); - - let local_family = if local_addr.ip().is_ipv4() { 4 } else { 6 }; - let msg = UdpDiscovery { - tid: 0, - payload: UdpXml { - c2r_c: Some(C2rC { - uid: uid.to_string(), - cli: IpPort { - ip: default_local_address.to_string(), - port: local_port, - }, - relay: relay_address, - cid: client_id, - family: local_family, - debug: false, - os: "MAC".to_string(), - revision: Some(3), - }), - ..Default::default() - }, - }; - trace!("Relay: Declaring Local IP: {}", msg.tid); - - let (device_sid, dmap, did) = discoverer - .retry_send(msg, register_socket, |bc, _| match bc { - UdpDiscovery { - tid: _, - payload: - UdpXml { - r2c_t: - Some(R2cT { - dmap: Some(dmap), - sid, - cid, - .. - }), - .. - }, - } if !dmap.ip.is_empty() && dmap.port > 0 && cid == client_id => { - Some((sid, Some(dmap), None)) - } - UdpDiscovery { - tid: _, - payload: - UdpXml { - r2c_c_r: Some(R2cCr { dmap, sid, .. }), - .. - }, - } if !dmap.ip.is_empty() && dmap.port > 0 => Some((sid, Some(dmap), None)), - UdpDiscovery { - tid: _, - payload: - UdpXml { - d2c_t: Some(D2cT { sid, did, .. }), - .. - }, - } => Some((sid, None, Some(did))), - _ => None, - }) + let reg_result = discoverer + .register_address(uid, client_id, lookup.reg, relay_addr) .await?; + trace!("reg_result: {:?}", reg_result); - debug!("Register revealed dmap address as SID: {:?}", device_sid); - debug!("Register revealed dmap address as IP: {:?}", dmap); - debug!("Register revealed device ID: {:?}", did); + let dmap_addr = reg_result.dmap.ok_or(Error::NoDmap)?; - let mut device_addr = register_socket; - let device_id = if let Some(dmap) = dmap { - let msg = UdpDiscovery { - tid: 0, - payload: UdpXml { - c2d_t: Some(C2dT { - sid: device_sid, - cid: client_id, - mtu: MTU, - conn: "relay".to_string(), - }), - ..Default::default() - }, - }; - trace!("Relay: Aquiring Device ID: {}", msg.tid); - - tokio::select! { - v = discoverer - .retry_send(msg, device_addr, |bc, _| match bc { - UdpDiscovery { - tid: _, - payload: - UdpXml { - d2c_cfm: Some(D2cCfm { cid, did, .. }), - .. - }, - } if cid == client_id => Some(did), - _ => None, - }) => {v?}, - // The camera can observe the registered details above and - // use them to establish it's own connection (hold punching) - // This branch handles those new camera connections - v = - discoverer - .handle_incoming(|bc, addr| match bc { - UdpDiscovery { - tid: _, - payload: - UdpXml { - d2c_t: - Some(D2cT { - sid, - cid, - did, - conn, - .. - }), - .. - }, - } if cid == client_id && sid == device_sid => { - Some((did, addr)) - } - _ => None, - }) => { - let (result, addr) = v?; - device_addr = addr; - result - } - } - } else { - did.unwrap() - }; - - debug!("Got device ID as: {:?}", device_id); - - let msg = UdpDiscovery { - tid: 0, - payload: UdpXml { - c2r_cfm: Some(C2rCfm { - sid: device_sid, - cid: client_id, - did: device_id, - rsp: 0, - conn: "relay".to_string(), - }), - ..Default::default() - }, - }; - trace!("Relay: Declare Relay: {}", msg.tid); + let connect_result = discoverer + .device_initiated_connect(client_id, ®_result) + .await?; + trace!("connect_result: {:?}", connect_result); discoverer - .retry_send( - msg, - SocketAddr::new(log_address.ip.parse()?, log_address.port), - |_, _| Some(()), + .confirm_relay( + client_id, + connect_result.camera_id, + reg_result.sid, + reg_addr, ) .await?; - trace!("Relay: Success"); + trace!("confirmed"); + Ok(DiscoveryResult { socket: discoverer.into_socket().await, - addr: device_addr, + addr: connect_result.addr, client_id, - camera_id: device_id, + camera_id: connect_result.camera_id, }) } } -// This function will contact the p2p relay servers -// -// It will ask each of the servers for details on a specific UID -// -// On success it returns the M2cQr that the p2p relay -// server has about the UID -async fn get_register(discoverer: &Discoverer, uid: &str) -> Result { - let mut addrs = vec![]; - for p2p_relay in P2P_RELAY_HOSTNAMES.iter() { - debug!("Trying register: {}", p2p_relay); - addrs.append( - &mut format!("{}:9999", p2p_relay) - .to_socket_addrs() - .map(|i| i.collect::>()) - .unwrap_or_else(|_| vec![]), - ); - } - let msg = UdpDiscovery { - tid: 0, - payload: UdpXml { - c2m_q: Some(C2mQ { - uid: uid.to_string(), - os: "MAC".to_string(), - }), - ..Default::default() - }, - }; - let (packet, addr) = discoverer - .retry_send_multi(msg, addrs.as_slice(), |bc, addr| match bc { - UdpDiscovery { - tid: _, - payload: - UdpXml { - m2c_q_r: Some(m2c_q_r), - .. - }, - } if m2c_q_r.reg.port != 0 || !m2c_q_r.reg.ip.is_empty() => Some((m2c_q_r, addr)), - _ => None, - }) - .await?; - - debug!( - "Registry details found at {:?}: {}:{}", - addr, packet.reg.ip, packet.reg.port, - ); - Ok(packet) -} - fn get_broadcasts(ports: &[u16]) -> Result> { let mut broadcasts = vec![Ipv4Addr::BROADCAST]; for iface in get_if_addrs::get_if_addrs()?.iter() { @@ -768,3 +834,247 @@ async fn connect() -> Result { Ok(socket) } + +/* + # Discovery Methods + + # Register + + This is the inital query to known hosts with a known UID + + - C->R Port 9999 + ```xml + + + 95270000YGAKNWKJ +

MAC

+
+
+ ``` + + Replies with details of the camera we want to connect to + + - R->C + ```xml + + + + 18.162.200.47 + 58200 + + + 18.162.200.47 + 58100 + + + 18.162.200.47 + 57850 + + + 18.162.200.47 + 9996 + + + + 1350 + 251658240 + -1700607721 + 0 + + + ``` + + ## Thread 1: Observed during relay + + - D->C 59733 + ```xml + + + 495151439 + map + 254000 + 735 + + + ``` + + - C->D + ```xml + + + 495151439 + map + 254000 + 735 + 1350 + + + ``` + + - D->C + ```xml + + + 495151439 + map + 0 + 254000 + 735 + 55607 + + + ``` + + ## Thread 2: Observed during relay + + - C->R: 58200 + ```xml + + + 95270000YGAKNWKJ + + 10.202.133.237 + 12254 + + + 18.162.200.47 + 58100 + + 254000 + 251658240 + 4 +

MAC

+ 3 +
+
+ ``` + + - R->C + ```xml + 192.168.1.10057933184.22.90.67579334951514392540000 + ``` + + - R->C + ```xml + + + + 184.22.90.67 + 57933 + + + 192.168.1.100 + 57933 + + + 18.162.200.47 + 51134 + + + 18.162.200.47 + 9997 + + NULL + 495151439 + 0 + 495151439 + + + ``` + + - R->C + ```xml + + + + 192.168.1.100 + 57933 + + + 184.22.90.67 + 57933 + + 495151439 + 254000 + 0 + + + ``` + + - R->C Repeats later so possibly was not responded to by client + ```xml + + + + 184.22.90.67 + 57933 + + + 192.168.1.100 + 57933 + + + 18.162.200.47 + 51134 + + + 18.162.200.47 + 9997 + + NULL + 495151439 + 0 + 495151439 + + + ``` + + - C->R + ```xml + + + 495151439 + map + 0 + 254000 + 735 + + + ``` + + - R->C + ```xml + + + + 192.168.1.100 + 57933 + + + 184.22.90.67 + 57933 + + 495151439 + 254000 + 0 + + + ``` + + # Thread 3: Observed during relay + After connection. No response + + - C->R + ```xml + + + 495151439 + map + 0 + 254000 + 735 + + + ``` + +*/ diff --git a/crates/core/src/bc_protocol/connection/mod.rs b/crates/core/src/bc_protocol/connection/mod.rs index e2f1692..19a45be 100644 --- a/crates/core/src/bc_protocol/connection/mod.rs +++ b/crates/core/src/bc_protocol/connection/mod.rs @@ -10,12 +10,12 @@ use tokio::net::UdpSocket; mod bcconn; mod bcsub; mod discovery; -mod source; +// mod source; mod tcpsource; mod udpsource; pub(crate) use self::{ - bcconn::BcConnection, bcsub::BcSubscription, discovery::Discovery, source::*, + bcconn::BcConnection, bcconn::*, bcsub::BcSubscription, discovery::Discovery, tcpsource::TcpSource, udpsource::UdpSource, }; diff --git a/crates/core/src/bc_protocol/connection/source.rs b/crates/core/src/bc_protocol/connection/source.rs index 0c7319e..aaee3a3 100644 --- a/crates/core/src/bc_protocol/connection/source.rs +++ b/crates/core/src/bc_protocol/connection/source.rs @@ -8,11 +8,14 @@ use crate::{bc::model::*, Result}; use async_trait::async_trait; #[async_trait] -pub trait Source: Send + Sync { +pub trait Source: + futures::stream::Stream> + + futures::sink::Sink + + Send + + Sync +{ async fn send(&mut self, bc: Bc) -> Result<()>; async fn recv(&mut self) -> Result; - fn get_encrypted(&self) -> &EncryptionProtocol; - fn set_encrypted(&mut self, protocol: EncryptionProtocol); } #[async_trait] @@ -23,13 +26,6 @@ impl Source for TcpSource { async fn recv(&mut self) -> Result { TcpSource::recv(self).await } - - fn get_encrypted(&self) -> &EncryptionProtocol { - TcpSource::get_encrypted(self) - } - fn set_encrypted(&mut self, protocol: EncryptionProtocol) { - TcpSource::set_encrypted(self, protocol) - } } #[async_trait] @@ -40,11 +36,4 @@ impl Source for UdpSource { async fn recv(&mut self) -> Result { UdpSource::recv(self).await } - - fn get_encrypted(&self) -> &EncryptionProtocol { - UdpSource::get_encrypted(self) - } - fn set_encrypted(&mut self, protocol: EncryptionProtocol) { - UdpSource::set_encrypted(self, protocol) - } } diff --git a/crates/core/src/bc_protocol/connection/tcpsource.rs b/crates/core/src/bc_protocol/connection/tcpsource.rs index bfc2244..96b2cbb 100644 --- a/crates/core/src/bc_protocol/connection/tcpsource.rs +++ b/crates/core/src/bc_protocol/connection/tcpsource.rs @@ -1,11 +1,8 @@ -use crate::bc::codex::BcCodex; use crate::bc::model::*; use crate::Result; +use crate::{bc::codex::BcCodex, Credentials}; use delegate::delegate; -use futures::{ - sink::{Sink, SinkExt}, - stream::{Stream, StreamExt}, -}; +use futures::{sink::Sink, stream::Stream}; use std::net::SocketAddr; use std::pin::Pin; use std::task::{Context, Poll}; @@ -17,30 +14,17 @@ pub(crate) struct TcpSource { } impl TcpSource { - pub(crate) async fn new(addr: SocketAddr) -> Result { + pub(crate) async fn new, U: Into>( + addr: SocketAddr, + username: T, + password: Option, + ) -> Result { let stream = connect_to(addr).await?; Ok(Self { - inner: Framed::new(stream, BcCodex::new()), + inner: Framed::new(stream, BcCodex::new(Credentials::new(username, password))), }) } - - pub(crate) async fn send(&mut self, bc: Bc) -> Result<()> { - self.inner.send(bc).await - } - pub(crate) async fn recv(&mut self) -> Result { - loop { - if let Some(result) = self.inner.next().await { - return result; - } - } - } - pub(crate) fn get_encrypted(&self) -> &EncryptionProtocol { - self.inner.codec().get_encrypted() - } - pub(crate) fn set_encrypted(&mut self, protocol: EncryptionProtocol) { - self.inner.codec_mut().set_encrypted(protocol) - } } impl Stream for TcpSource { diff --git a/crates/core/src/bc_protocol/connection/udpconn/aborthandle.rs b/crates/core/src/bc_protocol/connection/udpconn/aborthandle.rs deleted file mode 100644 index 989405e..0000000 --- a/crates/core/src/bc_protocol/connection/udpconn/aborthandle.rs +++ /dev/null @@ -1,25 +0,0 @@ -use std::sync::{ - atomic::{AtomicBool, Ordering}, - Arc, -}; - -#[derive(Clone)] -pub struct AbortHandle { - aborted: Arc, -} - -impl AbortHandle { - pub fn new() -> Self { - Self { - aborted: Arc::new(AtomicBool::new(false)), - } - } - - pub fn abort(&self) { - self.aborted.store(true, Ordering::Relaxed); - } - - pub fn is_aborted(&self) -> bool { - self.aborted.load(Ordering::Relaxed) - } -} diff --git a/crates/core/src/bc_protocol/connection/udpconn/discover.rs b/crates/core/src/bc_protocol/connection/udpconn/discover.rs deleted file mode 100644 index df8dd1f..0000000 --- a/crates/core/src/bc_protocol/connection/udpconn/discover.rs +++ /dev/null @@ -1,570 +0,0 @@ -use super::{aborthandle::AbortHandle, Error, Result, MTU, P2P_RELAY_HOSTNAMES, WAIT_TIME}; -use crate::bcudp::{model::*, xml::*}; -use local_ip_address::local_ip; -use log::*; -use rand::{thread_rng, Rng}; -use std::io::{Error as IoError, ErrorKind, Result as IoResult}; -use std::net::{Ipv4Addr, SocketAddr, ToSocketAddrs, UdpSocket}; -use std::time::Duration; -use time::OffsetDateTime; - -pub struct UdpDiscover { - pub client_id: i32, - pub camera_id: i32, - pub mtu: u32, - pub address: SocketAddr, -} - -impl UdpDiscover { - // Sends data on a UDP socket until aborted or max retries reached - fn retrying_send_to( - socket: &UdpSocket, - buf: &[u8], - addr: T, - ) -> IoResult<(usize, AbortHandle)> { - let addr = addr - .to_socket_addrs()? - .next() - .ok_or_else(|| IoError::new(ErrorKind::NotFound, Error::ConnectionUnavaliable))?; - Self::retrying_send_to_multi(socket, buf, &[addr]) - } - - // Sends data to multiple destinations using a UDP socket - // until aborted or max retries reached - fn retrying_send_to_multi( - socket: &UdpSocket, - buf: &[u8], - addrs: &[T], - ) -> IoResult<(usize, AbortHandle)> { - let handle = AbortHandle::new(); - - let addrs: Vec = addrs - .iter() - .flat_map(|a| { - a.to_socket_addrs() - .ok() - .and_then(|mut a_iter| a_iter.next()) - }) - .collect(); - - let mut bytes_send = 0; - for addr in addrs.iter() { - bytes_send = socket.send_to(buf, addr)?; - } - - let thread_buffer = buf.to_vec(); - let thread_handle = handle.clone(); - let thread_socket = socket.try_clone()?; - let thread_addr: Vec = addrs; - - const MAX_RETRIES: usize = 10; - - std::thread::spawn(move || { - std::thread::sleep(WAIT_TIME); - for _ in 0..MAX_RETRIES { - if thread_handle.is_aborted() { - break; - } - for addr in thread_addr.iter() { - let _ = thread_socket.send_to(&thread_buffer[..], addr); - } - std::thread::sleep(WAIT_TIME); - } - }); - - Ok((bytes_send, handle)) - } - - // Performs local discovery - // - // This involves broadcasting a C2dC - // Bc Discovery packet to ports 2015 and 2018 - // and awaiting a D2cCr reply - fn discover_from_uuid_local(socket: &UdpSocket, uid: &str, timeout: Duration) -> Result { - let mut rng = thread_rng(); - // If tid is too large it will overflow during encrypt so we just use a random u8 - let tid: u32 = (rng.gen::()) as u32; - let client_id: i32 = rng.gen(); - let local_addr = socket.local_addr()?; - let port = local_addr.port(); - - let mtu = MTU; - - let msg = BcUdp::Discovery(UdpDiscovery { - tid, - payload: UdpXml { - c2d_c: Some(C2dC { - uid: uid.to_string(), - cli: ClientList { port: port as u32 }, - cid: client_id, - mtu, - debug: false, - os: "MAC".to_string(), - }), - ..Default::default() - }, - }); - - let mut buf = vec![]; - msg.serialize(&mut buf)?; - - let mut broadcasts = vec![Ipv4Addr::BROADCAST]; - for iface in get_if_addrs::get_if_addrs()?.iter() { - if let get_if_addrs::IfAddr::V4(ifacev4) = &iface.addr { - if let Some(broadcast) = ifacev4.broadcast.as_ref() { - broadcasts.push(*broadcast); - } - } - } - let ports: [u16; 2] = [2015, 2018]; - let destinations: Vec<(Ipv4Addr, u16)> = broadcasts - .iter() - .flat_map(|&addr| { - ports - .iter() - .map(|&port| (addr, port)) - .collect::>() - }) - .collect(); - debug!("Broadcasting to: {:?}", destinations); - - let (amount_sent, abort) = Self::retrying_send_to_multi(socket, &buf[..], &destinations)?; - assert_eq!(amount_sent, buf.len()); - - let start_time = OffsetDateTime::now_utc(); - let camera_address; - let camera_id; - loop { - if (OffsetDateTime::now_utc() - start_time) <= timeout { - let mut buf = vec![0; mtu as usize]; - if let Ok((bytes_read, socket_addr)) = socket.recv_from(&mut buf) { - match BcUdp::deserialize(&buf[0..bytes_read]) { - Ok(BcUdp::Discovery(UdpDiscovery { - tid: _, - payload: - UdpXml { - d2c_c_r: Some(D2cCr { did, cid, .. }), - .. - }, - })) if cid == client_id => { - abort.abort(); - camera_address = socket_addr; - camera_id = did; - break; - } - Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt), - Err(_) => debug!( - "Udp Discovery got this unexpected binary: {:?}", - &buf[0..bytes_read] - ), - } - } - } else { - abort.abort(); - return Err(Error::Timeout); - } - } - - // Ensure we didn't forget to abort the sending - abort.abort(); - // Actual connection time! - socket.connect(camera_address)?; - - Ok(UdpDiscover { - address: camera_address, - client_id, - camera_id, - mtu, - }) - } - - // This function will contact the p2p relay servers - // - // It will ask each of the servers for details on a specific UID - // - // On success it returns the M2cQr that the p2p relay - // server has about the UID - fn get_register( - uid: &str, - socket: &UdpSocket, - timeout: Duration, - tid: u32, - // client_id: u32, - ) -> Result { - // let local_addr = socket.local_addr()?; - // let local_port = local_addr.port(); - - let mtu = MTU; - - for p2p_relay in P2P_RELAY_HOSTNAMES.iter() { - debug!("Trying register: {}", p2p_relay); - let msg = BcUdp::Discovery(UdpDiscovery { - tid, - payload: UdpXml { - c2m_q: Some(C2mQ { - uid: uid.to_string(), - os: "MAC".to_string(), - }), - ..Default::default() - }, - }); - - let mut buf = vec![]; - msg.serialize(&mut buf)?; - - let (amount_sent, abort) = - Self::retrying_send_to(socket, &buf[..], format!("{}:9999", p2p_relay))?; - assert_eq!(amount_sent, buf.len()); - - let start_time = OffsetDateTime::now_utc(); - loop { - if (OffsetDateTime::now_utc() - start_time) <= timeout { - let mut buf = vec![0; mtu as usize]; - if let Ok((bytes_read, _)) = socket.recv_from(&mut buf) { - match BcUdp::deserialize(&buf[0..bytes_read]) { - Ok(BcUdp::Discovery(UdpDiscovery { - tid: _, - payload: - UdpXml { - m2c_q_r: Some(m2c_q_r), - .. - }, - })) if m2c_q_r.reg.port == 0 || m2c_q_r.reg.ip.is_empty() => { - // This register is empty - // Sometimes a registery will return a M2cQr but it's port and addr - // will be 0 - // In this case we ignore this server and contact another - abort.abort(); - break; - } - Ok(BcUdp::Discovery(UdpDiscovery { - tid: _, - payload: - UdpXml { - m2c_q_r: Some(m2c_q_r), - .. - }, - })) => { - abort.abort(); - return Ok(m2c_q_r); - } - Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt), - Err(_) => debug!( - "Udp Discovery got this unexpected binary: {:?}", - &buf[0..bytes_read] - ), - } - } - } else { - abort.abort(); - // This p2p server didn't have the data we need try another - break; - } - } - } - debug!("All registers tried"); - Err(Error::ConnectionUnavaliable) - } - - // This will start remote discovery against the reolink p2p servers - // - // It has the following stages - // - Contact the p2p register: This will get the register, relay and log server ip addresses - // - Register the client ip address to the register (This can probably be used for NAT holepunching if the camera - // tries to connect to us using this data) - // - The register will return the camera ip/port and SID - // - Connect to the camera using register's provided ip/port and negotiate a cid and did - // - Send a message to the reolink log server that we will connect locally - // - Send a message to the camera that we could connect remotely - fn discover_from_uuid_remote( - socket: &UdpSocket, - uid: &str, - timeout: Duration, - ) -> Result { - let local_addr = socket.local_addr()?; - let local_port = local_addr.port(); - - let mut rng = thread_rng(); - // If tid is too large it will overflow during encrypt so we just use a random u8 - let tid: u32 = (rng.gen::()) as u32; - let mtu = MTU; - let client_id: i32 = rng.gen(); - - let m2c_q_r = Self::get_register(uid, socket, timeout, tid)?; - - debug!("Got this information from the register: {:?}", m2c_q_r); - - let register_address = m2c_q_r.reg; - let relay_address = m2c_q_r.relay; - let log_address = m2c_q_r.log; - // let device_address = m2c_q_r.t; - - let default_local_address = local_ip().expect("There to be a local ip"); - debug!("Register address found: {:?}", register_address); - debug!("Registering this address: {:?}", default_local_address); - - let local_family = if local_addr.ip().is_ipv4() { 4 } else { 6 }; - let msg = BcUdp::Discovery(UdpDiscovery { - tid, - payload: UdpXml { - c2r_c: Some(C2rC { - uid: uid.to_string(), - cli: IpPort { - ip: default_local_address.to_string(), - port: local_port, - }, - relay: relay_address, - cid: client_id, - family: local_family, - debug: false, - os: "MAC".to_string(), - }), - ..Default::default() - }, - }); - - let mut buf = vec![]; - msg.serialize(&mut buf)?; - - let (amount_sent, abort) = Self::retrying_send_to( - socket, - &buf[..], - format!("{}:{}", register_address.ip, register_address.port), - )?; - assert_eq!(amount_sent, buf.len()); - - let device_sid; - let dev_loc; - let start_time = OffsetDateTime::now_utc(); - loop { - if (OffsetDateTime::now_utc() - start_time) <= timeout { - let mut buf = vec![0; mtu as usize]; - if let Ok((bytes_read, _)) = socket.recv_from(&mut buf) { - match BcUdp::deserialize(&buf[0..bytes_read]) { - // Got camera data from register - Ok(BcUdp::Discovery(UdpDiscovery { - tid: _, - payload: - UdpXml { - r2c_t: Some(R2cT { dev, cid, sid, .. }), - .. - }, - })) if cid == client_id && !dev.ip.is_empty() && dev.port > 0 => { - abort.abort(); - // Make a local request to the camera with the dev info - device_sid = sid; - dev_loc = dev; - break; - } - Ok(BcUdp::Discovery(UdpDiscovery { - tid: _, - payload: - UdpXml { - r2c_t: Some(R2cT { cid, .. }), - .. - }, - })) if cid == client_id => { - // Got a reply but the ip/port was empty please wait - } - Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt), - Err(_) => debug!( - "Udp Discovery got this unexpected binary: {:?}", - &buf[0..bytes_read] - ), - } - } - } else { - abort.abort(); - return Err(Error::Timeout); - } - } - - debug!("Register revealed address as SID: {:?}", device_sid); - debug!("Register revealed address as IP: {:?}", dev_loc); - - // ensure abort - abort.abort(); - - let msg = BcUdp::Discovery(UdpDiscovery { - tid, - payload: UdpXml { - c2d_t: Some(C2dT { - sid: device_sid, - cid: client_id, - mtu, - conn: "local".to_string(), - }), - ..Default::default() - }, - }); - - let mut buf = vec![]; - msg.serialize(&mut buf)?; - - let (amount_sent, abort) = - Self::retrying_send_to(socket, &buf[..], format!("{}:{}", dev_loc.ip, dev_loc.port))?; - assert_eq!(amount_sent, buf.len()); - - let device_id; - let start_time = OffsetDateTime::now_utc(); - loop { - if (OffsetDateTime::now_utc() - start_time) <= timeout { - let mut buf = vec![0; mtu as usize]; - if let Ok((bytes_read, _)) = socket.recv_from(&mut buf) { - match BcUdp::deserialize(&buf[0..bytes_read]) { - // Got camera data from camera - Ok(BcUdp::Discovery(UdpDiscovery { - tid: _, - payload: - UdpXml { - d2c_t: Some(D2cT { cid, did, .. }), - .. - }, - })) if cid == client_id => { - device_id = did; - break; - } - // Got camera data from camera CFM - Ok(BcUdp::Discovery(UdpDiscovery { - tid: _, - payload: - UdpXml { - d2c_cfm: Some(D2cCfm { cid, did, .. }), - .. - }, - })) if cid == client_id => { - device_id = did; - break; - } - // Got camera data from camera disc - Ok(BcUdp::Discovery(UdpDiscovery { - tid: _, - payload: - UdpXml { - d2c_disc: Some(D2cDisc { cid, did, .. }), - .. - }, - })) if cid == client_id => { - device_id = did; - break; - } - Ok(smt) => debug!("Udp Discovery got this unexpected BcUdp {:?}", smt), - Err(_) => debug!( - "Udp Discovery got this unexpected binary: {:?}", - &buf[0..bytes_read] - ), - } - } - } else { - abort.abort(); - return Err(Error::Timeout); - } - } - - debug!("Got device ID as: {:?}", device_id); - - // Ensure aborted - abort.abort(); - - // Announce to the log that we will connect locally - let msg = BcUdp::Discovery(UdpDiscovery { - tid, - payload: UdpXml { - c2r_cfm: Some(C2rCfm { - sid: device_sid, - cid: client_id, - did: device_id, - conn: "local".to_string(), - rsp: 0, - }), - ..Default::default() - }, - }); - - let mut buf = vec![]; - msg.serialize(&mut buf)?; - - // Just let this retry to max limit as we don't get a reply - let (amount_sent, _) = Self::retrying_send_to( - socket, - &buf[..], - format!("{}:{}", log_address.ip, log_address.port), - )?; - assert_eq!(amount_sent, buf.len()); - - // Announce a map type connection (I think this means we could connect remotely) - let msg = BcUdp::Discovery(UdpDiscovery { - tid, - payload: UdpXml { - c2d_t: Some(C2dT { - sid: device_sid, - cid: client_id, - mtu, - conn: "map".to_string(), - }), - ..Default::default() - }, - }); - - let mut buf = vec![]; - msg.serialize(&mut buf)?; - - // Just let this retry to max limit as we don't get a reply - let (amount_sent, _) = - Self::retrying_send_to(socket, &buf[..], format!("{}:{}", dev_loc.ip, dev_loc.port))?; - assert_eq!(amount_sent, buf.len()); - - let camera_address: SocketAddr = format!("{}:{}", dev_loc.ip, dev_loc.port) - .to_socket_addrs()? - .next() - .ok_or(Error::ConnectionUnavaliable)?; - - socket.connect(camera_address)?; - - Ok(UdpDiscover { - address: camera_address, - client_id, - camera_id: device_id, - mtu, - }) - } - - pub fn discover_from_uuid( - socket: &UdpSocket, - uid: &str, - timeout: Duration, - allow_remote: bool, - ) -> Result { - match Self::discover_from_uuid_local(socket, uid, timeout) { - Err(Error::Timeout) if allow_remote => { - info!("Trying remote discovery against reolink servers"); - Self::discover_from_uuid_remote(socket, uid, timeout) - } - Ok(result) => Ok(result), - Err(e) => Err(e), - } - } - - pub fn send_client_disconnect(&self, socket: &UdpSocket) { - if let Ok(addr) = socket.peer_addr() { - let mut rng = thread_rng(); - let tid: u32 = (rng.gen::()) as u32; - let bcudp_msg = BcUdp::Discovery(UdpDiscovery { - tid, - payload: UdpXml { - c2d_disc: Some(C2dDisc { - cid: self.client_id, - did: self.camera_id, - }), - ..Default::default() - }, - }); - - let mut buf = vec![]; - let buf = bcudp_msg - .serialize(&mut buf) - .expect("Unable to serliaze udp disconnect"); - let _ = Self::retrying_send_to(socket, buf, addr); - } - } -} diff --git a/crates/core/src/bc_protocol/connection/udpconn/mod.rs b/crates/core/src/bc_protocol/connection/udpconn/mod.rs deleted file mode 100644 index 046ec52..0000000 --- a/crates/core/src/bc_protocol/connection/udpconn/mod.rs +++ /dev/null @@ -1,326 +0,0 @@ -/// The module will handle a udp connection -/// -/// Because of the complexity it is seperated into modules -/// -/// discover module handles the initial handshake which discovers -/// the ip from the uid and reconnects -/// -/// transmit handles the sending and recieving of data through the socket -/// this includes the BcUdp wrapping and the acknoledgements -/// -use super::{Error, Result}; -use crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender}; -use lazy_static::lazy_static; -use log::*; -use rand::{seq::SliceRandom, thread_rng, Rng}; -use std::{ - io::{BufRead, Error as IoError, ErrorKind, Read, Result as IoResult, Write}, - net::{SocketAddr, UdpSocket}, - sync::Arc, - time::Duration, -}; -use time::OffsetDateTime; - -mod aborthandle; -mod discover; -mod transmit; - -use aborthandle::*; -use discover::*; -use transmit::*; - -// How long to set the socket wait -// This is kept short so as to not block the read and write parts -const SOCKET_WAIT_TIME: Duration = Duration::from_millis(50); -// How long to wait between retransmits when no reply is recieved -const WAIT_TIME: Duration = Duration::from_millis(500); -// The maximum data size including header -// -// TODO: Maybe use path mtu discovery (although reolinks seems to just use this constant) -const MTU: u32 = 1030; - -lazy_static! { - static ref P2P_RELAY_HOSTNAMES: [&'static str; 10] = [ - "p2p.reolink.com", - "p2p1.reolink.com", - "p2p2.reolink.com", - "p2p3.reolink.com", - "p2p6.reolink.com", - "p2p7.reolink.com", - "p2p8.reolink.com", - "p2p9.reolink.com", - "p2p14.reolink.com", - "p2p15.reolink.com", - ]; -} - -pub struct UdpSource { - outgoing: Sender>, - incoming: Receiver>, - aborter: AbortHandle, - timeout: Duration, - mtu: u32, - - read_buffer: Buffered, - write_buffer: Buffered, -} - -impl UdpSource { - pub fn new(uid: &str, timeout: Duration) -> Result { - let (outgoing, from_outgoing) = unbounded(); - let (to_incoming, incoming) = unbounded(); - let aborter = AbortHandle::new(); - - Self::start_polling(uid, timeout, &aborter, to_incoming, from_outgoing)?; - - Ok(Self { - outgoing, - incoming, - aborter, - timeout, - mtu: MTU, - - read_buffer: Default::default(), - write_buffer: Default::default(), - }) - } - - fn start_polling( - uid: &str, - timeout: Duration, - aborter: &AbortHandle, - to_incoming: Sender>, - from_outgoing: Receiver>, - ) -> Result<()> { - let socket = Self::get_socket(SOCKET_WAIT_TIME)?; - let allow_remote = true; - let discovery_result = Arc::new(UdpDiscover::discover_from_uuid( - &socket, - uid, - timeout, - allow_remote, - )?); - socket.connect(discovery_result.address)?; - let transmit = Arc::new(UdpTransmit::new()); - - let thread_aborter = aborter.clone(); - let thread_transmit = transmit.clone(); - let thread_socket = socket.try_clone()?; - let thread_discovery_result = discovery_result.clone(); - - // Poll Read - std::thread::spawn(move || { - while !thread_aborter.is_aborted() { - if let Err(err) = thread_transmit.poll_read( - &thread_socket, - &thread_discovery_result, - &to_incoming, - ) { - if !thread_aborter.is_aborted() { - match err { - TransmitError::Disc => { - error!("Camera requested disconnect"); - thread_aborter.abort(); - } - e => { - error!("Error during Udp Transmit: {:?}", e); - thread_aborter.abort(); - } - } - } - } - } - error!("Udp read poll aborted"); - // We send client disconnect here - thread_discovery_result.send_client_disconnect(&thread_socket); - }); - - let thread_aborter = aborter.clone(); - let thread_socket = socket.try_clone()?; - let thread_transmit = transmit; - let thread_discovery_result = discovery_result; - - // Poll Write - std::thread::spawn(move || { - while !thread_aborter.is_aborted() { - if let Err(err) = thread_transmit.poll_write( - &thread_socket, - &thread_discovery_result, - &from_outgoing, - ) { - if !thread_aborter.is_aborted() { - match err { - TransmitError::Disc => { - error!("Camera requested disconnect"); - thread_aborter.abort(); - } - e => { - error!("Error during Udp Transmit: {:?}", e); - thread_aborter.abort(); - } - } - } - } - } - error!("Udp write poll aborted"); - }); - - Ok(()) - } - - pub fn try_clone(&self) -> IoResult { - Ok(Self { - outgoing: self.outgoing.clone(), - incoming: self.incoming.clone(), - aborter: self.aborter.clone(), - timeout: self.timeout, - mtu: self.mtu, - - // New buffer so they don't pollute each other - read_buffer: Default::default(), - write_buffer: Default::default(), - }) - } - - fn stop_polling(&self) { - self.aborter.abort(); - } - - fn get_socket(timeout: Duration) -> Result { - // Select a random port to bind to - let mut ports: Vec = (53500..54000).into_iter().collect(); - let mut rng = thread_rng(); - ports.shuffle(&mut rng); - - let addrs: Vec<_> = ports - .iter() - .map(|&port| SocketAddr::from(([0, 0, 0, 0], port))) - .collect(); - let socket = UdpSocket::bind(&addrs[..])?; - socket.set_read_timeout(Some(timeout))?; - socket.set_write_timeout(Some(timeout))?; - socket.set_nonblocking(false)?; - socket.set_broadcast(true)?; - Ok(socket) - } -} - -// Ensuring polling stops -impl Drop for UdpSource { - fn drop(&mut self) { - self.stop_polling(); - } -} - -#[derive(Default)] -struct Buffered { - buffer: Vec, - consumed: usize, -} - -impl Read for UdpSource { - fn read(&mut self, buf: &mut [u8]) -> IoResult { - if self.aborter.is_aborted() { - return Err(IoError::new( - ErrorKind::ConnectionAborted, - "Connection dropped", - )); - } - let buffer = self.fill_buf()?; - let amt = std::cmp::min(buf.len(), buffer.len()); - - // First check if the amount of bytes we want to read is small: - // `copy_from_slice` will generally expand to a call to `memcpy`, and - // for a single byte the overhead is significant. - if amt == 1 { - buf[0] = buffer[0]; - } else { - buf[..amt].copy_from_slice(&buffer[..amt]); - } - - self.consume(amt); - - Ok(amt) - } -} - -impl BufRead for UdpSource { - fn fill_buf(&mut self) -> IoResult<&[u8]> { - const CLEAR_CONSUMED_AT: usize = 1024; - // This is a trade off between caching too much dead memory - // and calling the drain method too often - if self.read_buffer.consumed > CLEAR_CONSUMED_AT { - let _ = self - .read_buffer - .buffer - .drain(0..self.read_buffer.consumed) - .collect::>(); - self.read_buffer.consumed = 0; - } - if self.read_buffer.buffer.len() <= self.read_buffer.consumed { - // Get next packet of the read queue - match self.incoming.recv_timeout(self.timeout) { - Ok(buf) => self.read_buffer.buffer.extend(buf), - Err(RecvTimeoutError::Timeout) => { - return Err(IoError::new( - ErrorKind::UnexpectedEof, - "Connection timedout when reading from udp channel", - )); - } - Err(RecvTimeoutError::Disconnected) => { - return Err(IoError::new( - ErrorKind::UnexpectedEof, - "Connection dropped when reading from udp channel", - )); - } - } - } - - Ok(&self.read_buffer.buffer.as_slice()[self.read_buffer.consumed..]) - } - - fn consume(&mut self, amt: usize) { - assert!(self.read_buffer.consumed + amt <= self.read_buffer.buffer.len()); - self.read_buffer.consumed += amt; - } -} - -const UDPDATA_HEADER_SIZE: usize = 20; -impl Write for UdpSource { - fn write(&mut self, buf: &[u8]) -> IoResult { - if self.aborter.is_aborted() { - return Err(IoError::new( - std::io::ErrorKind::ConnectionAborted, - "Connection dropped", - )); - } - self.write_buffer.buffer.extend(buf.to_vec()); - if self.write_buffer.buffer.len() > self.mtu as usize - UDPDATA_HEADER_SIZE { - let _ = self.flush(); - } - Ok(buf.len()) - } - - fn flush(&mut self) -> IoResult<()> { - if self.aborter.is_aborted() { - return Err(IoError::new( - std::io::ErrorKind::ConnectionAborted, - "Connection dropped", - )); - } - for chunk in self - .write_buffer - .buffer - .chunks(self.mtu as usize - UDPDATA_HEADER_SIZE) - { - if self.outgoing.send(chunk.to_vec()).is_err() { - return Err(IoError::new( - std::io::ErrorKind::ConnectionAborted, - "Connection dropped", - )); - } - } - self.write_buffer.buffer.clear(); - Ok(()) - } -} diff --git a/crates/core/src/bc_protocol/connection/udpconn/transmit.rs b/crates/core/src/bc_protocol/connection/udpconn/transmit.rs deleted file mode 100644 index 37982f1..0000000 --- a/crates/core/src/bc_protocol/connection/udpconn/transmit.rs +++ /dev/null @@ -1,346 +0,0 @@ -/// This module handles the data from Bc->BcUdp->UdpSocket -/// -/// - It recieves Bc as Vec (after serialize) -/// - It wraps it in BcUdp packet -/// - It retransmits when camera dosen't acknoledge -/// - It sends UdpAck to acknoledge recieved packets -/// -use super::{aborthandle::AbortHandle, discover::UdpDiscover, SOCKET_WAIT_TIME, WAIT_TIME}; -use crate::bcudp::{model::*, xml::*}; -use crossbeam_channel::{Receiver, SendError, Sender, TryRecvError}; -use err_derive::Error; -use log::*; -use std::collections::HashMap; -use std::io::{Error as IoError, ErrorKind}; -use std::net::UdpSocket; -use std::sync::{ - atomic::{AtomicU32, Ordering}, - Mutex, -}; -use time::OffsetDateTime; - -#[derive(Debug, Error)] -pub enum TransmitError { - /// Error raised when IO fails such as when the connection is lost - #[error(display = "Io error")] - Io(#[error(source)] IoError), - - /// Error raised when socket recv IO fails such as when the connection is lost - #[error(display = "Socket Recv error")] - SocketRecv { err: std::io::Error }, - - /// Error raised when socker IO fails such as when the connection is lost - #[error(display = "Socket Send error")] - SocketSend { err: std::io::Error }, - - /// Error raised when camera sends a disconnect request - #[error(display = "Camera Requested Disconnected")] - Disc, - - /// Error raised during deserlization - #[error(display = "BcUdp Deserialization error")] - BcUdpDeserialization(#[error(source)] crate::bcudp::de::Error), - - /// Error raised during serlization - #[error(display = "BcUdp Serialization error")] - BcUdpSerialization(#[error(source)] crate::bcudp::ser::Error), - - /// Error raised during send - #[error(display = "BcUdp Send to Channel Failed")] - BcUdpSend(#[error(source)] SendError>), - - /// Error raised during recv - #[error(display = "BcUdp Recv from Channel Failed")] - BcUdpRecv(#[error(source)] TryRecvError), -} - -type Result = std::result::Result; - -pub struct UdpTransmit { - client_recieved: ClientRecieved, - client_sent: ClientSent, -} - -#[derive(Default)] -struct ClientSent { - buffer: Mutex>>, - last_sent: Mutex>, -} - -impl ClientSent { - fn get_next_packet_id(&self) -> u32 { - let mut lock = self.last_sent.lock().unwrap(); - if let Some(current) = lock.as_ref() { - *lock = Some(current + 1); - } else { - *lock = Some(0); - } - lock.unwrap() - } - - fn register_payload(&self, packet_id: u32, payload: Vec) { - self.buffer - .lock() - .unwrap() - .entry(packet_id) - .or_insert(payload); - } - - // fn acknoledge_to(&self, packet_id: u32) { - // self.buffer.lock().unwrap().retain(|&k, _| k > packet_id); - // } - // - // fn acknoledge_these(&self, packet_ids: Vec) { - // let locked = self.buffer.lock().unwrap(); - // for packet_id in packet_ids { - // let _ = locked.remove(&packet_id); - // } - // } - - // This function translates the `UdpAck` payload directly - fn acknoledge_from_ack_data(&self, start: u32, payload: Vec) { - let mut locked = self.buffer.lock().unwrap(); - locked.retain(|&k, _| k > start); - - for (idx, &value) in payload.iter().enumerate() { - let packet_id = (start + 1) + idx as u32; - if value > 0 { - locked.remove(&packet_id); - } - } - } - - fn lock(&self) -> ClientSentLocked { - ClientSentLocked::new(self) - } -} - -struct ClientSentLocked<'a> { - locked: std::sync::MutexGuard<'a, HashMap>>, -} - -impl<'a> ClientSentLocked<'a> { - fn new(source: &'a ClientSent) -> Self { - let locked = source.buffer.lock().unwrap(); - Self { locked } - } - - fn needs_resend(&self) -> ResendIter { - ResendIter { - iter: self.locked.iter(), - } - } -} - -struct ResendIter<'a> { - iter: std::collections::hash_map::Iter<'a, u32, Vec>, -} - -impl<'a> std::iter::Iterator for ResendIter<'a> { - type Item = (u32, &'a Vec); - - fn next(&mut self) -> Option { - let (k, v) = self.iter.next()?; - Some((*k, v)) - } -} - -#[derive(Default)] -struct ClientRecieved { - buffer: Mutex>>, - consumed: Mutex>, -} - -impl ClientRecieved { - // Consume all contigious packets - fn consume(&self) -> Vec> { - let mut locked = self.buffer.lock().unwrap(); - let mut results = vec![]; - if locked.keys().max().is_some() { - // Just check there is some data - let mut packet_id_loc = self.consumed.lock().unwrap(); - let mut next_packet_id = packet_id_loc.map(|v| v + 1).unwrap_or(0); - while let Some(payload) = locked.remove(&next_packet_id) { - *packet_id_loc = Some(next_packet_id); - results.push(payload); - next_packet_id = packet_id_loc.unwrap() + 1; - } - } - results - } - - // Revieve a new packet only update if entry is new - fn receive(&self, packet_id: u32, payload: Vec) { - let mut locked = self.buffer.lock().unwrap(); - let _ = locked.entry(packet_id).or_insert(payload); - } - - // This gets the needed payload to form a `UdpAck` packet - fn get_needed_packets(&self) -> Option<(u32, Vec)> { - let locked = self.buffer.lock().unwrap(); - let packet_id_loc = self.consumed.lock().unwrap(); - - // If this is none we have recieved no packets so we cannot - // ack anything anyways. Just return None - let mut start = packet_id_loc.as_ref().copied()?; - - // Find the last contigous packet - while locked.contains_key(&(start + 1)) { - start += 1; - } - - // Find last packet in buffer - let vec = if let Some(end) = locked.keys().max() { - let mut vec = vec![]; - // From last contiguous packet to last recieved packet - // create a payload of `00` (unreceived) and `01` (received) - // that can be used to form the `UdpAck` packet - for i in (start + 1)..(end + 1) { - if locked.contains_key(&i) { - vec.push(1) - } else { - vec.push(0) - } - } - vec - } else { - vec![] - }; - - Some((start, vec)) - } -} - -impl UdpTransmit { - pub fn new() -> Self { - Self { - client_recieved: Default::default(), - client_sent: Default::default(), - } - } - - pub fn poll_read( - &self, - socket: &UdpSocket, - discovery_result: &UdpDiscover, - send_to_incoming: &Sender>, - ) -> Result<()> { - // Handle recv from `socket` and send to `send_to_incoming` - let mut buf = vec![0; discovery_result.mtu as usize]; - match socket.recv(&mut buf) { - Ok(amount_recv) => { - let bcudp_msg = BcUdp::deserialize(&buf[0..amount_recv])?; - match bcudp_msg { - BcUdp::Discovery(UdpDiscovery { - payload: - UdpXml { - d2c_disc: Some(D2cDisc { cid, did }), - .. - }, - .. - }) if cid == discovery_result.client_id - && did == discovery_result.camera_id => - { - // Camera sent a disconnect - // Reply with C2D_Disc and end - discovery_result.send_client_disconnect(socket); - return Err(TransmitError::Disc); - } - BcUdp::Discovery(packet) => { - debug!("Got unexpected discovery packet: {:?}", packet) - } - BcUdp::Ack(UdpAck { - connection_id, - packet_id, - payload, - }) if connection_id == discovery_result.client_id => { - self.client_sent - .acknoledge_from_ack_data(packet_id, payload); - } - BcUdp::Ack(UdpAck { .. }) => { - debug!("Got a UdpAck for another client id"); - } - BcUdp::Data(UdpData { - connection_id: cid, - packet_id, - payload, - }) if cid == discovery_result.client_id => { - trace!("Recieving UdpData packet {}", packet_id); - self.client_recieved.receive(packet_id, payload); - - for next_payload in self.client_recieved.consume().drain(..) { - send_to_incoming.send(next_payload)?; - } - } - BcUdp::Data(UdpData { .. }) => { - debug!("Got UdpData for another client id"); - } - } - } - Err(e) if e.kind() == ErrorKind::WouldBlock || e.kind() == ErrorKind::TimedOut => { - // Just a timeout - } - Err(e) => { - // Should stop - return Err(TransmitError::SocketRecv { err: e }); - } - } - Ok(()) - } - - #[allow(clippy::significant_drop_in_scrutinee)] - pub fn poll_write( - &self, - socket: &UdpSocket, - discovery_result: &UdpDiscover, - get_from_outgoing: &Receiver>, - ) -> Result<()> { - // Handle Retransmit - for (_, payload) in self.client_sent.lock().needs_resend() { - socket - .send(payload) - .map_err(|e| TransmitError::SocketSend { err: e })?; - std::thread::sleep(SOCKET_WAIT_TIME); - } - - // Handle recv from `send_to_incoming` and send to `socket` - match get_from_outgoing.try_recv() { - Ok(payload) => { - let packet_id = self.client_sent.get_next_packet_id(); - let bcudp_msg = BcUdp::Data(UdpData { - connection_id: discovery_result.camera_id, - packet_id, - payload, - }); - - let mut buf = vec![]; - bcudp_msg.serialize(&mut buf)?; - socket - .send(&buf) - .map_err(|e| TransmitError::SocketSend { err: e })?; - self.client_sent.register_payload(packet_id, buf); - } - Err(TryRecvError::Empty) => {} - Err(e) => return Err(e.into()), - } - - // Handle acknoledgment - let client_wants = self.client_recieved.get_needed_packets(); - if let Some((client_wants, also_wants)) = client_wants { - let bcack_msg = BcUdp::Ack(UdpAck { - connection_id: discovery_result.camera_id, - packet_id: client_wants, - payload: also_wants, - }); - - let mut buf = vec![]; - bcack_msg.serialize(&mut buf)?; - socket - .send(&buf) - .map_err(|e| TransmitError::SocketSend { err: e })?; - } - - std::thread::sleep(SOCKET_WAIT_TIME); - Ok(()) - } -} diff --git a/crates/core/src/bc_protocol/connection/udpsource.rs b/crates/core/src/bc_protocol/connection/udpsource.rs index e4c603b..9139f7a 100644 --- a/crates/core/src/bc_protocol/connection/udpsource.rs +++ b/crates/core/src/bc_protocol/connection/udpsource.rs @@ -3,7 +3,7 @@ use crate::bc::codex::BcCodex; use crate::bc::model::*; use crate::bcudp::codex::BcUdpCodex; use crate::bcudp::model::*; -use crate::{Error, Result}; +use crate::{Credentials, Error, Result}; use delegate::delegate; use futures::{ sink::{Sink, SinkExt}, @@ -36,51 +36,62 @@ pub(crate) struct UdpSource { impl UdpSource { #[allow(unused)] - pub(crate) async fn new(addr: SocketAddr, client_id: i32, camera_id: i32) -> Result { + pub(crate) async fn new, U: Into>( + addr: SocketAddr, + client_id: i32, + camera_id: i32, + username: T, + password: Option, + ) -> Result { let stream = connect().await?; - Self::new_from_socket(stream, addr, client_id, camera_id).await + Self::new_from_socket(stream, addr, client_id, camera_id, username, password).await } - pub(crate) async fn new_from_discovery(discovery: DiscoveryResult) -> Result { + pub(crate) async fn new_from_discovery, U: Into>( + discovery: DiscoveryResult, + username: T, + password: Option, + ) -> Result { Self::new_from_socket( discovery.socket, discovery.addr, discovery.client_id, discovery.camera_id, + username, + password, ) .await } - pub(crate) async fn new_from_socket( + pub(crate) async fn new_from_socket, U: Into>( stream: UdpSocket, addr: SocketAddr, client_id: i32, camera_id: i32, + username: T, + password: Option, ) -> Result { let bcudp_source = BcUdpSource::new_from_socket(stream, addr).await?; let payload_source = bcudp_source.into_payload_source(client_id, camera_id); let async_read = payload_source.into_async_read().compat(); - let framed = Framed::new(async_read, BcCodex::new()); + let framed = Framed::new( + async_read, + BcCodex::new(Credentials::new(username, password)), + ); Ok(Self { inner: framed }) } - pub(crate) async fn send(&mut self, bc: Bc) -> Result<()> { - self.inner.send(bc).await - } - pub(crate) async fn recv(&mut self) -> Result { - loop { - if let Some(result) = self.inner.next().await { - return result; - } - } - } - pub(crate) fn get_encrypted(&self) -> &EncryptionProtocol { - self.inner.codec().get_encrypted() - } - pub(crate) fn set_encrypted(&mut self, protocol: EncryptionProtocol) { - self.inner.codec_mut().set_encrypted(protocol) - } + // pub(crate) async fn send(&mut self, bc: Bc) -> Result<()> { + // self.inner.send(bc).await + // } + // pub(crate) async fn recv(&mut self) -> Result { + // loop { + // if let Some(result) = self.inner.next().await { + // return result; + // } + // } + // } } impl Stream for UdpSource { @@ -315,7 +326,7 @@ impl Stream for UdpPayloadSource { BcUdp::Ack(ack @ UdpAck { connection_id, .. }), addr, )))) if connection_id == this.client_id && addr == camera_addr => { - trace!("UDPSource.RecievedPacker: Ack"); + trace!("UDPSource.RecievedPacket: Ack"); this.handle_ack(ack); for (_, resend) in this.sent.iter() { diff --git a/crates/core/src/bc_protocol/errors.rs b/crates/core/src/bc_protocol/errors.rs index 6ceebb0..7d814c0 100644 --- a/crates/core/src/bc_protocol/errors.rs +++ b/crates/core/src/bc_protocol/errors.rs @@ -100,6 +100,24 @@ pub enum Error { #[error(display = "Could not parse as IP")] AddrParseError(#[error(source)] std::net::AddrParseError), + /// Raised when a relay connection is not possible + /// usually happens if the camera has not contacted reolink yet + #[error(display = "Cannot perform relay connection with this camera")] + NoDmap, + + /// Raised when a dev connection is not possible + /// usually happens if the camera has not contacted reolink yet + #[error(display = "Cannot perform lookup with this camera against reolink servers")] + NoDev, + + /// Raised when cannot determine local IP address + #[error(display = "Local IP address is unknown: {}", _0)] + LocalIpError(#[error(source)] std::sync::Arc), + + /// Raised when a discovery fails to be accepted by the register + #[error(display = "Register refuses to accept us")] + RegisterError, + /// Raised when the stream is not enough to complete a message #[error(display = "Nom Parsing incomplete: {}", _0)] NomIncomplete(usize), @@ -129,6 +147,12 @@ impl From for Error { } } +impl From for Error { + fn from(k: local_ip_address::Error) -> Self { + Error::LocalIpError(std::sync::Arc::new(k)) + } +} + impl<'a> From>> for Error { fn from(k: nom::Err>) -> Self { match k { diff --git a/crates/core/src/bc_protocol/login.rs b/crates/core/src/bc_protocol/login.rs index fb884f8..13aeda7 100644 --- a/crates/core/src/bc_protocol/login.rs +++ b/crates/core/src/bc_protocol/login.rs @@ -1,4 +1,4 @@ -use super::{make_aes_key, md5_string, BcCamera, Error, Result, Truncate, ZeroLast}; +use super::{md5_string, BcCamera, Error, Result, Truncate, ZeroLast}; use crate::bc::{model::*, xml::*}; use std::sync::atomic::Ordering; @@ -6,10 +6,11 @@ impl BcCamera { /// Login to the camera. /// /// This should be called before most other commands - pub async fn login(&self, username: &str, password: Option<&str>) -> Result { + pub async fn login(&self) -> Result { let device_info; // This { is here due to the connection and set_credentials both requiring a mutable borrow { + let credentials = self.get_credentials(); let connection = self.get_connection(); let msg_num = self.new_message_num(); let mut sub_login = connection.subscribe(msg_num).await?; @@ -23,8 +24,10 @@ 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(username, ZeroLast); - let md5_password = password + 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()); @@ -70,12 +73,12 @@ impl BcCamera { // In the modern login flow, the username/password are concat'd with the server's nonce // string, then MD5'd, then the hex of this MD5 is sent as the password. This nonce // prevents replay attacks if the server were to require modern flow, but not rainbow table - // attacks (since the plain user/password MD5s have already been sent). The upshot is that + // attacks (since the plain user/password MD5s have already been sent). The upshot is that // you should use a very strong random password that is not found in a rainbow table and // not feasibly crackable with John the Ripper. - let modern_password = password.unwrap_or(""); - let concat_username = format!("{}{}", username, nonce); + let modern_password = credentials.password.clone().unwrap_or_default(); + let concat_username = format!("{}{}", credentials.username, nonce); let concat_password = format!("{}{}", modern_password, nonce); let md5_username = md5_string(&concat_username, Truncate); let md5_password = md5_string(&concat_password, Truncate); @@ -131,18 +134,7 @@ impl BcCamera { }) } } - - if let EncryptionProtocol::Aes(_) = connection.get_encrypted().await { - // We setup the data for the AES key now - // as all subsequent communications will use it - let passwd = password.unwrap_or(""); - let full_key = make_aes_key(nonce, passwd); - connection - .set_encrypted(EncryptionProtocol::Aes(Some(full_key))) - .await; - } } - self.set_credentials(username.to_string(), password.map(|s| s.to_string())); Ok(device_info) } } diff --git a/crates/core/src/bc_protocol/logout.rs b/crates/core/src/bc_protocol/logout.rs index 2be9fc5..2f80f8a 100644 --- a/crates/core/src/bc_protocol/logout.rs +++ b/crates/core/src/bc_protocol/logout.rs @@ -6,39 +6,37 @@ impl BcCamera { /// Logout from the camera pub async fn logout(&self) -> Result<()> { if self.logged_in.load(Ordering::Relaxed) { - if let Some(credentials) = self.get_credentials() { - let connection = self.get_connection(); - let msg_num = self.new_message_num(); - let sub_logout = connection.subscribe(msg_num).await?; + let credentials = self.get_credentials(); + let connection = self.get_connection(); + let msg_num = self.new_message_num(); + let sub_logout = connection.subscribe(msg_num).await?; - let username = credentials.username.clone(); - let password = credentials.password.as_ref().cloned().unwrap_or_default(); + let username = credentials.username.clone(); + let password = credentials.password.as_ref().cloned().unwrap_or_default(); - let modern_logout = Bc::new_from_xml( - BcMeta { - msg_id: MSG_ID_LOGOUT, - channel_id: self.channel_id, - msg_num, - stream_type: 0, - response_code: 0, - class: 0x6414, - }, - BcXml { - login_user: Some(LoginUser { - version: xml_ver(), - user_name: username, - password, - user_ver: 1, - }), - login_net: Some(LoginNet::default()), - ..Default::default() - }, - ); + let modern_logout = Bc::new_from_xml( + BcMeta { + msg_id: MSG_ID_LOGOUT, + channel_id: self.channel_id, + msg_num, + stream_type: 0, + response_code: 0, + class: 0x6414, + }, + BcXml { + login_user: Some(LoginUser { + version: xml_ver(), + user_name: username, + password, + user_ver: 1, + }), + login_net: Some(LoginNet::default()), + ..Default::default() + }, + ); - sub_logout.send(modern_logout).await?; - } + sub_logout.send(modern_logout).await?; } - self.clear_credentials(); self.logged_in.store(false, Ordering::Relaxed); Ok(()) } diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index b048e8e..e94f27c 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -4,6 +4,7 @@ use crate::{ bcmedia::model::*, }; use futures::stream::StreamExt; +use log::*; use std::sync::{ atomic::{AtomicBool, Ordering}, Arc, @@ -13,7 +14,7 @@ use tokio::task::{self, JoinHandle}; /// The stream names supported by BC #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub enum Stream { +pub enum StreamKind { /// This is the HD stream Main, /// This is the SD stream @@ -74,20 +75,30 @@ impl BcCamera { /// To pull frames from the camera's buffer use `recv_data` on the returned object /// /// The buffer_size represents number of compete messages so 1 would be one complete message - /// which may be a single audio frame or a whole video key frame + /// which may be a single audio frame or a whole video key frame. If 0 a default of 100 is used /// - pub async fn start_video(&self, stream: Stream, buffer_size: usize) -> Result { + pub async fn start_video( + &self, + stream: StreamKind, + mut buffer_size: usize, + ) -> Result { + info!("start_video"); let connection = self.get_connection(); let msg_num = self.new_message_num(); let abort_handle = Arc::new(AtomicBool::new(false)); let abort_handle_thread = abort_handle.clone(); + if buffer_size == 0 { + buffer_size = 100; + } let (tx, rx) = channel(buffer_size); let channel_id = self.channel_id; let handle = task::spawn(async move { + info!("sub_video"); let mut sub_video = connection.subscribe(msg_num).await?; + info!("post sub_video"); // On an E1 and swann cameras: // - mainStream always has a value of 0 @@ -98,9 +109,9 @@ impl BcCamera { // - subStream is 0 // - externStream is 0 let stream_code = match stream { - Stream::Main => 0, - Stream::Sub => 1, - Stream::Extern => 0, + StreamKind::Main => 0, + StreamKind::Sub => 1, + StreamKind::Extern => 0, }; // Theses are the numbers used with the offical client @@ -113,15 +124,15 @@ impl BcCamera { // - subStream is 256 // - externStram is 1024 let handle = match stream { - Stream::Main => 0, - Stream::Sub => 256, - Stream::Extern => 1024, + StreamKind::Main => 0, + StreamKind::Sub => 256, + StreamKind::Extern => 1024, }; let stream_name = match stream { - Stream::Main => "mainStream", - Stream::Sub => "subStream", - Stream::Extern => "externStream", + StreamKind::Main => "mainStream", + StreamKind::Sub => "subStream", + StreamKind::Extern => "externStream", } .to_string(); @@ -145,7 +156,9 @@ impl BcCamera { }, ); + info!("Sending stream start"); sub_video.send(start_video).await?; + info!("Sent stream start"); let msg = sub_video.recv().await?; if let BcMeta { @@ -207,7 +220,7 @@ impl BcCamera { } /// Stop a camera from sending more stream data. - pub async fn stop_video(&self, stream: Stream) -> Result<()> { + pub async fn stop_video(&self, stream: StreamKind) -> Result<()> { let connection = self.get_connection(); let msg_num = self.new_message_num(); let mut sub_video = connection.subscribe(msg_num).await?; @@ -221,9 +234,9 @@ impl BcCamera { // - subStream is 0 // - externStream is 0 let stream_code = match stream { - Stream::Main => 0, - Stream::Sub => 1, - Stream::Extern => 0, + StreamKind::Main => 0, + StreamKind::Sub => 1, + StreamKind::Extern => 0, }; // Theses are the numbers used with the offical client @@ -236,9 +249,9 @@ impl BcCamera { // - subStream is 256 // - externStram is 1024 let handle = match stream { - Stream::Main => 0, - Stream::Sub => 256, - Stream::Extern => 1024, + StreamKind::Main => 0, + StreamKind::Sub => 256, + StreamKind::Extern => 1024, }; let stop_video = Bc::new_from_xml( diff --git a/crates/core/src/bcudp/xml.rs b/crates/core/src/bcudp/xml.rs index 248b540..3a80ec7 100644 --- a/crates/core/src/bcudp/xml.rs +++ b/crates/core/src/bcudp/xml.rs @@ -51,6 +51,9 @@ pub struct UdpXml { /// C2R_CFM xml client to register CFM #[yaserde(rename = "C2R_CFM")] pub c2r_cfm: Option, + /// C2D_A xml client to device accept + #[yaserde(rename = "C2D_A")] + pub c2d_a: Option, } impl UdpXml { @@ -319,3 +322,21 @@ pub struct C2rCfm { /// The camera connection ID pub did: i32, } + +/// C2D_A xml +/// +/// Client to device accept. +/// Sent it reply to a D2C_T +#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)] +pub struct C2dA { + /// The camera SID + pub sid: u32, + /// Type of connection observed values are `"local"` + pub conn: String, + /// The client connection ID + pub cid: i32, + /// The camera connection ID + pub did: i32, + /// Maximum size in bytes of a transmission + pub mtu: u32, +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 04d4a51..094db13 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -41,3 +41,53 @@ pub use bc_protocol::Error; pub(crate) use bc_protocol::Result; pub(crate) type NomErrorType<'a> = nom::error::VerboseError<&'a [u8]>; + +// Used for caching the credentials +#[derive(Clone)] +pub(crate) struct Credentials { + username: String, + password: Option, +} + +impl Default for Credentials { + /// Default credentials for some reolink cameras + fn default() -> Self { + Self { + username: "admin".to_string(), + password: Some("123456".to_string()), + } + } +} + +impl std::fmt::Debug for Credentials { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_map() + .entry(&"username", &self.username) + .entry(&"password", &"******") + .finish() + } +} + +use std::convert::TryInto; +impl Credentials { + pub(crate) fn new, U: Into>(username: T, password: Option) -> Self { + Self { + username: username.into(), + password: password.map(|t| t.into()), + } + } + + /// This is a convience function to make an AES key from the login password and the NONCE + /// negotiated during login + pub(crate) fn make_aeskey>(&self, nonce: T) -> [u8; 16] { + let key_phrase = format!( + "{}-{}", + nonce.as_ref(), + self.password.clone().unwrap_or_default() + ); + let key_phrase_hash = format!("{:X}\0", md5::compute(key_phrase)) + .to_uppercase() + .into_bytes(); + key_phrase_hash[0..16].try_into().unwrap() + } +} diff --git a/src/mqtt/event_cam.rs b/src/mqtt/event_cam.rs index 5c35591..cda7f65 100644 --- a/src/mqtt/event_cam.rs +++ b/src/mqtt/event_cam.rs @@ -151,7 +151,11 @@ impl EventCam { camera_config.name, camera_addr ); let camera = camera_addr - .connect_camera(camera_config.channel_id) + .connect_camera( + camera_config.channel_id, + &camera_config.username, + camera_config.password.as_ref(), + ) .await .with_context(|| { format!( @@ -162,7 +166,7 @@ impl EventCam { info!("{}: Logging in", camera_config.name); camera - .login(&camera_config.username, camera_config.password.as_deref()) + .login() .await .context("Failed to login to the camera")?; info!("{}: Connected and logged in", camera_config.name); diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index 5092b12..18d4141 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -218,6 +218,8 @@ impl GstOutputs { // - In the case of PausedSources::None we just pass through without encoding // - In the case of all other pause sources we need to recencode in order // to get smooth streams when the stream is paused/resumed + info!("self.video_format: {:?}", self.video_format); + info!("self.audio_format: {:?}", self.audio_format); let launch_vid_select = match self.when_paused { PausedSources::None => match self.video_format { Some(StreamFormat::H264) => "! rtph264pay name=pay0", diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 7b41c4c..54f6835 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -156,11 +156,13 @@ async fn camera_main( let _ = camera.manage().await; + info!("Init Stream"); camera .stream() .await .with_context(|| format!("{}: Could not start stream", config.name)) .map_err(CameraFailureKind::Retry)?; + info!("Inited Stream"); let backoff = Backoff::new(); let mut motion = if config.pause.on_motion { @@ -182,9 +184,9 @@ async fn camera_main( loop { match camera.get_state() { StateInfo::Streaming => { - let can_pause = camera.can_pause(); + let can_pause = camera.can_pause().await; if config.pause.on_disconnect { - if let Some(false) = camera.client_connected() { + if let Some(false) = camera.client_connected().await { if can_pause { info!("Pause on disconnect"); camera.pause().await.map_err(CameraFailureKind::Retry)?; @@ -231,7 +233,7 @@ async fn camera_main( } StateInfo::Paused => { if config.pause.on_disconnect { - if let Some(true) = camera.client_connected() { + if let Some(true) = camera.client_connected().await { info!("Resume on disconnect"); camera.stream().await.map_err(CameraFailureKind::Retry)?; } diff --git a/src/rtsp/states/loggedin.rs b/src/rtsp/states/loggedin.rs index 49f0318..6bb1532 100644 --- a/src/rtsp/states/loggedin.rs +++ b/src/rtsp/states/loggedin.rs @@ -15,10 +15,7 @@ pub(crate) struct LoggedIn {} #[async_trait] impl CameraState for LoggedIn { async fn setup(&mut self, shared: &Shared) -> Result<(), anyhow::Error> { - shared - .camera - .login(&shared.username, shared.password.as_deref()) - .await?; + shared.camera.login().await?; Ok(()) } diff --git a/src/rtsp/states/mod.rs b/src/rtsp/states/mod.rs index 48d2bde..186944b 100644 --- a/src/rtsp/states/mod.rs +++ b/src/rtsp/states/mod.rs @@ -6,7 +6,7 @@ use log::*; use std::collections::HashSet; use std::sync::Arc; -use neolink_core::bc_protocol::{MotionData, Stream}; +use neolink_core::bc_protocol::{MotionData, StreamKind as Stream}; use crate::config::{CameraConfig, UserConfig}; use crate::rtsp::gst::RtspServer; @@ -62,7 +62,11 @@ impl RtspCamera { info!("{}: Connecting to camera at {}", config.name, camera_addr); let camera = camera_addr - .connect_camera(config.channel_id) + .connect_camera( + config.channel_id, + &config.username, + config.password.as_ref(), + ) .await .with_context(|| { format!( @@ -149,16 +153,16 @@ impl RtspCamera { State::Streaming(ref mut connected) => { info!("{}: Logging in", &self.shared.name); connected.tear_down(&self.shared).await?; - let mut state: LoggedIn = Default::default(); - state.setup(&self.shared).await?; + let state: LoggedIn = Default::default(); + // state.setup(&self.shared).await?; self.state = State::LoggedIn(state); info!("{}: Successfully stopped straming", &self.shared.name); } State::Paused(ref mut connected) => { info!("{}: Logging in", &self.shared.name); connected.tear_down(&self.shared).await?; - let mut state: LoggedIn = Default::default(); - state.setup(&self.shared).await?; + let state: LoggedIn = Default::default(); + // state.setup(&self.shared).await?; self.state = State::LoggedIn(state); info!( "{}: Successfully stopped paused streaming", @@ -182,7 +186,7 @@ impl RtspCamera { } State::Paused(ref mut paused) => { info!("{}: Resuming stream", &self.shared.name); - let outputs = paused.take_outputs()?; + let outputs = paused.take_outputs().await?; paused.tear_down(&self.shared).await?; let mut state: Streaming = Default::default(); @@ -193,9 +197,9 @@ impl RtspCamera { self.state = State::Streaming(state); info!("{}: Successfully resumed streaming", &self.shared.name); } - State::LoggedIn(ref mut loggedin) => { + State::LoggedIn(ref mut _loggedin) => { info!("{}: Starting stream", &self.shared.name); - loggedin.tear_down(&self.shared).await?; + // loggedin.tear_down(&self.shared).await?; let mut state: Streaming = Default::default(); state.setup(&self.shared).await?; self.state = State::Streaming(state); @@ -236,10 +240,10 @@ impl RtspCamera { Ok(()) } - pub(crate) fn client_connected(&self) -> Option { + pub(crate) async fn client_connected(&self) -> Option { match &self.state { - State::Streaming(state) => Some(state.client_connected()), - State::Paused(state) => Some(state.client_connected()), + State::Streaming(state) => Some(state.client_connected().await), + State::Paused(state) => Some(state.client_connected().await), _ => None, } } @@ -260,9 +264,9 @@ impl RtspCamera { } } - pub(crate) fn can_pause(&self) -> bool { + pub(crate) async fn can_pause(&self) -> bool { match &self.state { - State::Streaming(state) => state.can_pause(), + State::Streaming(state) => state.can_pause().await, _ => false, } } diff --git a/src/rtsp/states/paused.rs b/src/rtsp/states/paused.rs index dad04fb..fbe49d3 100644 --- a/src/rtsp/states/paused.rs +++ b/src/rtsp/states/paused.rs @@ -8,10 +8,13 @@ use async_trait::async_trait; use crossbeam::utils::Backoff; use log::*; use std::collections::HashMap; -use std::sync::{Arc, Mutex}; -use std::thread::{self, JoinHandle}; +use std::sync::Arc; +use tokio::{ + sync::Mutex, + task::{self, JoinHandle}, +}; -use neolink_core::bc_protocol::Stream; +use neolink_core::bc_protocol::StreamKind as Stream; use super::{CameraState, Shared}; @@ -77,7 +80,7 @@ impl CameraState for Paused { // Lock and setup output { - let mut locked_output = output.lock().unwrap(); + let mut locked_output = output.lock().await; locked_output.set_input_source(InputMode::Paused)?; } @@ -89,10 +92,10 @@ impl CameraState for Paused { let arc_abort_handle = abort_handle.clone(); let output_thread = output.clone(); - let handle = thread::spawn(move || { + let handle = task::spawn(async move { let backoff = Backoff::new(); while arc_abort_handle.is_live() { - let mut locked_output = output_thread.lock().unwrap(); + let mut locked_output = output_thread.lock().await; locked_output.write_last_iframe()?; backoff.spin(); } @@ -118,7 +121,7 @@ impl CameraState for Paused { for (stream, handle) in self.handles.drain() { info!("{}: Stopping paused stream {:?}", &shared.name, stream); - match handle.join() { + match handle.await { Ok(Err(e)) => return Err(e), Err(_) => return Err(anyhow!("Panicked while streaming {:?}", stream)), Ok(Ok(_)) => {} @@ -133,13 +136,10 @@ impl CameraState for Paused { impl Drop for Paused { fn drop(&mut self) { self.abort_handle.abort(); - - for (stream, handle) in self.handles.drain() { - info!("During_drop: Aborting paused stream {:?}", stream); - if let Ok(Err(e)) = handle.join() { - warn!("During drop: {:?} did not stop cleanly: {:?}", stream, e); - } else { - warn!("During drop: Panicked while streaming: {:?}", stream); + let backoff = Backoff::new(); + for (_, handle) in self.handles.drain() { + while !handle.is_finished() { + backoff.spin(); } } } @@ -150,10 +150,10 @@ impl Paused { self.handles.iter().all(|(_, h)| !h.is_finished()) && self.abort_handle.is_live() } - pub(crate) fn take_outputs(&mut self) -> Result> { + pub(crate) async fn take_outputs(&mut self) -> Result> { self.abort_handle.abort(); for (stream, handle) in self.handles.drain() { - match handle.join() { + match handle.await { Ok(Err(e)) => return Err(e), Err(_) => return Err(anyhow!("Panicked while streaming {:?}", stream)), Ok(Ok(_)) => {} @@ -163,7 +163,7 @@ impl Paused { for (stream, arc_mutex_output) in self.outputs.drain() { let mutex_output = Arc::try_unwrap(arc_mutex_output).map_err(|_| anyhow!("Failed to unwrap ARC"))?; - let output = mutex_output.into_inner()?; + let output = mutex_output.into_inner(); result.insert(stream, output); } Ok(result) @@ -177,9 +177,12 @@ impl Paused { Ok(()) } - pub(crate) fn client_connected(&self) -> bool { - self.outputs - .iter() - .any(|(_, output)| output.lock().unwrap().is_connected()) + pub(crate) async fn client_connected(&self) -> bool { + for (_, output) in self.outputs.iter() { + if output.lock().await.is_connected() { + return true; + } + } + false } } diff --git a/src/rtsp/states/shared.rs b/src/rtsp/states/shared.rs index 6859d63..7caf32f 100644 --- a/src/rtsp/states/shared.rs +++ b/src/rtsp/states/shared.rs @@ -7,7 +7,7 @@ use async_trait::async_trait; use std::collections::HashSet; use std::sync::Arc; -use neolink_core::bc_protocol::{BcCamera, Stream}; +use neolink_core::bc_protocol::{BcCamera, StreamKind as Stream}; use crate::config::PauseConfig; use crate::rtsp::gst::RtspServer; diff --git a/src/rtsp/states/streaming.rs b/src/rtsp/states/streaming.rs index d25834e..5e61cc0 100644 --- a/src/rtsp/states/streaming.rs +++ b/src/rtsp/states/streaming.rs @@ -7,10 +7,13 @@ use async_trait::async_trait; use crossbeam::utils::Backoff; use log::*; use std::collections::HashMap; -use std::sync::{Arc, Mutex}; -use tokio::task::{self, JoinHandle}; +use std::sync::Arc; +use tokio::{ + sync::Mutex, + task::{self, JoinHandle}, +}; -use neolink_core::bc_protocol::Stream; +use neolink_core::bc_protocol::StreamKind as Stream; use super::{CameraState, Shared}; @@ -77,7 +80,7 @@ impl CameraState for Streaming { // Lock and setup output { - let mut locked_output = output.lock().unwrap(); + let mut locked_output = output.lock().await; locked_output.set_input_source(InputMode::Live)?; } @@ -93,11 +96,12 @@ impl CameraState for Streaming { let stream_thead = *stream; let handle = task::spawn(async move { let backoff = Backoff::new(); + info!("Pre start_video"); let mut stream_data = arc_camera.start_video(stream_thead, 0).await?; - + info!("Post start_video"); while arc_abort_handle.is_live() { let mut data = stream_data.get_data()?; - let mut locked_output = output_thread.lock().unwrap(); + let mut locked_output = output_thread.lock().await; for datum in data.drain(..) { locked_output.stream_recv(datum?)?; } @@ -166,7 +170,7 @@ impl Streaming { for (stream, arc_mutex_output) in self.outputs.drain() { let mutex_output = Arc::try_unwrap(arc_mutex_output).map_err(|_| anyhow!("Failed to unwrap ARC"))?; - let output = mutex_output.into_inner()?; + let output = mutex_output.into_inner(); result.insert(stream, output); } Ok(result) @@ -180,15 +184,21 @@ impl Streaming { Ok(()) } - pub(crate) fn client_connected(&self) -> bool { - self.outputs - .iter() - .any(|(_, output)| output.lock().unwrap().is_connected()) + pub(crate) async fn client_connected(&self) -> bool { + for (_, output) in self.outputs.iter() { + if output.lock().await.is_connected() { + return true; + } + } + false } - pub(crate) fn can_pause(&self) -> bool { - self.outputs - .iter() - .all(|(_, output)| output.lock().unwrap().has_last_iframe()) + pub(crate) async fn can_pause(&self) -> bool { + for (_, output) in self.outputs.iter() { + if !output.lock().await.has_last_iframe() { + return false; + } + } + true } } diff --git a/src/utils.rs b/src/utils.rs index 97ca336..2244eab 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -33,10 +33,19 @@ impl AddressOrUid { } // Convience method to get the BcCamera with the appropiate method - pub(crate) async fn connect_camera(&self, channel_id: u8) -> Result { + pub(crate) async fn connect_camera, U: Into>( + &self, + channel_id: u8, + username: T, + passwd: Option, + ) -> Result { match self { - AddressOrUid::Address(host) => Ok(BcCamera::new_with_addr(host, channel_id).await?), - AddressOrUid::Uid(host) => Ok(BcCamera::new_with_uid(host, channel_id).await?), + AddressOrUid::Address(host) => { + Ok(BcCamera::new_with_addr(host, channel_id, username, passwd).await?) + } + AddressOrUid::Uid(host) => { + Ok(BcCamera::new_with_uid(host, channel_id, username, passwd).await?) + } } } } @@ -55,7 +64,11 @@ pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result Result