Skip to content

Commit dc1a75e

Browse files
committed
ipv4 addresses
1 parent 7eaa093 commit dc1a75e

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/admin.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rocket::{
66

77
pub struct Admin {
88
pub username: String,
9+
pub ipv4_address: Option<String>,
910
}
1011

1112
#[derive(Debug)]
@@ -21,6 +22,7 @@ impl<'r> FromRequest<'r> for Admin {
2122
if cfg!(debug_assertions) {
2223
return request::Outcome::Success(Admin {
2324
username: "AdminBot".to_string(),
25+
ipv4_address: None,
2426
});
2527
}
2628

@@ -29,8 +31,17 @@ impl<'r> FromRequest<'r> for Admin {
2931
None => return request::Outcome::Error((Status::BadRequest, AdminError::Missing)),
3032
};
3133

34+
let mut ipv4_address = None;
35+
for header in req.headers().get("X-Forwarded-For") {
36+
if header.contains('.') {
37+
ipv4_address = Some(header.to_string());
38+
break;
39+
}
40+
}
41+
3242
request::Outcome::Success(Admin {
3343
username: username.to_string(),
44+
ipv4_address
3445
})
3546
}
3647
}

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod player;
2828
mod stickyban;
2929
mod ticket;
3030
mod whitelist;
31+
mod twofactor;
3132

3233
#[rocket::async_trait]
3334
impl Fairing for CORS {
@@ -142,4 +143,5 @@ fn rocket() -> _ {
142143
format!{"{}/NewPlayers", base_url},
143144
routes![new_players::get_new_players],
144145
)
146+
.mount(format!("{}/TwoFactor", base_url), routes![twofactor::twofactor_validate])
145147
}

src/twofactor.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use rocket_db_pools::Connection;
2+
use sqlx::query;
3+
4+
use crate::{admin::Admin, Cmdb};
5+
6+
#[get("/<cid>")]
7+
pub async fn twofactor_validate(mut db: Connection<Cmdb>, admin: Admin, cid: String) -> String {
8+
let ip_addr = match &admin.ipv4_address {
9+
Some(string) => string,
10+
None => return "No IP address".to_string(),
11+
};
12+
13+
match query("UPDATE twofactor SET approved = 1 WHERE cid = ? AND ckey = ? AND ip = ?").bind(cid).bind(&admin.username).bind(ip_addr).execute(&mut **db).await {
14+
Ok(res) => if res.rows_affected() > 0 { "Two factor request updated.".to_string() } else { "An error occured.".to_string() },
15+
Err(_) => "An error occured.".to_string(),
16+
}
17+
18+
}

0 commit comments

Comments
 (0)