diff --git a/native/native_rust_library/Cargo.toml b/native/native_rust_library/Cargo.toml --- a/native/native_rust_library/Cargo.toml +++ b/native/native_rust_library/Cargo.toml @@ -23,6 +23,8 @@ [build-dependencies] cxx-build = "1.0" regex = "1" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" [lib] crate-type = ["staticlib"] diff --git a/native/native_rust_library/build.rs b/native/native_rust_library/build.rs --- a/native/native_rust_library/build.rs +++ b/native/native_rust_library/build.rs @@ -1,8 +1,30 @@ use regex::Regex; +use serde::{Deserialize, Serialize}; use std::env; use std::fs; use std::path::Path; +const DEFAULT_DEBUG_IDENTITY_SOCKET_ADDR: &str = + "https://identity.staging.commtechnologies.org:50054"; +const DEFAULT_RELEASE_IDENTITY_SOCKET_ADDR: &str = + "https://identity.commtechnologies.org:50054"; +const IDENTITY_SERVICE_CONFIG_PATH: &str = + "../facts/identity_service_config.json"; + +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +struct IdentityServiceConfig { + identity_socket_addr: String, +} + +fn get_identity_service_config( +) -> Result> { + let path = Path::new(IDENTITY_SERVICE_CONFIG_PATH); + let file_content = fs::read_to_string(path)?; + + serde_json::from_str(&file_content).map_err(|e| e.into()) +} + fn main() { let _cxx_build = cxx_build::bridge("src/lib.rs").flag_if_supported("-std=c++17"); @@ -17,14 +39,14 @@ .lines() .find(|line| line.contains("const int codeVersion")) .expect("Failed to find codeVersion line"); - println!("Version line: {}", version_line); // The regex searches for the string "const int codeVersion", followed by any // number of whitespace characters, an escaped opening curly brace, more // optional whitespace, a series of one or more digits (which it captures), // some more optional whitespace, an escaped closing curly brace, and finally // a semicolon. - let re = Regex::new(r"const int codeVersion\s*\{\s*(\d+)\s*\};").unwrap(); + let re = Regex::new(r"const int codeVersion\s*\{\s*(\d+)\s*\};") + .expect("Failed to compile regular expression"); let version: u64 = re .captures(version_line) .and_then(|cap| cap.get(1)) @@ -37,15 +59,40 @@ }, ); - let out_dir = env::var("OUT_DIR").unwrap(); - let rust_path = Path::new(&out_dir).join("version.rs"); + let out_dir = env::var("OUT_DIR").expect("Error fetching OUT_DIR env var"); + let version_path = Path::new(&out_dir).join("version.rs"); fs::write( - rust_path, + version_path, format!("pub const CODE_VERSION: u64 = {};", version), ) .expect("Failed to write version.rs"); + let identity_socket_addr = match get_identity_service_config() { + Ok(config) => config.identity_socket_addr, + Err(_) => { + let profile = + env::var("PROFILE").expect("Error fetching PROFILE env var"); + if profile == "release" { + DEFAULT_RELEASE_IDENTITY_SOCKET_ADDR.to_string() + } else { + DEFAULT_DEBUG_IDENTITY_SOCKET_ADDR.to_string() + } + } + }; + + let socket_config_path = Path::new(&out_dir).join("socket_config.rs"); + + fs::write( + socket_config_path, + format!( + "pub const IDENTITY_SOCKET_ADDR: &str = \"{}\";", + identity_socket_addr + ), + ) + .expect("Failed to write socket_config.rs"); + println!("cargo:rerun-if-changed=src/lib.rs"); println!("cargo:rerun-if-changed={}", HEADER_PATH); + println!("cargo:rerun-if-changed={}", IDENTITY_SERVICE_CONFIG_PATH); } diff --git a/native/native_rust_library/src/lib.rs b/native/native_rust_library/src/lib.rs --- a/native/native_rust_library/src/lib.rs +++ b/native/native_rust_library/src/lib.rs @@ -1,6 +1,7 @@ -use crate::ffi::{bool_callback, string_callback, void_callback}; +use backup::ffi::*; use comm_opaque2::client::{Login, Registration}; use comm_opaque2::grpc::opaque_error_to_grpc_status as handle_error; +use ffi::{bool_callback, string_callback, void_callback}; use grpc_clients::identity::protos::authenticated::{ OutboundKeyInfo, OutboundKeysForUserRequest, UpdateUserPasswordFinishRequest, UpdateUserPasswordStartRequest, @@ -30,9 +31,12 @@ mod generated { // We get the CODE_VERSION from this generated file include!(concat!(env!("OUT_DIR"), "/version.rs")); + // We get the IDENTITY_SOCKET_ADDR from this generated file + include!(concat!(env!("OUT_DIR"), "/socket_config.rs")); } pub use generated::CODE_VERSION; +pub use generated::IDENTITY_SOCKET_ADDR; #[cfg(not(feature = "android"))] pub const DEVICE_TYPE: DeviceType = DeviceType::Ios; @@ -40,11 +44,10 @@ pub const DEVICE_TYPE: DeviceType = DeviceType::Android; lazy_static! { - pub static ref RUNTIME: Arc = + static ref RUNTIME: Arc = Arc::new(Builder::new_multi_thread().enable_all().build().unwrap()); } -use backup::ffi::*; #[cxx::bridge] mod ffi { @@ -245,7 +248,7 @@ async fn fetch_nonce() -> Result { let mut identity_client = get_unauthenticated_client( - "http://127.0.0.1:50054", + IDENTITY_SOCKET_ADDR, CODE_VERSION, DEVICE_TYPE.as_str_name().to_lowercase(), ) @@ -267,7 +270,7 @@ async fn version_supported_helper() -> Result { let mut identity_client = get_unauthenticated_client( - "http://127.0.0.1:50054", + IDENTITY_SOCKET_ADDR, CODE_VERSION, DEVICE_TYPE.as_str_name().to_lowercase(), ) @@ -375,7 +378,7 @@ }; let mut identity_client = get_unauthenticated_client( - "http://127.0.0.1:50054", + IDENTITY_SOCKET_ADDR, CODE_VERSION, DEVICE_TYPE.as_str_name().to_lowercase(), ) @@ -489,7 +492,7 @@ }; let mut identity_client = get_unauthenticated_client( - "http://127.0.0.1:50054", + IDENTITY_SOCKET_ADDR, CODE_VERSION, DEVICE_TYPE.as_str_name().to_lowercase(), ) @@ -613,7 +616,7 @@ }; let mut identity_client = get_unauthenticated_client( - "http://127.0.0.1:50054", + IDENTITY_SOCKET_ADDR, CODE_VERSION, DEVICE_TYPE.as_str_name().to_lowercase(), ) @@ -668,7 +671,7 @@ opaque_registration_request, }; let mut identity_client = get_auth_client( - "http://127.0.0.1:50054", + IDENTITY_SOCKET_ADDR, update_password_info.user_id, update_password_info.device_id, update_password_info.access_token, @@ -738,7 +741,7 @@ async fn delete_user_helper(auth_info: AuthInfo) -> Result<(), Error> { let mut identity_client = get_auth_client( - "http://127.0.0.1:50054", + IDENTITY_SOCKET_ADDR, auth_info.user_id, auth_info.device_id, auth_info.access_token, @@ -848,7 +851,7 @@ auth_info: AuthInfo, ) -> Result { let mut identity_client = get_auth_client( - "http://127.0.0.1:50054", + IDENTITY_SOCKET_ADDR, auth_info.user_id, auth_info.device_id, auth_info.access_token, @@ -889,9 +892,15 @@ #[cfg(test)] mod tests { use super::CODE_VERSION; + use super::IDENTITY_SOCKET_ADDR; #[test] fn test_code_version_exists() { assert!(CODE_VERSION > 0); } + + #[test] + fn test_identity_socket_addr_exists() { + assert!(IDENTITY_SOCKET_ADDR.len() > 0); + } }