resolve merge conflicts

This commit is contained in:
Johann150 2021-01-27 22:00:28 +01:00
commit 46866c4320
No known key found for this signature in database
GPG key ID: 9EE6577A2A06F8F1
2 changed files with 213 additions and 157 deletions

View file

@ -12,6 +12,7 @@ use {
borrow::Cow, borrow::Cow,
error::Error, error::Error,
ffi::OsStr, ffi::OsStr,
fmt::Write,
fs::File, fs::File,
io::BufReader, io::BufReader,
net::SocketAddr, net::SocketAddr,
@ -45,8 +46,14 @@ fn main() -> Result {
let (stream, _) = listener.accept().await?; let (stream, _) = listener.accept().await?;
let arc = mimetypes.clone(); let arc = mimetypes.clone();
tokio::spawn(async { tokio::spawn(async {
if let Err(e) = handle_request(stream, arc).await { match RequestHandle::new(stream, arc).await {
log::error!("{:?}", e); Ok(handle) => match handle.handle().await {
Ok(info) => log::info!("{}", info),
Err(err) => log::warn!("{}", err),
},
Err(log_line) => {
log::warn!("{}", log_line);
}
} }
}); });
} }
@ -71,6 +78,7 @@ struct Args {
language: Option<String>, language: Option<String>,
silent: bool, silent: bool,
serve_secret: bool, serve_secret: bool,
log_ips: bool,
} }
fn args() -> Result<Args> { fn args() -> Result<Args> {
@ -115,6 +123,7 @@ fn args() -> Result<Args> {
opts.optflag("s", "silent", "Disable logging output"); opts.optflag("s", "silent", "Disable logging output");
opts.optflag("h", "help", "Print this help menu"); opts.optflag("h", "help", "Print this help menu");
opts.optflag("", "serve-secret", "Enable serving secret files (files/directories starting with a dot)"); opts.optflag("", "serve-secret", "Enable serving secret files (files/directories starting with a dot)");
opts.optflag("", "log-ip", "Output IP addresses when logging");
let matches = opts.parse(&args[1..]).map_err(|f| f.to_string())?; let matches = opts.parse(&args[1..]).map_err(|f| f.to_string())?;
if matches.opt_present("h") { if matches.opt_present("h") {
@ -144,6 +153,7 @@ fn args() -> Result<Args> {
language: matches.opt_str("lang"), language: matches.opt_str("lang"),
silent: matches.opt_present("s"), silent: matches.opt_present("s"),
serve_secret: matches.opt_present("serve-secret"), serve_secret: matches.opt_present("serve-secret"),
log_ips: matches.opt_present("log-ip"),
}) })
} }
@ -155,18 +165,6 @@ fn check_path(s: String) -> Result<String, String> {
} }
} }
/// Handle a single client session (request + response).
async fn handle_request(stream: TcpStream, mimetypes: Arc<RwLock<FileOptions>>) -> Result {
let stream = &mut TLS.accept(stream).await?;
match parse_request(stream).await {
Ok(url) => send_response(url, stream, mimetypes).await?,
Err((status, msg)) => send_header(stream, status, &[msg]).await?,
}
stream.shutdown().await?;
Ok(())
}
/// TLS configuration. /// TLS configuration.
static TLS: Lazy<TlsAcceptor> = Lazy::new(|| acceptor().unwrap()); static TLS: Lazy<TlsAcceptor> = Lazy::new(|| acceptor().unwrap());
@ -182,164 +180,222 @@ fn acceptor() -> Result<TlsAcceptor> {
Ok(TlsAcceptor::from(Arc::new(config))) Ok(TlsAcceptor::from(Arc::new(config)))
} }
/// Return the URL requested by the client. struct RequestHandle {
async fn parse_request( stream: TlsStream<TcpStream>,
stream: &mut TlsStream<TcpStream>, log_line: String,
) -> std::result::Result<Url, (u8, &'static str)> { metadata: Arc<RwLock<FileOptions>>,
// Because requests are limited to 1024 bytes (plus 2 bytes for CRLF), we
// can use a fixed-sized buffer on the stack, avoiding allocations and
// copying, and stopping bad clients from making us use too much memory.
let mut request = [0; 1026];
let mut buf = &mut request[..];
let mut len = 0;
// Read until CRLF, end-of-stream, or there's no buffer space left.
loop {
let bytes_read = stream
.read(buf)
.await
.or(Err((59, "Request ended unexpectedly")))?;
len += bytes_read;
if request[..len].ends_with(b"\r\n") {
break;
} else if bytes_read == 0 {
return Err((59, "Request ended unexpectedly"));
}
buf = &mut request[len..];
}
let request = std::str::from_utf8(&request[..len - 2]).or(Err((59, "Non-UTF-8 request")))?;
log::info!("Got request for {:?}", request);
let url = Url::parse(request).or(Err((59, "Invalid URL")))?;
// Validate the URL, host and port.
if url.scheme() != "gemini" {
return Err((53, "Unsupported URL scheme"));
}
// TODO: Can be simplified by https://github.com/servo/rust-url/pull/651
if let (Some(Host::Domain(expected)), Some(Host::Domain(host))) = (url.host(), &ARGS.hostname) {
if host != expected {
return Err((53, "Proxy request refused"));
}
}
if let Some(port) = url.port() {
// Validate that the port in the URL is the same as for the stream this request came in on.
if port != stream.get_ref().0.local_addr().unwrap().port() {
return Err((53, "proxy request refused"));
}
}
Ok(url)
} }
/// Send the client the file located at the requested URL. impl RequestHandle {
async fn send_response( /// Creates a new request handle for the given stream. If establishing the TLS
url: Url, /// session fails, returns a corresponding log line.
stream: &mut TlsStream<TcpStream>, async fn new(stream: TcpStream, metadata: Arc<RwLock<FileOptions>>) -> Result<Self, String> {
mimetypes: Arc<RwLock<FileOptions>>, let log_line = format!(
) -> Result { "{} {}",
let mut path = std::path::PathBuf::from(&ARGS.content_dir); stream.local_addr().unwrap(),
if let Some(segments) = url.path_segments() { if ARGS.log_ips {
for segment in segments { stream
if !ARGS.serve_secret && segment.starts_with('.') { .peer_addr()
// Do not serve anything that looks like a hidden file. .expect("could not get peer address")
return send_header(stream, 52, &["If I told you, it would not be a secret."]) .to_string()
.await;
}
path.push(&*percent_decode_str(segment).decode_utf8()?);
}
}
if let Ok(metadata) = tokio::fs::metadata(&path).await {
if metadata.is_dir() {
if url.path().ends_with('/') || url.path().is_empty() {
// if the path ends with a slash or the path is empty, the links will work the same
// without a redirect
path.push("index.gmi");
if !path.exists() && path.with_file_name(".directory-listing-ok").exists() {
path.pop();
return list_directory(stream, &path).await;
}
} else { } else {
// if client is not redirected, links may not work as expected without trailing slash // Do not log IP address, but something else so columns still line up.
let mut url = url; "-".into()
url.set_path(&format!("{}/", url.path())); }
return send_header(stream, 31, &[url.as_str()]).await; );
match TLS.accept(stream).await {
Ok(stream) => Ok(Self { stream, log_line, metadata }),
Err(e) => Err(format!("{} error:{}", log_line, e)),
}
}
/// Do the necessary actions to handle this request. Returns a corresponding
/// log line as Err or Ok, depending on if the request finished with or
/// without errors.
async fn handle(mut self) -> Result<String, String> {
// not already in error condition
let result = match self.parse_request().await {
Ok(url) => self.send_response(url).await,
Err((status, msg)) => self.send_header(status, msg).await,
};
if let Err(e) = result {
Err(format!("{} error:{}", self.log_line, e))
} else if let Err(e) = self.stream.shutdown().await {
Err(format!("{} error:{}", self.log_line, e))
} else {
Ok(self.log_line)
}
}
/// Return the URL requested by the client.
async fn parse_request(&mut self) -> std::result::Result<Url, (u8, &'static str)> {
// Because requests are limited to 1024 bytes (plus 2 bytes for CRLF), we
// can use a fixed-sized buffer on the stack, avoiding allocations and
// copying, and stopping bad clients from making us use too much memory.
let mut request = [0; 1026];
let mut buf = &mut request[..];
let mut len = 0;
// Read until CRLF, end-of-stream, or there's no buffer space left.
loop {
let bytes_read = self
.stream
.read(buf)
.await
.or(Err((59, "Request ended unexpectedly")))?;
len += bytes_read;
if request[..len].ends_with(b"\r\n") {
break;
} else if bytes_read == 0 {
return Err((59, "Request ended unexpectedly"));
}
buf = &mut request[len..];
}
let request =
std::str::from_utf8(&request[..len - 2]).or(Err((59, "Non-UTF-8 request")))?;
// log literal request (might be different from or not an actual URL)
write!(self.log_line, " \"{}\"", request).unwrap();
let url = Url::parse(request).or(Err((59, "Invalid URL")))?;
// Validate the URL, host and port.
if url.scheme() != "gemini" {
return Err((53, "Unsupported URL scheme"));
}
// TODO: Can be simplified by https://github.com/servo/rust-url/pull/651
if let (Some(Host::Domain(expected)), Some(Host::Domain(host))) =
(url.host(), &ARGS.hostname)
{
if host != expected {
return Err((53, "Proxy request refused"));
} }
} }
if let Some(port) = url.port() {
// Validate that the port in the URL is the same as for the stream this request came in on.
if port != self.stream.get_ref().0.local_addr().unwrap().port() {
return Err((53, "proxy request refused"));
}
}
Ok(url)
} }
// Make sure the file opens successfully before sending the success header. /// Send the client the file located at the requested URL.
let mut file = match tokio::fs::File::open(&path).await { async fn send_response(&mut self, url: Url) -> Result {
Ok(file) => file, let mut path = std::path::PathBuf::from(&ARGS.content_dir);
Err(e) => { if let Some(segments) = url.path_segments() {
send_header(stream, 51, &["Not found, sorry."]).await?; for segment in segments {
Err(e)? if !ARGS.serve_secret && segment.starts_with('.') {
// Do not serve anything that looks like a hidden file.
return self
.send_header(52, "If I told you, it would not be a secret.")
.await;
}
path.push(&*percent_decode_str(segment).decode_utf8()?);
}
} }
};
// Send header. if let Ok(metadata) = tokio::fs::metadata(&path).await {
let mut locked = mimetypes.write().await; if metadata.is_dir() {
let data = locked.get(&path); if url.path().ends_with('/') || url.path().is_empty() {
if data.is_empty() || data.starts_with(";") { // if the path ends with a slash or the path is empty, the links will work the same
// guess MIME type // without a redirect
if path.extension() == Some(OsStr::new("gmi")) { path.push("index.gmi");
send_header(stream, 20, &["text/gemini", data]).await?; if !path.exists() && path.with_file_name(".directory-listing-ok").exists() {
path.pop();
return self.list_directory(&path).await;
}
} else {
// if client is not redirected, links may not work as expected without trailing slash
let mut url = url;
url.set_path(&format!("{}/", url.path()));
return self.send_header(31, url.as_str()).await;
}
}
}
// Make sure the file opens successfully before sending the success header.
let mut file = match tokio::fs::File::open(&path).await {
Ok(file) => file,
Err(e) => {
self.send_header(51, "Not found, sorry.").await?;
Err(e)?
}
};
// Send header.
let mut locked = self.metadata.write().await;
let data = locked.get(&path);
let mime = if data.is_empty() || data.starts_with(';') {
// guess MIME type
if path.extension() == Some(OsStr::new("gmi")) {
format!("text/gemini{}", data)
} else {
let mime = mime_guess::from_path(&path).first_or_octet_stream();
format!("{}{}", mime.essence_str(), data)
}
} else { } else {
let mime = mime_guess::from_path(&path).first_or_octet_stream(); // this must be a full MIME type
send_header(stream, 20, &[mime.essence_str(), data]).await?; data.to_owned()
}; };
} else { drop(locked);
// this must be a full MIME type self.send_header(20, &mime).await?;
send_header(stream, 20, &[data]).await?;
// Send body.
tokio::io::copy(&mut file, &mut self.stream).await?;
Ok(())
} }
drop(locked);
// Send body. async fn list_directory(&mut self, path: &Path) -> Result {
tokio::io::copy(&mut file, stream).await?; // https://url.spec.whatwg.org/#path-percent-encode-set
Ok(()) const ENCODE_SET: AsciiSet = CONTROLS
} .add(b' ')
.add(b'"')
.add(b'#')
.add(b'<')
.add(b'>')
.add(b'?')
.add(b'`')
.add(b'{')
.add(b'}');
async fn list_directory(stream: &mut TlsStream<TcpStream>, path: &Path) -> Result { log::info!("Listing directory {:?}", path);
// https://url.spec.whatwg.org/#path-percent-encode-set self.send_header(20, "text/gemini").await?;
const ENCODE_SET: AsciiSet = CONTROLS.add(b' ') let mut entries = tokio::fs::read_dir(path).await?;
.add(b'"').add(b'#').add(b'<').add(b'>') let mut lines = vec![];
.add(b'?').add(b'`').add(b'{').add(b'}'); while let Some(entry) = entries.next_entry().await? {
let mut name = entry
log::info!("Listing directory {:?}", path); .file_name()
send_header(stream, 20, &["text/gemini"]).await?; .into_string()
let mut entries = tokio::fs::read_dir(path).await?; .or(Err("Non-Unicode filename"))?;
let mut lines = vec![]; if name.starts_with('.') {
while let Some(entry) = entries.next_entry().await? { continue;
let mut name = entry }
.file_name() if entry.file_type().await?.is_dir() {
.into_string() name += "/";
.or(Err("Non-Unicode filename"))?; }
if name.starts_with('.') { let line = match percent_encode(name.as_bytes(), &ENCODE_SET).into() {
continue; Cow::Owned(url) => format!("=> {} {}\n", url, name),
Cow::Borrowed(url) => format!("=> {}\n", url), // url and name are identical
};
lines.push(line);
} }
if entry.file_type().await?.is_dir() { lines.sort();
name += "/"; for line in lines {
self.stream.write_all(line.as_bytes()).await?;
} }
let line = match percent_encode(name.as_bytes(), &ENCODE_SET).into() { Ok(())
Cow::Owned(url) => format!("=> {} {}\n", url, name),
Cow::Borrowed(url) => format!("=> {}\n", url), // url and name are identical
};
lines.push(line);
} }
lines.sort();
for line in lines {
stream.write_all(line.as_bytes()).await?;
}
Ok(())
}
async fn send_header(stream: &mut TlsStream<TcpStream>, status: u8, meta: &[&str]) -> Result { async fn send_header(&mut self, status: u8, meta: &str) -> Result {
use std::fmt::Write; // add response status and response meta
let mut response = String::with_capacity(64); write!(self.log_line, " {} \"{}\"", status, meta)?;
write!(response, "{} ", status)?;
response.extend(meta.iter().copied()); self.stream
log::info!("Responding with status {:?}", response); .write_all(format!("{} {}\r\n", status, meta).as_bytes())
response.push_str("\r\n"); .await?;
stream.write_all(response.as_bytes()).await?; Ok(())
Ok(()) }
} }

View file

@ -84,7 +84,7 @@ impl FileOptions {
// discard any I/O errors // discard any I/O errors
.filter_map(|line| line.ok()) .filter_map(|line| line.ok())
// filter out comment lines // filter out comment lines
.filter(|line| !line.trim_start().starts_with("#")) .filter(|line| !line.trim_start().starts_with('#'))
.for_each(|line| { .for_each(|line| {
// split line at colon // split line at colon
let parts = line.splitn(2, ':').collect::<Vec<_>>(); let parts = line.splitn(2, ':').collect::<Vec<_>>();