added more request logging code

This commit is contained in:
Jonas_Jones 2024-06-30 00:34:55 +02:00
parent c20e276de2
commit 9ef64b5b82
2 changed files with 27 additions and 11 deletions

View file

@ -1,10 +1,12 @@
use chrono::Utc;
use reqwest::Client;
use reqwest::blocking::get;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};
use std::fs::OpenOptions;
use std::io::{BufReader, BufWriter};
use warp::http::Method;
use std::path::Path;
#[derive(Serialize, Deserialize)]
@ -16,7 +18,7 @@ struct RequestLog {
ip_hash: String,
}
fn get_ip_country_code(ip: &str) -> Result<String, Box<dyn std::error::Error>> {
async fn get_ip_country_code(ip: &str) -> Result<String, Box<dyn std::error::Error>> {
let url = format!("http://ip-api.com/json/{}", ip);
let response: Value = get(&url)?.json()?;
if let Some(country_code) = response["countryCode"].as_str() {
@ -26,20 +28,21 @@ fn get_ip_country_code(ip: &str) -> Result<String, Box<dyn std::error::Error>> {
}
}
fn hash_ip(ip: &str) -> String {
async fn hash_ip(ip: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(ip);
format!("{:x}", hasher.finalize())
}
pub fn log_request(ip: &str, pathname: &str, method: &str, file_path: &str) /*-> Result<(), Box<dyn std::error::Error>>*/ {
pub async fn log_request(client: &Client, ip: &str, pathname: &str, method: &Method, file_path: &str) -> Result<(), Box<dyn std::error::Error>> {
let method_str = method.as_str();
let timestamp = Utc::now().to_rfc3339();
let ip_country_code = get_ip_country_code(ip)?;
let ip_hash = hash_ip(ip);
let ip_country_code = get_ip_country_code(ip).await?;
let ip_hash = hash_ip(ip).await;
let log_entry = RequestLog {
timestamp,
method: method.to_string(),
method: method_str.to_string(),
pathname: pathname.to_string(),
ip_country_code,
ip_hash,
@ -64,5 +67,5 @@ pub fn log_request(ip: &str, pathname: &str, method: &str, file_path: &str) /*->
let writer = BufWriter::new(&file);
serde_json::to_writer_pretty(writer, &logs)?;
//Ok(())
Ok(())
}

View file

@ -4,9 +4,12 @@ use std::env;
use lastfm::reqwest::StatusCode;
use warp::filters::path::FullPath;
use warp::http::Method;
use warp::Filter;
use warp::reply::Reply;
use reqwest::Client;
use crate::error_responses::{ErrorMessage, InternalServerError, BadRequestError, NotFoundError, NotImplementedError};
use crate::v1::get_v1_routes;
use crate::{parse_ip, request_logger, Logger};
@ -33,12 +36,22 @@ pub async fn serve() {
.and(warp::path::full())
.and(warp::addr::remote())
.and(warp::header::optional::<String>("x-forwarded-for"))
.map(|method, path: FullPath, addr: Option<SocketAddr>, fwd_for: Option<String>| {
.map(|method: Method, path: FullPath, addr: Option<SocketAddr>, fwd_for: Option<String>| {
let client_ip = fwd_for.unwrap_or_else(|| addr.map(|a| a.ip().to_string()).unwrap_or_else(|| String::from("unknown")));
let path_str = path.as_str();
let path_str = path.as_str().to_string(); // Convert to owned String
let method_clone = method.clone();
let method_str = method_clone.clone().as_str().to_string();
let client_ip_clone = client_ip.clone(); // Clone for use outside the async block
let path_str_clone = path_str.clone();
request_logger::log_request(&client_ip, path_str, method, "requests.json");
Logger::info(&format!(" {} {} from {} ({})", method, path_str, ip_lookup(&client_ip), client_ip));
/*tokio::spawn(async move {
let client = Client::new();
if let Err(e) = request_logger::log_request(&client, &client_ip, &path_str, &method_clone, "requests.json").await {
eprintln!("Failed to log request: {:?}", e);
}
});*/
Logger::info(&format!("{} {} from {} ({})", method_str, path_str_clone, ip_lookup(&client_ip_clone), client_ip_clone));
});
// GET (any) => reply with return from handle_path