Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Socket cleanup #682

Merged
merged 5 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions src/auth/flows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,15 +1002,19 @@ pub async fn ws_init(
pub async fn auth_callback(
req: HttpRequest,
Query(query): Query<HashMap<String, String>>,
sockets: Data<RwLock<ActiveSockets>>,
active_sockets: Data<RwLock<ActiveSockets>>,
client: Data<PgPool>,
file_host: Data<Arc<dyn FileHost + Send + Sync>>,
redis: Data<deadpool_redis::Pool>,
) -> Result<HttpResponse, super::templates::ErrorPage> {
let res = async move {
let state = query
.get("state")
.ok_or_else(|| AuthenticationError::InvalidCredentials)?.clone();
let state_string = query
.get("state")
.ok_or_else(|| AuthenticationError::InvalidCredentials)?
.clone();

let sockets = active_sockets.clone();
let state = state_string.clone();
let res: Result<HttpResponse, AuthenticationError> = (|| async move {

let flow = Flow::get(&state, &redis).await?;

Expand Down Expand Up @@ -1172,7 +1176,29 @@ pub async fn auth_callback(
} else {
Err::<HttpResponse, AuthenticationError>(AuthenticationError::InvalidCredentials)
}
}.await;
})().await;

// Because this is callback route, if we have an error, we need to ensure we close the original socket if it exists
if let Err(ref e) = res {
let db = active_sockets.read().await;
let mut x = db.auth_sockets.get_mut(&state_string);

if let Some(x) = x.as_mut() {
let mut ws_conn = x.value_mut().clone();

ws_conn
.text(
serde_json::json!({
"error": &e.error_name(),
"description": &e.to_string(),
} )
.to_string(),
)
.await
.map_err(|_| AuthenticationError::SocketError)?;
let _ = ws_conn.close(None).await;
}
}

Ok(res?)
}
Expand Down
40 changes: 23 additions & 17 deletions src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,30 @@ impl actix_web::ResponseError for AuthenticationError {

fn error_response(&self) -> HttpResponse {
HttpResponse::build(self.status_code()).json(ApiError {
error: match self {
AuthenticationError::Env(..) => "environment_error",
AuthenticationError::Sqlx(..) => "database_error",
AuthenticationError::Database(..) => "database_error",
AuthenticationError::SerDe(..) => "invalid_input",
AuthenticationError::Reqwest(..) => "network_error",
AuthenticationError::InvalidCredentials => "invalid_credentials",
AuthenticationError::Decoding(..) => "decoding_error",
AuthenticationError::Mail(..) => "mail_error",
AuthenticationError::InvalidAuthMethod => "invalid_auth_method",
AuthenticationError::InvalidClientId => "invalid_client_id",
AuthenticationError::Url => "url_error",
AuthenticationError::FileHosting(..) => "file_hosting",
AuthenticationError::DuplicateUser => "duplicate_user",
AuthenticationError::Custom(..) => "custom",
AuthenticationError::SocketError => "socket",
},
error: self.error_name(),
description: &self.to_string(),
})
}
}

impl AuthenticationError {
pub fn error_name(&self) -> &'static str {
match self {
AuthenticationError::Env(..) => "environment_error",
AuthenticationError::Sqlx(..) => "database_error",
AuthenticationError::Database(..) => "database_error",
AuthenticationError::SerDe(..) => "invalid_input",
AuthenticationError::Reqwest(..) => "network_error",
AuthenticationError::InvalidCredentials => "invalid_credentials",
AuthenticationError::Decoding(..) => "decoding_error",
AuthenticationError::Mail(..) => "mail_error",
AuthenticationError::InvalidAuthMethod => "invalid_auth_method",
AuthenticationError::InvalidClientId => "invalid_client_id",
AuthenticationError::Url => "url_error",
AuthenticationError::FileHosting(..) => "file_hosting",
AuthenticationError::DuplicateUser => "duplicate_user",
AuthenticationError::Custom(..) => "custom",
AuthenticationError::SocketError => "socket",
}
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::file_hosting::S3Host;
use crate::queue::analytics::AnalyticsQueue;
use crate::queue::download::DownloadQueue;
use crate::queue::payouts::{process_payout, PayoutsQueue};
use crate::queue::payouts::PayoutsQueue;
use crate::queue::session::AuthQueue;
use crate::queue::socket::ActiveSockets;
use crate::ratelimit::errors::ARError;
Expand Down
2 changes: 1 addition & 1 deletion src/validate/forge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl super::Validator for ForgeValidator {

fn validate(
&self,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
_archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
// if archive.by_name("META-INF/mods.toml").is_err() {
// return Ok(ValidationResult::Warning(
Expand Down
9 changes: 5 additions & 4 deletions src/validate/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ impl super::Validator for SpongeValidator {
&self,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
if !archive
.file_names()
.any(|name| name == "sponge_plugins.json" || name == "mcmod.info" || name == "META-INF/sponge_plugins.json")
{
if !archive.file_names().any(|name| {
name == "sponge_plugins.json"
|| name == "mcmod.info"
|| name == "META-INF/sponge_plugins.json"
}) {
return Ok(ValidationResult::Warning(
"No sponge_plugins.json or mcmod.info present for Sponge plugin.",
));
Expand Down