account/src/nnid/agreements.rs

79 lines
2 KiB
Rust
Raw Normal View History

2025-04-26 21:03:07 +02:00
use std::{env, io};
2025-07-31 12:34:21 +02:00
use std::collections::HashSet;
use once_cell::sync::Lazy;
2025-02-27 10:25:31 +01:00
use rocket::fs::NamedFile;
2025-07-31 12:34:21 +02:00
use rocket::{get, Request};
use rocket::http::Status;
use rocket::request::{FromRequest, Outcome};
2025-02-27 10:25:31 +01:00
use rocket::response::content::RawXml;
use tokio::fs::try_exists;
2025-07-31 12:34:21 +02:00
use tokio::sync::RwLock;
use tonic::async_trait;
2025-02-27 10:25:31 +01:00
use crate::dsresponse::Ds;
2025-07-31 12:34:21 +02:00
pub static EVIL_AGREEMENT_THING: Lazy<RwLock<HashSet<String>>> = Lazy::new(|| Default::default());
pub struct CFIP(pub String);
#[async_trait]
impl<'r> FromRequest<'r> for CFIP{
type Error = ();
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
match request.headers().get("CF-Connecting-IP").next(){
Some(v) => Outcome::Success(Self(v.to_owned())),
None => Outcome::Error((Status::ImATeapot, ()))
}
}
}
2025-02-27 10:25:31 +01:00
#[get("/v1/api/content/agreements/Nintendo-Network-EULA/<lang>/@latest")]
2025-07-31 12:34:21 +02:00
pub async fn get_agreement(lang: &str, ip: CFIP) -> io::Result<Ds<RawXml<NamedFile>>>{
2025-02-27 10:25:31 +01:00
let base_path = {
// if this crashes then something is wrong with the server setup so crashing here is fine imo
let mut path = env::current_dir().unwrap();
path.push("res");
path.push("agreement");
path
};
2025-07-31 12:34:21 +02:00
if EVIL_AGREEMENT_THING.read().await.contains(&ip.0) {
2025-07-31 14:24:11 +02:00
let path = {
let mut path = base_path;
path.push("EVIL.xml");
path
};
Ok(Ds(RawXml(NamedFile::open(&path).await?)))
} else {
2025-07-31 12:34:21 +02:00
let requested_file_path = {
let mut path = base_path.clone();
2025-02-27 10:25:31 +01:00
2025-07-31 12:34:21 +02:00
path.push(format!("{}.xml", lang));
2025-02-27 10:25:31 +01:00
2025-07-31 12:34:21 +02:00
path
};
if try_exists(&requested_file_path).await.is_ok_and(|v| v == true) {
Ok(Ds(RawXml(NamedFile::open(&requested_file_path).await?)))
} else {
let fallback_path = {
let mut path = base_path;
2025-02-27 10:25:31 +01:00
2025-07-31 12:34:21 +02:00
path.push("DEFAULT.xml");
2025-02-27 10:25:31 +01:00
2025-07-31 12:34:21 +02:00
path
};
2025-02-27 10:25:31 +01:00
2025-07-31 12:34:21 +02:00
Ok(Ds(RawXml(NamedFile::open(&fallback_path).await?)))
}
2025-02-27 10:25:31 +01:00
}
}