Add check command

This commit is contained in:
Andrey Golovizin 2021-06-29 23:11:06 +02:00
parent f48b500f64
commit 1a2a96911e

View file

@ -32,6 +32,7 @@ struct ConfigOpts {
enum Command {
Endpoints(EndpointsOpts),
Config(ConfigOpts),
Check,
}
#[derive(Serialize, Deserialize, Debug)]
@ -53,6 +54,13 @@ struct Locations {
locations: Vec<Location>,
}
#[derive(Serialize, Deserialize, Debug)]
struct CheckResult {
connected: bool,
ip: String, // XXX
location: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct WireguardConfig {
status: String,
@ -79,6 +87,7 @@ fn main() -> Result<(), anyhow::Error> {
match &opts.command {
Command::Endpoints(list_opts) => list(&list_opts)?,
Command::Config(get_config_opts) => get_config(&get_config_opts)?,
Command::Check => check()?,
}
Ok(())
}
@ -145,6 +154,15 @@ AllowedIPs = 0.0.0.0/0 #, ::/0",
Ok(())
}
fn check() -> Result<(), anyhow::Error> {
let url = format!("{}/check", BASE_URL);
let result: CheckResult = ureq::get(&url).call()?.into_json()?;
println!("Connected: {:?}", result.connected);
println!("IP: {}", result.ip);
println!("Location: {}", result.location);
Ok(())
}
fn generage_keys() -> Result<WireguardKeyPair, anyhow::Error> {
let privkey = process::Command::new("wg").arg("genkey").output()?.stdout;
let mut pubkey_cmd = process::Command::new("wg")