use std::collections::HashMap;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use anyhow::{Context, Result, bail};
use axum::extract::{Request, State};
use axum::http::{HeaderMap, StatusCode, header};
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use serde_json::json;
pub const COOKIE: &str = "kjarni_token";
const CODE_LIFETIME: Duration = Duration::from_secs(120);
pub fn random_hex(n: usize) -> String {
let mut bytes = vec![0u8; n];
getrandom::fill(&mut bytes).expect("the system's random number generator failed");
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
pub fn load_or_create_token(path: &Path) -> Result<String> {
if let Ok(text) = std::fs::read_to_string(path) {
let token = text.trim();
if token.len() >= 32 {
return Ok(token.to_string());
}
}
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
}
let token = random_hex(32);
write_private(path, &token).with_context(|| format!("writing {}", path.display()))?;
Ok(token)
}
pub fn write_private(path: &Path, text: &str) -> std::io::Result<()> {
use std::io::Write;
let mut options = std::fs::OpenOptions::new();
options.write(true).create(true).truncate(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
let mut file = options.open(path)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
file.set_permissions(std::fs::Permissions::from_mode(0o600))?;
}
file.write_all(text.as_bytes())
}
pub struct Guard {
token: String,
any_host: bool,
allowed_hosts: Vec<String>,
codes: Mutex<HashMap<String, Instant>>,
}
impl Guard {
pub fn new(token: String, any_host: bool) -> Self {
Self {
token,
any_host,
allowed_hosts: Vec::new(),
codes: Mutex::new(HashMap::new()),
}
}
pub fn allow_hosts(mut self, hosts: Vec<String>) -> Self {
self.allowed_hosts = hosts;
self
}
fn answers_to(&self, host: &str) -> bool {
if self.any_host || is_loopback_host(host) {
return true;
}
host_name(host).is_some_and(|name| self.allowed_hosts.contains(&name))
}
pub fn token(&self) -> &str {
&self.token
}
pub fn new_login_code(&self) -> String {
let code = random_hex(16);
let mut codes = self.codes.lock().expect("login codes lock");
codes.retain(|_, made| made.elapsed() < CODE_LIFETIME);
codes.insert(code.clone(), Instant::now());
code
}
pub fn redeem(&self, code: &str) -> bool {
let mut codes = self.codes.lock().expect("login codes lock");
matches!(codes.remove(code), Some(made) if made.elapsed() < CODE_LIFETIME)
}
pub fn is_token(&self, candidate: &str) -> bool {
same(candidate.as_bytes(), self.token.as_bytes())
}
fn signed_in(&self, headers: &HeaderMap) -> bool {
let bearer = headers
.get(header::AUTHORIZATION)
.and_then(|v| v.to_str().ok())
.and_then(|v| v.strip_prefix("Bearer "));
if bearer.is_some_and(|b| same(b.trim().as_bytes(), self.token.as_bytes())) {
return true;
}
cookie(headers, COOKIE).is_some_and(|c| same(c.as_bytes(), self.token.as_bytes()))
}
}
pub fn cookie<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> {
headers
.get_all(header::COOKIE)
.iter()
.filter_map(|v| v.to_str().ok())
.flat_map(|v| v.split(';'))
.filter_map(|pair| pair.trim().split_once('='))
.find(|(k, _)| *k == name)
.map(|(_, v)| v)
}
fn same(a: &[u8], b: &[u8]) -> bool {
a.len() == b.len() && a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0
}
pub fn host_name(host: &str) -> Option<String> {
let name = if let Some(rest) = host.strip_prefix('[') {
match rest.split_once(']') {
Some((name, tail)) if tail.is_empty() || port(tail) => name,
_ => return None,
}
} else {
match host.split_once(':') {
Some((name, tail)) if port(&format!(":{tail}")) => name,
Some(_) => return None,
None => host,
}
};
let valid = !name.is_empty()
&& name
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | ':'));
valid.then(|| name.to_ascii_lowercase())
}
fn port(tail: &str) -> bool {
tail.strip_prefix(':')
.is_some_and(|p| !p.is_empty() && p.chars().all(|c| c.is_ascii_digit()))
}
pub fn is_loopback_host(host: &str) -> bool {
host_name(host).is_some_and(|name| name == "localhost" || name == "127.0.0.1" || name == "::1")
}
pub fn allowed_host(text: &str) -> Result<String> {
let trimmed = text.trim();
let bare = trimmed
.strip_prefix("https://")
.or_else(|| trimmed.strip_prefix("http://"))
.unwrap_or(trimmed);
let bare = bare.strip_suffix('/').unwrap_or(bare);
match host_name(bare) {
Some(name) if !bare.contains('/') => Ok(name),
_ => bail!("'{text}' is not a host name, such as mybox.tail1234.ts.net"),
}
}
fn refuse(status: StatusCode, message: &str) -> Response {
(
status,
axum::Json(json!({ "error": { "message": message } })),
)
.into_response()
}
fn open_path(path: &str) -> bool {
path == "/login" || path == "/health" || !(path.starts_with("/api") || path.starts_with("/v1"))
}
pub async fn guard(State(guard): State<Arc<Guard>>, request: Request, next: Next) -> Response {
let headers = request.headers();
let host = headers
.get(header::HOST)
.and_then(|v| v.to_str().ok())
.unwrap_or_default();
if !guard.answers_to(host) {
let message = match host_name(host) {
Some(name) => format!(
"the daemon does not answer to '{name}'. If that is this computer, start the daemon with --allow-host {name}"
),
None => "the daemon answers only to localhost, 127.0.0.1, [::1] and names given with --allow-host".into(),
};
return refuse(StatusCode::FORBIDDEN, &message);
}
if let Some(origin) = headers.get(header::ORIGIN) {
let origin = origin.to_str().unwrap_or_default();
let ours = origin
.strip_prefix("http://")
.or_else(|| origin.strip_prefix("https://"))
.is_some_and(|rest| rest == host);
if !ours {
return refuse(
StatusCode::FORBIDDEN,
"requests from other web pages are refused",
);
}
}
if !open_path(request.uri().path()) && !guard.signed_in(headers) {
return refuse(
StatusCode::UNAUTHORIZED,
"sign in with `kjarni daemon open`, or send the token from the daemon's token file as a bearer token",
);
}
next.run(request).await
}