diff --git a/Dockerfile b/Dockerfile index f1ba75e..375c93e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -49,6 +49,8 @@ LABEL maintainer="$OWNER" # hadolint ignore=DL3008 RUN apt-get update && \ apt-get install -y --no-install-recommends \ + openssl \ + ca-certificates \ libgstrtspserver-1.0-0 \ libgstreamer1.0-0 \ gstreamer1.0-tools \ diff --git a/src/common/neocam.rs b/src/common/neocam.rs index 40796eb..8fcd518 100644 --- a/src/common/neocam.rs +++ b/src/common/neocam.rs @@ -278,17 +278,7 @@ impl NeoCam { let pn_permit_instance = pn_root_instance.subscribe().await?; let r = tokio::select! { // This thread handles the push notfications - v = async { - while { - if let Err(e) = push_notifier.run(&mut pn_request_rx).await { - log::debug!("Push notification failed restarting: {:?}", e); - true - } else { - false - } - }{} - AnyResult::Ok(()) - } => v, + v = push_notifier.run(&mut pn_request_rx) => v, // Push notification permits v = async { let mut prev_noti = None; diff --git a/src/common/pushnoti.rs b/src/common/pushnoti.rs index b699aed..e4b0fab 100644 --- a/src/common/pushnoti.rs +++ b/src/common/pushnoti.rs @@ -46,93 +46,108 @@ impl PushNotiThread { &mut self, pn_request_rx: &mut MpscReceiver, ) -> AnyResult<()> { - let sender_id = "743639030586"; // andriod - // let sender_id = "696841269229"; // ios + loop { + let sender_id = "743639030586"; // andriod + // let sender_id = "696841269229"; // ios - let token_path = dirs::config_dir().map(|mut d| { - d.push("./neolink_token.toml"); - d - }); - log::debug!("Push notification details are saved to {:?}", token_path); + let token_path = dirs::config_dir().map(|mut d| { + d.push("./neolink_token.toml"); + d + }); + log::debug!("Push notification details are saved to {:?}", token_path); - let registration = if let Some(Ok(Ok(registration))) = - token_path.as_ref().map(|token_path| { - fs::read_to_string(token_path).map(|v| toml::from_str::(&v)) - }) { - log::debug!("Loaded push notification token"); - registration - } else { - log::debug!("Registering new push notification token"); - let registration = fcm_push_listener::register(sender_id).await?; - let new_token = toml::to_string(®istration)?; - if let Some(Err(e)) = token_path - .as_ref() - .map(|token_path| fs::write(token_path, &new_token)) - { - log::warn!( - "Unable to save push notification details ({}) to {:#?} because of the error {:#?}", - new_token, - token_path, - e - ); - } - registration - }; - - // Send registration.fcm_token to the server to allow it to send push messages to you. - log::debug!("registration.fcm_token: {}", registration.fcm_token); - - let md5ed = md5::compute(format!("WHY_REOLINK_{:?}", registration.fcm_token)); - let uid = format!("{:X}", md5ed); - log::debug!("push notification UID: {}", uid); - self.instance - .run_task(|camera| { - let uid = uid.clone(); - let fcm_token = registration.fcm_token.clone(); - Box::pin(async move { - camera.send_pushinfo_android(&fcm_token, &uid).await?; - AnyResult::Ok(()) - }) - }) - .await?; - - log::debug!("Push notification Listening"); - let thread_pn_watcher = self.pn_watcher.clone(); - let mut listener = FcmPushListener::create( - registration, - |message: FcmMessage| { - thread_pn_watcher.send_replace(Some(PushNoti { - message: message.payload_json, - id: message.persistent_id, - })); - }, - vec![], - ); - tokio::select! { - v = async { - let r = listener.connect().await; - if r.is_err() { - // Wipe data so next call is a new token - token_path.map(|token_path| - fs::write(token_path, "") + let registration = if let Some(Ok(Ok(registration))) = + token_path.as_ref().map(|token_path| { + fs::read_to_string(token_path).map(|v| toml::from_str::(&v)) + }) { + log::debug!("Loaded push notification token"); + registration + } else { + log::debug!("Registering new push notification token"); + let registration = fcm_push_listener::register(sender_id).await?; + let new_token = toml::to_string(®istration)?; + if let Some(Err(e)) = token_path + .as_ref() + .map(|token_path| fs::write(token_path, &new_token)) + { + log::warn!( + "Unable to save push notification details ({}) to {:#?} because of the error {:#?}", + new_token, + token_path, + e ); - log::debug!("Error on push notification listener: {:?}. Clearing token", r); } - r?; - AnyResult::Ok(()) - } => v, - v = async { - while let Some(msg) = pn_request_rx.recv().await { - match msg { - PnRequest::Get{sender} => { - let _ = sender.send(self.pn_watcher.subscribe()); + registration + }; + + // Send registration.fcm_token to the server to allow it to send push messages to you. + log::debug!("registration.fcm_token: {}", registration.fcm_token); + + let md5ed = md5::compute(format!("WHY_REOLINK_{:?}", registration.fcm_token)); + let uid = format!("{:X}", md5ed); + log::debug!("push notification UID: {}", uid); + self.instance + .run_task(|camera| { + let uid = uid.clone(); + let fcm_token = registration.fcm_token.clone(); + Box::pin(async move { + camera.send_pushinfo_android(&fcm_token, &uid).await?; + AnyResult::Ok(()) + }) + }) + .await?; + + log::debug!("Push notification Listening"); + let thread_pn_watcher = self.pn_watcher.clone(); + let mut listener = FcmPushListener::create( + registration, + |message: FcmMessage| { + thread_pn_watcher.send_replace(Some(PushNoti { + message: message.payload_json, + id: message.persistent_id, + })); + }, + vec![], + ); + tokio::select! { + v = async { + let r = listener.connect().await; + if let Err(e) = r { + // Wipe data so next call is a new token + token_path.map(|token_path| + fs::write(token_path, "") + ); + use fcm_push_listener::Error::*; + match &e { + MissingMessagePayload | MissingCryptoMetadata | ProtobufDecode(_) | Base64Decode(_) => { + log::debug!("Error on push notification listener: {:?}. Clearing token", e); + AnyResult::Ok(()) // Allow to restart + } + _ => { + log::warn!("Error on push notification listener: {:?}. Clearing token", e); + Err(e.into()) // Propegate error so it breaks + } + } + } else { + AnyResult::Ok(()) + } + } => v, + v = async { + while let Some(msg) = pn_request_rx.recv().await { + match msg { + PnRequest::Get{sender} => { + let _ = sender.send(self.pn_watcher.subscribe()); + } } } - } - AnyResult::Ok(()) - } => v, - }?; - - AnyResult::Ok(()) + AnyResult::Ok(()) + } => { + // These are critical errors + // break the loop and return + log::debug!("Push Notification thread ended {:?}", v); + break v; + }, + }?; + } } }