diff --git a/gamestream-webtransport-proxy/src/pair.rs b/gamestream-webtransport-proxy/src/pair.rs index 88e822c..9aeb171 100644 --- a/gamestream-webtransport-proxy/src/pair.rs +++ b/gamestream-webtransport-proxy/src/pair.rs @@ -292,7 +292,11 @@ async fn send_client_pairing_secret( #[craft] impl crate::config::ConfigFile { - #[craft(endpoint(status_codes(StatusCode::OK, StatusCode::INTERNAL_SERVER_ERROR)))] + #[craft(endpoint(status_codes( + StatusCode::OK, + StatusCode::BAD_REQUEST, + StatusCode::INTERNAL_SERVER_ERROR + )))] pub async fn post_pair( self: ::std::sync::Arc, body: salvo::oapi::extract::JsonBody, @@ -305,6 +309,30 @@ impl crate::config::ConfigFile { name: params.name, }; + let reader = self.read().await; + + let servers = match reader.servers() { + Ok(s) => s, + Err(e) => { + error!("Could not get servers: {e}"); + return Err(AppError { + status_code: StatusCode::INTERNAL_SERVER_ERROR, + description: "Pairing failed".to_string(), + }); + } + }; + + if servers.contains_key(&server.name) { + error!( + "Could not pair server with name {}, server with that name already exists.", + server.name + ); + return Err(AppError { + status_code: StatusCode::BAD_REQUEST, + description: format!("Server with name {} already exists.", server.name), + }); + } + let unique_id = match self.read().await.unique_id() { Ok(u) => u, Err(e) => {