From f99df6e5e704c317e66100f29858739c1dfb2da6 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Fri, 19 May 2023 09:18:01 +0700 Subject: [PATCH] Add fcm test project --- .gitignore | 3 + .markdownlint.yml | 2 + .pre-commit-config.yaml | 1 + crates/core/src/bc_protocol.rs | 1 + .../src/bc_protocol/connection/discovery.rs | 2 + crates/core/src/bc_protocol/pushinfo.rs | 72 +++++++ crates/pushnoti/Cargo.toml | 21 ++ crates/pushnoti/README.md | 204 ++++++++++++++++++ crates/pushnoti/src/config.rs | 80 +++++++ crates/pushnoti/src/main.rs | 82 +++++++ crates/pushnoti/src/opt.rs | 16 ++ crates/pushnoti/src/utils.rs | 152 +++++++++++++ 12 files changed, 636 insertions(+) create mode 100644 .markdownlint.yml create mode 100644 crates/core/src/bc_protocol/pushinfo.rs create mode 100644 crates/pushnoti/Cargo.toml create mode 100644 crates/pushnoti/README.md create mode 100644 crates/pushnoti/src/config.rs create mode 100644 crates/pushnoti/src/main.rs create mode 100644 crates/pushnoti/src/opt.rs create mode 100644 crates/pushnoti/src/utils.rs diff --git a/.gitignore b/.gitignore index 0e46dd9..98443d3 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ ._.DS_Store **/.DS_Store **/._.DS_Store + +# The token file for the FCM tests +token.toml diff --git a/.markdownlint.yml b/.markdownlint.yml new file mode 100644 index 0000000..e19cae0 --- /dev/null +++ b/.markdownlint.yml @@ -0,0 +1,2 @@ +MD013: + code_blocks: false diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 04984d9..af9fb14 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,4 +16,5 @@ repos: hooks: - id: markdownlint-fix files: README.md + args: [--disable, MD041, --] diff --git a/crates/core/src/bc_protocol.rs b/crates/core/src/bc_protocol.rs index 4648b88..a206f95 100644 --- a/crates/core/src/bc_protocol.rs +++ b/crates/core/src/bc_protocol.rs @@ -40,6 +40,7 @@ pub use login::MaxEncryption; pub use motion::{MotionData, MotionStatus}; pub use pirstate::PirState; pub use ptz::Direction; +pub use pushinfo::PhoneType; pub use resolution::*; use std::sync::Arc; pub use stream::{StreamData, StreamKind}; diff --git a/crates/core/src/bc_protocol/connection/discovery.rs b/crates/core/src/bc_protocol/connection/discovery.rs index 076928c..f302945 100644 --- a/crates/core/src/bc_protocol/connection/discovery.rs +++ b/crates/core/src/bc_protocol/connection/discovery.rs @@ -445,6 +445,8 @@ impl Discoverer { }, }; + trace!("Registering: {:?}", msg.payload.c2r_c); + // Send and await acceptance let (sid, dev, dmap, relay) = self .retry_send(msg, lookup.reg, |bc, _| match bc { diff --git a/crates/core/src/bc_protocol/pushinfo.rs b/crates/core/src/bc_protocol/pushinfo.rs new file mode 100644 index 0000000..66c07bc --- /dev/null +++ b/crates/core/src/bc_protocol/pushinfo.rs @@ -0,0 +1,72 @@ +use super::{BcCamera, Error, Result}; +use crate::bc::{model::*, xml::*}; + +/// Specifies the phone type for the push notification +pub enum PhoneType { + /// Specify that this is an ios push notfication + /// + /// In this case the token must be the APNS + Ios, + /// Specify that this is an andriod push notfication + /// + /// In this case the token must firebase cloud messaging token + Andriod, +} + +impl BcCamera { + /// Convenience method for andriod of `[send_pushinfo]` + pub async fn send_pushinfo_andriod(&self, token: &str, client_id: &str) -> Result<()> { + self.send_pushinfo(token, client_id, PhoneType::Andriod) + .await + } + /// Convenience method for andriod of `[send_pushinfo]` + pub async fn send_pushinfo_ios(&self, token: &str, client_id: &str) -> Result<()> { + self.send_pushinfo(token, client_id, PhoneType::Ios).await + } + /// Send the push info to regsiter for push notfications + pub async fn send_pushinfo( + &self, + token: &str, + client_id: &str, + phone_type: PhoneType, + ) -> Result<()> { + let connection = self.get_connection(); + let msg_num = self.new_message_num(); + let mut sub = connection.subscribe(msg_num).await?; + + let phone_type_str = match phone_type { + PhoneType::Ios => "reo_iphone", + PhoneType::Andriod => "reo_fcm", + }; + + let msg = Bc { + meta: BcMeta { + msg_id: MSG_ID_PUSH_INFO, + channel_id: self.channel_id, + msg_num, + response_code: 0, + stream_type: 0, + class: 0x6414, + }, + body: BcBody::ModernMsg(ModernMsg { + extension: None, + payload: Some(BcPayloads::BcXml(BcXml { + push_info: Some(PushInfo { + token: token.to_owned(), + phone_type: phone_type_str.to_owned(), + client_id: client_id.to_owned(), + }), + ..Default::default() + })), + }), + }; + + sub.send(msg).await?; + let msg = sub.recv().await?; + if msg.meta.response_code != 200 { + return Err(Error::CameraServiceUnavaliable); + } + + Ok(()) + } +} diff --git a/crates/pushnoti/Cargo.toml b/crates/pushnoti/Cargo.toml new file mode 100644 index 0000000..10f19d6 --- /dev/null +++ b/crates/pushnoti/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "pushnoti" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.71" +clap = { version = "4.2.2", features = ["derive", "cargo"] } +env_logger = "0.10.0" +fcm-push-listener = "2.0.1" +lazy_static = "1.4.0" +log = { version = "0.4.17", features = [ "release_max_level_debug" ] } +neolink_core = { path = "../core", version = "0.5" } +regex = "1.8.1" +serde = "1.0.163" +tokio = { version = "1.28.1", features = ["full"] } +toml = "0.7.3" +validator = "0.16.0" +validator_derive = "0.16.0" diff --git a/crates/pushnoti/README.md b/crates/pushnoti/README.md new file mode 100644 index 0000000..40eeab1 --- /dev/null +++ b/crates/pushnoti/README.md @@ -0,0 +1,204 @@ +# Reolink Push Notifications + +This is the test crate to experient with REing the push notifications for a +camera + +The goal here is to connect to Google's firebase cloud messaging system (FCM) +and recieve push notificaions for motion updates. This way we can get motion +notifications without being logged in to the camera and this will let us +disconnect from battery cameras and save power. + +The following is the reolink data for google extracted from ios. + +```xml + + + + + AD_UNIT_ID_FOR_BANNER_TEST + ca-app-pub-3940256099942544/2934735716 + AD_UNIT_ID_FOR_INTERSTITIAL_TEST + ca-app-pub-3940256099942544/4411468910 + API_KEY + AIzaSyAV5l5DbbAkYB5X61wgn81Lb2f7s1h-ANE + BUNDLE_ID + com.reo.link + CLIENT_ID + 696841269229-mfqfluqdkb0s2raassbsul48h09l2eiv.apps.googleusercontent.com + DATABASE_URL + https://reolink-for-ios.firebaseio.com + GCM_SENDER_ID + 696841269229 + GOOGLE_APP_ID + 1:696841269229:ios:690529b18debb2e3 + IS_ADS_ENABLED + + IS_ANALYTICS_ENABLED + + IS_APPINVITE_ENABLED + + IS_GCM_ENABLED + + IS_SIGNIN_ENABLED + + PLIST_VERSION + 1 + PROJECT_ID + reolink-for-ios + REVERSED_CLIENT_ID + com.googleusercontent.apps.696841269229-mfqfluqdkb0s2raassbsul48h09l2eiv + STORAGE_BUCKET + reolink-for-ios.appspot.com + + +``` + +Key information from this is + +```xml +GCM_SENDER_ID +696841269229 +``` + +Which we use to register for FCM + +On andriod the details are little harder for me to extract (ios here). By +extracing the apk with apktook I found the following: + +```xml +743639030586 +AIzaSyBEUIuWHnnOEwFahxWgQB4Yt4NsgOmkPyE +1:743639030586:android:86f60a4fb7143876 +743639030586-ove5uqrausoqtjjkev4b1tk4vjdfpt4l.apps.googleusercontent.com +``` + +Where the key we need is `743639030586` + +We also need to send the PushInfo command to the camera with the token we get +from FCM and some client ID. On IOS the token is the APNS number. + +The client ID is an all CAPS UID of hexadecimal of 33 chars. Not sure how +these are generated but they seem to be unique to the device, perhaps a MD5 of +something or some generated UID on app initial login. On data wipe the app +generates a new UID + +```xml + + + +TOKEN_FROM_FCM_OR_APNS +reo_iphone +SOMEID + + +``` + +For ios the phonetype is `reo_iphone` which I observed live but for andriod +no such luck. + +First I just tried `reo_andriod` but the camera replied with an error code. +Which at least told me that I can test other string quickly by seeing if it +returns the error code. + +Next attempt was dumping the binaries from the apk and searching for a string +like `reo_` + +```bash +grep -Ria "reo_" . +./lib/armeabi-v7a/libavcodec.so:stereo_modeError splitting the input into NAL units. +./lib/arm64-v8a/libavcodec.so:stereo_modeError splitting the input into NAL units. +./smali_classes2/com/google/android/exoplayer2/extractor/mkv/MatroskaExtractor.smali:.field private static final ID_STEREO_MODE:I = 0x53b8 +./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_LEFT_RIGHT:I = 0x2 +./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_MONO:I = 0x0 +./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_STEREO_MESH:I = 0x3 +./smali_classes2/com/google/android/exoplayer2/C.smali:.field public static final STEREO_MODE_TOP_BOTTOM:I = 0x1 +./smali_classes2/com/android/bc/util/DeviceIdUtil$UpdatePushClientIdRequest.smali: const-string v2, "reo_fcm" +./smali_classes2/com/android/bc/pushmanager/MyPushAdapterImpl.smali: const-string v0, "reo_fcm" +./smali/androidx/core/text/BidiFormatter.smali:.field private static final FLAG_STEREO_RESET:I = 0x2 +./smali/com/android/bc/component/BaseWebViewFragment.smali: const-string v2, "REO_LANGUAGE=" +``` + +Interesting one was `MyPushAdapterImpl.smali: const-string v0, "reo_fcm"` +which suggested that it is `reo_fcm` + +stuck that in and voilla no error messages from the camera. + +--- + +To disable a PUSH request the following is done: + +```bash +curl -v -X DELETE 'https://pushx.reolink.com/devices//notifications/listeners/' -H 'Cookie:' +``` + +Reply is: + +```json +{ + "status" : { + "detail" : "OK", + "code" : 0 + } +} +``` + +Nothing seems to be sent to the camera just this to the reolink api + +--- + +On ios application start reolink send this: + +```bash +curl -v -X GET 'https://pushx.reolink.com/listeners//devices' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept: */*' -H 'Accept-Language: en-GB,en;q=0.9' -H 'Cookie:' +``` + +Which resonds with this json which contains a list of all currently +enabled push notification camera UIDs for this client + +```json +{ + "status" : { + "detail" : "OK", + "code" : 0 + }, + "data" : [ + { + "uid" : "", + "secret" : "" + } + ] +} +``` + +--- + +You can also confirm a device has a listener with + +```bash +curl -v -X GET 'https://pushx.reolink.com/devices//listeners//config' -H 'Cookie:' +``` + +Which returns + +```json +{ + "status": { + "code":0, + "detail":"OK" + }, + "data": { + "sound": { + "soundType":0, + "soundName":"", + "soundVolume":0 + } + } +} +``` + +--- + +We can use the +`https://pushx.reolink.com/listeners//devices` +api to confirm if our client is regeristed for push notifications. +Which was the next thing to do once we passed the token onto the camera diff --git a/crates/pushnoti/src/config.rs b/crates/pushnoti/src/config.rs new file mode 100644 index 0000000..ce9085f --- /dev/null +++ b/crates/pushnoti/src/config.rs @@ -0,0 +1,80 @@ +use lazy_static::lazy_static; +use neolink_core::bc_protocol::{DiscoveryMethods, PrintFormat}; +use regex::Regex; +use serde::Deserialize; +use std::clone::Clone; +use validator::{Validate, ValidationError}; +use validator_derive::Validate; + +lazy_static! { + static ref RE_STREAM_SRC: Regex = + Regex::new(r"^(mainStream|subStream|externStream|both|all)$").unwrap(); + static ref RE_TLS_CLIENT_AUTH: Regex = Regex::new(r"^(none|request|require)$").unwrap(); + static ref RE_PAUSE_MODE: Regex = Regex::new(r"^(black|still|test|none)$").unwrap(); + static ref RE_MAXENC_SRC: Regex = + Regex::new(r"^([nN]one|[Aa][Ee][Ss]|[Bb][Cc][Ee][Nn][Cc][Rr][Yy][Pp][Tt])$").unwrap(); +} + +#[derive(Debug, Deserialize, Validate, Clone)] +pub(crate) struct Config { + #[validate] + pub(crate) cameras: Vec, +} + +#[derive(Debug, Deserialize, Validate, Clone)] +#[validate(schema(function = "validate_camera_config"))] +pub(crate) struct CameraConfig { + pub(crate) name: String, + + #[serde(rename = "address")] + pub(crate) camera_addr: Option, + + #[serde(rename = "uid")] + pub(crate) camera_uid: Option, + + pub(crate) username: String, + pub(crate) password: Option, + + #[validate(range(min = 0, max = 31, message = "Invalid channel", code = "channel_id"))] + #[serde(default = "default_channel_id")] + pub(crate) channel_id: u8, + + #[serde(default = "default_discovery")] + pub(crate) discovery: DiscoveryMethods, + + #[serde(default = "default_maxenc")] + #[validate(regex( + path = "RE_MAXENC_SRC", + message = "Invalid maximum encryption method", + code = "max_encryption" + ))] + pub(crate) max_encryption: String, + + #[serde(default = "default_print", alias = "print")] + pub(crate) print_format: PrintFormat, +} + +fn default_print() -> PrintFormat { + PrintFormat::None +} + +fn default_discovery() -> DiscoveryMethods { + DiscoveryMethods::Relay +} + +fn default_maxenc() -> String { + "Aes".to_string() +} + +fn default_channel_id() -> u8 { + 0 +} + +fn validate_camera_config(camera_config: &CameraConfig) -> Result<(), ValidationError> { + match (&camera_config.camera_addr, &camera_config.camera_uid) { + (None, None) => Err(ValidationError::new( + "Either camera address or uid must be given", + )), + _ => Ok(()), + } +} diff --git a/crates/pushnoti/src/main.rs b/crates/pushnoti/src/main.rs new file mode 100644 index 0000000..1beef79 --- /dev/null +++ b/crates/pushnoti/src/main.rs @@ -0,0 +1,82 @@ +use anyhow::{Context, Result}; +use clap::Parser; +use fcm_push_listener::*; +use log::*; +use std::{fs, path::PathBuf}; +use validator::Validate; + +mod config; +mod opt; +mod utils; + +use config::Config; +use opt::Opt; +use utils::find_and_connect; + +#[tokio::main] +async fn main() -> Result<()> { + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); + + let opt = Opt::parse(); + + let conf_path = opt.config.context("Must supply --config file")?; + let config: Config = toml::from_str( + &fs::read_to_string(&conf_path) + .with_context(|| format!("Failed to read {:?}", conf_path))?, + ) + .with_context(|| format!("Failed to parse the {:?} config file", conf_path))?; + + config + .validate() + .with_context(|| format!("Failed to validate the {:?} config file", conf_path))?; + + let camera = find_and_connect(&config, &opt.camera).await?; + + // 696841269229 is the reo_iphone FCM Sender_ID + // let registration = fcm_push_listener::register("696841269229").await?; + // 743639030586 is the reo_fcm FCM Sender_ID + // 263684512460 is my test Sender_ID + // let registration = fcm_push_listener::register("743639030586").await? + // I have confirmed that I can recieve test messages with this SenderID + // into this program + // let registration = fcm_push_listener::register("263684512460").await?; + + let sender_id = "743639030586"; // andriod + + // let sender_id = "696841269229"; // ios + + // let sender_id = "263684512460"; // test + + let token_path = PathBuf::from("./token.toml"); + let registration = if let Ok(Ok(registration)) = + fs::read_to_string(&token_path).map(|v| toml::from_str::(&v)) + { + info!("Loaded token"); + registration + } else { + info!("Registering new token"); + let registration = fcm_push_listener::register(sender_id).await?; + let new_token = toml::to_string(®istration)?; + fs::write(token_path, new_token)?; + registration + }; + + // Send registration.fcm_token to the server to allow it to send push messages to you. + info!("registration.fcm_token: {}", registration.fcm_token); + let uid = "6A5443E486511B0D828543445DC55A7D"; // MD5 Hash of "WHY_REOLINK" + camera + .send_pushinfo_ios(®istration.fcm_token, uid) + .await?; + + info!("Listening"); + let mut listener = FcmPushListener::create( + registration, + |message: FcmMessage| { + info!("Message JSON: {}", message.payload_json); + info!("Persistent ID: {:?}", message.persistent_id); + }, + vec!["1:696841269229:ios:695029b18debb23e".to_owned()], + ); + listener.connect().await?; + Ok(()) +} diff --git a/crates/pushnoti/src/opt.rs b/crates/pushnoti/src/opt.rs new file mode 100644 index 0000000..73b9c94 --- /dev/null +++ b/crates/pushnoti/src/opt.rs @@ -0,0 +1,16 @@ +use clap::{crate_authors, crate_version, Parser}; +use std::path::PathBuf; +use std::str::FromStr; + +/// A standards-compliant bridge to Reolink IP cameras +/// +/// Neolink is free software released under the GNU AGPL v3. +/// You can find its source code at https://github.com/thirtythreeforty/neolink +#[derive(Parser, Debug)] +#[command(name = "pushnoti", arg_required_else_help = true, version = crate_version!(), author = crate_authors!("\n"))] +pub struct Opt { + #[arg(short, long, value_parser = PathBuf::from_str)] + pub config: Option, + /// The name of the camera. Must be a name in the config + pub camera: String, +} diff --git a/crates/pushnoti/src/utils.rs b/crates/pushnoti/src/utils.rs new file mode 100644 index 0000000..7ff6c05 --- /dev/null +++ b/crates/pushnoti/src/utils.rs @@ -0,0 +1,152 @@ +//! Contains code that is not specific to any of the subcommands +//! +use log::*; + +use super::config::{CameraConfig, Config}; +use anyhow::{anyhow, Context, Error, Result}; +use neolink_core::bc_protocol::{ + BcCamera, BcCameraOpt, ConnectionProtocol, Credentials, DiscoveryMethods, MaxEncryption, +}; +use std::{ + fmt::{Display, Error as FmtError, Formatter}, + net::{IpAddr, ToSocketAddrs}, + str::FromStr, +}; + +pub(crate) enum AddressOrUid { + Address(String), + Uid(String, DiscoveryMethods), + AddressWithUid(String, String, DiscoveryMethods), +} + +impl Display for AddressOrUid { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { + match self { + AddressOrUid::AddressWithUid(addr, uid, _) => { + write!(f, "Address: {}, UID: {}", addr, uid) + } + AddressOrUid::Address(host) => write!(f, "Address: {}", host), + AddressOrUid::Uid(host, _) => write!(f, "UID: {}", host), + } + } +} + +impl AddressOrUid { + // Created by translating the config fields directly + pub(crate) fn new( + address: &Option, + uid: &Option, + method: &DiscoveryMethods, + ) -> Result { + match (address, uid) { + (None, None) => Err(anyhow!("Neither address or uid given")), + (Some(host), Some(uid)) => Ok(AddressOrUid::AddressWithUid( + host.clone(), + uid.clone(), + *method, + )), + (Some(host), None) => Ok(AddressOrUid::Address(host.clone())), + (None, Some(host)) => Ok(AddressOrUid::Uid(host.clone(), *method)), + } + } + + // Convience method to get the BcCamera with the appropiate method + // from a camera_config + pub(crate) async fn connect_camera( + &self, + camera_config: &CameraConfig, + ) -> Result { + let (port, addrs) = { + if let Some(addr_str) = camera_config.camera_addr.as_ref() { + match addr_str.to_socket_addrs() { + Ok(addr_iter) => { + let mut port = None; + let mut ipaddrs = vec![]; + for addr in addr_iter { + port = Some(addr.port()); + ipaddrs.push(addr.ip()); + } + Ok((port, ipaddrs)) + } + Err(_) => match IpAddr::from_str(addr_str) { + Ok(ip) => Ok((None, vec![ip])), + Err(_) => Err(anyhow!("Could not parse address in config")), + }, + } + } else { + Ok((None, vec![])) + } + }?; + + let options = BcCameraOpt { + name: camera_config.name.clone(), + channel_id: camera_config.channel_id, + addrs, + port, + uid: camera_config.camera_uid.clone(), + protocol: ConnectionProtocol::TcpUdp, + discovery: camera_config.discovery, + aux_printing: camera_config.print_format, + credentials: Credentials { + username: camera_config.username.clone(), + password: camera_config.password.clone(), + }, + }; + + trace!("Camera Info: {:?}", options); + + Ok(BcCamera::new(&options).await?) + } +} + +pub(crate) async fn find_and_connect(config: &Config, name: &str) -> Result { + let camera_config = find_camera_by_name(config, name)?; + connect_and_login(camera_config).await +} + +pub(crate) async fn connect_and_login(camera_config: &CameraConfig) -> Result { + let camera_addr = AddressOrUid::new( + &camera_config.camera_addr, + &camera_config.camera_uid, + &camera_config.discovery, + ) + .unwrap(); + info!( + "{}: Connecting to camera at {}", + camera_config.name, camera_addr + ); + + let camera = camera_addr + .connect_camera(camera_config) + .await + .with_context(|| { + format!( + "Failed to connect to camera {} at {} on channel {}", + camera_config.name, camera_addr, camera_config.channel_id + ) + })?; + + let max_encryption = match camera_config.max_encryption.to_lowercase().as_str() { + "none" => MaxEncryption::None, + "bcencrypt" => MaxEncryption::BcEncrypt, + "aes" => MaxEncryption::Aes, + _ => MaxEncryption::Aes, + }; + info!("{}: Logging in", camera_config.name); + camera + .login_with_maxenc(max_encryption) + .await + .with_context(|| format!("Failed to login to {}", camera_config.name))?; + + info!("{}: Connected and logged in", camera_config.name); + + Ok(camera) +} + +pub(crate) fn find_camera_by_name<'a>(config: &'a Config, name: &str) -> Result<&'a CameraConfig> { + config + .cameras + .iter() + .find(|c| c.name == name) + .ok_or_else(|| anyhow!("Camera {} not found in the config file", name)) +}