Skip to content
Snippets Groups Projects
Unverified Commit 9ef8b6f2 authored by Volkor Barbarian Warrior's avatar Volkor Barbarian Warrior
Browse files

clean up guess_ip, template defaults

This cleans up the guess_ip function to use a match block instead of ifs
Templating has been cleaned up as to not break on a new domain
parent 64aab02e
No related branches found
No related tags found
No related merge requests found
# I've merged all these into a single job since it's /way/ faster. caching should work for other jobs like building
# name: Format, check and test
name: format, check and test
on: [push, pull_request]
jobs:
fmt-check-test:
name: format check and test
check:
name: cargo fmt
runs-on: ubuntu-latest
steps:
- uses: https://github.com/actions/checkout@v3
- uses: https://github.com/dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- run: cargo fmt
- run: cargo fmt -- --all-features
- run: cargo clippy --all-features
- run: cargo test --all-features
- uses: https://github.com/Swatinem/rust-cache@v2
\ No newline at end of file
- uses: https://github.com/Swatinem/rust-cache@v2
test:
name: cargo test
runs-on: ubuntu-latest
steps:
- uses: https://github.com/actions/checkout@v3
- uses: https://github.com/dtolnay/rust-toolchain@stable
- uses: https://github.com/Swatinem/rust-cache@v2
- run: cargo test --all-features
\ No newline at end of file
......@@ -17,14 +17,13 @@ pub async fn delete_file(req: &mut Request, res: &mut Response) {
tracing::debug!("delete_file(adminkey): {:?}", adminkey);
// This is ugly, but this grabs the invisible form data that determines if the file was uploaded via the web.
// REVIEW: upload_by_web might be a more legible name
let mut web = false;
let mut upload_by_web = false;
// REVIEW: this entire if condition is a boolean. If it's true, you set web = true.
// This means you can cut out the if statement AND remove mutability e.g.
// let web = req.form_data().await...
// Don't know what type this is supposed to be but there is definitely a better way to handle that "web" string
if req.form_data().await.unwrap().fields.get("source").unwrap() == &"web".to_string() {
web = true;
upload_by_web = true;
}
// Checks if the adminkey is valid, and the file is active.
......@@ -35,7 +34,7 @@ pub async fn delete_file(req: &mut Request, res: &mut Response) {
.clone();
// if the filename is None, we fail. otherwise we delete it.
if filename.is_none() {
if web {
if upload_by_web {
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap();
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
......@@ -60,7 +59,7 @@ pub async fn delete_file(req: &mut Request, res: &mut Response) {
engine::delete_file(fname.clone()).await;
// Respond to the client.
if web {
if upload_by_web {
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap();
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
......
......@@ -12,11 +12,11 @@ pub async fn index(req: &mut Request, res: &mut Response) {
// Renders the template based on `/templates/$host/upload.html`
let template_host_path = PathBuf::from("./templates/").join(host);
let template_host_path_default = PathBuf::from("./templates/localhost:8282");
tracing::debug!("template_with_host: {:?}", template_host_path);
// Now we need to setup the templating engine.
let tpls: Ramhorns = Ramhorns::from_folder(&template_host_path)
.expect("Unable to find template, please place the templates correctly!");
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let rendered = tpls.get("upload.html").unwrap().render(&"");
res.render(Text::Html(rendered));
......
......@@ -40,63 +40,35 @@ struct TemplateStruct {
/// let ip = engine::guess_ip(headers, remote_addr);
/// ```
pub fn guess_ip(headers: &HeaderMap, ip: &SocketAddr) -> String {
// REVIEW: Whenever possible, try to use a specialized type over a String. Paths and IPs are most relevant here
// Error handling sucks, and avoiding it for conversion to/from strings is good
// Define cloudflare ip, or None
let cloudflare_ip: Option<String> = headers.get("CF-Connecting-IP").map(|hv| hv.to_str().unwrap().to_owned());
tracing::debug!("guess_ip(CF-Connecting-IP: {:?})", cloudflare_ip);
// Define x_real_ip, or None
let x_real_ip: Option<String> = headers.get("X-Real-IP").map(|hv| hv.to_str().unwrap().to_owned());
tracing::debug!("guess_ip(X-Real-IP: {:?})", x_real_ip);
// REVIEW: This should definitely be a variable as you return it later
if headers.get("CF-Connecting-IP").is_none() {
if cloudflare_ip.is_none() {
// Either there was a problem with the cloudflare header, or there is no cloudflare.
tracing::debug!(
"guess_ip(No cloudflare header detected!, falling back to X-Real-IP header)"
);
// REVIEW: this should also be a variable. I recommend you just define a bunch of variables for headers before doing much logic
if headers.get("X-Real-IP").is_none() {
if x_real_ip.is_none() {
tracing::debug!("guess_ip(No X-Real-IP detected!, falling back to remote_addr header)");
// thanks trev
// REVIEW: I think you can match ipv4 vs ipv6 here instead
if ip.as_ipv4().is_none() {
tracing::debug!("guess_ip(We ain't using ipv4.)");
if ip.as_ipv6().is_none() {
tracing::debug!("guess_ip(We ain't using ipv6.)");
if ip.as_unix().is_none() {
} else {
tracing::error!("This request came from a unix socket, How the hell?",);
}
} else {
tracing::debug!("guess_ip(we out here using ipv4.)");
let ip_address = ip.as_ipv6().map(|ip| ip.ip());
tracing::debug!("guess_ip(ip_address): {:?}", ip_address);
return ip_address.unwrap().to_string();
}
} else {
tracing::debug!("guess_ip(we out here using ipv4.)");
let ip_address = ip.as_ipv4().map(|ip| ip.ip());
tracing::debug!("guess_ip(ip_address): {:?}", ip_address);
return ip_address.unwrap().to_string();
match ip {
SocketAddr::IPv4(addr) => return addr.ip().to_string(),
SocketAddr::IPv6(addr) => return addr.ip().to_string(),
SocketAddr::Unix(_) => return "0.0.0.0".to_string(),
_ => return "0.0.0.0".to_string(),
}
} else {
return headers
.get("X-Real-IP")
.unwrap()
.to_str()
.unwrap()
.to_string();
return x_real_ip.unwrap_or("0.0.0.0".to_string())
}
} else {
// wow this is ugly.
// REVIEW: use variable. Error handling sucks, I recommend not trying to do it more than once for any given thing
return headers
.get("CF-Connecting-IP")
.unwrap()
.to_str()
.unwrap()
.to_string();
return cloudflare_ip.unwrap_or("0.0.0.0".to_string())
}
// tracing::debug!("guess_ip(ip): {:?}", ip);
// ip.to_str().unwrap().to_string()
// Unable to determine ip, use 0.0.0.0
tracing::debug!("guess_ip(headers): {:?}", headers);
tracing::error!("guess_ip(Failed to guess ip, falling back to 0.0.0.0)");
......
......@@ -19,6 +19,7 @@ pub async fn serve_static(req: &mut Request, res: &mut Response) {
let remote_addr = &req.remote_addr().unwrap().clone();
let host = headers[HOST].to_str().unwrap_or("localhost:8282");
let template_host_path = PathBuf::from("./templates/").join(host);
let template_host_path_default = PathBuf::from("./templates/localhost:8282");
let ip = guess_ip(headers, remote_addr);
let useragent = headers[USER_AGENT].to_str().unwrap();
......@@ -28,7 +29,7 @@ pub async fn serve_static(req: &mut Request, res: &mut Response) {
match req.uri().path() {
"/services" => {
tracing::info!("New Request: /services");
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
..Default::default()
......@@ -39,7 +40,7 @@ pub async fn serve_static(req: &mut Request, res: &mut Response) {
}
"/about" => {
tracing::info!("New Request: /about");
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
..Default::default()
......@@ -50,7 +51,7 @@ pub async fn serve_static(req: &mut Request, res: &mut Response) {
}
"/faq" => {
tracing::info!("New Request: /faq");
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
..Default::default()
......@@ -61,7 +62,7 @@ pub async fn serve_static(req: &mut Request, res: &mut Response) {
}
"/dmca" => {
tracing::info!("New Request: /dmca");
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
message1: rand::thread_rng().gen_range(0..1).to_string(),
......@@ -73,7 +74,7 @@ pub async fn serve_static(req: &mut Request, res: &mut Response) {
}
"/welcome" => {
tracing::info!("New Request: /welcome");
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
message1: rand::thread_rng().gen_range(0..1).to_string(),
......@@ -85,7 +86,7 @@ pub async fn serve_static(req: &mut Request, res: &mut Response) {
}
"/czb" => {
tracing::info!("New Request: /czb");
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
message1: rand::thread_rng().gen_range(0..1).to_string(),
......@@ -109,7 +110,7 @@ pub async fn serve_static(req: &mut Request, res: &mut Response) {
tracing::error!("Failed to add QR Scan to the database.");
}
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
message1: rand::thread_rng().gen_range(0..1).to_string(),
......
......@@ -20,6 +20,8 @@ pub async fn upload(req: &mut Request, res: &mut Response) {
let remote_addr = &req.remote_addr().unwrap().clone();
// Get the host header for nicely setting up the response.
let host = headers[HOST].to_str().unwrap_or("localhost:8282");
let template_host_path = PathBuf::from("./templates/").join(host);
let template_host_path_default = PathBuf::from("./templates/localhost:8282");
tracing::debug!("upload(req): {:?}", req);
// Check if the request was done in https or not by seeing if "x-forwarded-proto" exists.
......@@ -81,9 +83,7 @@ pub async fn upload(req: &mut Request, res: &mut Response) {
// Render a html error if the file is blocked and the request is from web
if web {
let template_with_host =
"./templates/".to_owned() + headers[HOST].to_str().unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_with_host).unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
message1: String::from("Error 403"),
......@@ -124,9 +124,7 @@ pub async fn upload(req: &mut Request, res: &mut Response) {
// If web is true, render html, otherwise json.
if web {
// REVIEW: I see this a lot. Maybe make it its own fn?
let template_with_host =
"./templates/".to_owned() + headers[HOST].to_str().unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_with_host).unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
// REVIEW: String::new()
......@@ -205,9 +203,7 @@ pub async fn upload(req: &mut Request, res: &mut Response) {
// REVIEW: the similarity between this and what follows makes me think this doesn't need to be in the if statement
if web {
let template_with_host =
"./templates/".to_owned() + headers[HOST].to_str().unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_with_host).unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
fileurl: String::from(&fileurl),
......@@ -228,8 +224,7 @@ pub async fn upload(req: &mut Request, res: &mut Response) {
}
};
} else if web {
let template_with_host = "./templates/".to_owned() + headers[HOST].to_str().unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_with_host).unwrap();
let tpls: Ramhorns = Ramhorns::from_folder(template_host_path).unwrap_or(Ramhorns::from_folder(template_host_path_default).expect("Couldn't find default template"));
let template = TemplateStruct {
domain: String::from(headers[HOST].to_str().unwrap()),
message1: String::from("400"),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment