File tree Expand file tree Collapse file tree 3 files changed +31
-0
lines changed Expand file tree Collapse file tree 3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ use rocket::{
6
6
7
7
pub struct Admin {
8
8
pub username : String ,
9
+ pub ipv4_address : Option < String > ,
9
10
}
10
11
11
12
#[ derive( Debug ) ]
@@ -21,6 +22,7 @@ impl<'r> FromRequest<'r> for Admin {
21
22
if cfg ! ( debug_assertions) {
22
23
return request:: Outcome :: Success ( Admin {
23
24
username : "AdminBot" . to_string ( ) ,
25
+ ipv4_address : None ,
24
26
} ) ;
25
27
}
26
28
@@ -29,8 +31,17 @@ impl<'r> FromRequest<'r> for Admin {
29
31
None => return request:: Outcome :: Error ( ( Status :: BadRequest , AdminError :: Missing ) ) ,
30
32
} ;
31
33
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
+
32
42
request:: Outcome :: Success ( Admin {
33
43
username : username. to_string ( ) ,
44
+ ipv4_address
34
45
} )
35
46
}
36
47
}
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ mod player;
28
28
mod stickyban;
29
29
mod ticket;
30
30
mod whitelist;
31
+ mod twofactor;
31
32
32
33
#[ rocket:: async_trait]
33
34
impl Fairing for CORS {
@@ -142,4 +143,5 @@ fn rocket() -> _ {
142
143
format ! { "{}/NewPlayers" , base_url} ,
143
144
routes ! [ new_players:: get_new_players] ,
144
145
)
146
+ . mount ( format ! ( "{}/TwoFactor" , base_url) , routes ! [ twofactor:: twofactor_validate] )
145
147
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments