|
| 1 | +mod check_ea_desktop_session; |
| 2 | +mod connectors; |
| 3 | +mod gatherer; |
| 4 | +mod structs; |
| 5 | + |
| 6 | +use gatherer::battlefield_grpc_bf2042; |
| 7 | +use grpc_rust::access_token::ea_desktop_access_token; |
| 8 | +use std::{ |
| 9 | + collections::HashMap, |
| 10 | + env, |
| 11 | + sync::{atomic, Arc}, |
| 12 | + time::Duration, |
| 13 | +}; |
| 14 | +use tokio::time::sleep; |
| 15 | +use warp::Filter; |
| 16 | + |
| 17 | +use crate::connectors::mongo::MongoClient; |
| 18 | + |
| 19 | +#[tokio::main] |
| 20 | +async fn main() -> anyhow::Result<()> { |
| 21 | + match dotenvy::dotenv() { |
| 22 | + Ok(_) => {} |
| 23 | + Err(_) => log::info!(".env not found, using env variables..."), |
| 24 | + }; |
| 25 | + |
| 26 | + let last_update = Arc::new(atomic::AtomicI64::new(chrono::Utc::now().timestamp() / 60)); |
| 27 | + let last_update_clone = Arc::clone(&last_update); |
| 28 | + |
| 29 | + flexi_logger::Logger::try_with_str("info")?.start()?; |
| 30 | + log::info!("Starting..."); |
| 31 | + |
| 32 | + tokio::spawn(async move { |
| 33 | + let hello = warp::any().map(move || { |
| 34 | + let last_update_i64 = last_update_clone.load(atomic::Ordering::Relaxed); |
| 35 | + let now_minutes = chrono::Utc::now().timestamp() / 60; |
| 36 | + |
| 37 | + // error if 10 minutes without updates |
| 38 | + if (now_minutes - last_update_i64) > 10 { |
| 39 | + warp::reply::with_status( |
| 40 | + format!("{}", now_minutes - last_update_i64), |
| 41 | + warp::http::StatusCode::SERVICE_UNAVAILABLE, |
| 42 | + ) |
| 43 | + } else { |
| 44 | + warp::reply::with_status( |
| 45 | + format!("{}", now_minutes - last_update_i64), |
| 46 | + warp::http::StatusCode::OK, |
| 47 | + ) |
| 48 | + } |
| 49 | + }); |
| 50 | + warp::serve(hello).run(([0, 0, 0, 0], 3030)).await; |
| 51 | + }); |
| 52 | + let mut mongo_client = MongoClient::connect().await?; |
| 53 | + |
| 54 | + let api_bf2042_account = env::var("API_BF2042_ACCOUNT").expect("API_BF2042_ACCOUNT wasn't set"); |
| 55 | + |
| 56 | + let (mut bf2042_cookie, mut ea_access_token) = |
| 57 | + match mongo_client.get_cookies(&api_bf2042_account).await { |
| 58 | + Ok(result) => result, |
| 59 | + Err(e) => { |
| 60 | + log::warn!("Cookie failed, {}", e); |
| 61 | + ( |
| 62 | + bf_sparta::cookie::Cookie { |
| 63 | + sid: "".to_string(), |
| 64 | + remid: "".to_string(), |
| 65 | + }, |
| 66 | + "".to_string(), |
| 67 | + ) |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + if ea_access_token.is_empty() { |
| 72 | + match ea_desktop_access_token(bf2042_cookie.clone()).await { |
| 73 | + Ok(res) => { |
| 74 | + (ea_access_token, bf2042_cookie) = res; |
| 75 | + mongo_client |
| 76 | + .push_new_cookies(&api_bf2042_account, &bf2042_cookie, ea_access_token.clone()) |
| 77 | + .await?; |
| 78 | + } |
| 79 | + Err(e) => log::error!("access_token for ea desktop failed: {:#?}", e), |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + let empty_game_hash: HashMap<String, String> = HashMap::new(); |
| 84 | + let mut sessions: HashMap<String, HashMap<String, String>> = HashMap::new(); |
| 85 | + |
| 86 | + log::info!("Started"); |
| 87 | + |
| 88 | + loop { |
| 89 | + match battlefield_grpc_bf2042::check_session( |
| 90 | + sessions |
| 91 | + .get("kingston") |
| 92 | + .unwrap_or(&empty_game_hash) |
| 93 | + .to_owned(), |
| 94 | + bf2042_cookie.clone(), |
| 95 | + ea_access_token.clone(), |
| 96 | + ) |
| 97 | + .await |
| 98 | + { |
| 99 | + Ok(session) => { |
| 100 | + sessions.insert("kingston".to_string(), session); |
| 101 | + last_update.store( |
| 102 | + chrono::Utc::now().timestamp() / 60, |
| 103 | + atomic::Ordering::Relaxed, |
| 104 | + ); |
| 105 | + log::info!("kingston: Finished auth check!"); |
| 106 | + } |
| 107 | + Err(e) => { |
| 108 | + log::error!("Failed kingston_grpc, auth_check reason: {:#?}", e); |
| 109 | + match ea_desktop_access_token(bf2042_cookie.clone()).await { |
| 110 | + Ok(res) => { |
| 111 | + (ea_access_token, bf2042_cookie) = res; |
| 112 | + mongo_client |
| 113 | + .push_new_cookies( |
| 114 | + &api_bf2042_account, |
| 115 | + &bf2042_cookie, |
| 116 | + ea_access_token.clone(), |
| 117 | + ) |
| 118 | + .await?; |
| 119 | + } |
| 120 | + Err(e) => log::error!("access_token for ea desktop failed: {:#?}", e), |
| 121 | + }; |
| 122 | + } |
| 123 | + }; |
| 124 | + sleep(Duration::from_secs(30)).await; |
| 125 | + } |
| 126 | +} |
0 commit comments