1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use log::debug;
use thiserror::Error;

const IPIFY: &str = "https://api.ipify.org";

#[derive(Error, Debug)]
pub enum WayfinderError<T> {
    #[error("{0}")]
    Ipify(T),
    #[error("{0}")]
    Godaddy(T),
}

pub struct Config {
    pub domain: String,
    pub records: Vec<String>,
    pub wait: u64,
}

pub async fn get_external_ip() -> Result<String, IpifyError> {
    debug!("Getting external ip (ipify.org)");
    let url: String = IPIFY.to_owned();

    let response = reqwest::Client::new().get(url).send().await.unwrap();

    debug!("Response: {:?}", response);

    if !response.status().is_success() {
        return Err(IpifyError::GenericHttp(response.status().to_string()));
    }

    match response.text().await {
        Ok(d) => {
            debug!("Got external ip '{}' (ipify.org)", d);
            Ok(d)
        }
        Err(e) => Err(IpifyError::RequestFailed(e)),
    }
}

/// All error types for godaddy
#[derive(Error, Debug)]
pub enum IpifyError {
    #[error("Generic http error, {0}")]
    GenericHttp(String),
    #[error("Request failed, {0}")]
    RequestFailed(#[from] reqwest::Error),
}