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

Reject connections before accepting them #72

Open
UkoeHB opened this issue Aug 18, 2023 · 3 comments
Open

Reject connections before accepting them #72

UkoeHB opened this issue Aug 18, 2023 · 3 comments

Comments

@UkoeHB
Copy link
Collaborator

UkoeHB commented Aug 18, 2023

Currently you can only reject a connection after the websockets backend has accepted the connection. This means clients will always see 'connected' -> 'disconnected' events even if their connection request was rejected by your custom ServerExt.

There should be an additional step in the connection-acceptance protocol for pre-validating requests before the websockets backend can accept the connection.

It should be possible to move that step into the axum Upgrade::from_request() method and tungstenite Acceptor .. callback closure, although it's not clear the optimal/correct approach since server actors are async from their servers (there is a risk of introducing race conditions between pre-validating requests and registering connections in the ServerExt, e.g. if you reject duplicate connections).

@gbaranski
Copy link
Owner

In Axum there's a way - take a look at this example:

let kick_me = query.get("kick_me");
let kick_me = kick_me.map(|s| s.as_str());
if matches!(kick_me, Some("Yes")) {
return (
axum::http::StatusCode::BAD_REQUEST,
"we won't accept you because of kick_me query parameter",
)
.into_response();
}

You can do some work before calling ezsocket.on_upgrade(server).

@UkoeHB
Copy link
Collaborator Author

UkoeHB commented Sep 22, 2023

It's good to know this can be solved in Axum. The tungstenite integration does not have a solution right now, so I am reopening to track that issue.

@gbaranski
Copy link
Owner

Yes that's true.
I think ezsockets had this feature in one of its prior version, not sure though.
We can implement this feature, but I'd prefer to make it exclusive to tungstenite runtime, so that it doesn't affect axum and doesn't make two ways to do rejection before accept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants