feat: add different ban types

This commit is contained in:
Maple 2025-07-31 12:34:21 +02:00
commit 8f08d07fa2
4 changed files with 70 additions and 15 deletions

File diff suppressed because one or more lines are too long

0
res/agreement/EVIL.xml Normal file
View file

View file

@ -1,12 +1,38 @@
use std::{env, io};
use std::collections::HashSet;
use gxhash::HashMap;
use once_cell::sync::Lazy;
use rocket::fs::NamedFile;
use rocket::get;
use rocket::{get, Request};
use rocket::http::Status;
use rocket::request::{FromRequest, Outcome};
use rocket::response::content::RawXml;
use tokio::fs::try_exists;
use tokio::sync::RwLock;
use tonic::async_trait;
use crate::dsresponse::Ds;
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, ()))
}
}
}
#[get("/v1/api/content/agreements/Nintendo-Network-EULA/<lang>/@latest")]
pub async fn get_agreement(lang: &str) -> io::Result<Ds<RawXml<NamedFile>>>{
pub async fn get_agreement(lang: &str, ip: CFIP) -> io::Result<Ds<RawXml<NamedFile>>>{
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();
@ -17,27 +43,38 @@ pub async fn get_agreement(lang: &str) -> io::Result<Ds<RawXml<NamedFile>>>{
path
};
let requested_file_path = {
let mut path = base_path.clone();
if EVIL_AGREEMENT_THING.read().await.contains(&ip.0) {
let requested_file_path = {
let mut path = base_path.clone();
path.push(format!("{}.xml", lang));
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;
path.push("DEFAULT.xml");
path.push(format!("{}.xml", lang));
path
};
Ok(Ds(RawXml(NamedFile::open(&fallback_path).await?)))
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;
path.push("DEFAULT.xml");
path
};
Ok(Ds(RawXml(NamedFile::open(&fallback_path).await?)))
}
} else {
let path = {
let mut path = base_path;
path.push("EVIL.xml");
path
};
Ok(Ds(RawXml(NamedFile::open(&path).await?)))
}
}

View file

@ -3,6 +3,7 @@ use rocket::form::Form;
use serde::{Serialize};
use crate::account::account::User;
use crate::error::{Error, Errors};
use crate::nnid::agreements::{CFIP, EVIL_AGREEMENT_THING};
use crate::nnid::oauth::generate_token::token_type::{AUTH_REFRESH_TOKEN, AUTH_TOKEN};
use crate::nnid::oauth::TokenData;
use crate::Pool;
@ -26,12 +27,21 @@ const ACCOUNT_ID_OR_PASSWORD_ERRORS: Errors = Errors{
const ACCOUNT_BANNED_ERRORS: Errors = Errors{
error: &[
Error{
code: "0123",
code: "0122",
message: "Device has been banned by game server"
}
]
};
const REREAD_EULA_EXTRABANNED_ERRORS: Errors = Errors{
error: &[
Error{
code: "0109",
message: "REREAD THE EULA LOL"
}
]
};
#[derive(FromForm)]
pub struct TokenRequestData<'a>{
grant_type: &'a str,
@ -90,7 +100,7 @@ pub struct TokenRequestReturnData{
}
#[post("/v1/api/oauth20/access_token/generate", data="<data>")]
pub async fn generate_token(pool: &State<Pool>, data: Form<TokenRequestData<'_>>) -> Result<Xml<TokenRequestReturnData>, Option<Errors<'static>>>{
pub async fn generate_token(pool: &State<Pool>, data: Form<TokenRequestData<'_>>, ip: CFIP) -> Result<Xml<TokenRequestReturnData>, Option<Errors<'static>>>{
let pool = pool.inner();
let user = User::get_by_username(data.user_id, pool).await
@ -101,6 +111,14 @@ pub async fn generate_token(pool: &State<Pool>, data: Form<TokenRequestData<'_>>
}
if user.account_level < 0{
if user.account_level == -2 {
return Err(Some(REREAD_EULA_EXTRABANNED_ERRORS));
}
if user.account_level == -3{
EVIL_AGREEMENT_THING.write().await.insert(ip.0);
return Err(Some(REREAD_EULA_EXTRABANNED_ERRORS));
}
return Err(Some(ACCOUNT_BANNED_ERRORS));
}