Use get_ip in ratelimit middleware

This commit is contained in:
asonix 2020-04-20 13:02:25 -05:00
parent e66b07b96c
commit 63b2bde448
3 changed files with 6 additions and 13 deletions

View file

@ -34,7 +34,7 @@ pub mod settings;
pub mod version; pub mod version;
pub mod websocket; pub mod websocket;
use actix_web::HttpRequest; use actix_web::dev::ConnectionInfo;
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{DateTime, NaiveDateTime, Utc};
use isahc::prelude::*; use isahc::prelude::*;
use lettre::smtp::authentication::{Credentials, Mechanism}; use lettre::smtp::authentication::{Credentials, Mechanism};
@ -233,9 +233,8 @@ pub fn markdown_to_html(text: &str) -> String {
comrak::markdown_to_html(text, &comrak::ComrakOptions::default()) comrak::markdown_to_html(text, &comrak::ComrakOptions::default())
} }
pub fn get_ip(req: &HttpRequest) -> String { pub fn get_ip(conn_info: &ConnectionInfo) -> String {
req conn_info
.connection_info()
.remote() .remote()
.unwrap_or("127.0.0.1:12345") .unwrap_or("127.0.0.1:12345")
.split(':') .split(':')

View file

@ -2,6 +2,7 @@ pub mod rate_limiter;
use super::{IPAddr, Settings}; use super::{IPAddr, Settings};
use crate::api::APIError; use crate::api::APIError;
use crate::get_ip;
use crate::settings::RateLimitConfig; use crate::settings::RateLimitConfig;
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform}; use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
use failure::Error; use failure::Error;
@ -181,14 +182,7 @@ where
} }
fn call(&mut self, req: S::Request) -> Self::Future { fn call(&mut self, req: S::Request) -> Self::Future {
let ip_addr = req let ip_addr = get_ip(&req.connection_info());
.connection_info()
.remote()
.unwrap_or("127.0.0.1:12345")
.split(':')
.next()
.unwrap_or("127.0.0.1")
.to_string();
let fut = self let fut = self
.rate_limited .rate_limited

View file

@ -18,7 +18,7 @@ pub async fn chat_route(
cs_addr: chat_server.get_ref().to_owned(), cs_addr: chat_server.get_ref().to_owned(),
id: 0, id: 0,
hb: Instant::now(), hb: Instant::now(),
ip: get_ip(&req), ip: get_ip(&req.connection_info()),
}, },
&req, &req,
stream, stream,