Skip to main content

i3status_rs/blocks/
external_ip.rs

1//! External IP address and various information about it
2//!
3//! # Configuration
4//!
5//! Key | Values | Default
6//! ----|--------|--------
7//! `format` | A string to customise the output of this block. See below for available placeholders. | `" $ip $country_flag "`
8//! `interval` | Interval in seconds for automatic updates | `300`
9//! `autolocate_interval` | How long in seconds to reuse the last result from the geolocation service instead of contacting it again. Kept small by default so that network changes are picked up promptly. | `1`
10//! `with_network_manager` | If 'true', listen for NetworkManager events and update the IP immediately if there was a change | `true`
11//! `use_ipv4` | If 'true', use IPv4 for obtaining all info | `false`
12//!
13//!  Key | Value | Type | Unit
14//! -----|-------|------|------
15//! `ip` | The external IP address, as seen from a remote server | Text | -
16//! `version` | IPv4 or IPv6 | Text | -
17//! `city` | City name, such as "San Francisco" | Text | -
18//! `region` | Region name, such as "California" | Text | -
19//! `region_code` | Region code, such as "CA" for California | Text | -
20//! `country` | Country code (2 letter, ISO 3166-1 alpha-2) | Text | -
21//! `country_name` | Short country name | Text | -
22//! `country_code` | Country code (2 letter, ISO 3166-1 alpha-2) | Text | -
23//! `country_code_iso3` | Country code (3 letter, ISO 3166-1 alpha-3) | Text | -
24//! `country_capital` | Capital of the country | Text | -
25//! `country_tld` | Country specific TLD (top-level domain) | Text | -
26//! `continent_code` | Continent code | Text | -
27//! `in_eu` | Region code, such as "CA" | Flag | -
28//! `postal` | ZIP / Postal code | Text | -
29//! `latitude` | Latitude | Number | - (TODO: make degrees?)
30//! `longitude` | Longitude | Number | - (TODO: make degrees?)
31//! `timezone` | City | Text | -
32//! `utc_offset` | UTC offset (with daylight saving time) as +HHMM or -HHMM (HH is hours, MM is minutes) | Text | -
33//! `country_calling_code` | Country calling code (dial in code, comma separated) | Text | -
34//! `currency` | Currency code (ISO 4217) | Text | -
35//! `currency_name` | Currency name | Text | -
36//! `languages` | Languages spoken (comma separated 2 or 3 letter ISO 639 code with optional hyphen separated country suffix) | Text | -
37//! `country_area` | Area of the country (in sq km) | Number | -
38//! `country_population` | Population of the country | Number | -
39//! `timezone` | Time zone | Text | -
40//! `org` | Organization | Text | -
41//! `asn` | Autonomous system (AS) | Text | -
42//! `country_flag` | Flag of the country | Text (glyph) | -
43//!
44//! # Example
45//!
46//! ```toml
47//! [[block]]
48//! block = "external_ip"
49//! format = " $ip $country_code "
50//! ```
51//!
52//! # Notes
53//! All the information comes from <https://ipapi.co/json/>
54//! Check their documentation here: <https://ipapi.co/api/#complete-location5>
55//!
56//! The IP is queried, 1) When i3status-rs starts, 2) When a signal is received
57//! on D-Bus about a network configuration change, 3) Every 5 minutes. This
58//! periodic refresh exists to catch IP updates that don't trigger a notification,
59//! for example due to a IP refresh at the router.
60//!
61//! If the service reports rate limiting, the block keeps showing the last
62//! known IP and waits for the geolocator's `rate_limit_interval` (10 minutes
63//! by default) before asking again.
64//!
65//! Flags: They are not icons but unicode glyphs. You will need a font that
66//! includes them. Tested with: <https://www.babelstone.co.uk/Fonts/Flags.html>
67
68use zbus::MatchRule;
69
70use super::prelude::*;
71use crate::geolocator::is_rate_limited;
72use crate::util::{country_flag_from_iso_code, new_system_dbus_connection};
73
74make_log_macro!(debug, "external_ip");
75
76/// How long the network-change signal stream must stay quiet before the IP is
77/// re-queried; a transition keeps emitting signals for a while, often before
78/// connectivity is actually usable.
79const SETTLE_QUIET: Duration = Duration::from_secs(1);
80
81/// Upper bound on the settle wait. NetworkManager can keep emitting signals
82/// (IP config, DNS, connectivity checks) for many seconds after a transition;
83/// without a cap the quiet window keeps sliding and the update is delayed
84/// indefinitely. If the network is still not usable when we query, the retry
85/// backoff covers it.
86const SETTLE_MAX: Duration = Duration::from_secs(3);
87
88#[derive(Deserialize, Debug, SmartDefault)]
89#[serde(deny_unknown_fields, default)]
90pub struct Config {
91    pub format: FormatConfig,
92    #[default(300.into())]
93    pub interval: Seconds,
94    /// Unlike the weather block this defaults to 1 second, not `interval`:
95    /// picking up a fresh IP right after a network change is the whole point
96    /// of this block, so cached results must expire quickly.
97    #[default(1.into())]
98    pub autolocate_interval: Seconds,
99    #[default(true)]
100    pub with_network_manager: bool,
101    #[default(false)]
102    pub use_ipv4: bool,
103}
104
105pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
106    let format = config.format.with_default(" $ip $country_flag ")?;
107
108    type UpdatesStream = Pin<Box<dyn Stream<Item = ()>>>;
109    let mut stream: UpdatesStream = if config.with_network_manager {
110        let dbus = new_system_dbus_connection().await?;
111        let proxy = zbus::fdo::DBusProxy::new(&dbus)
112            .await
113            .error("Failed to create DBusProxy")?;
114        proxy
115            .add_match_rule(
116                MatchRule::builder()
117                    .msg_type(zbus::message::Type::Signal)
118                    .path("/org/freedesktop/NetworkManager")
119                    .and_then(|x| x.interface("org.freedesktop.DBus.Properties"))
120                    .and_then(|x| x.member("PropertiesChanged"))
121                    .unwrap()
122                    .build(),
123            )
124            .await
125            .error("Failed to add match")?;
126        proxy
127            .add_match_rule(
128                MatchRule::builder()
129                    .msg_type(zbus::message::Type::Signal)
130                    .path_namespace("/org/freedesktop/NetworkManager/ActiveConnection")
131                    .and_then(|x| x.interface("org.freedesktop.DBus.Properties"))
132                    .and_then(|x| x.member("PropertiesChanged"))
133                    .unwrap()
134                    .build(),
135            )
136            .await
137            .error("Failed to add match")?;
138        proxy
139            .add_match_rule(
140                MatchRule::builder()
141                    .msg_type(zbus::message::Type::Signal)
142                    .path_namespace("/org/freedesktop/NetworkManager/IP4Config")
143                    .and_then(|x| x.interface("org.freedesktop.DBus.Properties"))
144                    .and_then(|x| x.member("PropertiesChanged"))
145                    .unwrap()
146                    .build(),
147            )
148            .await
149            .error("Failed to add match")?;
150        let stream: zbus::MessageStream = dbus.into();
151        // If the D-Bus connection dies the stream ends; without the chained
152        // pending stream, polling it again would resolve instantly forever,
153        // turning the loop below into a busy loop of API requests.
154        Box::pin(stream.map(|_| ()).chain(futures::stream::pending()))
155    } else {
156        Box::pin(futures::stream::pending())
157    };
158
159    let client = if config.use_ipv4 {
160        &REQWEST_CLIENT_IPV4
161    } else {
162        &REQWEST_CLIENT
163    };
164
165    loop {
166        let fetch_start = tokio::time::Instant::now();
167        let info = match api
168            .find_ip_location(client, config.autolocate_interval.0)
169            .await
170        {
171            Ok(info) => info,
172            Err(err) if is_rate_limited(&err) => {
173                // Keep displaying the last known IP and try again once the
174                // geolocator's rate limit interval has passed (plus a margin
175                // so we don't wake up just before it expires); erroring out
176                // here would make the block restart machinery re-query every
177                // `error_interval` seconds, which keeps the rate limit from
178                // ever lifting.
179                sleep(api.locator_rate_limit_interval() + Duration::from_secs(1)).await;
180                continue;
181            }
182            Err(err) => return Err(err),
183        };
184        debug!("got {} after {:?}", info.ip, fetch_start.elapsed());
185
186        let mut values = map! {
187            "ip" => Value::text(info.ip),
188            "city" => Value::text(info.city),
189            "latitude" => Value::number(info.latitude),
190            "longitude" => Value::number(info.longitude),
191        };
192
193        macro_rules! map_push_if_some { ($($key:ident: $type:ident),* $(,)?) => {
194            $({
195                let key = stringify!($key);
196                if let Some(value) = info.$key {
197                    values.insert(key.into(), Value::$type(value));
198                } else if format.contains_key(key) {
199                    return Err(Error::new(format!(
200                        "The format string contains '{key}', but the {key} field is not provided by {} (an api key may be required)",
201                        api.locator_name()
202                    )));
203                }
204            })*
205        } }
206
207        map_push_if_some!(
208            version: text,
209            region: text,
210            region_code: text,
211            country: text,
212            country_name: text,
213            country_code_iso3: text,
214            country_capital: text,
215            country_tld: text,
216            continent_code: text,
217            postal: text,
218            timezone: text,
219            utc_offset: text,
220            country_calling_code: text,
221            currency: text,
222            currency_name: text,
223            languages: text,
224            country_area: number,
225            country_population: number,
226            asn: text,
227            org: text,
228        );
229
230        if let Some(country_code) = info.country_code {
231            values.insert(
232                "country_flag".into(),
233                Value::text(country_flag_from_iso_code(&country_code)),
234            );
235            values.insert("country_code".into(), Value::text(country_code));
236        } else if format.contains_key("country_code") || format.contains_key("country_flag") {
237            return Err(Error::new(format!(
238                "The format string contains 'country_code' or 'country_flag', but the country_code field is not provided by {}",
239                api.locator_name()
240            )));
241        }
242
243        if let Some(in_eu) = info.in_eu {
244            if in_eu {
245                values.insert("in_eu".into(), Value::flag());
246            }
247        } else if format.contains_key("in_eu") {
248            return Err(Error::new(format!(
249                "The format string contains 'in_eu', but the in_eu field is not provided by {}",
250                api.locator_name()
251            )));
252        }
253
254        let mut widget = Widget::new().with_format(format.clone());
255        widget.set_values(values);
256        api.set_widget(widget)?;
257
258        select! {
259            _ = sleep(config.interval.0) => (),
260            _ = api.wait_for_update_request() => (),
261            _ = stream.next_debounced() => {
262                // Wait for the burst of signals to die down before re-querying,
263                // so that one network transition results in one request, made
264                // once the new connection is likely up.
265                let settle_start = tokio::time::Instant::now();
266                while let Ok(Some(_)) = tokio::time::timeout(SETTLE_QUIET, stream.next()).await {
267                    if settle_start.elapsed() >= SETTLE_MAX {
268                        break;
269                    }
270                }
271                debug!("signals settled after {:?}", settle_start.elapsed());
272            }
273        }
274    }
275}