Add back the check command

This commit is contained in:
Andrey Golovizin 2023-04-09 23:41:35 +02:00
parent f663a4f8b4
commit ebccc59bf0
2 changed files with 34 additions and 3 deletions

View file

@ -3,7 +3,14 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use log::debug; use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
const BASE_URL: &str = "https://api.azirevpn.com/v2"; const BASE_URL: &str = "https://api.azirevpn.com";
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct CheckResult {
pub connected: bool,
pub ip: String, // XXX
pub location: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub(crate) struct Locations { pub(crate) struct Locations {
@ -40,15 +47,21 @@ pub(crate) struct WireguardConfigIpv6 {
pub netmask: u8, pub netmask: u8,
} }
pub(crate) fn check() -> anyhow::Result<CheckResult> {
let url = format!("{}/v1/check", BASE_URL);
let response: CheckResult = ureq::get(&url).call()?.into_json()?;
debug!("response = {:?}", &response);
Ok(response)
}
pub(crate) fn get_locations() -> anyhow::Result<Locations> { pub(crate) fn get_locations() -> anyhow::Result<Locations> {
let url = format!("{}/locations", BASE_URL); let url = format!("{}/v2/locations", BASE_URL);
let response: Locations = ureq::get(&url).call()?.into_json()?; let response: Locations = ureq::get(&url).call()?.into_json()?;
debug!("response = {:?}", &response); debug!("response = {:?}", &response);
Ok(response) Ok(response)
} }
pub(crate) fn add_ip(username: &str, token: &str, public_key: &str) -> anyhow::Result<Addresses> { pub(crate) fn add_ip(username: &str, token: &str, public_key: &str) -> anyhow::Result<Addresses> {
let url = format!("{}/ip/add", BASE_URL); let url = format!("{}/v2/ip/add", BASE_URL);
let response: Addresses = ureq::post(&url) let response: Addresses = ureq::post(&url)
.send_form(&[ .send_form(&[
("username", username), ("username", username),

View file

@ -41,6 +41,9 @@ struct ConfigOpts {
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug)]
enum Command { enum Command {
/// Checks connection status
Check,
/// Prints the list of VPN endpoints /// Prints the list of VPN endpoints
Locations, Locations,
@ -52,12 +55,27 @@ fn main() -> Result<(), anyhow::Error> {
env_logger::init(); env_logger::init();
let opts = Opts::parse(); let opts = Opts::parse();
match &opts.command { match &opts.command {
Command::Check => check(&opts)?,
Command::Locations => list_locations(&opts)?, Command::Locations => list_locations(&opts)?,
Command::Config(get_config_opts) => get_config(&opts, get_config_opts)?, Command::Config(get_config_opts) => get_config(&opts, get_config_opts)?,
} }
Ok(()) Ok(())
} }
fn check(opts: &Opts) -> Result<(), anyhow::Error> {
let check_result = api::check()?;
if opts.json {
println!("{}", serde_json::to_string(&check_result)?);
} else {
println!("Connected: {}", check_result.connected);
println!("Ip: {}", check_result.ip);
if let Some(location) = check_result.location {
println!("Location: {}", location);
}
}
Ok(())
}
fn list_locations(opts: &Opts) -> Result<(), anyhow::Error> { fn list_locations(opts: &Opts) -> Result<(), anyhow::Error> {
let locations = api::get_locations()?; let locations = api::get_locations()?;
if opts.json { if opts.json {