socketioxide icon indicating copy to clipboard operation
socketioxide copied to clipboard

how to get the remote ip?

Open jqiris opened this issue 2 years ago • 3 comments

jqiris avatar Oct 13 '23 03:10 jqiris

Currently you can't but it is on my to-do list. This issue can serve as a tracking issue for this feature

Totodore avatar Oct 13 '23 04:10 Totodore

Currently you can't but it is on my to-do list. This issue can serve as a tracking issue for this feature Hi,

I'm interested in working on this issue. Do you have any specific suggestions or preferred approaches for tackling it?

Thanks!

DnaBoss avatar Nov 23 '23 18:11 DnaBoss

Hi!

The main issue is that there is no standardized way to manage the remote IP between http frameworks.

For example, Axum is storing connect information in extensions whereas salvo is storing them in its own Request type.

Therefore socketioxide can't manage remote IP in a standardized way.

What could be done is documenting this case and doing examples for each framework.

My main idea was to put a framework-specific middleware in front of the /socket.io path to store the client ip into the http extensions and then extract it with req_parts. However this wouldn't work with connections initialized with the ws transport (see the req_parts doc for more info) so another way could be to add it to the headers map.

To summarize, currently, this issue is quite stuck. The only thing to do would be to do framework-specific example and document cases.

If you want to contribute you can also take a look at the other issues :).

Totodore avatar Nov 23 '23 18:11 Totodore

As said before, there is no way to do that with socketioxide because it is not included in the tower/hyper spec. The way to get the IP is different between each HTTP framework. I will do a recap here for axum and close the issue:

Axum

Use into_make_service_with_connect_info to create the server and later use the HttpExtension provided by socketioxide:

fn connect(s: SocketRef, client_ip: HttpExtension<ConnectInfo<SocketAddr>) {
  println!("client_ip: {:?}", client_ip);
}

Totodore avatar Nov 03 '24 17:11 Totodore