diff --git a/lib/grammers-client/src/client/net.rs b/lib/grammers-client/src/client/net.rs index 8e95406a..122b4c7c 100644 --- a/lib/grammers-client/src/client/net.rs +++ b/lib/grammers-client/src/client/net.rs @@ -30,6 +30,7 @@ use tokio::sync::{Mutex as AsyncMutex, RwLock as AsyncRwLock}; /// /// The addresses were obtained from the `static` addresses through a call to /// `functions::help::GetConfig`. +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] const DC_ADDRESSES: [(Ipv4Addr, u16); 6] = [ (Ipv4Addr::new(0, 0, 0, 0), 0), (Ipv4Addr::new(149, 154, 175, 53), 443), @@ -72,12 +73,13 @@ pub(crate) async fn connect_sender( #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] let transport = transport::Obfuscated::new(transport::Intermediate::new()); - let tcp_addr = DC_ADDRESSES[dc_id as usize].into(); let addr: ServerAddr = if let Some(ref sa) = config.params.server_addr { sa.clone() } else { #[cfg(all(not(all(target_arch = "wasm32", target_os = "unknown")),))] let addr = { + let tcp_addr = DC_ADDRESSES[dc_id as usize].into(); + #[cfg(not(feature = "proxy"))] let addr = ServerAddr::Tcp { address: tcp_addr }; @@ -117,9 +119,31 @@ pub(crate) async fn connect_sender( ); let (sender, tx) = - sender::connect(transport, addr, config.params.reconnection_policy).await?; - - config.session.insert_dc(dc_id, tcp_addr, sender.auth_key()); + sender::connect(transport, addr.clone(), config.params.reconnection_policy).await?; + + match addr { + #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] + ServerAddr::Tcp { ref address, .. } => { + config + .session + .insert_dc_tcp(dc_id, address, sender.auth_key()); + } + #[cfg(all( + not(all(target_arch = "wasm32", target_os = "unknown")), + feature = "proxy" + ))] + ServerAddr::Proxied { ref address, .. } => { + config + .session + .insert_dc_tcp(dc_id, address, sender.auth_key()); + } + #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] + ServerAddr::Ws { ref address } => { + config + .session + .insert_dc_ws(dc_id, address, sender.auth_key()); + } + } (sender, tx) }; diff --git a/lib/grammers-session/build.rs b/lib/grammers-session/build.rs index 0e12e2b3..79323860 100644 --- a/lib/grammers-session/build.rs +++ b/lib/grammers-session/build.rs @@ -12,7 +12,7 @@ use std::fs::File; use std::io::{BufWriter, Write}; use std::path::Path; -const CURRENT_VERSION: i32 = 2; +const CURRENT_VERSION: i32 = 3; fn main() -> std::io::Result<()> { let mut file = BufWriter::new(File::create( @@ -23,6 +23,7 @@ fn main() -> std::io::Result<()> { let definitions = parse_tl_file( r#" dataCenter flags:# id:int ipv4:flags.0?int ipv6:flags.1?int128 port:int auth:flags.2?bytes = DataCenter; + dataCenterWs flags:# id:int url:string auth:flags.0?bytes = DataCenter; user id:long dc:int bot:Bool = User; channelState channel_id:long pts:int = ChannelState; updateState pts:int qts:int date:int seq:int channels:Vector = UpdateState; diff --git a/lib/grammers-session/src/lib.rs b/lib/grammers-session/src/lib.rs index 2a2b1bd7..f7edda5d 100644 --- a/lib/grammers-session/src/lib.rs +++ b/lib/grammers-session/src/lib.rs @@ -91,38 +91,37 @@ impl Session { .unwrap() .dcs .iter() - .filter_map(|enums::DataCenter::Center(dc)| { - if dc.id == dc_id { - if let Some(auth) = &dc.auth { - let mut bytes = [0; 256]; - bytes.copy_from_slice(auth); - Some(bytes) - } else { - None - } - } else { - None - } + .filter_map(|dc| match dc { + enums::DataCenter::Center(types::DataCenter { + id, + auth: Some(auth), + .. + }) if *id == dc_id => auth.clone().try_into().ok(), + enums::DataCenter::Ws(types::DataCenterWs { + id, + auth: Some(auth), + .. + }) if *id == dc_id => auth.clone().try_into().ok(), + _ => None, }) .next() } - pub fn insert_dc(&self, id: i32, addr: SocketAddr, auth: [u8; 256]) { + fn insert_dc(&self, dc: enums::DataCenter) { let mut session = self.session.lock().unwrap(); - if let Some(pos) = session - .dcs - .iter() - .position(|enums::DataCenter::Center(dc)| dc.id == id) - { + if let Some(pos) = session.dcs.iter().position(|dc| dc.id() == dc.id()) { session.dcs.remove(pos); } + session.dcs.push(dc); + } - let (ip_v4, ip_v6): (Option<&SocketAddrV4>, Option<&SocketAddrV6>) = match &addr { + pub fn insert_dc_tcp(&self, id: i32, addr: &SocketAddr, auth: [u8; 256]) { + let (ip_v4, ip_v6): (Option<&SocketAddrV4>, Option<&SocketAddrV6>) = match addr { SocketAddr::V4(ip_v4) => (Some(ip_v4), None), SocketAddr::V6(ip_v6) => (None, Some(ip_v6)), }; - session.dcs.push( + self.insert_dc( types::DataCenter { id, ipv4: ip_v4.map(|addr| i32::from_le_bytes(addr.ip().octets())), @@ -134,6 +133,17 @@ impl Session { ); } + pub fn insert_dc_ws(&self, id: i32, url: &str, auth: [u8; 256]) { + self.insert_dc( + types::DataCenterWs { + id, + url: url.to_string(), + auth: Some(auth.into()), + } + .into(), + ); + } + pub fn set_user(&self, id: i64, dc: i32, bot: bool) { self.session.lock().unwrap().user = Some(User { id, dc, bot }.into()) } @@ -158,14 +168,8 @@ impl Session { self.session.lock().unwrap().state = Some(state.into()) } - pub fn get_dcs(&self) -> Vec { - self.session - .lock() - .unwrap() - .dcs - .iter() - .map(|enums::DataCenter::Center(dc)| dc.clone()) - .collect() + pub fn get_dcs(&self) -> Vec { + self.session.lock().unwrap().dcs.iter().cloned().collect() } #[must_use]