Skip to content

Commit 8823735

Browse files
committed
use random session of manager session is not available
1 parent 2a4f3f3 commit 8823735

File tree

2 files changed

+111
-10
lines changed

2 files changed

+111
-10
lines changed

src/connectors/mongo.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,57 @@ use std::env;
22

33
use crate::structs::old_games;
44
use bf_sparta::cookie::Cookie;
5+
use bf_sparta::sparta_api;
56
use bson::Document;
67
use chrono::{DateTime, Utc};
8+
use futures::StreamExt;
79
use mongodb::error::Result;
10+
use mongodb::options::FindOptions;
811
use mongodb::{options::ReplaceOptions, results::UpdateResult, Client, Collection};
912
use serde::{Deserialize, Serialize};
1013

1114
pub struct MongoClient {
1215
pub backend_cookies: Collection<BackendCookie>,
16+
pub community_cookies: Collection<CommunityCookie>,
17+
pub cookie_check: Collection<CookieCheck>,
1318
pub community_servers: Collection<Document>,
1419
pub community_groups: Collection<Document>,
1520
pub player_list: Collection<Document>,
1621
pub logging: Collection<Document>,
1722
pub old_games_servers: Collection<old_games::OldGameServerList>,
1823
}
1924

25+
#[derive(Serialize, Deserialize, Debug, Clone)]
26+
pub struct CookieCheck {
27+
pub _id: String,
28+
pub prefix: String,
29+
#[serde(rename = "personaId")]
30+
pub persona_id: String,
31+
#[serde(rename = "sessionId")]
32+
pub session_id: String,
33+
}
34+
35+
#[derive(Serialize, Deserialize, Debug, Clone)]
36+
pub struct CommunityCookie {
37+
pub _id: String,
38+
pub sid: String,
39+
pub remid: String,
40+
#[serde(rename = "personaId")]
41+
pub persona_id: String,
42+
pub username: String,
43+
#[serde(rename = "supportedGames")]
44+
pub supported_games: Vec<String>,
45+
}
46+
47+
impl From<CommunityCookie> for Cookie {
48+
fn from(cookie: CommunityCookie) -> Self {
49+
Cookie {
50+
remid: cookie.remid,
51+
sid: cookie.sid,
52+
}
53+
}
54+
}
55+
2056
#[derive(Serialize, Deserialize, Debug, Clone)]
2157
pub struct BackendCookie {
2258
pub _id: String,
@@ -71,6 +107,8 @@ impl MongoClient {
71107
let gamestats_db = client.database("gamestats");
72108

73109
Ok(MongoClient {
110+
cookie_check: db.collection("cookieCheck"),
111+
community_cookies: db.collection("communityCookies"),
74112
backend_cookies: db.collection("backendCookies"),
75113
community_servers: db.collection("communityServers"),
76114
community_groups: db.collection("communityGroups"),
@@ -166,6 +204,56 @@ impl MongoClient {
166204
))
167205
}
168206

207+
pub async fn get_random_cookie(&mut self) -> anyhow::Result<Cookie> {
208+
let mut cookie_check = self
209+
.cookie_check
210+
.find(bson::doc! {})
211+
.with_options(
212+
FindOptions::builder()
213+
.sort(bson::doc! {"timeStamp": -1})
214+
.build(),
215+
)
216+
.await?;
217+
let mut result_cookie = None;
218+
while let Some(maybe_check) = cookie_check.next().await {
219+
let check = maybe_check?;
220+
match self
221+
.community_cookies
222+
.find_one(bson::doc! {"_id": check._id})
223+
.await
224+
{
225+
Ok(e) => {
226+
if let Some(cookie) = e {
227+
match sparta_api::get_token(
228+
cookie.clone().into(),
229+
"pc",
230+
"tunguska",
231+
"en-us",
232+
)
233+
.await
234+
{
235+
Ok(_) => {
236+
result_cookie = Some(cookie);
237+
break;
238+
}
239+
Err(e) => {
240+
log::info!("Cookie not valid, trying another one - {}", e);
241+
}
242+
}
243+
} else {
244+
continue;
245+
}
246+
}
247+
Err(_) => continue,
248+
};
249+
}
250+
251+
Ok(match result_cookie {
252+
Some(cookie) => cookie.into(),
253+
None => anyhow::bail!("no cookie found"),
254+
})
255+
}
256+
169257
pub async fn gather_old_title(
170258
&mut self,
171259
game_name: &str,

src/main.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,33 @@ async fn main() -> anyhow::Result<()> {
117117
Ok(_) => cookie.clone(),
118118
Err(e) => {
119119
log::warn!("Cookie failed, {} - requesting new cookie", e);
120-
let cookie_auth = cookie_request::request_cookie(cookie_request::Login {
120+
match cookie_request::request_cookie(cookie_request::Login {
121121
email: api_main_account.clone(),
122122
pass: env::var("API_MAIN_ACCOUNT_PASSWORD")
123123
.expect("API_MAIN_ACCOUNT_PASSWORD wasn't set"),
124124
})
125-
.await?;
126-
let cookie = bf_sparta::cookie::Cookie {
127-
sid: cookie_auth.sid,
128-
remid: cookie_auth.remid,
129-
};
130-
mongo_client
131-
.push_new_cookies(&api_main_account, &cookie, "".to_string())
132-
.await?;
133-
cookie
125+
.await
126+
{
127+
Ok(cookie_auth) => {
128+
let cookie = bf_sparta::cookie::Cookie {
129+
sid: cookie_auth.sid,
130+
remid: cookie_auth.remid,
131+
};
132+
mongo_client
133+
.push_new_cookies(&api_main_account, &cookie, "".to_string())
134+
.await?;
135+
cookie
136+
}
137+
Err(e) => {
138+
log::warn!(
139+
"Requesting new cookie failed, {} - using one from the manager",
140+
e
141+
);
142+
143+
let cookie = mongo_client.get_random_cookie().await?;
144+
cookie
145+
}
146+
}
134147
}
135148
};
136149

0 commit comments

Comments
 (0)