grindvakt_core/lib.rs
1//! # grindvakt-core
2//!
3//! The engine of [Grindvakt](https://grindvakt.weirdbrains.com/), the dynamic DNS updater,
4//! as a library you embed. It keeps **A, AAAA, TXT and CNAME** records on **DNSimple** or
5//! **DigitalOcean** pointed at where your machine is, including the libp2p
6//! `_dnsaddr` TXT record a peer-to-peer node publishes and aliases such as `www`
7//! ([`record::Record::cname`]):
8//!
9//! 1. [`ip::discover`] finds your public IPv4 and IPv6 addresses separately, from several
10//! HTTPS sources that must agree, and says [`Error::NoGlobalAddress`] when there is
11//! nothing worth publishing (CGNAT, private-only, no IPv6). Or pass addresses you
12//! already know.
13//! 2. [`plan()`] compares the [`record::Record`]s you want with the zone, through a
14//! [`provider::DnsProvider`], and returns a [`plan::Plan`]: no change, create, update,
15//! or refuse because it is ambiguous. The plan is the dry run.
16//! 3. [`apply()`] writes it. Running it again changes nothing.
17//!
18//! The caller owns everything around that: tokens arrive as [`SecretString`]s, every file
19//! path is an argument, and *when* to sync is up to you; there is no scheduler, CLI, prompt
20//! or global state in the crate. Errors are typed ([`Error::Auth`],
21//! [`Error::RateLimited`], [`Error::Ambiguous`], [`Error::NoGlobalAddress`],
22//! [`Error::Network`], [`Error::Provider`]) so a UI can explain them.
23//!
24//! ## Example
25//!
26//! ```no_run
27//! # #[cfg(all(feature = "digitalocean", feature = "public-ip"))]
28//! # async fn run(token: grindvakt_core::SecretString, peer: &str) -> grindvakt_core::Result<()> {
29//! use grindvakt_core::{apply, ip, plan, provider, record::Record};
30//!
31//! let dns = provider::digitalocean(&token)?; // or provider::dnsimple(&token, None)?
32//! let addrs = ip::discover(ip::Family::Both, &ip::Sources::default()).await?;
33//!
34//! let mut want = vec![Record::txt(
35//! "_dnsaddr.home",
36//! format!("dnsaddr=/dns4/home.example.com/tcp/4747/p2p/{peer}"),
37//! )];
38//! if let Some(v4) = addrs.v4 { want.push(Record::a("home", v4)); }
39//! if let Some(v6) = addrs.v6 { want.push(Record::aaaa("home", v6)); }
40//!
41//! let p = plan(&dns, "example.com", &want).await?;
42//! println!("{p}"); // show the dry run
43//! apply(&dns, &p).await?; // then write it
44//! # Ok(()) }
45//! ```
46//!
47//! A complete offline run against an in-memory provider is in the [`plan`](mod@plan) module docs and
48//! in `examples/`.
49//!
50//! ## Features
51//!
52//! | feature | default | adds |
53//! |---|---|---|
54//! | `dnsimple` | yes | [`provider::DnsimpleProvider`] (official `dnsimple` crate, rustls) |
55//! | `digitalocean` | yes | [`provider::DigitalOceanProvider`] (reqwest, rustls) |
56//! | `public-ip` | yes | [`ip::discover`] (reqwest, rustls) |
57//! | `vault` | no | [`vault`]: encrypted token file (Argon2id password and 24-word recovery phrase, XChaCha20-Poly1305 via [`weirdnet_keys`]) |
58//! | `standalone` | no | The standalone Grindvakt app's [`config::AppConfig`], [`paths`], single-record [`sync`], and [`standalone::find_conflicts`] |
59//!
60//! With `default-features = false` the crate makes no network calls at all; plug in your
61//! own [`provider::DnsProvider`] and addresses. Every feature builds for Android and iOS
62//! (rustls only, no OpenSSL). On Android, initialize `rustls-platform-verifier` from your
63//! app before the first HTTPS call, as that crate documents.
64//!
65//! ## What is not here
66//!
67//! Everything that ties Grindvakt to a machine stays in the Grindvakt app: the command
68//! line and prompts, schedules and timers, the systemd/launchd/Windows services, the
69//! unattended credential stores (systemd-creds, Keychain, DPAPI), installers and logging.
70//!
71//! ## Platforms and MSRV
72//!
73//! Linux, macOS, Windows, Android and iOS; Rust 1.99 or newer (edition 2024). The network
74//! features need a Tokio runtime.
75//!
76//! ## Versioning
77//!
78//! `grindvakt-core` is released in lockstep with the Grindvakt app: crate version X.Y.Z is
79//! the library inside Grindvakt vX.Y.Z, and is published to crates.io before that app
80//! release is tagged. Semver applies to the public API. The vault and config formats are
81//! stable; a format change would come with a major version and a migration path.
82#![cfg_attr(docsrs, feature(doc_cfg))]
83#![deny(missing_docs)]
84#![warn(rust_2018_idioms)]
85
86pub mod cname;
87pub mod config;
88mod error;
89pub mod fs;
90pub mod ip;
91pub mod lock;
92#[cfg(feature = "standalone")]
93#[cfg_attr(docsrs, doc(cfg(feature = "standalone")))]
94pub mod paths;
95pub mod plan;
96pub mod provider;
97pub mod record;
98#[cfg(feature = "standalone")]
99#[cfg_attr(docsrs, doc(cfg(feature = "standalone")))]
100pub mod standalone;
101#[cfg(feature = "standalone")]
102#[cfg_attr(docsrs, doc(cfg(feature = "standalone")))]
103pub mod sync;
104#[cfg(feature = "vault")]
105#[cfg_attr(docsrs, doc(cfg(feature = "vault")))]
106pub mod vault;
107
108pub use error::{Error, Result};
109pub use plan::{apply, plan};
110/// Re-exported so callers use the same secret type as the providers and vault.
111pub use secrecy::{ExposeSecret, SecretString};
112/// Re-exported so callers can match the crypto crate in use.
113#[cfg(feature = "vault")]
114#[cfg_attr(docsrs, doc(cfg(feature = "vault")))]
115pub use weirdnet_keys;
116
117/// Compiles and runs the README's examples as doctests.
118#[cfg(all(
119 doctest,
120 feature = "dnsimple",
121 feature = "digitalocean",
122 feature = "public-ip",
123 feature = "vault",
124 feature = "standalone",
125 feature = "weirdnet"
126))]
127#[doc = include_str!("../README.md")]
128pub struct ReadmeDoctests;