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
//! tailscale-rs is a client library for [Tailscale](https://tailscale.com).
//!
//! All versions v0.X.X are unofficial, and if Tailscale, Inc. chooses
//! to create an official client library, the ownership of this crate
//! will be transferred to them to release and maintain V1+. At this
//! time, breaking API changes may or may not be introduced.
extern crate ipnetwork;
extern crate pnet;

use ipnetwork::IpNetwork;
use pnet::datalink;
use std::net::IpAddr;

fn maybe_tailscale(s: &str) -> bool {
    s.starts_with("wg")
        || s.starts_with("ts")
        || s.starts_with("tailscale")
        || s.starts_with("utun")
}

/// Retrieve the IP address of the current machine's Tailscale interface, if any.
/// ```
/// let iface = tailscale::interface().expect("no tailscale interface found");
/// ```
pub fn interface() -> Option<IpAddr> {
    let ifaces = datalink::interfaces();
    let netmask: IpNetwork = "100.64.0.0/10".parse().unwrap();
    ifaces
        .iter()
        .filter(|iface| maybe_tailscale(&iface.name))
        .map(|iface| iface.ips.clone())
        .flatten()
        .filter(|ipnet| ipnet.is_ipv4() && netmask.contains(ipnet.network()))
        .map(|ipnet| ipnet.ip())
        .next()
}