use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::OnceLock;
use std::time::{Duration, Instant};
use crate::crypto::ring::ct_eq;
use crate::error::{Error, Result};
use crate::net::http::{Request, Response};
use crate::net::httpd;
use crate::net::tunnel::PeerLookup;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Via {
Local,
Public,
}
struct Public {
host: String,
token: String,
expires: Option<Instant>,
peers: Option<PeerLookup>,
expiry_logged: AtomicBool,
}
pub struct Access {
name: String,
port: u16,
token: String,
public: OnceLock<Public>,
}
pub struct Checked<'a> {
pub via: Via,
pub path: &'a str,
pub token_ok: bool,
}
impl Access {
pub fn new(name: &str, port: u16, token: String) -> Access {
Access {
name: name.to_owned(),
port,
token,
public: OnceLock::new(),
}
}
pub fn port(&self) -> u16 {
self.port
}
pub fn token(&self) -> &str {
&self.token
}
pub fn publish(
&self,
host: &str,
ttl: Option<Duration>,
peers: Option<PeerLookup>,
) -> Result<String> {
let token = crate::util::hex::encode(&crate::crypto::ring::random_array::<16>()?);
let public = Public {
host: host.to_ascii_lowercase(),
token: token.clone(),
expires: ttl.map(|t| Instant::now() + t),
peers,
expiry_logged: AtomicBool::new(false),
};
self.public
.set(public)
.map_err(|_| Error::usage("a page has one public host"))?;
Ok(token)
}
pub fn public_host(&self) -> Option<&str> {
self.public.get().map(|p| p.host.as_str())
}
pub fn expired(&self) -> bool {
self.public
.get()
.and_then(|p| p.expires)
.is_some_and(|e| Instant::now() >= e)
}
fn is_loopback(&self, authority: &str) -> bool {
let port = self.port;
[
format!("127.0.0.1:{port}"),
format!("localhost:{port}"),
format!("[::1]:{port}"),
]
.iter()
.any(|h| h.eq_ignore_ascii_case(authority))
}
fn is_public(&self, authority: &str) -> bool {
self.public.get().is_some_and(|p| {
let a = authority.strip_suffix(":443").unwrap_or(authority);
a.eq_ignore_ascii_case(&p.host)
})
}
pub fn via(&self, host: &str) -> Option<Via> {
if self.is_loopback(host) {
Some(Via::Local)
} else if self.is_public(host) {
Some(Via::Public)
} else {
None
}
}
pub fn token_ok(&self, via: Via, query: &str) -> bool {
let want = match via {
Via::Local => self.token.as_str(),
Via::Public => match self.public.get() {
Some(p) => p.token.as_str(),
None => return false,
},
};
query.split('&').any(|kv| {
kv.strip_prefix("t=")
.is_some_and(|t| ct_eq(t.as_bytes(), want.as_bytes()))
})
}
pub fn origin_ok(&self, via: Via, origin: Option<&str>) -> bool {
let Some(o) = origin else { return true };
match via {
Via::Local => o
.strip_prefix("http://")
.is_some_and(|a| self.is_loopback(a)),
Via::Public => o
.strip_prefix("https://")
.is_some_and(|a| self.is_public(a)),
}
}
pub fn check<'a>(
&self,
req: &'a Request,
free: &[&str],
) -> std::result::Result<Checked<'a>, Response> {
let Some(via) = self.via(req.headers.get("host").unwrap_or("")) else {
return Err(httpd::error_response(421, "Misdirected Request"));
};
if req.method != "GET" && req.method != "HEAD" {
return Err(httpd::error_response(405, "Method Not Allowed"));
}
if via == Via::Public && self.expired() {
if let Some(p) = self.public.get() {
if !p.expiry_logged.swap(true, Ordering::SeqCst) {
info!(
"{}: the public link expired; it no longer lets anyone in",
self.name
);
}
}
return Err(httpd::error_response(410, "Gone (this link has expired)"));
}
let (path, query) = req.target.split_once('?').unwrap_or((&req.target, ""));
let token_ok = self.token_ok(via, query);
let free_path = free.contains(&path) || path.starts_with("/fonts/");
if via == Via::Public && !token_ok && !free_path {
info!(
"{}: refused a public request without the token: {path}",
self.name
);
}
Ok(Checked {
via,
path,
token_ok,
})
}
pub fn remote_peer(&self, local: Option<SocketAddr>) -> Option<String> {
let lookup = self.public.get()?.peers.as_ref()?;
local.and_then(|a| lookup(a)).filter(|p| !p.is_empty())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::net::http::Version;
fn req(target: &str, host: &str) -> Request {
Request::new("GET", target, Version::Http11).header("Host", host)
}
#[test]
fn local_and_public() {
let a = Access::new("test", 8765, "local".into());
assert_eq!(a.via("127.0.0.1:8765"), Some(Via::Local));
assert_eq!(a.via("LOCALHOST:8765"), Some(Via::Local));
assert_eq!(a.via("[::1]:8765"), Some(Via::Local));
assert_eq!(a.via("127.0.0.1:8766"), None);
assert_eq!(a.via("x.opentunnel.xyz"), None);
assert!(!a.token_ok(Via::Public, "t="));
let public = a.publish("X.opentunnel.xyz", None, None).unwrap();
assert!(a.publish("y.opentunnel.xyz", None, None).is_err());
assert_eq!(a.public_host(), Some("x.opentunnel.xyz"));
assert_eq!(a.via("x.opentunnel.xyz"), Some(Via::Public));
assert_eq!(a.via("x.opentunnel.xyz:443"), Some(Via::Public));
assert_eq!(a.via("x.opentunnel.xyz:8765"), None);
assert_eq!(a.via("y.opentunnel.xyz"), None);
assert!(a.token_ok(Via::Local, "delay=1&t=local"));
assert!(!a.token_ok(Via::Public, "t=local"));
assert!(a.token_ok(Via::Public, &format!("t={public}")));
assert!(!a.token_ok(Via::Local, &format!("t={public}")));
assert!(!a.token_ok(Via::Local, "t=loca"));
assert!(a.origin_ok(Via::Local, None));
assert!(a.origin_ok(Via::Local, Some("http://localhost:8765")));
assert!(!a.origin_ok(Via::Local, Some("https://x.opentunnel.xyz")));
assert!(a.origin_ok(Via::Public, Some("https://x.opentunnel.xyz")));
assert!(!a.origin_ok(Via::Public, Some("http://x.opentunnel.xyz")));
assert!(!a.origin_ok(Via::Public, Some("https://evil.example")));
assert!(!a.origin_ok(Via::Public, Some("http://127.0.0.1:8765")));
assert!(!a.expired());
assert!(a.remote_peer(None).is_none());
}
#[test]
fn checks_and_expiry() {
let a = Access::new("test", 1, "local".into());
let lookup: PeerLookup = std::sync::Arc::new(|addr: SocketAddr| {
(addr.port() == 5).then(|| "203.0.113.9:4000".to_owned())
});
let t = a
.publish("p.example", Some(Duration::from_millis(200)), Some(lookup))
.unwrap();
let status = |r: &Request| a.check(r, &["/x.js"]).err().map(|e| e.status);
assert_eq!(status(&req("/", "evil:1")), Some(421));
let post = Request::new("POST", "/", Version::Http11).header("Host", "127.0.0.1:1");
assert_eq!(status(&post), Some(405));
let r = req(&format!("/ws?t={t}"), "p.example");
let c = a.check(&r, &[]).ok().unwrap();
assert!(c.via == Via::Public && c.token_ok && c.path == "/ws");
let r = req("/x.js", "p.example");
let c = a.check(&r, &["/x.js"]).ok().unwrap();
assert!(!c.token_ok);
assert_eq!(
a.remote_peer(Some("127.0.0.1:5".parse().unwrap()))
.as_deref(),
Some("203.0.113.9:4000")
);
assert_eq!(a.remote_peer(Some("127.0.0.1:6".parse().unwrap())), None);
std::thread::sleep(Duration::from_millis(250));
assert!(a.expired());
assert_eq!(status(&req(&format!("/?t={t}"), "p.example")), Some(410));
assert!(a.check(&req("/?t=local", "127.0.0.1:1"), &[]).is_ok());
}
}