network: connections availability check fix

This commit is contained in:
ardocrat 2024-05-16 16:52:56 +03:00
parent 913c53303e
commit 7412d888c6
3 changed files with 58 additions and 18 deletions

26
Cargo.lock generated
View file

@ -3788,6 +3788,7 @@ dependencies = [
"grin_wallet_libwallet", "grin_wallet_libwallet",
"grin_wallet_util", "grin_wallet_util",
"hyper 0.14.28", "hyper 0.14.28",
"hyper-tls 0.5.0",
"image 0.25.1", "image 0.25.1",
"jni", "jni",
"lazy_static", "lazy_static",
@ -4677,6 +4678,19 @@ dependencies = [
"tokio-tls", "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]] [[package]]
name = "i18n-config" name = "i18n-config"
version = "0.4.6" version = "0.4.6"
@ -7385,7 +7399,7 @@ dependencies = [
"http-body 0.3.1", "http-body 0.3.1",
"hyper 0.13.10", "hyper 0.13.10",
"hyper-rustls 0.21.0", "hyper-rustls 0.21.0",
"hyper-tls", "hyper-tls 0.4.3",
"ipnet", "ipnet",
"js-sys", "js-sys",
"lazy_static", "lazy_static",
@ -9008,6 +9022,16 @@ dependencies = [
"syn 2.0.60", "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]] [[package]]
name = "tokio-rustls" name = "tokio-rustls"
version = "0.13.1" version = "0.13.1"

View file

@ -74,6 +74,7 @@ sha2 = "0.10.0"
ed25519-dalek = "2.1.1" ed25519-dalek = "2.1.1"
curve25519-dalek = "4.1.2" curve25519-dalek = "4.1.2"
hyper = { version = "0.14.28", features = ["full"] } hyper = { version = "0.14.28", features = ["full"] }
hyper-tls = "0.5.0"
tls-api = "0.9.0" tls-api = "0.9.0"
tls-api-native-tls = "0.9.0" tls-api-native-tls = "0.9.0"

View file

@ -12,11 +12,10 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::time::Duration;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use tor_rtcompat::BlockOn;
use tor_rtcompat::tokio::TokioNativeTlsRuntime;
use crate::AppConfig;
use crate::wallet::ConnectionsConfig; use crate::wallet::ConnectionsConfig;
/// External connection for the wallet. /// External connection for the wallet.
@ -38,9 +37,6 @@ impl ExternalConnection {
/// Default external node URL for main network. /// Default external node URL for main network.
pub const DEFAULT_MAIN_URL: &'static str = "https://grinnode.live:3413"; 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. /// Create default external connection.
pub fn default_main() -> Self { pub fn default_main() -> Self {
Self { id: 1, url: Self::DEFAULT_MAIN_URL.to_string(), secret: None, available: None } 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. // Check every connection at separate thread.
let conn = self.clone(); let conn = self.clone();
std::thread::spawn(move || { std::thread::spawn(move || {
let url = url::Url::parse(conn.url.as_str()).unwrap(); let runtime = TokioNativeTlsRuntime::create().unwrap();
if let Ok(addr) = url.socket_addrs(|| None) { runtime.block_on(async {
match std::net::TcpStream::connect_timeout(&addr[0], Self::AV_CHECK_DELAY) { let url = url::Url::parse(conn.url.as_str()).unwrap();
Ok(_) => { if let Ok(_) = url.socket_addrs(|| None) {
ConnectionsConfig::update_ext_conn_availability(conn.id, true); let client = hyper::Client::builder()
} .build::<_, hyper::Body>(hyper_tls::HttpsConnector::new());
Err(_) => { let req = hyper::Request::builder()
ConnectionsConfig::update_ext_conn_availability(conn.id, false); .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);
}
}); });
} }