diff --git a/Cargo.lock b/Cargo.lock index 531ccc85ceee8bbd8d9380a7df9db1997499bd9f..e0037f05310d7c078ba00171c9566b6d50c2ec5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -559,7 +559,7 @@ checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" [[package]] name = "drone" -version = "0.1.2" +version = "0.1.3" dependencies = [ "actix-web", "config", diff --git a/Cargo.toml b/Cargo.toml index d4fc0bb5c6736852c223069835d837333bb01eda..d72638a11e6cc56e8a1a6aa5355b33fd8a9dd39f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drone" -version = "0.1.2" +version = "0.1.3" edition = "2021" authors = ["Deathwing <hi@deathwing.me>"] description = "A caching reverse-proxy application for the Hive blockchain." diff --git a/src/main.rs b/src/main.rs index fd5cbd905eaac3eefce36e2c849a486d5f3e3841..1e2eb09b8859fa0c399ceac8f9763d8091635901 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use std::{time::Duration, sync::Mutex}; -use actix_web::{web, App, HttpResponse, HttpServer, Responder, HttpRequest}; +use actix_web::{web::{self, JsonConfig}, App, HttpResponse, HttpServer, Responder, HttpRequest}; use serde::{Deserialize, Serialize}; use reqwest::{Client, ClientBuilder}; use lru_time_cache::LruCache; @@ -90,16 +90,25 @@ async fn api_call(req: HttpRequest, call: web::Json<APICall>, data: web::Data<Ap // Log the request, if there's Cloudflare header (CF-Connecting-IP) use that instead of peer_addr. let get_cloudflare_ip = req.headers().get("CF-Connecting-IP"); + let client_ip = match get_cloudflare_ip { - Some(ip) => ip.to_str().unwrap().to_string(), - None => req.peer_addr().unwrap().ip().to_string(), - }; + Some(ip) => ip.to_str().map(|ip| ip.to_string()), + None => Ok(req.peer_addr().unwrap().ip().to_string()), + }; + let client_ip = match client_ip { + Ok(ip) => ip, + Err(_) => return HttpResponse::InternalServerError().json(ErrorStructure { + code: 9999, + message: "Internal Server Error".to_string(), + error_data: "Invalid Cloudflare Proxy Header.".to_string(), + }), + }; + let formatted_log = format!( - "Timestamp: {} || IP: {} || HTTP Version: {:?} || Request Method: {} || Request Params: {}", + "Timestamp: {} || IP: {} || Request Method: {} || Request Params: {}", human_timestamp, client_ip, - req.version(), json_rpc_call.method, json_rpc_call.params, ); @@ -258,6 +267,7 @@ async fn main() -> std::io::Result<()> { HttpServer::new(move || { App::new() .app_data(_cache.clone()) + .app_data(JsonConfig::default().content_type_required(false)) .route("/", web::get().to(index)) .route("/", web::post().to(api_call)) .route("/health", web::get().to(index))