network: connections availability check auth
This commit is contained in:
parent
7412d888c6
commit
3e67d2f379
1 changed files with 20 additions and 10 deletions
|
@ -12,6 +12,7 @@
|
||||||
// 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 grin_util::to_base64;
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
use tor_rtcompat::BlockOn;
|
use tor_rtcompat::BlockOn;
|
||||||
use tor_rtcompat::tokio::TokioNativeTlsRuntime;
|
use tor_rtcompat::tokio::TokioNativeTlsRuntime;
|
||||||
|
@ -57,26 +58,35 @@ impl ExternalConnection {
|
||||||
runtime.block_on(async {
|
runtime.block_on(async {
|
||||||
let url = url::Url::parse(conn.url.as_str()).unwrap();
|
let url = url::Url::parse(conn.url.as_str()).unwrap();
|
||||||
if let Ok(_) = url.socket_addrs(|| None) {
|
if let Ok(_) = url.socket_addrs(|| None) {
|
||||||
|
let addr = format!("{}v2/foreign", url.to_string());
|
||||||
|
// Setup http client.
|
||||||
let client = hyper::Client::builder()
|
let client = hyper::Client::builder()
|
||||||
.build::<_, hyper::Body>(hyper_tls::HttpsConnector::new());
|
.build::<_, hyper::Body>(hyper_tls::HttpsConnector::new());
|
||||||
let req = hyper::Request::builder()
|
let mut req_setup = hyper::Request::builder()
|
||||||
.method(hyper::Method::GET)
|
.method(hyper::Method::POST)
|
||||||
.uri(format!("{}/v2/owner", url.to_string()))
|
.uri(addr.clone());
|
||||||
.body(hyper::Body::from(
|
// Setup secret key auth.
|
||||||
r#"{"id":1,"jsonrpc":"2.0","method":"get_status","params":{} }"#)
|
if let Some(key) = conn.secret {
|
||||||
)
|
let basic_auth = format!("Basic {}", to_base64(&format!("grin:{}", key)));
|
||||||
.unwrap();
|
req_setup = req_setup
|
||||||
|
.header(hyper::header::AUTHORIZATION, basic_auth.clone());
|
||||||
|
println!("{} auth: {}", addr, basic_auth);
|
||||||
|
}
|
||||||
|
let req = req_setup.body(hyper::Body::from(
|
||||||
|
r#"{"id":1,"jsonrpc":"2.0","method":"get_version","params":{} }"#)
|
||||||
|
).unwrap();
|
||||||
|
// Send request.
|
||||||
match client.request(req).await {
|
match client.request(req).await {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let status = res.status().as_u16();
|
let status = res.status().as_u16();
|
||||||
// Available on 200 and 401 status code.
|
// Available on 200 HTTP status code.
|
||||||
if status == 200 || status == 401 {
|
if status == 200 {
|
||||||
ConnectionsConfig::update_ext_conn_availability(conn.id, true);
|
ConnectionsConfig::update_ext_conn_availability(conn.id, true);
|
||||||
} else {
|
} else {
|
||||||
ConnectionsConfig::update_ext_conn_availability(conn.id, false);
|
ConnectionsConfig::update_ext_conn_availability(conn.id, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(_) => {
|
||||||
ConnectionsConfig::update_ext_conn_availability(conn.id, false);
|
ConnectionsConfig::update_ext_conn_availability(conn.id, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue