2022-08-07 06:24:57 +08:00
|
|
|
use dns_lookup::{lookup_addr, lookup_host};
|
2022-07-21 17:07:16 +08:00
|
|
|
use hbb_common::{bail, ResultType};
|
2022-07-14 21:59:39 +08:00
|
|
|
use sodiumoxide::crypto::sign;
|
2022-08-07 06:24:57 +08:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
net::{IpAddr, TcpStream},
|
|
|
|
process, str,
|
|
|
|
};
|
2022-07-14 21:59:39 +08:00
|
|
|
|
|
|
|
fn print_help() {
|
2022-07-21 17:07:16 +08:00
|
|
|
println!(
|
|
|
|
"Usage:
|
|
|
|
rustdesk-util [command]\n
|
|
|
|
Available Commands:
|
|
|
|
genkeypair Generate a new keypair
|
2022-08-07 06:24:57 +08:00
|
|
|
validatekeypair [public key] [secret key] Validate an existing keypair
|
|
|
|
doctor [rustdesk-server] Check for server connection problems"
|
2022-07-21 17:07:16 +08:00
|
|
|
);
|
2022-07-14 21:59:39 +08:00
|
|
|
process::exit(0x0001);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error_then_help(msg: &str) {
|
|
|
|
println!("ERROR: {}\n", msg);
|
|
|
|
print_help();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gen_keypair() {
|
|
|
|
let (pk, sk) = sign::gen_keypair();
|
|
|
|
let public_key = base64::encode(pk);
|
|
|
|
let secret_key = base64::encode(sk);
|
|
|
|
println!("Public Key: {public_key}");
|
|
|
|
println!("Secret Key: {secret_key}");
|
|
|
|
}
|
|
|
|
|
2022-07-21 17:07:16 +08:00
|
|
|
fn validate_keypair(pk: &str, sk: &str) -> ResultType<()> {
|
2022-08-02 15:52:01 +08:00
|
|
|
let sk1 = base64::decode(sk);
|
2022-07-21 17:07:16 +08:00
|
|
|
if sk1.is_err() {
|
|
|
|
bail!("Invalid secret key");
|
2022-07-14 21:59:39 +08:00
|
|
|
}
|
|
|
|
let sk1 = sk1.unwrap();
|
|
|
|
|
|
|
|
let secret_key = sign::SecretKey::from_slice(sk1.as_slice());
|
2022-07-21 17:07:16 +08:00
|
|
|
if secret_key.is_none() {
|
|
|
|
bail!("Invalid Secret key");
|
2022-07-14 21:59:39 +08:00
|
|
|
}
|
|
|
|
let secret_key = secret_key.unwrap();
|
|
|
|
|
2022-08-02 15:52:01 +08:00
|
|
|
let pk1 = base64::decode(pk);
|
2022-07-21 17:07:16 +08:00
|
|
|
if pk1.is_err() {
|
|
|
|
bail!("Invalid public key");
|
2022-07-14 21:59:39 +08:00
|
|
|
}
|
|
|
|
let pk1 = pk1.unwrap();
|
|
|
|
|
|
|
|
let public_key = sign::PublicKey::from_slice(pk1.as_slice());
|
2022-07-21 17:07:16 +08:00
|
|
|
if public_key.is_none() {
|
|
|
|
bail!("Invalid Public key");
|
2022-07-14 21:59:39 +08:00
|
|
|
}
|
|
|
|
let public_key = public_key.unwrap();
|
|
|
|
|
|
|
|
let random_data_to_test = b"This is meh.";
|
|
|
|
let signed_data = sign::sign(random_data_to_test, &secret_key);
|
|
|
|
let verified_data = sign::verify(&signed_data, &public_key);
|
2022-07-21 17:07:16 +08:00
|
|
|
if verified_data.is_err() {
|
|
|
|
bail!("Key pair is INVALID");
|
2022-07-14 21:59:39 +08:00
|
|
|
}
|
|
|
|
let verified_data = verified_data.unwrap();
|
|
|
|
|
2022-07-21 17:07:16 +08:00
|
|
|
if random_data_to_test != &verified_data[..] {
|
|
|
|
bail!("Key pair is INVALID");
|
2022-07-14 21:59:39 +08:00
|
|
|
}
|
2022-07-21 17:07:16 +08:00
|
|
|
|
|
|
|
Ok(())
|
2022-07-14 21:59:39 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 06:24:57 +08:00
|
|
|
fn doctor_tcp(address: std::net::IpAddr, port: &str, desc: &str) {
|
|
|
|
let start = std::time::Instant::now();
|
|
|
|
let conn = format!("{}:{}", address, port);
|
|
|
|
if let Ok(_stream) = TcpStream::connect(conn.as_str()) {
|
|
|
|
let elapsed = std::time::Instant::now().duration_since(start);
|
|
|
|
println!(
|
|
|
|
"TCP Port {} ({}): OK in {} ms",
|
|
|
|
port,
|
|
|
|
desc,
|
|
|
|
elapsed.as_millis()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
println!("TCP Port {} ({}): ERROR", port, desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn doctor_ip(server_ip_address: std::net::IpAddr, server_address: Option<&str>) {
|
|
|
|
println!("\nChecking IP address: {}", server_ip_address);
|
|
|
|
println!("Is IPV4: {}", server_ip_address.is_ipv4());
|
|
|
|
println!("Is IPV6: {}", server_ip_address.is_ipv6());
|
|
|
|
|
|
|
|
// reverse dns lookup
|
|
|
|
// TODO: (check) doesn't seem to do reverse lookup on OSX...
|
|
|
|
let reverse = lookup_addr(&server_ip_address).unwrap();
|
2022-08-02 15:52:01 +08:00
|
|
|
if let Some(server_address) = server_address {
|
|
|
|
if reverse == server_address {
|
2022-08-07 06:24:57 +08:00
|
|
|
println!("Reverse DNS lookup: '{}' MATCHES server address", reverse);
|
|
|
|
} else {
|
|
|
|
println!(
|
|
|
|
"Reverse DNS lookup: '{}' DOESN'T MATCH server address '{}'",
|
2022-08-02 15:52:01 +08:00
|
|
|
reverse, server_address
|
2022-08-07 06:24:57 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: ICMP ping?
|
|
|
|
|
|
|
|
// port check TCP (UDP is hard to check)
|
|
|
|
doctor_tcp(server_ip_address, "21114", "API");
|
|
|
|
doctor_tcp(server_ip_address, "21115", "hbbs extra port for nat test");
|
|
|
|
doctor_tcp(server_ip_address, "21116", "hbbs");
|
|
|
|
doctor_tcp(server_ip_address, "21117", "hbbr tcp");
|
|
|
|
doctor_tcp(server_ip_address, "21118", "hbbs websocket");
|
|
|
|
doctor_tcp(server_ip_address, "21119", "hbbr websocket");
|
|
|
|
|
|
|
|
// TODO: key check
|
|
|
|
}
|
|
|
|
|
|
|
|
fn doctor(server_address_unclean: &str) {
|
|
|
|
let server_address3 = server_address_unclean.trim();
|
|
|
|
let server_address2 = server_address3.to_lowercase();
|
|
|
|
let server_address = server_address2.as_str();
|
|
|
|
println!("Checking server: {}\n", server_address);
|
2022-08-02 15:52:01 +08:00
|
|
|
if let Ok(server_ipaddr) = server_address.parse::<IpAddr>() {
|
|
|
|
// user requested an ip address
|
|
|
|
doctor_ip(server_ipaddr, None);
|
|
|
|
} else {
|
2022-08-07 06:24:57 +08:00
|
|
|
// the passed string is not an ip address
|
|
|
|
let ips: Vec<std::net::IpAddr> = lookup_host(server_address).unwrap();
|
2022-08-02 15:52:01 +08:00
|
|
|
println!("Found {} IP addresses: ", ips.len());
|
2022-08-07 06:24:57 +08:00
|
|
|
|
2022-08-09 16:07:01 +08:00
|
|
|
ips.iter().for_each(|ip| println!(" - {ip}"));
|
|
|
|
|
2022-08-02 15:52:01 +08:00
|
|
|
ips.iter()
|
|
|
|
.for_each(|ip| doctor_ip(*ip, Some(server_address)));
|
2022-08-07 06:24:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 21:59:39 +08:00
|
|
|
fn main() {
|
|
|
|
let args: Vec<_> = env::args().collect();
|
|
|
|
if args.len() <= 1 {
|
|
|
|
print_help();
|
|
|
|
}
|
|
|
|
|
|
|
|
let command = args[1].to_lowercase();
|
|
|
|
match command.as_str() {
|
|
|
|
"genkeypair" => gen_keypair(),
|
|
|
|
"validatekeypair" => {
|
|
|
|
if args.len() <= 3 {
|
|
|
|
error_then_help("You must supply both the public and the secret key");
|
|
|
|
}
|
2022-07-21 17:07:16 +08:00
|
|
|
let res = validate_keypair(args[2].as_str(), args[3].as_str());
|
|
|
|
if let Err(e) = res {
|
|
|
|
println!("{}", e);
|
|
|
|
process::exit(0x0001);
|
|
|
|
}
|
|
|
|
println!("Key pair is VALID");
|
2022-07-16 03:39:19 +08:00
|
|
|
}
|
2022-08-07 06:24:57 +08:00
|
|
|
"doctor" => {
|
|
|
|
if args.len() <= 2 {
|
|
|
|
error_then_help("You must supply the rustdesk-server address");
|
|
|
|
}
|
|
|
|
doctor(args[2].as_str());
|
|
|
|
}
|
2022-07-16 03:39:19 +08:00
|
|
|
_ => print_help(),
|
2022-07-14 21:59:39 +08:00
|
|
|
}
|
2022-07-16 03:39:19 +08:00
|
|
|
}
|