Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues after async-upgrade branch review #126

Merged
merged 3 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ spsc-bip-buffer = { git = "https://github.com/Devolutions/spsc-bip-buffer.git",
indexmap = "1.0"

[dependencies.saphir]
git = "https://github.com/Devolutions/saphir.git"
branch = "graceful-shutdown-fix"
git = "https://github.com/richerarc/saphir.git"
rev = "d0fcd6adc0a8a5e22c095d41a9a2c6c0d0d91316"
default-features = false
features = ["https", "json", "macro", "form"]

Expand Down
6 changes: 3 additions & 3 deletions src/http/controllers/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ impl HealthController {
#[controller(name = "health")]
impl HealthController {
#[get("/")]
async fn get_health(&self) -> (u16, String) {
async fn get_health(&self) -> (StatusCode, String) {
build_health_response(&self.config)
}
}

pub fn build_health_response(config: &Config) -> (u16, String) {
pub fn build_health_response(config: &Config) -> (StatusCode, String) {
let hostname = &config.hostname;
let response_body = format!("Devolutions Gateway \"{}\" is alive and healthy.", hostname);
(StatusCode::OK.as_u16(), response_body)
(StatusCode::OK, response_body)
}
52 changes: 23 additions & 29 deletions src/http/controllers/jet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
candidate::Candidate,
},
jet_client::JetAssociationsMap,
utils::association::{RemoveAssociation, ACCEPT_REQUEST_TIMEOUT},
utils::association::{remove_jet_association, ACCEPT_REQUEST_TIMEOUT},
};

pub struct JetController {
Expand All @@ -43,35 +43,32 @@ impl JetController {
#[controller(name = "jet")]
impl JetController {
#[get("/association")]
async fn get_associations(&self, detail: Option<bool>) -> (u16, Option<String>) {
async fn get_associations(&self, detail: Option<bool>) -> (StatusCode, Option<String>) {
let with_detail = detail.unwrap_or(false);

let associations_response: Vec<AssociationResponse>;
if let Ok(associations) = self.jet_associations.try_lock() {
associations_response = associations
.values()
.map(|association| AssociationResponse::from(association, with_detail))
.collect();
} else {
return (StatusCode::INTERNAL_SERVER_ERROR.as_u16(), None);
};
let associations = self.jet_associations.lock().compat().await;

associations_response = associations
.values()
.map(|association| AssociationResponse::from(association, with_detail))
.collect();

if let Ok(body) = serde_json::to_string(&associations_response) {
return (StatusCode::OK.as_u16(), Some(body));
return (StatusCode::OK, Some(body));
}

(StatusCode::BAD_REQUEST.as_u16(), None)
(StatusCode::BAD_REQUEST, None)
}

#[post("/association/<association_id>")]
async fn create_association(&self, req: Request) -> (u16, ()) {
async fn create_association(&self, req: Request) -> (StatusCode, ()) {
let association_id = match req
.captures()
.get("association_id")
.and_then(|id| Uuid::parse_str(id).ok())
{
Some(id) => id,
None => return (StatusCode::BAD_REQUEST.as_u16(), ()),
None => return (StatusCode::BAD_REQUEST, ()),
};

// check the session token is signed by our provider if unrestricted mode is not set
Expand All @@ -80,15 +77,15 @@ impl JetController {
Err(e) => {
slog_scope::error!("Couldn't validate session token: {}", e);

return (StatusCode::UNAUTHORIZED.as_u16(), ());
return (StatusCode::UNAUTHORIZED, ());
}
Ok(expected_id) if expected_id != association_id => {
slog_scope::error!(
"Invalid session token: expected {}, got {}",
expected_id,
association_id
);
return (StatusCode::FORBIDDEN.as_u16(), ());
return (StatusCode::FORBIDDEN, ());
}
Ok(_) => { /* alright */ }
}
Expand All @@ -102,41 +99,41 @@ impl JetController {
jet_associations.insert(association_id, Association::new(association_id, JET_VERSION_V2));
start_remove_association_future(self.jet_associations.clone(), association_id).await;

(StatusCode::OK.as_u16(), ())
(StatusCode::OK, ())
}

#[post("/association/<association_id>/candidates")]
async fn gather_association_candidates(&self, req: Request) -> (u16, Option<String>) {
async fn gather_association_candidates(&self, req: Request) -> (StatusCode, Option<String>) {
let association_id = match req
.captures()
.get("association_id")
.and_then(|id| Uuid::parse_str(id).ok())
{
Some(id) => id,
None => return (StatusCode::BAD_REQUEST.as_u16(), None),
None => return (StatusCode::BAD_REQUEST, None),
};

// check the session token is signed by our provider if unrestricted mode is not set
if !self.config.unrestricted {
match validate_session_token(self.config.as_ref(), &req) {
Err(e) => {
slog_scope::error!("Couldn't validate session token: {}", e);
return (StatusCode::UNAUTHORIZED.as_u16(), None);
return (StatusCode::UNAUTHORIZED, None);
}
Ok(expected_id) if expected_id != association_id => {
slog_scope::error!(
"Invalid session token: expected {}, got {}",
expected_id,
association_id
);
return (StatusCode::FORBIDDEN.as_u16(), None);
return (StatusCode::FORBIDDEN, None);
}
Ok(_) => { /* alright */ }
}
}

// create association
let mut jet_associations = self.jet_associations.lock().compat().await; // no need to deal with lock poisoning
let mut jet_associations = self.jet_associations.lock().compat().await;

if !jet_associations.contains_key(&association_id) {
jet_associations.insert(association_id, Association::new(association_id, JET_VERSION_V2));
Expand All @@ -154,14 +151,11 @@ impl JetController {
}
}

(
StatusCode::OK.as_u16(),
Some(association.gather_candidate().to_string()),
)
(StatusCode::OK, Some(association.gather_candidate().to_string()))
}

#[get("/health")]
async fn health(&self) -> (u16, String) {
async fn health(&self) -> (StatusCode, String) {
build_health_response(&self.config)
}
}
Expand All @@ -174,7 +168,7 @@ pub async fn remove_association(jet_associations: JetAssociationsMap, uuid: Uuid
if let Ok(runtime_handle) = Handle::try_current() {
runtime_handle.spawn(async move {
tokio_02::time::delay_for(ACCEPT_REQUEST_TIMEOUT).await;
if RemoveAssociation::new(jet_associations, uuid, None).compat().await {
if remove_jet_association(jet_associations, uuid, None).compat().await {
info!(
"No connect request received with association {}. Association removed!",
uuid
Expand Down
4 changes: 2 additions & 2 deletions src/http/controllers/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl Default for SessionsController {
#[controller(name = "sessions")]
impl SessionsController {
#[get("/count")]
async fn get_count(&self) -> (u16, String) {
async fn get_count(&self) -> (StatusCode, String) {
let sessions_count = SESSION_IN_PROGRESS_COUNT.load(Ordering::Relaxed).to_string();
(StatusCode::OK.as_u16(), sessions_count)
(StatusCode::OK, sessions_count)
}
}
Loading