Add fcm test project

This commit is contained in:
QuantumEntangledAndy committed 2023-05-19 09:18:01 +07:00
1 parent e09d9d30dc
commit f99df6e5e7
12 files changed
+636

No files matched your search

+3
View File
@@ -20,3 +20,6 @@
._.DS_Store
**/.DS_Store
**/._.DS_Store
# The token file for the FCM tests
token.toml
+2
View File
@@ -0,0 +1,2 @@
MD013:
code_blocks: false
+1
View File
@@ -16,4 +16,5 @@ repos:
hooks:
- id: markdownlint-fix
files: README.md
args: [--disable, MD041, --]
+1
View File
@@ -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};
@@ -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 {
+72
View File
@@ -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(())
}
}
+21
View File
@@ -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"
+204
View File
@@ -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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AD_UNIT_ID_FOR_BANNER_TEST</key>
<string>ca-app-pub-3940256099942544/2934735716</string>
<key>AD_UNIT_ID_FOR_INTERSTITIAL_TEST</key>
<string>ca-app-pub-3940256099942544/4411468910</string>
<key>API_KEY</key>
<string>AIzaSyAV5l5DbbAkYB5X61wgn81Lb2f7s1h-ANE</string>
<key>BUNDLE_ID</key>
<string>com.reo.link</string>
<key>CLIENT_ID</key>
<string>696841269229-mfqfluqdkb0s2raassbsul48h09l2eiv.apps.googleusercontent.com</string>
<key>DATABASE_URL</key>
<string>https://reolink-for-ios.firebaseio.com</string>
<key>GCM_SENDER_ID</key>
<string>696841269229</string>
<key>GOOGLE_APP_ID</key>
<string>1:696841269229:ios:690529b18debb2e3</string>
<key>IS_ADS_ENABLED</key>
<false/>
<key>IS_ANALYTICS_ENABLED</key>
<false/>
<key>IS_APPINVITE_ENABLED</key>
<false/>
<key>IS_GCM_ENABLED</key>
<false/>
<key>IS_SIGNIN_ENABLED</key>
<false/>
<key>PLIST_VERSION</key>
<string>1</string>
<key>PROJECT_ID</key>
<string>reolink-for-ios</string>
<key>REVERSED_CLIENT_ID</key>
<string>com.googleusercontent.apps.696841269229-mfqfluqdkb0s2raassbsul48h09l2eiv</string>
<key>STORAGE_BUCKET</key>
<string>reolink-for-ios.appspot.com</string>
</dict>
</plist>
```
Key information from this is
```xml
<key>GCM_SENDER_ID</key>
<string>696841269229</string>
```
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
<string name="gcm_defaultSenderId">743639030586</string>
<string name="google_api_key">AIzaSyBEUIuWHnnOEwFahxWgQB4Yt4NsgOmkPyE</string>
<string name="google_app_id">1:743639030586:android:86f60a4fb7143876</string>
<string name="google_client_id">743639030586-ove5uqrausoqtjjkev4b1tk4vjdfpt4l.apps.googleusercontent.com</string>
```
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
<?xml version="1.0" encoding="UTF-8" ?>
<body>
<PushInfo version="1.1">
<token>TOKEN_FROM_FCM_OR_APNS</token>
<phoneType>reo_iphone</phoneType>
<clientID>SOMEID</clientID>
</PushInfo>
</body>
```
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/<CAMERA_UID>/notifications/listeners/<clientID_FROM_PushInfo>' -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/<clientID_FROM_PushInfo>/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" : "<CAMERA_UID>",
"secret" : ""
}
]
}
```
---
You can also confirm a device has a listener with
```bash
curl -v -X GET 'https://pushx.reolink.com/devices/<CAMERA_UID>/listeners/<clientID_FROM_PushInfo>/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/<clientID_FROM_PushInfo>/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
+80
View File
@@ -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<CameraConfig>,
}
#[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<String>,
#[serde(rename = "uid")]
pub(crate) camera_uid: Option<String>,
pub(crate) username: String,
pub(crate) password: Option<String>,
#[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(()),
}
}
+82
View File
@@ -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::<Registration>(&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(&registration)?;
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(&registration.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(())
}
+16
View File
@@ -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<PathBuf>,
/// The name of the camera. Must be a name in the config
pub camera: String,
}
+152
View File
@@ -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<String>,
uid: &Option<String>,
method: &DiscoveryMethods,
) -> Result<Self, Error> {
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<BcCamera, Error> {
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<BcCamera> {
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<BcCamera> {
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))
}