From 56cca06199cdb690173d3cf96f3cf47a62ba1fff Mon Sep 17 00:00:00 2001 From: calcu1on Date: Sun, 22 Dec 2024 21:43:52 -0500 Subject: [PATCH] API Key Handling and some more dynamic functions. --- .gitignore | 1 + Cargo.toml | 2 ++ src/main.rs | 56 +++++++++++++++++++++++++++-------------------------- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index efe3eb1..9f7fb86 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ Cargo.lock # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +keys.txt # Added by cargo diff --git a/Cargo.toml b/Cargo.toml index c949f62..e173c71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +base64 = "0.22.1" +rand = "0.8" diff --git a/src/main.rs b/src/main.rs index b642551..4d61329 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ struct IncomingRequest { // Listen on port and return response. fn main() { - let run_local = false; + let run_local = true; let mut listener = TcpListener::bind("127.0.0.1:6942").unwrap(); if !run_local { @@ -44,15 +44,15 @@ fn handle_connection(mut stream: TcpStream) { let mut response = String::new(); // If not a GET request, or missing API key, throw 404. if inc_request.req_type != "GET" || api_key.is_empty() { - response = return_404(); + response = return_response("404"); } else { - // Check that the API key is in fact fact valuid. - // Not really checking right now. - response = return_200(); + let mut keyfile: Vec = fs::read_to_string("./keys.txt").into_iter().collect(); + keyfile[0].pop().expect("Not a string").to_string(); + if api_key == keyfile[0] { + response = return_response("200"); + } } - // If not present, return 404. - // Perhaps move both of those conditions to a new function. stream.write_all(response.as_bytes()).unwrap(); } @@ -72,26 +72,28 @@ fn get_api_key(headers: Vec) -> String { api_key } -// Return a 404. -fn return_404() -> String { - let status_line = "HTTP/1.1 404 Not Found"; - let contents = fs::read_to_string("./public/404.json").unwrap(); - let length = contents.len(); +#[allow(unused_assignments)] +fn return_response(code: &str) -> String { + let mut response = String::new(); + match code { + "404" => { + let status_line = "HTTP/1.1 404 Not Found"; + let contents = fs::read_to_string("./public/404.json").unwrap(); + let length = contents.len(); - let response = - format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + }, + "200" => { + let status_line = "HTTP/1.1 200 OK"; + let contents = fs::read_to_string("./public/200.json").unwrap(); + let length = contents.len(); - response -} - -// Return a 200 with the json. -fn return_200() -> String { - let status_line = "HTTP/1.1 200 OK"; - let contents = fs::read_to_string("./public/200.json").unwrap(); - let length = contents.len(); - - let response = - format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); - - response + response = + format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + }, + &_ => todo!() + } + + response }