From 8b18dfba410f749ad30fe814fe1aa731cba24527 Mon Sep 17 00:00:00 2001 From: Inaha Date: Mon, 9 Feb 2026 23:18:46 +0800 Subject: [PATCH 1/5] Support IPv6: add a env var for IPv6 selection Signed-off-by: Inaha --- grammers-mtsender/src/sender_pool.rs | 14 +++++++++++--- grammers-session/src/dc_options.rs | 16 ++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/grammers-mtsender/src/sender_pool.rs b/grammers-mtsender/src/sender_pool.rs index 8be1bfaf..24bb09db 100644 --- a/grammers-mtsender/src/sender_pool.rs +++ b/grammers-mtsender/src/sender_pool.rs @@ -291,22 +291,30 @@ impl SenderPoolRunner { ) -> Result, InvocationError> { let transport = transport::Full::new; + let address = match std::env::var("PREFER_V6") { + Ok(val) if val == "1" || val.eq_ignore_ascii_case("true") => { + log::info!("Using IPv6 connection"); + dc_option.ipv6.into() + } + _ => dc_option.ipv4.into() + }; + #[cfg(feature = "proxy")] let addr = || { if let Some(proxy) = self.connection_params.proxy_url.clone() { ServerAddr::Proxied { - address: dc_option.ipv4.into(), + address, proxy, } } else { ServerAddr::Tcp { - address: dc_option.ipv4.into(), + address, } } }; #[cfg(not(feature = "proxy"))] let addr = || ServerAddr::Tcp { - address: dc_option.ipv4.into(), + address, }; let init_connection = tl::functions::InvokeWithLayer { diff --git a/grammers-session/src/dc_options.rs b/grammers-session/src/dc_options.rs index e5fc8d5b..7af0aad5 100644 --- a/grammers-session/src/dc_options.rs +++ b/grammers-session/src/dc_options.rs @@ -6,7 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::net::{Ipv4Addr, SocketAddrV4, SocketAddrV6}; +use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6}; use crate::types::DcOption; @@ -16,8 +16,8 @@ const fn ipv4(a: u8, b: u8, c: u8, d: u8) -> SocketAddrV4 { SocketAddrV4::new(Ipv4Addr::new(a, b, c, d), 443) } -const fn ipv6(a: u8, b: u8, c: u8, d: u8) -> SocketAddrV6 { - SocketAddrV6::new(ipv4(a, b, c, d).ip().to_ipv6_compatible(), 443, 0, 0) +const fn ipv6(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> SocketAddrV6 { + SocketAddrV6::new(Ipv6Addr::new(a, b, c, d, e, f, g, h), 443, 0, 0) } /// Hardcoded known `static` options from `functions::help::GetConfig`. @@ -25,31 +25,31 @@ pub(crate) const KNOWN_DC_OPTIONS: [DcOption; 5] = [ DcOption { id: 1, ipv4: ipv4(149, 154, 175, 53), - ipv6: ipv6(149, 154, 175, 53), + ipv6: ipv6(0x2001, 0xb28, 0xf23d, 0xf001, 0, 0, 0, 0xa), auth_key: None, }, DcOption { id: 2, ipv4: ipv4(149, 154, 167, 41), - ipv6: ipv6(149, 154, 167, 41), + ipv6: ipv6(0x2001, 0x67c, 0x4e8, 0xf002, 0, 0, 0, 0xa), auth_key: None, }, DcOption { id: 3, ipv4: ipv4(149, 154, 175, 100), - ipv6: ipv6(149, 154, 175, 100), + ipv6: ipv6(0x2001, 0xb28, 0xf23d, 0xf003, 0, 0, 0, 0xa), auth_key: None, }, DcOption { id: 4, ipv4: ipv4(149, 154, 167, 92), - ipv6: ipv6(149, 154, 167, 92), + ipv6: ipv6(0x2001, 0x67c, 0x4e8, 0xf004, 0, 0, 0, 0xa), auth_key: None, }, DcOption { id: 5, ipv4: ipv4(91, 108, 56, 104), - ipv6: ipv6(91, 108, 56, 104), + ipv6: ipv6(0x2001, 0xb28, 0xf23f, 0xf005, 0, 0, 0, 0xa), auth_key: None, }, ]; From 4d4dcd41cecb7b3727aeb1c5c0794d5af4993f25 Mon Sep 17 00:00:00 2001 From: Inaha Date: Tue, 10 Feb 2026 12:38:21 +0800 Subject: [PATCH 2/5] Add an `use_ipv6` option in `ConnectionParam` and a method `use_ipv6()` to enable it in `SenderPool` Signed-off-by: Inaha --- grammers-mtsender/src/configuration.rs | 2 ++ grammers-mtsender/src/sender_pool.rs | 30 ++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/grammers-mtsender/src/configuration.rs b/grammers-mtsender/src/configuration.rs index 60d11878..c4d76866 100644 --- a/grammers-mtsender/src/configuration.rs +++ b/grammers-mtsender/src/configuration.rs @@ -38,6 +38,7 @@ pub struct ConnectionParams { /// the host manually and selecting an IP address of your choice. #[cfg(feature = "proxy")] pub proxy_url: Option, + pub use_ipv6: bool, #[doc(hidden)] pub __non_exhaustive: (), } @@ -67,6 +68,7 @@ impl Default for ConnectionParams { app_version: env!("CARGO_PKG_VERSION").to_string(), system_lang_code, lang_code, + use_ipv6: false, #[cfg(feature = "proxy")] proxy_url: None, __non_exhaustive: (), diff --git a/grammers-mtsender/src/sender_pool.rs b/grammers-mtsender/src/sender_pool.rs index 24bb09db..f46558b7 100644 --- a/grammers-mtsender/src/sender_pool.rs +++ b/grammers-mtsender/src/sender_pool.rs @@ -161,6 +161,12 @@ impl SenderPool { Self::with_configuration(session, api_id, Default::default()) } + // Enable IPv6 for connections created by this sender pool. + pub fn use_ipv6(mut self) -> Self { + self.runner.connection_params.use_ipv6 = true; + self + } + /// Creates a new sender pool with non-[`ConnectionParams::default`] configuration. pub fn with_configuration( session: Arc, @@ -291,31 +297,23 @@ impl SenderPoolRunner { ) -> Result, InvocationError> { let transport = transport::Full::new; - let address = match std::env::var("PREFER_V6") { - Ok(val) if val == "1" || val.eq_ignore_ascii_case("true") => { - log::info!("Using IPv6 connection"); - dc_option.ipv6.into() - } - _ => dc_option.ipv4.into() + let address = if self.connection_params.use_ipv6 { + log::info!("Using IPv6 connection"); + dc_option.ipv6.into() + } else { + dc_option.ipv4.into() }; #[cfg(feature = "proxy")] let addr = || { if let Some(proxy) = self.connection_params.proxy_url.clone() { - ServerAddr::Proxied { - address, - proxy, - } + ServerAddr::Proxied { address, proxy } } else { - ServerAddr::Tcp { - address, - } + ServerAddr::Tcp { address } } }; #[cfg(not(feature = "proxy"))] - let addr = || ServerAddr::Tcp { - address, - }; + let addr = || ServerAddr::Tcp { address }; let init_connection = tl::functions::InvokeWithLayer { layer: tl::LAYER, From 08e8bbaf610e3d467a5fe015f23ba6204d9cb92b Mon Sep 17 00:00:00 2001 From: Lonami Date: Tue, 10 Feb 2026 18:06:16 +0100 Subject: [PATCH 3/5] Apply suggestions from code review --- grammers-mtsender/src/configuration.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/grammers-mtsender/src/configuration.rs b/grammers-mtsender/src/configuration.rs index c4d76866..0163ba94 100644 --- a/grammers-mtsender/src/configuration.rs +++ b/grammers-mtsender/src/configuration.rs @@ -38,6 +38,7 @@ pub struct ConnectionParams { /// the host manually and selecting an IP address of your choice. #[cfg(feature = "proxy")] pub proxy_url: Option, + /// Whether to connect via IPv6 instead of defaulting to IPv4. pub use_ipv6: bool, #[doc(hidden)] pub __non_exhaustive: (), From 78b3b43c7b48541fd5f49fa098087161cbd33f0f Mon Sep 17 00:00:00 2001 From: Lonami Date: Tue, 10 Feb 2026 18:06:55 +0100 Subject: [PATCH 4/5] Update grammers-mtsender/src/sender_pool.rs --- grammers-mtsender/src/sender_pool.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/grammers-mtsender/src/sender_pool.rs b/grammers-mtsender/src/sender_pool.rs index f46558b7..3f7c00e5 100644 --- a/grammers-mtsender/src/sender_pool.rs +++ b/grammers-mtsender/src/sender_pool.rs @@ -161,12 +161,6 @@ impl SenderPool { Self::with_configuration(session, api_id, Default::default()) } - // Enable IPv6 for connections created by this sender pool. - pub fn use_ipv6(mut self) -> Self { - self.runner.connection_params.use_ipv6 = true; - self - } - /// Creates a new sender pool with non-[`ConnectionParams::default`] configuration. pub fn with_configuration( session: Arc, From b6296ea944fa3b17ce80996520477542e4d8a889 Mon Sep 17 00:00:00 2001 From: Lonami Date: Tue, 10 Feb 2026 18:07:03 +0100 Subject: [PATCH 5/5] Update grammers-mtsender/src/sender_pool.rs --- grammers-mtsender/src/sender_pool.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/grammers-mtsender/src/sender_pool.rs b/grammers-mtsender/src/sender_pool.rs index 3f7c00e5..2afd5b47 100644 --- a/grammers-mtsender/src/sender_pool.rs +++ b/grammers-mtsender/src/sender_pool.rs @@ -292,7 +292,6 @@ impl SenderPoolRunner { let transport = transport::Full::new; let address = if self.connection_params.use_ipv6 { - log::info!("Using IPv6 connection"); dc_option.ipv6.into() } else { dc_option.ipv4.into()