Add back the check command
This commit is contained in:
parent
f663a4f8b4
commit
ebccc59bf0
2 changed files with 34 additions and 3 deletions
19
src/api.rs
19
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<String>,
|
||||
}
|
||||
|
||||
#[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<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> {
|
||||
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<Addresses> {
|
||||
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),
|
||||
|
|
|
|||
18
src/main.rs
18
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue