[BE] Get html from arbitrary URL
This commit is contained in:
parent
2df8aa383c
commit
62e7b1a29c
@ -54,6 +54,7 @@ async fn rocket() -> _ {
|
|||||||
// Online classroom routes
|
// Online classroom routes
|
||||||
//
|
//
|
||||||
online_classroom::connection,
|
online_classroom::connection,
|
||||||
|
online_classroom::users::get_users,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,4 @@
|
|||||||
use once_cell::sync::OnceCell;
|
|
||||||
|
|
||||||
use reqwest::Client;
|
|
||||||
use rocket::{http::Status, serde::json::Json};
|
use rocket::{http::Status, serde::json::Json};
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use self::{json_result::JsonResult, session::ensure_session};
|
use self::{json_result::JsonResult, session::ensure_session};
|
||||||
|
|
||||||
|
@ -1,14 +1,51 @@
|
|||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use reqwest::Client;
|
use reqwest::{Client, cookie::Jar, Url};
|
||||||
|
|
||||||
|
/// Stores the ch_sid cookie value
|
||||||
static SESSION_COOKIE: OnceCell<String> = OnceCell::new();
|
static SESSION_COOKIE: OnceCell<String> = OnceCell::new();
|
||||||
|
/// Stores the last time a request was made, in seconds since UNIX epoch
|
||||||
static SESSION_TIME: OnceCell<u64> = OnceCell::new();
|
static SESSION_TIME: OnceCell<u64> = OnceCell::new();
|
||||||
|
|
||||||
/// Makes a request to the online classroom, and returns the html string
|
/// Makes a request to the online classroom, and returns the html string
|
||||||
pub async fn _request(url: String) -> Result<String, String> {
|
pub async fn request(url: String) -> Result<String, String> {
|
||||||
Ok(url)
|
let classroom_url = std::env::var("CLASSROOM_URL").expect("CLASSROOM_URL env var is not set!");
|
||||||
|
|
||||||
|
ensure_session().await?;
|
||||||
|
|
||||||
|
// Create a client & set cookies
|
||||||
|
let cookie = SESSION_COOKIE.get().expect("SESSION_COOKIE was not set, even after calling ensure_session");
|
||||||
|
let cookie = format!("ch_sid={};", cookie);
|
||||||
|
let cookie_url = format!("{}", classroom_url).parse::<Url>().expect("Error parsing CLASSROOM_URL into a url");
|
||||||
|
|
||||||
|
let jar = Jar::default();
|
||||||
|
jar.add_cookie_str(cookie.as_str(), &cookie_url);
|
||||||
|
|
||||||
|
let client = reqwest::Client::builder()
|
||||||
|
.cookie_provider(jar.into())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let client = match client {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(error) => return Err(format!("Error creating client: {:?}", error))
|
||||||
|
};
|
||||||
|
|
||||||
|
// Do the request
|
||||||
|
let response = client
|
||||||
|
.get(format!("{}{}", classroom_url, url))
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let response = match response {
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(err) => return Err(format!("Error sending request: {:?}", err))
|
||||||
|
};
|
||||||
|
|
||||||
|
match response.text().await {
|
||||||
|
Ok(t) => Ok(t),
|
||||||
|
Err(err) => Err(format!("Error getting text from response: {:?}", err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Makes sure that the session cookie is set, and that it is valid
|
/// Makes sure that the session cookie is set, and that it is valid
|
||||||
@ -39,7 +76,7 @@ pub async fn ensure_session() -> Result<(), String> {
|
|||||||
|
|
||||||
/// Logins to the online classroom, and sets the session cookie
|
/// Logins to the online classroom, and sets the session cookie
|
||||||
async fn login() -> Result<(), String> {
|
async fn login() -> Result<(), String> {
|
||||||
let clasroom_url = std::env::var("CLASSROOM_URL").expect("CLASSROOM_URL env var is not set!");
|
let classroom_url = std::env::var("CLASSROOM_URL").expect("CLASSROOM_URL env var is not set!");
|
||||||
let clasroom_user =
|
let clasroom_user =
|
||||||
std::env::var("CLASSROOM_USER").expect("CLASSROOM_USER env var is not set!");
|
std::env::var("CLASSROOM_USER").expect("CLASSROOM_USER env var is not set!");
|
||||||
let clasroom_password =
|
let clasroom_password =
|
||||||
@ -53,7 +90,7 @@ async fn login() -> Result<(), String> {
|
|||||||
];
|
];
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
let result = client
|
let result = client
|
||||||
.post(format!("{}/index.php", clasroom_url))
|
.post(format!("{}/index.php", classroom_url))
|
||||||
.form(¶ms)
|
.form(¶ms)
|
||||||
.send()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use rocket::{http::Status, serde::json::Json};
|
use rocket::{http::Status, serde::json::Json};
|
||||||
|
|
||||||
use super::json_result::JsonResult;
|
use super::{json_result::JsonResult, session::request};
|
||||||
|
|
||||||
// Instead of requesting pages and managing session & cookies manually,
|
// Instead of requesting pages and managing session & cookies manually,
|
||||||
// create a wrapper that:
|
// create a wrapper that:
|
||||||
@ -11,5 +11,15 @@ use super::json_result::JsonResult;
|
|||||||
|
|
||||||
#[get("/classroom/users/<full_name>")]
|
#[get("/classroom/users/<full_name>")]
|
||||||
pub async fn get_users(full_name: String) -> (Status, Json<JsonResult<()>>) {
|
pub async fn get_users(full_name: String) -> (Status, Json<JsonResult<()>>) {
|
||||||
|
let html = request(format!("/main/admin/user_list.php?keyword={}&submit=&_qf__search_simple=", full_name)).await;
|
||||||
|
|
||||||
|
match html {
|
||||||
|
Ok(html) => {
|
||||||
|
println!("{}", html);
|
||||||
(Status::Ok, JsonResult::ok(()))
|
(Status::Ok, JsonResult::ok(()))
|
||||||
|
}
|
||||||
|
Err(reason) => {
|
||||||
|
(Status::InternalServerError, JsonResult::err(reason))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user