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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use std::fmt::Display;
use std::future::Future;
use std::net::IpAddr;
use std::pin::Pin;

/// IP Address family to try to resolve for
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Family {
    /// Doesn't provide a specific IP family, so it will try all of them
    Any,
    /// Lookup only IPv4 addresses
    IPv4,
    /// Lookup only IPv6 addresses
    IPv6,
}

impl Default for Family {
    fn default() -> Self {
        Family::Any
    }
}

#[derive(Debug)]
pub enum Error {
    Http(reqwest::Error),
    DecodeError(std::str::Utf8Error),
    InvalidAddress(std::net::AddrParseError),
    Dns(trust_dns_resolver::error::ResolveError),
    DnsResolutionEmpty,
    UnsupportedFamily,

    #[cfg(feature = "igd")]
    IgdExternalIp(igd::GetExternalIpError),
    #[cfg(feature = "igd")]
    IgdSearch(igd::SearchError),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Error {:?}", self)
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Http(e) => Some(e),
            Error::DecodeError(e) => Some(e),
            Error::InvalidAddress(e) => Some(e),
            Error::Dns(e) => Some(e),
            Error::DnsResolutionEmpty => None,
            Error::UnsupportedFamily => None,
            #[cfg(feature = "igd")]
            Error::IgdExternalIp(e) => Some(e),
            #[cfg(feature = "igd")]
            Error::IgdSearch(e) => Some(e),
        }
    }
}

impl From<reqwest::Error> for Error {
    fn from(err: reqwest::Error) -> Error {
        Error::Http(err)
    }
}

impl From<std::str::Utf8Error> for Error {
    fn from(err: std::str::Utf8Error) -> Error {
        Error::DecodeError(err)
    }
}

impl From<std::net::AddrParseError> for Error {
    fn from(err: std::net::AddrParseError) -> Error {
        Error::InvalidAddress(err)
    }
}

impl From<trust_dns_resolver::error::ResolveError> for Error {
    fn from(err: trust_dns_resolver::error::ResolveError) -> Error {
        Error::Dns(err)
    }
}

#[cfg(feature = "igd")]
impl From<igd::GetExternalIpError> for Error {
    fn from(err: igd::GetExternalIpError) -> Error {
        Error::IgdExternalIp(err)
    }
}

#[cfg(feature = "igd")]
impl From<igd::SearchError> for Error {
    fn from(err: igd::SearchError) -> Error {
        Error::IgdSearch(err)
    }
}

pub type IpResult = Result<IpAddr, Error>;
pub type IpFuture<'a> = Pin<Box<dyn Future<Output = IpResult> + Send + 'a>>;

/// Interface for any kind of external ip source
#[cfg_attr(test, mockall::automock)]
pub trait Source: Display {
    /// Returns a future that will represent the IP the source obtained
    fn get_ip<'a>(&'a self, family: Family) -> IpFuture<'a>;

    /// Clones the Source into a new Boxed trait object.
    fn box_clone(&self) -> Box<dyn Source>;
}

#[cfg(test)]
impl Display for MockSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MockedSource")
    }
}

impl Clone for Box<dyn Source> {
    fn clone(&self) -> Box<dyn Source> {
        self.box_clone()
    }
}