use futures::channel::oneshot;
use std::io::{self, Read, Write};
use std::net::{Ipv4Addr, Ipv6Addr, TcpListener, TcpStream};
use std::sync::mpsc;
use std::time::{Duration, Instant};
const ACCEPT_POLL: Duration = Duration::from_millis(100);
const STREAM_TIMEOUT: Duration = Duration::from_secs(10);
const EXCHANGE_WAIT: Duration = Duration::from_secs(120);
const MAX_REQUEST_BYTES: usize = 16 * 1024;
const ANY_PORT_ATTEMPTS: usize = 6;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LoopbackHost {
Ipv4,
Localhost,
}
#[derive(Debug, thiserror::Error)]
pub enum LoopbackError {
#[error(transparent)]
Bind(io::Error),
#[error(transparent)]
Io(io::Error),
#[error("timed out waiting for the sign-in in the browser")]
TimedOut,
#[error("{0}")]
Refused(String),
#[error("the sign-in listener stopped unexpectedly")]
Stopped,
}
#[derive(Clone, Debug)]
pub struct Loopback {
provider: String,
app: String,
host: LoopbackHost,
ports: Vec<u16>,
path: String,
}
impl Loopback {
pub fn new(
provider: impl Into<String>,
host: LoopbackHost,
ports: impl IntoIterator<Item = u16>,
path: impl Into<String>,
) -> Self {
Self {
provider: provider.into(),
app: "rig".into(),
host,
ports: ports.into_iter().collect(),
path: path.into(),
}
}
#[must_use]
pub fn with_app_name(mut self, app: impl Into<String>) -> Self {
self.app = app.into();
self
}
pub fn listen(&self, state: String, deadline: Instant) -> Result<Listener, LoopbackError> {
let (listeners, port) = self.bind()?;
for listener in &listeners {
listener.set_nonblocking(true).map_err(LoopbackError::Io)?;
}
let host = match self.host {
LoopbackHost::Ipv4 => "127.0.0.1",
LoopbackHost::Localhost => "localhost",
};
let redirect_uri = format!("http://{host}:{port}{}", self.path);
let (sender, callback) = oneshot::channel();
let serving = Serving {
pages: Pages {
provider: escape_html(&self.provider),
app: escape_html(&self.app),
},
path: self.path.clone(),
state,
deadline,
};
std::thread::Builder::new()
.name("sign-in-listener".into())
.spawn(move || serving.serve(&listeners, sender))
.map_err(LoopbackError::Io)?;
Ok(Listener {
redirect_uri,
callback,
})
}
fn bind(&self) -> Result<(Vec<TcpListener>, u16), LoopbackError> {
for &port in &self.ports {
let attempts = if port == 0 { ANY_PORT_ATTEMPTS } else { 1 };
for _ in 0..attempts {
if let Some(bound) = self.bind_port(port)? {
return Ok(bound);
}
}
}
let ports: Vec<String> = self.ports.iter().map(u16::to_string).collect();
Err(LoopbackError::Bind(io::Error::new(
io::ErrorKind::AddrInUse,
format!(
"the sign-in callback ports {} on the loopback address are in use",
ports.join(", ")
),
)))
}
fn bind_port(&self, port: u16) -> Result<Option<(Vec<TcpListener>, u16)>, LoopbackError> {
let ipv4 = match TcpListener::bind((Ipv4Addr::LOCALHOST, port)) {
Ok(listener) => listener,
Err(error) if taken(&error) => return Ok(None),
Err(error) => return Err(LoopbackError::Bind(error)),
};
let port = ipv4.local_addr().map_err(LoopbackError::Bind)?.port();
if self.host == LoopbackHost::Ipv4 {
return Ok(Some((vec![ipv4], port)));
}
match TcpListener::bind((Ipv6Addr::LOCALHOST, port)) {
Ok(ipv6) => Ok(Some((vec![ipv4, ipv6], port))),
Err(error) if taken(&error) => Ok(None),
Err(_) => Ok(Some((vec![ipv4], port))),
}
}
}
fn taken(error: &io::Error) -> bool {
matches!(
error.kind(),
io::ErrorKind::AddrInUse | io::ErrorKind::PermissionDenied
)
}
pub struct Listener {
redirect_uri: String,
callback: oneshot::Receiver<Result<Received, LoopbackError>>,
}
impl Listener {
pub fn redirect_uri(&self) -> &str {
&self.redirect_uri
}
pub async fn code(self) -> Result<Received, LoopbackError> {
self.callback.await.map_err(|_| LoopbackError::Stopped)?
}
}
pub struct Received {
code: String,
outcome: mpsc::Sender<Result<(), String>>,
}
impl Received {
pub fn code(&self) -> &str {
&self.code
}
pub fn succeeded(self) {
self.outcome.send(Ok(())).ok();
}
pub fn failed(self, reason: &str) {
self.outcome.send(Err(reason.to_owned())).ok();
}
}
#[derive(Debug, PartialEq, Eq)]
enum Callback {
Code(String),
Failed(String),
StateMismatch,
NoCode,
Other,
}
fn parse_callback(request_line: &str, path: &str, expected_state: &str) -> Callback {
let mut parts = request_line.split_whitespace();
let (Some("GET"), Some(target)) = (parts.next(), parts.next()) else {
return Callback::Other;
};
let (target_path, query) = target.split_once('?').unwrap_or((target, ""));
if target_path != path {
return Callback::Other;
}
let (mut code, mut state, mut error, mut description) = (Vec::new(), Vec::new(), None, None);
for (key, value) in url::form_urlencoded::parse(query.as_bytes()) {
match key.as_ref() {
"code" => code.push(value.into_owned()),
"state" => state.push(value.into_owned()),
"error" => error = Some(value.into_owned()),
"error_description" => description = Some(value.into_owned()),
_ => {}
}
}
if state.as_slice() != [expected_state] {
return Callback::StateMismatch;
}
if let Some(error) = error {
return Callback::Failed(match description.filter(|text| !text.trim().is_empty()) {
Some(description) => format!("the browser sign-in returned {error} ({description})"),
None => format!("the browser sign-in returned {error}"),
});
}
match code.as_slice() {
[code] if !code.is_empty() => Callback::Code(code.clone()),
_ => Callback::NoCode,
}
}
struct Serving {
pages: Pages,
path: String,
state: String,
deadline: Instant,
}
impl Serving {
fn serve(
&self,
listeners: &[TcpListener],
sender: oneshot::Sender<Result<Received, LoopbackError>>,
) {
let pages = &self.pages;
let result = 'serve: loop {
if sender.is_canceled() {
return;
}
if Instant::now() >= self.deadline {
break Err(LoopbackError::TimedOut);
}
let mut accepted = None;
for listener in listeners {
match listener.accept() {
Ok((stream, _)) => {
accepted = Some(stream);
break;
}
Err(error)
if matches!(
error.kind(),
io::ErrorKind::WouldBlock | io::ErrorKind::Interrupted
) => {}
Err(error) => break 'serve Err(LoopbackError::Io(error)),
}
}
let Some(mut stream) = accepted else {
std::thread::sleep(ACCEPT_POLL);
continue;
};
let until = (Instant::now() + STREAM_TIMEOUT).min(self.deadline);
let Ok(line) = read_request_line(&mut stream, until, || sender.is_canceled()) else {
continue;
};
match parse_callback(&line, &self.path, &self.state) {
Callback::Other => respond(&mut stream, "404 Not Found", "Not found."),
Callback::StateMismatch => {
respond(&mut stream, "400 Bad Request", &pages.state_mismatch())
}
Callback::NoCode => respond(&mut stream, "400 Bad Request", &pages.no_code()),
Callback::Failed(message) => {
respond(&mut stream, "200 OK", &pages.failed(&message));
break Err(LoopbackError::Refused(message));
}
Callback::Code(code) => {
let (outcome, wait) = mpsc::channel();
if sender.send(Ok(Received { code, outcome })).is_err() {
respond(&mut stream, "200 OK", &pages.cancelled());
return;
}
let body = match wait.recv_timeout(EXCHANGE_WAIT) {
Ok(Ok(())) => pages.signed_in(),
Ok(Err(message)) => pages.failed(&message),
Err(_) => pages.cancelled(),
};
respond(&mut stream, "200 OK", &body);
return;
}
}
};
sender.send(result).ok();
}
}
fn read_request_line(
stream: &mut TcpStream,
until: Instant,
stopped: impl Fn() -> bool,
) -> io::Result<String> {
stream.set_nonblocking(false)?;
stream.set_write_timeout(Some(STREAM_TIMEOUT))?;
let mut head = Vec::new();
let mut chunk = [0u8; 1024];
while !head.windows(4).any(|window| window == b"\r\n\r\n") {
if head.len() >= MAX_REQUEST_BYTES {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"request head too large",
));
}
let left = until.saturating_duration_since(Instant::now());
if left.is_zero() || stopped() {
return Err(io::ErrorKind::TimedOut.into());
}
stream.set_read_timeout(Some(left.min(ACCEPT_POLL)))?;
match stream.read(&mut chunk) {
Ok(0) => break,
Ok(read) => head.extend(chunk.iter().take(read)),
Err(error)
if matches!(
error.kind(),
io::ErrorKind::WouldBlock
| io::ErrorKind::TimedOut
| io::ErrorKind::Interrupted
) => {}
Err(error) => return Err(error),
}
}
let head = String::from_utf8_lossy(&head);
Ok(head.lines().next().unwrap_or_default().to_owned())
}
fn respond(stream: &mut TcpStream, status: &str, body: &str) {
let response = format!(
"HTTP/1.1 {status}\r\nContent-Type: text/html; charset=utf-8\r\n\
Content-Length: {}\r\nCache-Control: no-store\r\nConnection: close\r\n\r\n{body}",
body.len()
);
stream.write_all(response.as_bytes()).ok();
stream.flush().ok();
}
struct Pages {
provider: String,
app: String,
}
impl Pages {
fn signed_in(&self) -> String {
let Self { provider, app } = self;
page(
&format!("Signed in to {provider}"),
&format!("You can close this tab and return to {app}."),
)
}
fn failed(&self, message: &str) -> String {
let message = escape_html(message);
page(
"Sign-in failed",
&format!("Sign-in failed: {message}. Return to {}.", self.app),
)
}
fn state_mismatch(&self) -> String {
let app = &self.app;
page(
"Sign-in not recognised",
&format!(
"This sign-in link is not the one {app} is waiting for. Start it again from {app}."
),
)
}
fn no_code(&self) -> String {
let app = &self.app;
page(
"No sign-in code",
&format!(
"This page brought back no sign-in code. {app} is still waiting: finish signing in from the provider's page."
),
)
}
fn cancelled(&self) -> String {
let app = &self.app;
page(
"Sign-in cancelled",
&format!("{app} stopped waiting for this sign-in. Start it again from {app}."),
)
}
}
fn page(title: &str, message: &str) -> String {
format!(
"<!doctype html><html><head><meta charset=\"utf-8\"><title>{title}</title></head>\
<body style=\"font-family: system-ui, sans-serif; margin: 4rem auto; max-width: 32rem\">\
<h1>{title}</h1><p>{message}</p></body></html>"
)
}
fn escape_html(text: &str) -> String {
let mut escaped = String::with_capacity(text.len());
for character in text.chars() {
match character {
'&' => escaped.push_str("&"),
'<' => escaped.push_str("<"),
'>' => escaped.push_str(">"),
'"' => escaped.push_str("""),
'\'' => escaped.push_str("'"),
other => escaped.push(other),
}
}
escaped
}
#[cfg(test)]
mod tests;