temp
This commit is contained in:
@@ -506,36 +506,103 @@ fn parse_pack(s: &str) -> Option<[u8; 6]> {
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn quick_ping(addr: &SocketAddr, timeout_ms: u64) -> bool {
|
||||||
|
let connect =
|
||||||
|
std::net::TcpStream::connect_timeout(addr, std::time::Duration::from_millis(timeout_ms));
|
||||||
|
connect.is_ok()
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// sends a get request to the server to get the socket address of the given peer
|
/// sends a get request to the server to get the socket address of the given peer
|
||||||
///
|
///
|
||||||
|
|
||||||
pub async fn get_socket_address(username: String, ip: String) -> Option<SocketAddr> {
|
pub async fn get_socket_address(username: String, ip: String) -> Option<SocketAddr> {
|
||||||
let client = Client::builder()
|
let client = match Client::builder().timeout(Duration::from_secs(5)).build() {
|
||||||
.timeout(Duration::from_secs(5))
|
Ok(c) => c,
|
||||||
.build()
|
Err(e) => {
|
||||||
.expect("cannot create client");
|
eprintln!("Failed to build HTTP client: {}", e);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let uri = format!("{}/peers/{}/addresses", ip, username);
|
let uri = format!("{}/peers/{}/addresses", ip, username);
|
||||||
let res = client.get(uri).send().await.expect("couldnt get response");
|
let res = match client.get(&uri).send().await {
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("HTTP request error: {}", e);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if res.status().is_success() {
|
if res.status().is_success() {
|
||||||
println!("Successfully retreived the addresses. {}", res.status());
|
println!("Successfully retrieved the addresses. {}", res.status());
|
||||||
} else {
|
} else {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"Failed to get the peers addresses from the server. Status: {}",
|
"Failed to get the peers addresses from the server. Status: {}",
|
||||||
res.status()
|
res.status()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let body: Bytes = res.bytes().await.expect("couldnt get bytes");
|
|
||||||
|
|
||||||
match String::from_utf8(body.to_vec()) {
|
let body = match res.bytes().await {
|
||||||
Ok(s) => {
|
Ok(b) => b,
|
||||||
let addresses = parse_addresses(&s);
|
Err(e) => {
|
||||||
if let Some(first) = addresses.first() {
|
eprintln!("Failed to read response body: {}", e);
|
||||||
Some(first.clone())
|
return None;
|
||||||
} else {
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let s = match String::from_utf8(body.to_vec()) {
|
||||||
|
Ok(st) => st,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Response not valid UTF-8: {}", e);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let addresses = parse_addresses(&s); // assumes parse_addresses: &str -> Vec<SocketAddr>
|
||||||
|
|
||||||
|
for addr in addresses {
|
||||||
|
if quick_ping(&addr, 300) {
|
||||||
|
return Some(addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: nat_traversal(&addr).await;
|
||||||
|
// after NAT traversal attempt, ping again
|
||||||
|
|
||||||
|
if quick_ping(&addr, 300) {
|
||||||
|
return Some(addr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Err(_) => None,
|
//pub async fn get_socket_address(username: String, ip: String) -> Option<SocketAddr> {
|
||||||
}
|
// let client = Client::builder()
|
||||||
}
|
// .timeout(Duration::from_secs(5))
|
||||||
|
// .build()
|
||||||
|
// .expect("cannot create client");
|
||||||
|
// let uri = format!("{}/peers/{}/addresses", ip, username);
|
||||||
|
// let res = client.get(uri).send().await.expect("couldnt get response");
|
||||||
|
// if res.status().is_success() {
|
||||||
|
// println!("Successfully retreived the addresses. {}", res.status());
|
||||||
|
// } else {
|
||||||
|
// eprintln!(
|
||||||
|
// "Failed to get the peers addresses from the server. Status: {}",
|
||||||
|
// res.status()
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// let body: Bytes = res.bytes().await.expect("couldnt get bytes");
|
||||||
|
//
|
||||||
|
// match String::from_utf8(body.to_vec()) {
|
||||||
|
// Ok(s) => {
|
||||||
|
// let addresses = parse_addresses(&s);
|
||||||
|
// if let Some(first) = addresses.first() {
|
||||||
|
// Some(first.clone())
|
||||||
|
// } else {
|
||||||
|
// None
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// Err(_) => None,
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|||||||
Reference in New Issue
Block a user