diff --git a/src/api.rs b/src/api.rs index 24bc185..67677fa 100644 --- a/src/api.rs +++ b/src/api.rs @@ -3,7 +3,14 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use log::debug; 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, +} #[derive(Serialize, Deserialize, Debug)] pub(crate) struct Locations { @@ -40,15 +47,21 @@ pub(crate) struct WireguardConfigIpv6 { pub netmask: u8, } +pub(crate) fn check() -> anyhow::Result { + 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 { - let url = format!("{}/locations", BASE_URL); + let url = format!("{}/v2/locations", BASE_URL); let response: Locations = ureq::get(&url).call()?.into_json()?; debug!("response = {:?}", &response); Ok(response) } pub(crate) fn add_ip(username: &str, token: &str, public_key: &str) -> anyhow::Result { - let url = format!("{}/ip/add", BASE_URL); + let url = format!("{}/v2/ip/add", BASE_URL); let response: Addresses = ureq::post(&url) .send_form(&[ ("username", username), diff --git a/src/main.rs b/src/main.rs index f93dc76..e5877d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,6 +41,9 @@ struct ConfigOpts { #[derive(Subcommand, Debug)] enum Command { + /// Checks connection status + Check, + /// Prints the list of VPN endpoints Locations, @@ -52,12 +55,27 @@ fn main() -> Result<(), anyhow::Error> { env_logger::init(); let opts = Opts::parse(); match &opts.command { + Command::Check => check(&opts)?, Command::Locations => list_locations(&opts)?, Command::Config(get_config_opts) => get_config(&opts, get_config_opts)?, } 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> { let locations = api::get_locations()?; if opts.json {