mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-12-05 07:48:06 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
047bbcc7ca | ||
|
|
8f2e25f837 |
+82
-5
@@ -4,10 +4,11 @@ use hbb_common::{
|
||||
config::Config,
|
||||
get_time,
|
||||
password_security::{decrypt_vec_or_original, encrypt_vec_or_original},
|
||||
tls::TlsType,
|
||||
ResultType,
|
||||
};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::sync::Mutex;
|
||||
use std::{fmt, sync::Mutex, time::Duration};
|
||||
use totp_rs::{Algorithm, Secret, TOTP};
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
@@ -111,7 +112,7 @@ pub fn get_2fa(raw: Option<String>) -> Option<TOTP> {
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[derive(Clone, Default, Serialize, Deserialize)]
|
||||
pub struct TelegramBot {
|
||||
#[serde(skip)]
|
||||
pub token_str: String,
|
||||
@@ -119,6 +120,16 @@ pub struct TelegramBot {
|
||||
pub chat_id: String,
|
||||
}
|
||||
|
||||
impl fmt::Debug for TelegramBot {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("TelegramBot")
|
||||
.field("token_str", &"[redacted]")
|
||||
.field("token", &"[redacted]")
|
||||
.field("chat_id", &self.chat_id)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl TelegramBot {
|
||||
fn into_string(&self) -> ResultType<String> {
|
||||
let token = encrypt_vec_or_original(self.token_str.as_bytes(), "00", 1024);
|
||||
@@ -154,18 +165,39 @@ impl TelegramBot {
|
||||
}
|
||||
}
|
||||
|
||||
// The URL carries the bot token: never fall back to accepting an invalid certificate, and drop
|
||||
// the URL from errors so the token stays out of the log.
|
||||
async fn post_telegram(url: String, body: String) -> ResultType<String> {
|
||||
let resp = crate::hbbs_http::create_http_client_async(TlsType::Rustls, false)
|
||||
.post(url)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(body)
|
||||
.timeout(Duration::from_secs(12))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| anyhow!("{:?}", e.without_url()))?;
|
||||
resp.text()
|
||||
.await
|
||||
.map_err(|e| anyhow!("{:?}", e.without_url()))
|
||||
}
|
||||
|
||||
// https://gist.github.com/dideler/85de4d64f66c1966788c1b2304b9caf1
|
||||
pub async fn send_2fa_code_to_telegram(text: &str, bot: TelegramBot) -> ResultType<()> {
|
||||
let url = format!("https://api.telegram.org/bot{}/sendMessage", bot.token_str);
|
||||
let params = serde_json::json!({"chat_id": bot.chat_id, "text": text});
|
||||
crate::post_request(url, params.to_string(), "").await?;
|
||||
post_telegram(url, params.to_string()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_chatid_telegram(bot_token: &str) -> ResultType<Option<String>> {
|
||||
let url = format!("https://api.telegram.org/bot{}/getUpdates", bot_token);
|
||||
// because caller is in tokio runtime, so we must call post_request_sync in new thread.
|
||||
let handle = std::thread::spawn(move || crate::post_request_sync(url, "".to_owned(), ""));
|
||||
// because caller is in tokio runtime, so we must run the request in a new thread.
|
||||
let handle = std::thread::spawn(move || -> ResultType<String> {
|
||||
hbb_common::tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()?
|
||||
.block_on(post_telegram(url, String::new()))
|
||||
});
|
||||
let resp = handle.join().map_err(|_| anyhow!("Thread panicked"))??;
|
||||
let value = serde_json::from_str::<serde_json::Value>(&resp).map_err(|e| anyhow!(e))?;
|
||||
|
||||
@@ -202,3 +234,48 @@ pub fn get_chatid_telegram(bot_token: &str) -> ResultType<Option<String>> {
|
||||
|
||||
Ok(chat_id)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{post_telegram, TelegramBot};
|
||||
use hbb_common::tokio::{self, net::TcpListener};
|
||||
use std::sync::{
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
Arc,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn telegram_bot_debug_redacts_both_token_fields() {
|
||||
let bot = TelegramBot {
|
||||
token_str: "test-telegram-token".to_owned(),
|
||||
token: vec![17, 29, 43],
|
||||
chat_id: "test-chat".to_owned(),
|
||||
};
|
||||
let debug = format!("{bot:?}");
|
||||
|
||||
assert!(!debug.contains(&bot.token_str));
|
||||
assert!(!debug.contains(&format!("{:?}", bot.token)));
|
||||
assert!(debug.contains(&bot.chat_id));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn telegram_request_has_no_tls_fallback_and_hides_the_token() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
let attempts = Arc::new(AtomicUsize::new(0));
|
||||
let counter = attempts.clone();
|
||||
tokio::spawn(async move {
|
||||
while let Ok((stream, _)) = listener.accept().await {
|
||||
counter.fetch_add(1, Ordering::SeqCst);
|
||||
drop(stream);
|
||||
}
|
||||
});
|
||||
let token = "123456:test-telegram-token";
|
||||
let url = format!("https://127.0.0.1:{port}/bot{token}/sendMessage");
|
||||
|
||||
let err = post_telegram(url, String::new()).await.unwrap_err();
|
||||
|
||||
assert!(!err.to_string().contains(token), "{err}");
|
||||
assert_eq!(attempts.load(Ordering::SeqCst), 1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user