From 572261def9537dea009569e2523d6e154b7d88de Mon Sep 17 00:00:00 2001 From: itsusinn Date: Thu, 5 Aug 2021 11:54:12 +0800 Subject: [PATCH] refactor(proxy): force using rustls_tls --- Cargo.toml | 3 +-- src/main.rs | 8 ++------ src/net.rs | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 src/net.rs diff --git a/Cargo.toml b/Cargo.toml index e17fb7f..fa16213 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,8 +48,7 @@ bytes = "1.0.1" # webhook warp = "0.3.0" -reqwest = { version = "0.11.4" ,default-features = false ,features = ["rustls"] } - +reqwest = { version = "0.11.4" ,default-features = false ,features = ["rustls","socks","rustls-tls-webpki-roots"] } nats = "0.10.1" async-nats = "0.10.1" diff --git a/src/main.rs b/src/main.rs index 696ebb3..a2fdd3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ mod data; mod message; mod webhook; mod despatch; +mod net; use config::CONFIG; use data::DATA; @@ -53,14 +54,9 @@ async fn run() -> Result<(), anyhow::Error> { Arc::new(Headers { inner }) }; - if CONFIG.proxy.enabled { - log::info!("Proxy will be enable"); - env::set_var("TELOXIDE_PROXY", &CONFIG.proxy.address); - } - let bot = Bot::with_client( CONFIG.telegram.token.clone(), - teloxide::net::client_from_env() + net::client_from_config() ).auto_send(); let clone_header = nats_header.clone(); diff --git a/src/net.rs b/src/net.rs new file mode 100644 index 0000000..1a67f3b --- /dev/null +++ b/src/net.rs @@ -0,0 +1,15 @@ + +fn default_reqwest_settings() -> reqwest::ClientBuilder { + teloxide_core::net::default_reqwest_settings() +} +pub fn client_from_config() -> reqwest::Client { + use crate::config::CONFIG; + let builder = default_reqwest_settings().use_rustls_tls(); + if CONFIG.proxy.enabled { + builder.proxy(reqwest::Proxy::all(&CONFIG.proxy.address).expect("reqwest::Proxy creation failed")) + } else { + builder + } + .build() + .expect("creating reqwest::Client") +} \ No newline at end of file