diff --git a/Cargo.lock b/Cargo.lock index c03eceb..174e217 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3788,6 +3788,7 @@ dependencies = [ "grin_wallet_libwallet", "grin_wallet_util", "hyper 0.14.28", + "hyper-tls 0.5.0", "image 0.25.1", "jni", "lazy_static", @@ -4677,6 +4678,19 @@ dependencies = [ "tokio-tls", ] +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes 1.6.0", + "hyper 0.14.28", + "native-tls", + "tokio 1.37.0", + "tokio-native-tls", +] + [[package]] name = "i18n-config" version = "0.4.6" @@ -7385,7 +7399,7 @@ dependencies = [ "http-body 0.3.1", "hyper 0.13.10", "hyper-rustls 0.21.0", - "hyper-tls", + "hyper-tls 0.4.3", "ipnet", "js-sys", "lazy_static", @@ -9008,6 +9022,16 @@ dependencies = [ "syn 2.0.60", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio 1.37.0", +] + [[package]] name = "tokio-rustls" version = "0.13.1" diff --git a/Cargo.toml b/Cargo.toml index 3d10cd3..dd07c67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,6 +74,7 @@ sha2 = "0.10.0" ed25519-dalek = "2.1.1" curve25519-dalek = "4.1.2" hyper = { version = "0.14.28", features = ["full"] } +hyper-tls = "0.5.0" tls-api = "0.9.0" tls-api-native-tls = "0.9.0" diff --git a/src/wallet/connections/external.rs b/src/wallet/connections/external.rs index 35e91ee..45400af 100644 --- a/src/wallet/connections/external.rs +++ b/src/wallet/connections/external.rs @@ -12,11 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::time::Duration; - use serde_derive::{Deserialize, Serialize}; +use tor_rtcompat::BlockOn; +use tor_rtcompat::tokio::TokioNativeTlsRuntime; -use crate::AppConfig; use crate::wallet::ConnectionsConfig; /// External connection for the wallet. @@ -38,9 +37,6 @@ impl ExternalConnection { /// Default external node URL for main network. pub const DEFAULT_MAIN_URL: &'static str = "https://grinnode.live:3413"; - /// External connections availability check delay. - const AV_CHECK_DELAY: Duration = Duration::from_millis(60 * 1000); - /// Create default external connection. pub fn default_main() -> Self { Self { id: 1, url: Self::DEFAULT_MAIN_URL.to_string(), secret: None, available: None } @@ -57,19 +53,38 @@ impl ExternalConnection { // Check every connection at separate thread. let conn = self.clone(); std::thread::spawn(move || { - let url = url::Url::parse(conn.url.as_str()).unwrap(); - if let Ok(addr) = url.socket_addrs(|| None) { - match std::net::TcpStream::connect_timeout(&addr[0], Self::AV_CHECK_DELAY) { - Ok(_) => { - ConnectionsConfig::update_ext_conn_availability(conn.id, true); - } - Err(_) => { - ConnectionsConfig::update_ext_conn_availability(conn.id, false); + let runtime = TokioNativeTlsRuntime::create().unwrap(); + runtime.block_on(async { + let url = url::Url::parse(conn.url.as_str()).unwrap(); + if let Ok(_) = url.socket_addrs(|| None) { + let client = hyper::Client::builder() + .build::<_, hyper::Body>(hyper_tls::HttpsConnector::new()); + let req = hyper::Request::builder() + .method(hyper::Method::GET) + .uri(format!("{}/v2/owner", url.to_string())) + .body(hyper::Body::from( + r#"{"id":1,"jsonrpc":"2.0","method":"get_status","params":{} }"#) + ) + .unwrap(); + match client.request(req).await { + Ok(res) => { + let status = res.status().as_u16(); + // Available on 200 and 401 status code. + if status == 200 || status == 401 { + ConnectionsConfig::update_ext_conn_availability(conn.id, true); + } else { + ConnectionsConfig::update_ext_conn_availability(conn.id, false); + } + } + Err(e) => { + ConnectionsConfig::update_ext_conn_availability(conn.id, false); + } } + } else { + ConnectionsConfig::update_ext_conn_availability(conn.id, false); } - } else { - ConnectionsConfig::update_ext_conn_availability(conn.id, false); - } + }); + }); }