rust-libp2p icon indicating copy to clipboard operation
rust-libp2p copied to clipboard

Websocket transport (with `tokio`) doesn't work on Android

Open jsamol opened this issue 1 year ago • 2 comments

Summary

Since the websocket transport relies on the DNS transport, it's affected by the same issue as its dependency (#2064) and fails to work on Android. For DNS, the issue can be mitigated by providing custom libp2p_dns::ResolverConfig and libp2p_dns::ResolverOpts instead of trying to read the system configuration, but this is not currently possible for websockets, since the websocket builder (for tokio) creates a DNS transport instance internally and doesn't allow any customization: https://github.com/libp2p/rust-libp2p/blob/4192fc3daed761a127e6986a75e016d483846555/libp2p/src/builder/phase/websocket.rs#L126-L135

Expected behavior

SwarmBuilder::with_websocket successfully configures the websocket transport on Android.

Actual behavior

SwarmBuilder::with_websocket returns an error due to an internal DNS issue where /etc/resolv.conf can't be found.

Relevant log output

No response

Possible Solution

I'm not very familiar with the library’s internals, but the most straightforward solution appears to be to allow custom configuration for the internal DNS transport so the builder doesn’t attempt to read the system config. This could be achieved by introducing a libp2p_websocket::Config structure, similar to those used by other transports (e.g., TCP), with optional DNS configuration for creating the DNS transport instance. However, the DNS configuration should probably be only included when applicable, such as when using the tokio runtime, to avoid cluttering the interface.

Version

0.54.1

Would you like to work on fixing this bug ?

Maybe

jsamol avatar Nov 11 '24 18:11 jsamol

Thank you for the report. A workaround would probably be to use Swarm::new and supply a transport that way with your option. Long term, we should probably pass a configuration option from the builder or maybe just use TokioDnsConfig::custom by default.

dariusc93 avatar Nov 11 '24 18:11 dariusc93

For new devs, here's how i solved min, i used the system resolver

 let swarm = SwarmBuilder::with_new_identity()
    .with_tokio()
    .with_other_transport(|key| {
        use libp2p::{dns::{ResolverConfig, ResolverOpts}, Transport};

        let noise_config = noise::Config::new(key).unwrap();
        let yamux_config = yamux::Config::default();

        let tcp_transport = libp2p::tcp::tokio::Transport::default();
        let tokio = libp2p::dns::tokio::Transport::custom(tcp_transport, ResolverConfig::default(), ResolverOpts::default());
        let ws_transport = libp2p::websocket::WsConfig::new(tokio).upgrade(Version::V1Lazy).authenticate(noise_config).multiplex(yamux_config).boxed();
        ws_transport
    })?
    // .with_tokio()
    // .with_websocket(
    //     libp2p::noise::Config::new,
    //     libp2p::yamux::Config::default,
    // ).await?
    
    .with_relay_client(
        libp2p::noise::Config::new,
        libp2p::yamux::Config::default,
    )?

Zedonboy avatar Aug 13 '25 09:08 Zedonboy