Skip to content

Commit

Permalink
Support storing ws datacenters in the session
Browse files Browse the repository at this point in the history
  • Loading branch information
er-azh authored and Lonami committed Dec 23, 2024
1 parent 49a17d7 commit a2aa7a4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
32 changes: 28 additions & 4 deletions lib/grammers-client/src/client/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(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 };

Expand Down Expand Up @@ -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)
};

Expand Down
3 changes: 2 additions & 1 deletion lib/grammers-session/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<ChannelState> = UpdateState;
Expand Down
60 changes: 32 additions & 28 deletions lib/grammers-session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
Expand All @@ -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())
}
Expand All @@ -158,14 +168,8 @@ impl Session {
self.session.lock().unwrap().state = Some(state.into())
}

pub fn get_dcs(&self) -> Vec<types::DataCenter> {
self.session
.lock()
.unwrap()
.dcs
.iter()
.map(|enums::DataCenter::Center(dc)| dc.clone())
.collect()
pub fn get_dcs(&self) -> Vec<enums::DataCenter> {
self.session.lock().unwrap().dcs.iter().cloned().collect()
}

#[must_use]
Expand Down

0 comments on commit a2aa7a4

Please sign in to comment.