use core::{
net::{Ipv4Addr, Ipv6Addr},
time::Duration,
};
use std::sync::Arc;
use futures::StreamExt;
use kameo::{
actor::{ActorRef, Spawn},
message::{Context, StreamMessage},
prelude::Message,
};
use tokio::sync::watch;
use ts_control::{
AsyncControlClient, Endpoint, EndpointType, Error as ControlError, IdTokenError, LogoutError,
Node, SetDnsError, SshPolicy, StateUpdate, TkaStatus,
};
use ts_magicsock::SelfEndpointType;
use crate::{
derp_latency::{DerpLatencyMeasurement, DerpLatencyMeasurer},
direct::EndpointAdvertisement,
};
pub struct ControlRunner {
client: AsyncControlClient,
params: Params,
self_node: watch::Sender<Option<Node>>,
ssh_policy: watch::Sender<Option<SshPolicy>>,
tka: watch::Sender<Option<TkaStatus>>,
tka_synced: Option<crate::tka_sync::SyncedTka>,
tka_authority: watch::Sender<Option<Arc<ts_tka::Authority>>>,
tka_syncing: bool,
cert_domains: watch::Sender<Vec<String>>,
dns_config: watch::Sender<Option<ts_control::DnsConfig>>,
pop_browser_url: watch::Sender<Option<url::Url>>,
netcheck: watch::Sender<crate::status::NetcheckReport>,
}
pub struct Params {
pub(crate) config: ts_control::Config,
pub(crate) auth_key: Option<String>,
pub(crate) env: crate::Env,
pub(crate) state_tx: watch::Sender<crate::DeviceState>,
}
#[doc(hidden)]
#[derive(Debug, thiserror::Error)]
pub enum ControlRunnerError {
#[error(transparent)]
Control(#[from] ControlError),
#[error(transparent)]
Crate(#[from] crate::Error),
}
impl kameo::Actor for ControlRunner {
type Args = Params;
type Error = ControlRunnerError;
async fn on_start(params: Params, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
loop {
match AsyncControlClient::check_auth(
¶ms.config,
¶ms.env.keys,
params.auth_key.as_deref(),
)
.await
{
Ok(()) => break,
Err(ControlError::MachineNotAuthorized(u)) => {
tracing::info!(auth_url = %u, "please authorize this machine or pass an auth key");
params
.state_tx
.send_replace(crate::DeviceState::NeedsLogin(u.clone()));
tokio::time::sleep(Duration::from_secs(5)).await;
}
Err(e) => {
let reason = crate::RegistrationError::from(&e);
tracing::error!(error = %e, "registration failed; control runner stopping");
params
.state_tx
.send_replace(crate::DeviceState::Failed(reason));
return Err(e.into());
}
}
}
let bring_up = async {
let (client, stream) = AsyncControlClient::connect(
¶ms.config,
¶ms.env.keys,
params.auth_key.as_deref(),
)
.await?;
DerpLatencyMeasurer::spawn_link(&slf, params.env.clone()).await;
params.env.subscribe::<DerpLatencyMeasurement>(&slf).await?;
params.env.subscribe::<EndpointAdvertisement>(&slf).await?;
slf.attach_stream(stream.boxed(), (), ());
Ok::<_, ControlRunnerError>(client)
};
let client = match bring_up.await {
Ok(client) => client,
Err(e) => {
tracing::error!(error = %e, "bringing up the control session failed");
params.state_tx.send_replace(crate::DeviceState::Failed(
crate::RegistrationError::NetworkUnreachable,
));
return Err(e);
}
};
params.state_tx.send_replace(crate::DeviceState::Running);
Ok(Self {
client,
params,
self_node: Default::default(),
ssh_policy: Default::default(),
tka: Default::default(),
tka_synced: None,
tka_authority: Default::default(),
tka_syncing: false,
cert_domains: Default::default(),
dns_config: Default::default(),
pop_browser_url: Default::default(),
netcheck: Default::default(),
})
}
}
impl ControlRunner {
fn maybe_sync_tka(&mut self, tka: &TkaStatus, self_ref: ActorRef<Self>) {
if !tka.is_enabled() {
if self.tka_synced.is_some() {
self.tka_synced = None;
self.tka_authority.send_replace(None);
}
return;
}
if self.tka_syncing {
return; }
if let Some(synced) = &self.tka_synced
&& let Some(control_head) = ts_tka::AumHash::from_base32(&tka.head)
&& synced.authority.head_matches(&control_head)
{
return;
}
self.tka_syncing = true;
let current = self.tka_synced.take();
let config = self.params.config.clone();
let keys = self.params.env.keys.clone();
tokio::spawn(async move {
let result = crate::tka_sync::sync_tka(&config, &keys, current).await;
if let Err(e) = self_ref.tell(TkaSynced { result }).await {
tracing::debug!(error = ?e, "TKA sync result not delivered (actor gone)");
}
});
}
async fn apply_tka_synced(
&mut self,
result: Result<Option<crate::tka_sync::SyncedTka>, crate::tka_sync::TkaSyncDriverError>,
) {
self.tka_syncing = false;
match result {
Ok(Some(synced)) => {
tracing::info!(
head = %synced.authority.head().to_base32(),
"TKA sync succeeded; publishing verified Authority (observe-only)"
);
self.tka_authority
.send_replace(Some(synced.authority.clone()));
if let Err(e) = self
.params
.env
.publish(crate::peer_tracker::TkaAuthorityUpdate(
synced.authority.clone(),
))
.await
{
tracing::warn!(error = %e, "publishing TKA authority to peer tracker failed");
}
self.tka_synced = Some(synced);
}
Ok(None) => {
tracing::debug!("TKA sync: control reported no lock for this node (inert)");
}
Err(e) => {
tracing::warn!(error = %e, "TKA sync failed; staying inert (no peer impact)");
}
}
}
fn with_self_node<F, R>(&self, f: F) -> impl Future<Output = Option<R>> + use<F, R>
where
F: FnOnce(&Node) -> R,
{
let mut sub = self.self_node.subscribe();
let mut shutdown = self.params.env.shutdown.clone();
async move {
tokio::select! {
_ = shutdown.wait_for(|x| *x) => {
None
},
node = sub.wait_for(Option::is_some) => {
Some(f(node.ok()?.as_ref()?))
},
}
}
}
}
fn sticky_update_pop_browser_url(
cell: &watch::Sender<Option<url::Url>>,
incoming: Option<&url::Url>,
) {
if let Some(url) = incoming {
cell.send_if_modified(|current| {
if current.as_ref() == Some(url) {
false
} else {
*current = Some(url.clone());
true
}
});
}
}
pub use msg_impl::*;
#[allow(missing_docs)]
mod msg_impl {
use kameo::{message::Context, reply::DelegatedReply};
use super::*;
#[kameo::messages]
impl ControlRunner {
#[message(ctx)]
pub fn ipv4(
&self,
ctx: &mut Context<Self, DelegatedReply<Option<Ipv4Addr>>>,
) -> DelegatedReply<Option<Ipv4Addr>> {
let (deleg, replier) = ctx.reply_sender();
if let Some(replier) = replier {
let fut = self.with_self_node(|node| node.tailnet_address.ipv4.addr());
tokio::spawn(async move {
let ip = fut.await;
replier.send(ip);
});
}
deleg
}
#[message(ctx)]
pub fn ipv6(
&self,
ctx: &mut Context<Self, DelegatedReply<Option<Ipv6Addr>>>,
) -> DelegatedReply<Option<Ipv6Addr>> {
let (deleg, replier) = ctx.reply_sender();
if let Some(replier) = replier {
let fut = self.with_self_node(|node| node.tailnet_address.ipv6.addr());
tokio::spawn(async move {
let ip = fut.await;
replier.send(ip);
});
}
deleg
}
#[message(ctx)]
pub fn self_node(
&self,
ctx: &mut Context<Self, DelegatedReply<Option<Node>>>,
) -> DelegatedReply<Option<Node>> {
let (deleg, replier) = ctx.reply_sender();
if let Some(replier) = replier {
let node = self.with_self_node(|node| node.clone());
tokio::spawn(async move {
let node = node.await;
replier.send(node)
});
}
deleg
}
#[message]
pub fn current_ssh_policy(&self) -> Option<SshPolicy> {
self.ssh_policy.borrow().clone()
}
#[message]
pub fn current_tka_status(&self) -> Option<TkaStatus> {
self.tka.borrow().clone()
}
#[message]
pub fn cert_domains(&self) -> Vec<String> {
self.cert_domains.borrow().clone()
}
#[message]
pub fn dns_config(&self) -> Option<ts_control::DnsConfig> {
self.dns_config.borrow().clone()
}
#[message]
pub fn pop_browser_url(&self) -> Option<url::Url> {
self.pop_browser_url.borrow().clone()
}
#[message(derive(Clone))]
pub fn watch_browser_url(&self) -> watch::Receiver<Option<url::Url>> {
self.pop_browser_url.subscribe()
}
#[message]
pub fn netcheck(&self) -> crate::status::NetcheckReport {
self.netcheck.borrow().clone()
}
#[message(ctx)]
pub fn fetch_id_token(
&self,
ctx: &mut Context<Self, DelegatedReply<Result<String, IdTokenError>>>,
audience: String,
) -> DelegatedReply<Result<String, IdTokenError>> {
let (deleg, replier) = ctx.reply_sender();
if let Some(replier) = replier {
let config = self.params.config.clone();
let keys = self.params.env.keys.clone();
tokio::spawn(async move {
let result = ts_control::fetch_id_token(&config, &keys, &audience).await;
replier.send(result);
});
}
deleg
}
#[message(ctx)]
pub fn logout(
&self,
ctx: &mut Context<Self, DelegatedReply<Result<(), LogoutError>>>,
) -> DelegatedReply<Result<(), LogoutError>> {
let (deleg, replier) = ctx.reply_sender();
if let Some(replier) = replier {
let config = self.params.config.clone();
let keys = self.params.env.keys.clone();
tokio::spawn(async move {
let result = ts_control::logout(&config, &keys).await;
replier.send(result);
});
}
deleg
}
#[message(ctx)]
pub fn set_dns(
&self,
ctx: &mut Context<Self, DelegatedReply<Result<(), SetDnsError>>>,
name: String,
value: String,
) -> DelegatedReply<Result<(), SetDnsError>> {
let (deleg, replier) = ctx.reply_sender();
if let Some(replier) = replier {
let config = self.params.config.clone();
let keys = self.params.env.keys.clone();
tokio::spawn(async move {
let result = ts_control::set_dns(&config, &keys, &name, "TXT", &value).await;
replier.send(result);
});
}
deleg
}
}
#[cfg(feature = "acme")]
pub type CertPairReply = Result<(String, String), ts_control::CertError>;
#[cfg(feature = "acme")]
#[kameo::messages]
impl ControlRunner {
#[message(ctx)]
pub fn get_certificate(
&self,
ctx: &mut Context<
Self,
DelegatedReply<Result<ts_control::tls::CertifiedKey, ts_control::CertError>>,
>,
name: String,
) -> DelegatedReply<Result<ts_control::tls::CertifiedKey, ts_control::CertError>> {
let (deleg, replier) = ctx.reply_sender();
if let Some(replier) = replier {
let config = self.params.config.clone();
let keys = self.params.env.keys.clone();
tokio::spawn(async move {
let result = issue_certificate(&config, &keys, &name).await;
replier.send(result);
});
}
deleg
}
#[message(ctx)]
pub fn get_cert_pair(
&self,
ctx: &mut Context<Self, DelegatedReply<CertPairReply>>,
name: String,
) -> DelegatedReply<CertPairReply> {
let (deleg, replier) = ctx.reply_sender();
if let Some(replier) = replier {
let config = self.params.config.clone();
let keys = self.params.env.keys.clone();
tokio::spawn(async move {
let result = issue_cert_pair(&config, &keys, &name).await;
replier.send(result);
});
}
deleg
}
}
}
#[cfg(feature = "acme")]
async fn issue_certificate(
config: &ts_control::Config,
keys: &ts_keys::NodeState,
name: &str,
) -> Result<ts_control::tls::CertifiedKey, ts_control::CertError> {
issue_cert_pair_inner(config, keys, name)
.await
.map(|issued| issued.certified)
}
#[cfg(feature = "acme")]
async fn issue_cert_pair(
config: &ts_control::Config,
keys: &ts_keys::NodeState,
name: &str,
) -> Result<(String, String), ts_control::CertError> {
issue_cert_pair_inner(config, keys, name)
.await
.map(|issued| (issued.cert_chain_pem, issued.key_pem))
}
#[cfg(feature = "acme")]
async fn issue_cert_pair_inner(
config: &ts_control::Config,
keys: &ts_keys::NodeState,
name: &str,
) -> Result<ts_control::acme::IssuedCert, ts_control::CertError> {
let account_key = match keys.acme_account_key.as_deref() {
Some(der) => ts_control::acme::AcmeAccountKey::from_pkcs8(der)?,
None => {
tracing::debug!(
"no persisted ACME account key in key state; generating an ephemeral per-call key \
(a new ACME account this issuance — not persisted back)"
);
ts_control::acme::AcmeAccountKey::generate()?.0
}
};
let directory = ts_control::acme::LETS_ENCRYPT_PRODUCTION_DIRECTORY
.parse()
.map_err(|e| {
ts_control::CertError::Acme(format!("parsing Let's Encrypt directory URL: {e}"))
})?;
ts_control::issue_cert_pair_via_setdns(config, keys, name, &account_key, &directory).await
}
impl Message<StreamMessage<Arc<StateUpdate>, (), ()>> for ControlRunner {
type Reply = ();
async fn handle(
&mut self,
msg: StreamMessage<Arc<StateUpdate>, (), ()>,
ctx: &mut Context<Self, Self::Reply>,
) {
match msg {
StreamMessage::Started(_) => {
tracing::trace!("started listening to state updates");
}
StreamMessage::Next(msg) => {
if let Some(node) = msg.node.as_ref() {
let now_unix = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0);
let next = if node.key_expired_at_unix(now_unix) {
crate::DeviceState::Expired
} else {
crate::DeviceState::Running
};
self.params.state_tx.send_if_modified(|s| {
if *s != next {
*s = next.clone();
true
} else {
false
}
});
self.self_node.send_replace(Some(node.clone()));
}
if let Some(policy) = msg.ssh_policy.as_ref() {
self.ssh_policy.send_replace(Some(policy.clone()));
}
if let Some(tka) = msg.tka.as_ref() {
self.tka.send_replace(Some(tka.clone()));
self.maybe_sync_tka(tka, ctx.actor_ref().clone());
}
let cert_domains = msg
.dns_config
.as_ref()
.map(|d| d.cert_domains.clone())
.unwrap_or_default();
self.cert_domains.send_replace(cert_domains);
self.dns_config.send_replace(msg.dns_config.clone());
sticky_update_pop_browser_url(&self.pop_browser_url, msg.pop_browser_url.as_ref());
if let Err(e) = self.params.env.publish(msg).await {
tracing::error!(error = %e, "publishing netmap update");
}
}
StreamMessage::Finished(_) => {
tracing::error!("state update stream terminated")
}
}
}
}
#[doc(hidden)]
pub struct TkaSynced {
pub(crate) result:
Result<Option<crate::tka_sync::SyncedTka>, crate::tka_sync::TkaSyncDriverError>,
}
impl Message<TkaSynced> for ControlRunner {
type Reply = ();
async fn handle(&mut self, msg: TkaSynced, _ctx: &mut Context<Self, Self::Reply>) {
self.apply_tka_synced(msg.result).await;
}
}
impl Message<DerpLatencyMeasurement> for ControlRunner {
type Reply = ();
async fn handle(&mut self, msg: DerpLatencyMeasurement, _ctx: &mut Context<Self, Self::Reply>) {
let measurements = msg.measurement.as_ref().clone();
self.netcheck
.send_replace(crate::status::NetcheckReport::from_region_results(
&measurements,
));
let Some(result) = measurements.first() else {
tracing::debug!("derp latency measurements empty");
return;
};
let iter = measurements.iter().map(|result| {
(
result.latency_map_key.as_str(),
result.latency.as_secs_f64(),
)
});
tracing::debug!(selected_region_id = ?result.id, "updating home region");
self.client.set_home_region(result.id, iter).await;
}
}
impl Message<EndpointAdvertisement> for ControlRunner {
type Reply = ();
async fn handle(&mut self, msg: EndpointAdvertisement, _ctx: &mut Context<Self, Self::Reply>) {
let endpoints: Vec<Endpoint> = msg
.endpoints
.iter()
.map(|ep| Endpoint {
endpoint: ep.addr,
ty: match ep.ty {
SelfEndpointType::Local => EndpointType::Local,
SelfEndpointType::Stun => EndpointType::Stun,
SelfEndpointType::Stun4LocalPort => EndpointType::Stun4LocalPort,
},
})
.collect();
tracing::debug!(
n_endpoints = endpoints.len(),
"advertising endpoints to control"
);
self.client.set_endpoints(endpoints).await;
}
}
#[derive(Debug)]
pub struct SetAdvertiseRoutes {
pub routes: Vec<ipnet::IpNet>,
}
impl Message<SetAdvertiseRoutes> for ControlRunner {
type Reply = ();
async fn handle(&mut self, msg: SetAdvertiseRoutes, _ctx: &mut Context<Self, Self::Reply>) {
tracing::debug!(n_routes = msg.routes.len(), "advertising routes to control");
self.client.set_routable_ips(msg.routes).await;
}
}
#[derive(Debug)]
pub struct SetHostname {
pub hostname: String,
}
impl Message<SetHostname> for ControlRunner {
type Reply = ();
async fn handle(&mut self, msg: SetHostname, _ctx: &mut Context<Self, Self::Reply>) {
tracing::debug!("updating hostname at control");
self.client.set_hostname(msg.hostname).await;
}
}
#[cfg(test)]
mod sticky_pop_browser_url_tests {
use tokio::sync::watch;
use super::sticky_update_pop_browser_url;
fn url(s: &str) -> url::Url {
s.parse().unwrap()
}
#[test]
fn non_empty_url_publishes() {
let (tx, rx) = watch::channel(None);
let u = url("https://login.example/consent");
sticky_update_pop_browser_url(&tx, Some(&u));
assert_eq!(*rx.borrow(), Some(u));
}
#[test]
fn absent_update_does_not_reset() {
let u = url("https://login.example/consent");
let (tx, rx) = watch::channel(Some(u.clone()));
for _ in 0..5 {
sticky_update_pop_browser_url(&tx, None);
}
assert_eq!(
*rx.borrow(),
Some(u),
"empty updates must not clear the URL"
);
}
#[test]
fn repeated_same_url_does_not_refire() {
let u = url("https://login.example/consent");
let (tx, mut rx) = watch::channel(None);
sticky_update_pop_browser_url(&tx, Some(&u)); assert!(rx.has_changed().unwrap(), "first non-empty URL fires");
rx.mark_unchanged();
sticky_update_pop_browser_url(&tx, Some(&u)); assert!(
!rx.has_changed().unwrap(),
"repeating the same URL must not re-fire the watch"
);
}
#[test]
fn new_url_after_prior_fires() {
let a = url("https://login.example/a");
let b = url("https://login.example/b");
let (tx, rx) = watch::channel(None);
sticky_update_pop_browser_url(&tx, Some(&a));
sticky_update_pop_browser_url(&tx, Some(&b));
assert_eq!(*rx.borrow(), Some(b));
}
#[test]
fn sticky_through_none_gap_then_new_url_fires() {
let a = url("https://login.example/a");
let b = url("https://login.example/b");
let (tx, rx) = watch::channel(None);
sticky_update_pop_browser_url(&tx, Some(&a));
for _ in 0..3 {
sticky_update_pop_browser_url(&tx, None);
}
assert_eq!(*rx.borrow(), Some(a), "stayed sticky through the None gap");
sticky_update_pop_browser_url(&tx, Some(&b));
assert_eq!(
*rx.borrow(),
Some(b),
"a new URL after a None gap still fires"
);
}
#[test]
fn returning_to_prior_url_refires() {
let a = url("https://login.example/a");
let b = url("https://login.example/b");
let (tx, mut rx) = watch::channel(None);
sticky_update_pop_browser_url(&tx, Some(&a));
sticky_update_pop_browser_url(&tx, Some(&b));
rx.mark_unchanged();
sticky_update_pop_browser_url(&tx, Some(&a)); assert!(
rx.has_changed().unwrap(),
"returning to a prior URL re-fires"
);
assert_eq!(*rx.borrow(), Some(a));
}
#[tokio::test]
async fn end_to_end_one_change_survives_none_thrash() {
let u = url("https://login.example/consent");
let (tx, mut rx) = watch::channel(None);
let cadence = [None, None, Some(&u), None, None];
for incoming in cadence {
sticky_update_pop_browser_url(&tx, incoming);
}
let mut changes = 0;
while rx.has_changed().unwrap() {
let v = rx.borrow_and_update().clone();
changes += 1;
assert_eq!(v, Some(u.clone()), "the surviving change carries the URL");
}
assert_eq!(changes, 1, "exactly one change survives the None thrash");
}
}