mirror of
https://github.com/telemt/telemt.git
synced 2026-04-17 18:44:10 +03:00
Format
This commit is contained in:
@@ -120,20 +120,18 @@ pub async fn detect_ip() -> IpInfo {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if !info.has_any() {
|
||||
warn!("Failed to detect public IP address");
|
||||
}
|
||||
|
||||
|
||||
info
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn is_private_ip(ip: IpAddr) -> bool {
|
||||
match ip {
|
||||
IpAddr::V4(ipv4) => {
|
||||
ipv4.is_private() || ipv4.is_loopback() || ipv4.is_link_local()
|
||||
}
|
||||
IpAddr::V4(ipv4) => ipv4.is_private() || ipv4.is_loopback() || ipv4.is_link_local(),
|
||||
IpAddr::V6(ipv6) => {
|
||||
ipv6.is_loopback() || (ipv6.segments()[0] & 0xfe00) == 0xfc00 // Unique Local
|
||||
}
|
||||
@@ -163,12 +161,12 @@ pub fn detect_ip_sync() -> IpInfo {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_ip_info() {
|
||||
let info = IpInfo::default();
|
||||
assert!(!info.has_any());
|
||||
|
||||
|
||||
let info = IpInfo {
|
||||
ipv4: Some("1.2.3.4".parse().unwrap()),
|
||||
ipv6: None,
|
||||
@@ -176,7 +174,7 @@ mod tests {
|
||||
assert!(info.has_any());
|
||||
assert_eq!(info.preferred(false), Some("1.2.3.4".parse().unwrap()));
|
||||
assert_eq!(info.preferred(true), Some("1.2.3.4".parse().unwrap()));
|
||||
|
||||
|
||||
let info = IpInfo {
|
||||
ipv4: Some("1.2.3.4".parse().unwrap()),
|
||||
ipv6: Some("::1".parse().unwrap()),
|
||||
@@ -184,4 +182,4 @@ mod tests {
|
||||
assert_eq!(info.preferred(false), Some("1.2.3.4".parse().unwrap()));
|
||||
assert_eq!(info.preferred(true), Some("::1".parse().unwrap()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,4 @@ pub mod time;
|
||||
#[allow(unused_imports)]
|
||||
pub use ip::*;
|
||||
#[allow(unused_imports)]
|
||||
pub use time::*;
|
||||
pub use time::*;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
//! Time Sync
|
||||
|
||||
use std::time::Duration;
|
||||
use chrono::{DateTime, Utc};
|
||||
use tracing::{debug, warn, error};
|
||||
use std::time::Duration;
|
||||
use tracing::{debug, error, warn};
|
||||
|
||||
#[allow(dead_code)]
|
||||
const TIME_SYNC_URL: &str = "https://core.telegram.org/getProxySecret";
|
||||
@@ -26,29 +26,29 @@ pub async fn check_time_sync() -> Option<TimeSyncResult> {
|
||||
.timeout(Duration::from_secs(10))
|
||||
.build()
|
||||
.ok()?;
|
||||
|
||||
|
||||
let response = client.get(TIME_SYNC_URL).send().await.ok()?;
|
||||
|
||||
|
||||
// Get Date header
|
||||
let date_header = response.headers().get("date")?;
|
||||
let date_str = date_header.to_str().ok()?;
|
||||
|
||||
|
||||
// Parse date
|
||||
let server_time = DateTime::parse_from_rfc2822(date_str)
|
||||
.ok()?
|
||||
.with_timezone(&Utc);
|
||||
|
||||
|
||||
let local_time = Utc::now();
|
||||
let skew_secs = (local_time - server_time).num_seconds();
|
||||
let is_skewed = skew_secs.abs() > MAX_TIME_SKEW_SECS;
|
||||
|
||||
|
||||
let result = TimeSyncResult {
|
||||
server_time,
|
||||
local_time,
|
||||
skew_secs,
|
||||
is_skewed,
|
||||
};
|
||||
|
||||
|
||||
if is_skewed {
|
||||
warn!(
|
||||
server = %server_time,
|
||||
@@ -59,7 +59,7 @@ pub async fn check_time_sync() -> Option<TimeSyncResult> {
|
||||
} else {
|
||||
debug!(skew = skew_secs, "Time sync OK");
|
||||
}
|
||||
|
||||
|
||||
Some(result)
|
||||
}
|
||||
|
||||
@@ -78,4 +78,4 @@ pub async fn time_sync_task(check_interval: Duration) -> ! {
|
||||
|
||||
tokio::time::sleep(check_interval).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user