diff --git a/services/tunnelbroker/CMakeLists.txt b/services/tunnelbroker/CMakeLists.txt --- a/services/tunnelbroker/CMakeLists.txt +++ b/services/tunnelbroker/CMakeLists.txt @@ -9,6 +9,7 @@ endif(COMMAND cmake_policy) set(CMAKE_CXX_STANDARD 17) + # For C++17 on MacOS, we must set minimum target to 10.14+ set(CMAKE_OSX_DEPLOYMENT_TARGET 10.14) @@ -67,12 +68,6 @@ find_package(OpenSSL REQUIRED) find_package(glog) -# Corrosion Rust integration -find_package(Corrosion REQUIRED) -corrosion_import_crate(MANIFEST_PATH "rust-notifications/Cargo.toml") -set(RUST_HEADERS_INCLUDE_DIR "rust-notifications/extern") -set(RUST_LIBS "rust-notifications") - file(GLOB_RECURSE SOURCE_CODE "./src/*.cpp") set( diff --git a/services/tunnelbroker/rust-notifications/src/apns.rs b/services/tunnelbroker/rust-notifications/src/apns.rs new file mode 100644 --- /dev/null +++ b/services/tunnelbroker/rust-notifications/src/apns.rs @@ -0,0 +1,30 @@ +use a2::{ + Client, Endpoint, NotificationBuilder, NotificationOptions, + PlainNotificationBuilder, +}; +use anyhow::Result; +use std::fs::File; + +pub async fn send_by_a2_client( + certificate_path: &str, + certificate_password: &str, + device_token: &str, + message: &str, + sandbox: bool, +) -> Result { + let mut certificate = File::open(certificate_path)?; + let endpoint = if sandbox { + Endpoint::Sandbox + } else { + Endpoint::Production + }; + let client = + Client::certificate(&mut certificate, certificate_password, endpoint)?; + let options = NotificationOptions { + ..Default::default() + }; + let builder = PlainNotificationBuilder::new(message); + let payload = builder.build(device_token, options); + let response = client.send(payload).await?; + Ok(response.code) +} diff --git a/services/tunnelbroker/rust-notifications/src/lib.rs b/services/tunnelbroker/rust-notifications/src/lib.rs --- a/services/tunnelbroker/rust-notifications/src/lib.rs +++ b/services/tunnelbroker/rust-notifications/src/lib.rs @@ -1 +1,39 @@ +use anyhow::Result; +use lazy_static::lazy_static; +use tokio::runtime::Runtime; +pub mod apns; +#[cxx::bridge] +mod ffi { + extern "Rust" { + #[cxx_name = "sendNotifToAPNS"] + fn send_notif_to_apns( + certificate_path: &str, + certificate_password: &str, + device_token: &str, + message: &str, + sandbox: bool, + ) -> Result; + } +} + +lazy_static! { + // Lazy static Tokio runtime initialization + pub static ref RUNTIME: Runtime = Runtime::new().unwrap(); +} + +pub fn send_notif_to_apns( + certificate_path: &str, + certificate_password: &str, + device_token: &str, + message: &str, + sandbox: bool, +) -> Result { + RUNTIME.block_on(apns::send_by_a2_client( + certificate_path, + certificate_password, + device_token, + message, + sandbox, + )) +}