mirror of
https://github.com/Cyrix126/gupaxx.git
synced 2025-01-09 07:29:25 +00:00
node: deserialize get_info
when pinging
This commit is contained in:
parent
fc4e910e89
commit
2e5521e468
2 changed files with 45 additions and 13 deletions
50
src/node.rs
50
src/node.rs
|
@ -236,6 +236,25 @@ impl NodeData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------------------------- `/get_info`
|
||||||
|
// A struct repr of the JSON-RPC we're
|
||||||
|
// expecting back from the pinged nodes.
|
||||||
|
//
|
||||||
|
// This structs leave out most fields on purpose,
|
||||||
|
// we only need a few to verify it works.
|
||||||
|
#[derive(Debug, serde::Deserialize)]
|
||||||
|
struct GetInfo<'a> {
|
||||||
|
id: &'a str,
|
||||||
|
jsonrpc: &'a str,
|
||||||
|
result: GetInfoResult,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, serde::Deserialize)]
|
||||||
|
struct GetInfoResult {
|
||||||
|
mainnet: bool,
|
||||||
|
synchronized: bool,
|
||||||
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Ping data
|
//---------------------------------------------------------------------------------------------------- Ping data
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Ping {
|
pub struct Ping {
|
||||||
|
@ -363,26 +382,37 @@ impl Ping {
|
||||||
|
|
||||||
async fn response(client: Client<HttpConnector>, request: Request<Body>, ip: &'static str, ping: Arc<Mutex<Self>>, percent: f32, node_vec: Arc<Mutex<Vec<NodeData>>>) {
|
async fn response(client: Client<HttpConnector>, request: Request<Body>, ip: &'static str, ping: Arc<Mutex<Self>>, percent: f32, node_vec: Arc<Mutex<Vec<NodeData>>>) {
|
||||||
let ms;
|
let ms;
|
||||||
let info;
|
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
|
|
||||||
|
const DEAD_NODE_PING: u128 = 5000;
|
||||||
|
|
||||||
match tokio::time::timeout(Duration::from_secs(5), client.request(request)).await {
|
match tokio::time::timeout(Duration::from_secs(5), client.request(request)).await {
|
||||||
Ok(_) => {
|
Ok(Ok(json_rpc)) => {
|
||||||
|
// Attempt to convert to JSON-RPC.
|
||||||
|
match hyper::body::to_bytes(json_rpc.into_body()).await {
|
||||||
|
Ok(b) => {
|
||||||
|
if serde_json::from_slice::<GetInfo<'_>>(&b).is_ok() {
|
||||||
ms = now.elapsed().as_millis();
|
ms = now.elapsed().as_millis();
|
||||||
info = format!("{}ms ... {}", ms, ip);
|
} else {
|
||||||
info!("Ping | {}", info)
|
ms = DEAD_NODE_PING;
|
||||||
},
|
warn!("Ping | {ip} responded but with invalid get_info, remove this node!");
|
||||||
Err(_) => {
|
}
|
||||||
ms = 5000;
|
|
||||||
info = format!("{}ms ... {}", ms, ip);
|
|
||||||
warn!("Ping | {}", info)
|
|
||||||
},
|
},
|
||||||
|
_ => ms = DEAD_NODE_PING,
|
||||||
};
|
};
|
||||||
|
},
|
||||||
|
_ => ms = DEAD_NODE_PING,
|
||||||
|
};
|
||||||
|
|
||||||
|
let info = format!("{ms}ms ... {ip}");
|
||||||
|
info!("Ping | {ms}ms ... {ip}");
|
||||||
|
|
||||||
let color;
|
let color;
|
||||||
if ms < 300 {
|
if ms < 300 {
|
||||||
color = GREEN;
|
color = GREEN;
|
||||||
} else if ms < 500 {
|
} else if ms < 500 {
|
||||||
color = YELLOW;
|
color = YELLOW;
|
||||||
} else if ms < 5000 {
|
} else if ms < DEAD_NODE_PING {
|
||||||
color = RED;
|
color = RED;
|
||||||
} else {
|
} else {
|
||||||
color = BLACK;
|
color = BLACK;
|
||||||
|
|
|
@ -826,9 +826,11 @@ impl Pkg {
|
||||||
//---------------------------------------------------------------------------------------------------- Pkg functions
|
//---------------------------------------------------------------------------------------------------- Pkg functions
|
||||||
// Generate fake [User-Agent] HTTP header
|
// Generate fake [User-Agent] HTTP header
|
||||||
pub fn get_user_agent() -> &'static str {
|
pub fn get_user_agent() -> &'static str {
|
||||||
let rand = thread_rng().gen_range(0..50);
|
let index = FAKE_USER_AGENT.len() - 1;
|
||||||
|
|
||||||
|
let rand = thread_rng().gen_range(0..index);
|
||||||
let user_agent = FAKE_USER_AGENT[rand];
|
let user_agent = FAKE_USER_AGENT[rand];
|
||||||
info!("Randomly selected User-Agent ({}/50) ... {}", rand, user_agent);
|
info!("Randomly selected User-Agent ({rand}/{index}) ... {user_agent}");
|
||||||
user_agent
|
user_agent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue