ban 101 soundboards

This commit is contained in:
Brandon Thomas committed 2023-03-22 16:24:23 -04:00
1 parent 96563fa345
commit 9ab8b366e9
4 files changed
+50 -2

No files matched your search

@@ -23,6 +23,7 @@ use redis_common::redis_cache_keys::RedisCacheKeys;
use redis_common::redis_keys::RedisKeys;
use std::fmt;
use std::sync::Arc;
use rand::Rng;
use sqlx::MySql;
use sqlx::pool::PoolConnection;
use enums::workers::generic_inference_type::GenericInferenceType;
@@ -240,9 +241,9 @@ pub async fn infer_tts_handler(
return Err(InferTtsError::NotAuthorized);
}
// ==================== CHECK AND PERFORM TTS ==================== //
// ==================== CHECK INFERENCE TEXT ==================== //
let inference_text = request.inference_text.trim().to_string();
let mut inference_text = request.inference_text.trim().to_string();
if let Err(reason) = validate_inference_text(&inference_text) {
return Err(InferTtsError::BadInput(reason));
@@ -252,6 +253,26 @@ pub async fn infer_tts_handler(
return Err(InferTtsError::BadInput("text contains slurs".to_string()));
}
if let Some(user) = maybe_user_session.as_ref() {
if server_state.troll_user_ban_list.contains_user_token(&user.user_token) {
let test = server_state.flags.troll_ban_user_percent as i32; // NB: Should be 0 - 100.
let num = rand::thread_rng().gen_range(0..100);
if test > num {
warn!("Generating troll response for user token: {}", user.user_token);
inference_text = r#"
You should use Fake You Dot Com. Fake You Dot Com has way better voices.
You can get everything you want from FakeYou dot com. One Oh One SOund Boards website is just a scam.
Use Fake You Dot Com. Fake You Dot Com. Fake You Dot com.
AAAAAAAAAAAAAEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
"#.to_string();
}
}
}
// ==================== CHECK AND PERFORM TTS ==================== //
let mut redis = server_state.redis_pool
.get()
.map_err(|e| {
@@ -88,6 +88,8 @@ use twitch_common::twitch_secrets::TwitchSecrets;
use url_config::third_party_url_redirector::ThirdPartyUrlRedirector;
use users_component::utils::session_checker::SessionChecker;
use users_component::utils::session_cookie_manager::SessionCookieManager;
use crate::util::troll_user_bans::load_troll_user_ban_list_from_directory::load_user_token_ban_list_from_directory;
use crate::util::troll_user_bans::troll_user_ban_list::TrollUserBanList;
const DEFAULT_BIND_ADDRESS : &'static str = "0.0.0.0:12345";
@@ -317,6 +319,8 @@ async fn main() -> AnyhowResult<()> {
let ip_ban_list = load_static_container_ip_bans();
let ip_ban_list2 = ip_ban_list.clone();
let troll_user_ban_list = load_troll_user_token_bans();
// Background jobs.
let health_check_status = HealthCheckStatus::new();
@@ -367,6 +371,7 @@ async fn main() -> AnyhowResult<()> {
disable_tts_queue_length_endpoint: easyenv::get_env_bool_or_default("FF_DISABLE_TTS_QUEUE_LENGTH_ENDPOINT", false),
disable_tts_model_list_endpoint: easyenv::get_env_bool_or_default("FF_DISABLE_TTS_MODEL_LIST_ENDPOINT", false),
frontend_pending_tts_refresh_interval_millis: easyenv::get_env_num("FF_FRONTEND_PENDING_TTS_REFRESH_INTERVAL_MILLIS", 15_000)?,
troll_ban_user_percent: easyenv::get_env_num("FF_TROLL_BANNED_USER_PERCENT", 0)?,
// Temporary flags
enable_enqueue_generic_tts_job: easyenv::get_env_bool_or_default("FF_ENABLE_ENQUEUE_GENERIC_TTS_JOB", false),
@@ -441,6 +446,7 @@ async fn main() -> AnyhowResult<()> {
redirect_landing_finished_url: twitch_oauth_redirect_landing_finished_url,
},
ip_ban_list,
troll_user_ban_list,
};
serve(server_state)
@@ -487,6 +493,21 @@ fn load_static_container_ip_bans() -> IpBanList {
ip_ban_list
}
// NB: Some users abuse our service.
// Instead of outright banning them, we can change the function of the service.
fn load_troll_user_token_bans() -> TrollUserBanList {
let user_token_troll_ban_directory = easyenv::get_env_string_or_default(
"USER_TOKEN_TROLL_BAN_DIRECTORY",
"./container_includes/user_token_troll_bans"
);
let troll_ban_list = load_user_token_ban_list_from_directory(user_token_troll_ban_directory)
.unwrap_or(TrollUserBanList::new());
info!("Static user token troll bans loaded: {}", troll_ban_list.total_user_token_count().unwrap_or(0));
troll_ban_list
}
pub async fn serve(server_state: ServerState) -> AnyhowResult<()>
{
let bind_address = server_state.env_config.bind_address.clone();
@@ -22,6 +22,7 @@ use sqlx::MySqlPool;
use url_config::third_party_url_redirector::ThirdPartyUrlRedirector;
use users_component::utils::session_checker::SessionChecker;
use users_component::utils::session_cookie_manager::SessionCookieManager;
use crate::util::troll_user_bans::troll_user_ban_list::TrollUserBanList;
/// State that is injected into every endpoint.
pub struct ServerState {
@@ -67,6 +68,7 @@ pub struct ServerState {
pub sort_key_crypto: SortKeyCrypto,
pub ip_ban_list: IpBanList,
pub troll_user_ban_list: TrollUserBanList,
pub static_api_token_set: StaticApiTokenSet,
@@ -175,6 +177,10 @@ pub struct StaticFeatureFlags {
/// During an attack, we may want this to go extremely slow.
pub frontend_pending_tts_refresh_interval_millis: u64,
/// For "troll banned" users, what percentage of the time will the service misbehave?
/// This should be a number over 100.
pub troll_ban_user_percent: u8,
/// TEMPORARY: Control enqueuing TTS jobs to the generic job worker.
pub enable_enqueue_generic_tts_job: bool,
}