]> Untitled Git - lemmy.git/commitdiff
Use get_ip in ratelimit middleware
authorasonix <asonix@asonix.dog>
Mon, 20 Apr 2020 18:02:25 +0000 (13:02 -0500)
committerasonix <asonix@asonix.dog>
Mon, 20 Apr 2020 18:02:25 +0000 (13:02 -0500)
server/src/lib.rs
server/src/rate_limit/mod.rs
server/src/routes/websocket.rs

index 2d0ecc3a3c84f4036d7fb88170b0ac9e22438fa8..79a1437e7bb91813fa61af69ff2ef5f37c540b65 100644 (file)
@@ -34,7 +34,7 @@ pub mod settings;
 pub mod version;
 pub mod websocket;
 
-use actix_web::HttpRequest;
+use actix_web::dev::ConnectionInfo;
 use chrono::{DateTime, NaiveDateTime, Utc};
 use isahc::prelude::*;
 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())
 }
 
-pub fn get_ip(req: &HttpRequest) -> String {
-  req
-    .connection_info()
+pub fn get_ip(conn_info: &ConnectionInfo) -> String {
+  conn_info
     .remote()
     .unwrap_or("127.0.0.1:12345")
     .split(':')
index bb77db29c05ca25a01f8735afe9feeb4ec23b1de..de45002ebab4dc3aa88b8bc74986feff404c59bf 100644 (file)
@@ -2,6 +2,7 @@ pub mod rate_limiter;
 
 use super::{IPAddr, Settings};
 use crate::api::APIError;
+use crate::get_ip;
 use crate::settings::RateLimitConfig;
 use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
 use failure::Error;
@@ -181,14 +182,7 @@ where
   }
 
   fn call(&mut self, req: S::Request) -> Self::Future {
-    let ip_addr = req
-      .connection_info()
-      .remote()
-      .unwrap_or("127.0.0.1:12345")
-      .split(':')
-      .next()
-      .unwrap_or("127.0.0.1")
-      .to_string();
+    let ip_addr = get_ip(&req.connection_info());
 
     let fut = self
       .rate_limited
index e1efdc0f5ce6dc16180c919ac4427d81d0aafe37..8088594519ef326168d72f21d64a12e51b0c789e 100644 (file)
@@ -18,7 +18,7 @@ pub async fn chat_route(
       cs_addr: chat_server.get_ref().to_owned(),
       id: 0,
       hb: Instant::now(),
-      ip: get_ip(&req),
+      ip: get_ip(&req.connection_info()),
     },
     &req,
     stream,