update: changed to fft
This commit is contained in:
parent
e198fb0ce0
commit
90e05bae02
2 changed files with 74 additions and 75 deletions
115
src/main.rs
115
src/main.rs
|
|
@ -7,7 +7,6 @@
|
|||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
sync::Arc,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use axum::{
|
||||
|
|
@ -26,7 +25,6 @@ use base64::Engine;
|
|||
use color_eyre::eyre::Context;
|
||||
use reqwest::tls::Version;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use tokio::sync::Mutex;
|
||||
use tracing::{error, info};
|
||||
|
||||
|
|
@ -42,7 +40,7 @@ mod oauth2;
|
|||
struct AppState {
|
||||
_http: reqwest::Client,
|
||||
oauth: Arc<oauth2::OauthClient>,
|
||||
tutors: Arc<Mutex<HashSet<u64>>>,
|
||||
allowed: Arc<Mutex<HashSet<u64>>>,
|
||||
key: Key,
|
||||
}
|
||||
|
||||
|
|
@ -62,37 +60,7 @@ struct GroupsUsers {
|
|||
id: u64,
|
||||
}
|
||||
|
||||
async fn tutors(config: AppState) {
|
||||
loop {
|
||||
{
|
||||
let mut lock = config.tutors.lock().await;
|
||||
lock.clear();
|
||||
let mut page_nb = 0;
|
||||
loop {
|
||||
info!("tutor request (page {page_nb})");
|
||||
let res = config
|
||||
.oauth
|
||||
.do_request::<Vec<User42>>(
|
||||
"https://api.intra.42.fr/v2/groups/166/users",
|
||||
json! ({
|
||||
"page[number]": page_nb,
|
||||
"page[size]": 100,
|
||||
}),
|
||||
Option::<&oauth2::Token>::None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let do_next = res.len() == 100;
|
||||
lock.extend(res.into_iter().map(|s| s.id));
|
||||
if !do_next {
|
||||
break;
|
||||
}
|
||||
page_nb += 1;
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(Duration::new(3600 * 24 /*tout les jours*/, 0)).await;
|
||||
}
|
||||
}
|
||||
const ALLOWED_USERS: &[u64] = &[/*maiboyer*/ 159559, /*nfelsemb*/ 95340];
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
|
@ -104,7 +72,7 @@ async fn main() {
|
|||
let http = reqwest::ClientBuilder::new()
|
||||
// Following redirects opens the client up to SSRF vulnerabilities.
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.user_agent("AlertePoste/1.0")
|
||||
.user_agent("FftManager/1.0")
|
||||
.tls_info(true)
|
||||
.min_tls_version(Version::TLS_1_0)
|
||||
.max_tls_version(Version::TLS_1_2)
|
||||
|
|
@ -120,7 +88,7 @@ async fn main() {
|
|||
http.clone(),
|
||||
unwrap_env!("CLIENT_ID"),
|
||||
unwrap_env!("CLIENT_SECRET"),
|
||||
"http://local.maix.me:9911/auth/callback",
|
||||
"https://fft.maix.me/manager/auth/callback",
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
|
@ -129,15 +97,15 @@ async fn main() {
|
|||
_http: http,
|
||||
key,
|
||||
oauth: Arc::new(oauth),
|
||||
tutors: Default::default(),
|
||||
allowed: Arc::new(Mutex::new(ALLOWED_USERS.iter().copied().collect())),
|
||||
};
|
||||
tokio::task::spawn_local(tutors(state.clone()));
|
||||
|
||||
// build our application with a route
|
||||
let app = Router::new()
|
||||
// `GET /` goes to `root`
|
||||
.route("/", get(root))
|
||||
.route("/status", get(status))
|
||||
.route("/status_container", get(status_container))
|
||||
.route("/stop", get(stop))
|
||||
.route("/start", get(start))
|
||||
.route("/restart", get(restart))
|
||||
|
|
@ -153,7 +121,7 @@ async fn main() {
|
|||
std::env::args()
|
||||
.nth(1)
|
||||
.and_then(|s| s.parse::<u16>().ok())
|
||||
.unwrap_or(9911)
|
||||
.unwrap_or(9912)
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
|
|
@ -242,7 +210,7 @@ impl FromRequestParts<AppState> for UserLoggedIn {
|
|||
return Err((
|
||||
StatusCode::TEMPORARY_REDIRECT,
|
||||
jar,
|
||||
Redirect::to("/auth/login"),
|
||||
Redirect::to("/manager/auth/login"),
|
||||
));
|
||||
};
|
||||
|
||||
|
|
@ -251,18 +219,18 @@ impl FromRequestParts<AppState> for UserLoggedIn {
|
|||
return Err((
|
||||
StatusCode::TEMPORARY_REDIRECT,
|
||||
jar,
|
||||
Redirect::to("/auth/login"),
|
||||
Redirect::to("/manager/auth/login"),
|
||||
));
|
||||
};
|
||||
|
||||
if state.tutors.lock().await.contains(&user_id) {
|
||||
if state.allowed.lock().await.contains(&user_id) {
|
||||
Ok(UserLoggedIn)
|
||||
} else {
|
||||
let jar = jar.remove("token");
|
||||
Err((
|
||||
StatusCode::TEMPORARY_REDIRECT,
|
||||
jar,
|
||||
Redirect::to("/auth/error"),
|
||||
Redirect::to("/manager/auth/error"),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
@ -272,7 +240,7 @@ async fn auth_error() -> Html<&'static str> {
|
|||
info!("Request auth_error page");
|
||||
Html(
|
||||
r#"
|
||||
<h1>Hello TUTORS ONLY :D</h1>
|
||||
<h1>Hello you aren't allowed here :) :D</h1>
|
||||
"#,
|
||||
)
|
||||
}
|
||||
|
|
@ -282,11 +250,23 @@ async fn root(_user: UserLoggedIn) -> Html<&'static str> {
|
|||
info!("Request link page");
|
||||
Html(
|
||||
r#"
|
||||
<a href="/restart">restart</a><br>
|
||||
<a href="/stop">stop</a><br>
|
||||
<a href="/start">start</a><br>
|
||||
<a href="/status">status</a><br>
|
||||
<a href="/pull">git pull (ask before!)</a><br>
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
* {
|
||||
font-size: 30px;
|
||||
};
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<a href="/manager/restart">restart</a><br>
|
||||
<a href="/manager/stop">stop</a><br>
|
||||
<a href="/manager/start">start</a><br>
|
||||
<a href="/manager/status">status</a><br>
|
||||
<a href="/manager/status_container">status for whole container</a><br>
|
||||
<a href="/manager/pull">git pull (ask before!)</a><br>
|
||||
</body>
|
||||
</html>
|
||||
"#,
|
||||
)
|
||||
}
|
||||
|
|
@ -295,7 +275,7 @@ async fn restart(_user: UserLoggedIn) -> Redirect {
|
|||
info!("Requested to restart the bot");
|
||||
tokio::spawn(async {
|
||||
tokio::process::Command::new("systemctl")
|
||||
.args(["--user", "restart", "botloc.service"])
|
||||
.args(["restart", "container@fft.service"])
|
||||
.spawn()
|
||||
.unwrap()
|
||||
});
|
||||
|
|
@ -306,7 +286,7 @@ async fn start(_user: UserLoggedIn) -> Redirect {
|
|||
info!("Requested to start the bot");
|
||||
tokio::spawn(async {
|
||||
tokio::process::Command::new("systemctl")
|
||||
.args(["--user", "start", "botloc.service"])
|
||||
.args(["restart", "container@fft.service"])
|
||||
.spawn()
|
||||
.unwrap()
|
||||
});
|
||||
|
|
@ -317,7 +297,7 @@ async fn stop(_user: UserLoggedIn) -> Redirect {
|
|||
info!("Requested to stop the bot");
|
||||
tokio::spawn(async {
|
||||
tokio::process::Command::new("systemctl")
|
||||
.args(["--user", "stop", "botloc.service"])
|
||||
.args(["stop", "container@fft.service"])
|
||||
.spawn()
|
||||
.unwrap()
|
||||
});
|
||||
|
|
@ -326,8 +306,8 @@ async fn stop(_user: UserLoggedIn) -> Redirect {
|
|||
|
||||
async fn status() -> Result<String, StatusCode> {
|
||||
info!("Requested status");
|
||||
let mut output = tokio::process::Command::new("journalctl")
|
||||
.args(["--user", "-xeu", "botloc"])
|
||||
let mut output = tokio::process::Command::new("nixos-container")
|
||||
.args(["run", "fft", "--", "journalctl", "-xeu", "fft-1"])
|
||||
.output()
|
||||
.await
|
||||
// let mut output = child.wait_with_output().await
|
||||
|
|
@ -338,22 +318,41 @@ async fn status() -> Result<String, StatusCode> {
|
|||
output.stdout.push(b'\n');
|
||||
output.stdout.append(&mut output.stderr);
|
||||
String::from_utf8(output.stdout).map_err(|e| {
|
||||
error!("Error with systemctl status output {e}");
|
||||
error!("Error with systemctl status output {e}");
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
})
|
||||
}
|
||||
|
||||
async fn status_container() -> Result<String, StatusCode> {
|
||||
info!("Requested status");
|
||||
let mut output = tokio::process::Command::new("nixos-container")
|
||||
.args(["run", "fft", "--", "journalctl"])
|
||||
.output()
|
||||
.await
|
||||
// let mut output = child.wait_with_output().await
|
||||
.map_err(|e| {
|
||||
error!("Error with systemctl status {e}");
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
})?;
|
||||
output.stdout.push(b'\n');
|
||||
output.stdout.append(&mut output.stderr);
|
||||
String::from_utf8(output.stdout).map_err(|e| {
|
||||
error!("Error with systemctl status output {e}");
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
})
|
||||
}
|
||||
|
||||
async fn git_pull() -> Result<String, (StatusCode, &'static str)> {
|
||||
info!("Requested to pull");
|
||||
let mut output = tokio::process::Command::new("/home/maix/.nix-profile/bin/git")
|
||||
.current_dir(std::env::var("BOTLOC_DIR").map_err(|e| {
|
||||
let mut output = tokio::process::Command::new("/run/wrappers/bin/sudo")
|
||||
.current_dir(std::env::var("FFT_DIR").map_err(|e| {
|
||||
error!("Error with git pull command {e}");
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Please set the BOTLOC_DIR variable",
|
||||
)
|
||||
})?)
|
||||
.args(["pull"])
|
||||
.args(["-u", "maix", "/home/maix/.nix-profile/bin/git", "pull"])
|
||||
.output()
|
||||
.await
|
||||
// let mut output = child.wait_with_output().await
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue