From 4846d822bfa9f3903783ae227ba84443bc6c5214 Mon Sep 17 00:00:00 2001 From: DeathwingTheBoss <ozcanbarisucar@gmail.com> Date: Mon, 20 Mar 2023 13:08:50 +0300 Subject: [PATCH] - Improve unwrap survival - Ignore content-type header (jussi-like) - Version bump --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 22 ++++++++++++++++------ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 531ccc8..e0037f0 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 d4fc0bb..d72638a 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 fd5cbd9..1e2eb09 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)) -- GitLab