mirror of
https://github.com/YGGverse/agate.git
synced 2026-04-08 20:45:29 +00:00
Refactor error handling code
This commit is contained in:
parent
9097318e28
commit
cd5918e1b9
1 changed files with 18 additions and 23 deletions
41
src/main.rs
41
src/main.rs
|
|
@ -11,7 +11,7 @@ use rustls::{
|
||||||
NoClientAuth, ServerConfig,
|
NoClientAuth, ServerConfig,
|
||||||
};
|
};
|
||||||
use std::{error::Error, ffi::OsStr, fs::File, io::BufReader, marker::Unpin, sync::Arc};
|
use std::{error::Error, ffi::OsStr, fs::File, io::BufReader, marker::Unpin, sync::Arc};
|
||||||
use url::Url;
|
use url::{Host, Url};
|
||||||
|
|
||||||
fn main() -> Result {
|
fn main() -> Result {
|
||||||
env_logger::Builder::from_env("AGATE_LOG").init();
|
env_logger::Builder::from_env("AGATE_LOG").init();
|
||||||
|
|
@ -44,7 +44,7 @@ struct Args {
|
||||||
content_dir: String,
|
content_dir: String,
|
||||||
cert_file: String,
|
cert_file: String,
|
||||||
key_file: String,
|
key_file: String,
|
||||||
domain: Option<String>,
|
domain: Option<Host>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn args() -> Option<Args> {
|
fn args() -> Option<Args> {
|
||||||
|
|
@ -54,7 +54,7 @@ fn args() -> Option<Args> {
|
||||||
content_dir: args.next()?,
|
content_dir: args.next()?,
|
||||||
cert_file: args.next()?,
|
cert_file: args.next()?,
|
||||||
key_file: args.next()?,
|
key_file: args.next()?,
|
||||||
domain: args.next(),
|
domain: args.next().and_then(|s| Host::parse(&s).ok()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -73,10 +73,9 @@ async fn handle_request(stream: TcpStream) -> Result {
|
||||||
};
|
};
|
||||||
if let Err(e) = send_response(url, stream).await {
|
if let Err(e) = send_response(url, stream).await {
|
||||||
respond(stream, "51", &["Not found, sorry."]).await?;
|
respond(stream, "51", &["Not found, sorry."]).await?;
|
||||||
Err(e)
|
Err(e)?
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TLS configuration.
|
/// TLS configuration.
|
||||||
|
|
@ -129,24 +128,20 @@ async fn parse_request<R: Read + Unpin>(
|
||||||
|
|
||||||
// Validate the URL, host and port.
|
// Validate the URL, host and port.
|
||||||
if url.scheme() != "gemini" {
|
if url.scheme() != "gemini" {
|
||||||
Err((53, "unsupported URL scheme"))
|
return Err((53, "unsupported URL scheme"));
|
||||||
} else if ARGS.domain.as_ref().map_or(false, |domain| {
|
|
||||||
url.host().map_or(false, |host| &host.to_string() != domain)
|
|
||||||
}) {
|
|
||||||
Err((53, "proxy request refused"))
|
|
||||||
} else if url.port().map_or(false, |port| {
|
|
||||||
port != ARGS
|
|
||||||
.sock_addr
|
|
||||||
.rsplitn(2, ':')
|
|
||||||
.next()
|
|
||||||
.unwrap()
|
|
||||||
.parse()
|
|
||||||
.unwrap()
|
|
||||||
}) {
|
|
||||||
Err((53, "proxy request refused"))
|
|
||||||
} else {
|
|
||||||
Ok(url)
|
|
||||||
}
|
}
|
||||||
|
// TODO: Can be simplified by https://github.com/servo/rust-url/pull/651
|
||||||
|
if let (Some(Host::Domain(domain)), Some(Host::Domain(host))) = (&ARGS.domain, url.host()) {
|
||||||
|
if domain != host {
|
||||||
|
return Err((53, "proxy request refused"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(port) = url.port() {
|
||||||
|
if !ARGS.sock_addr.ends_with(&format!(":{}", port)) {
|
||||||
|
return Err((53, "proxy request refused"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send the client the file located at the requested URL.
|
/// Send the client the file located at the requested URL.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue