2023-09-23 15:56:21 +00:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
|
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
use reqwest::Client;
|
|
|
|
|
|
|
|
static SESSION_COOKIE: OnceCell<String> = OnceCell::new();
|
|
|
|
static SESSION_TIME: OnceCell<u64> = OnceCell::new();
|
|
|
|
|
|
|
|
/// Makes a request to the online classroom, and returns the html string
|
2023-09-23 16:03:29 +00:00
|
|
|
pub async fn _request(url: String) -> Result<String, String> {
|
|
|
|
Ok(url)
|
2023-09-23 15:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Makes sure that the session cookie is set, and that it is valid
|
|
|
|
pub async fn ensure_session() -> Result<(), String> {
|
|
|
|
let last_usage_time = match SESSION_TIME.get() {
|
|
|
|
Some(time) => *time,
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get current time in seconds
|
|
|
|
let current_time = SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.expect("Time went backwards")
|
|
|
|
.as_secs();
|
|
|
|
|
|
|
|
let time_passed = current_time - last_usage_time;
|
|
|
|
|
|
|
|
// Default PHP session timeout is 1440 seconds. Use a 1400 seconds timeout to be safe
|
|
|
|
if time_passed > 1400 {
|
|
|
|
login().await?;
|
|
|
|
if let Err(err) = SESSION_TIME.set(current_time) {
|
|
|
|
return Err(format!("Error setting session time: {:?}", err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Logins to the online classroom, and sets the session cookie
|
|
|
|
async fn login() -> Result<(), String> {
|
|
|
|
let clasroom_url = std::env::var("CLASSROOM_URL").expect("CLASSROOM_URL env var is not set!");
|
|
|
|
let clasroom_user =
|
|
|
|
std::env::var("CLASSROOM_USER").expect("CLASSROOM_USER env var is not set!");
|
|
|
|
let clasroom_password =
|
|
|
|
std::env::var("CLASSROOM_PASSWORD").expect("CLASSROOM_PASSWORD env var is not set!");
|
|
|
|
|
|
|
|
let params = [
|
|
|
|
("login", clasroom_user),
|
|
|
|
("password", clasroom_password),
|
|
|
|
("submitAuth", "".into()),
|
|
|
|
("_qf__formLogin", "".into()),
|
|
|
|
];
|
|
|
|
let client = Client::new();
|
|
|
|
let result = client
|
|
|
|
.post(format!("{}/index.php", clasroom_url))
|
|
|
|
.form(¶ms)
|
|
|
|
.send()
|
|
|
|
.await;
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(response) => {
|
|
|
|
let Some(session_cookie) = response.cookies().find(|c| c.name() == "ch_sid") else {
|
|
|
|
return Err("Response succeeded, but no session cookie was foun".into());
|
|
|
|
};
|
|
|
|
|
|
|
|
match SESSION_COOKIE.set(session_cookie.value().into()) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(error) => Err(format!("Error setting session cookie: {:?}", error)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(error) => Err(format!("Error connecting to online classroom: {:?}", error)),
|
|
|
|
}
|
|
|
|
}
|