paritytech / jsonrpsee

Rust JSON-RPC library on top of async/await
MIT License
611 stars 161 forks source link

Error: TransportError(NoAddressFound) #255

Closed songtianyi closed 3 years ago

songtianyi commented 3 years ago

jsonrpsee new client report error on vaild wss address To reproduce the error

use jsonrpsee_types::{
    jsonrpc::{JsonValue, Params},
    traits::Client,
};
use jsonrpsee_ws_client::{WsClient, WsConfig};
const SERVER_URI: &str = "wss://pc2-rpc.darwinia.network";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let client = WsClient::new(WsConfig::with_url(SERVER_URI)).await?;
        println!("OK");
    Ok(())
}
[package]
name = "ws-rpc-test"
version = "0.1.0"
authors = ["songtianyi"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
jsonrpsee-ws-client = "0.2.0-alpha.2"
jsonrpsee-types = "0.2.0-alpha.3"
[dependencies.tokio]
package = "tokio"
version = "0.2.22"
features = ["full"]
songtianyi commented 3 years ago

upgrade to jsonrpsee-ws-client = "0.2.0-alpha.3" fixed this

niklasad1 commented 3 years ago

Ok, I'm quite surprised by this I think https://github.com/paritytech/jsonrpsee/pull/209 fixed this.

Which version did you get this on? AFAIK, all 0.2.0-alpha.x releases should work...

songtianyi commented 3 years ago

Ok, I'm quite surprised by this I think #209 fixed this.

Which version did you get this on? AFAIK, all 0.2.0-alpha.x releases should work...

jsonrpsee-ws-client = "0.2.0-alpha.2" still does

niklasad1 commented 3 years ago

I can't reproduce this, are you sure? Can you double check Cargo.lock please?

However, don't matter that much when alpha.3 works

usbalbin commented 3 years ago

I got the same error when trying to connect to a server with a self signed cert. Could this be the same issue? Any way to get it to work with self signed certs?

Running version 0.2.0-alpha.6 with RUST_LOG=debug

env_logger::init();
let client = WsClientBuilder::default().build("wss://some-domain:1234").await.unwrap();
[2021-05-05T21:29:17Z DEBUG rustls::client::hs] No cached session for DNSNameRef("some-domain")
[2021-05-05T21:29:17Z DEBUG rustls::client::hs] Not resuming any session
[2021-05-05T21:29:18Z DEBUG rustls::client::hs] Using ciphersuite TLS13_AES_256_GCM_SHA384
[2021-05-05T21:29:18Z DEBUG rustls::client::tls13] Not resuming
[2021-05-05T21:29:18Z DEBUG rustls::client::tls13] TLS1.3 encrypted extensions: []
[2021-05-05T21:29:18Z DEBUG rustls::client::hs] ALPN protocol is None
[2021-05-05T21:29:18Z WARN  rustls::session] Sending fatal alert BadCertificate
[2021-05-05T21:29:18Z DEBUG jsonrpsee_ws_client::transport] Failed to connect to sockaddr: some-ip:1234 with err: Io(Custom { kind: InvalidData, error: WebPKIError(CAUsedAsEndEntity) })
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: TransportError(NoAddressFound)', src/main.rs:26:91
niklasad1 commented 3 years ago

It's not the same issue, the issue before was that the hostname and target were confused

This is probably a bad certificate.

Are you sure that you generated the self-signed certificate correctly? Can you try to generate the certificate by the following

$ openssl req -x509 -newkey rsa:4096 -keyout server.key -out server.crt -days 365 -sha256 -nodes --subj '/CN=localhost/'

usbalbin commented 3 years ago

Ok, thanks a lot! Will give it a try as soon as I can :)

usbalbin commented 3 years ago

Hi, I just tried running

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -sha256 -nodes --subj '/CN=localhost/'

I can access the site using chrome just fine and make sure the new cert is in use. However I still get an error.

By the way, the thing I am trying to access is a my Xen Orchestra server, a web based management tool for virtual machines written in js(if this is relevant).

[2021-05-15T14:01:28Z DEBUG rustls::client::hs] No cached session for DNSNameRef("localhost")
[2021-05-15T14:01:28Z DEBUG rustls::client::hs] Not resuming any session
[2021-05-15T14:01:28Z DEBUG rustls::client::hs] Using ciphersuite TLS13_AES_256_GCM_SHA384
[2021-05-15T14:01:28Z DEBUG rustls::client::tls13] Not resuming
[2021-05-15T14:01:28Z DEBUG rustls::client::tls13] TLS1.3 encrypted extensions: []
[2021-05-15T14:01:28Z DEBUG rustls::client::hs] ALPN protocol is None
[2021-05-15T14:01:28Z WARN  rustls::session] Sending fatal alert BadCertificate
[2021-05-15T14:01:28Z DEBUG jsonrpsee_ws_client::transport] Failed to connect to sockaddr: [::1]:443 with err: Io(Custom { kind: InvalidData, error: WebPKIError(CAUsedAsEndEntity) })
[2021-05-15T14:01:28Z DEBUG rustls::client::hs] No cached session for DNSNameRef("localhost")
[2021-05-15T14:01:28Z DEBUG rustls::client::hs] Not resuming any session
[2021-05-15T14:01:28Z DEBUG rustls::client::hs] Using ciphersuite TLS13_AES_256_GCM_SHA384
[2021-05-15T14:01:28Z DEBUG rustls::client::tls13] Not resuming
[2021-05-15T14:01:28Z DEBUG rustls::client::tls13] TLS1.3 encrypted extensions: []
[2021-05-15T14:01:28Z DEBUG rustls::client::hs] ALPN protocol is None
[2021-05-15T14:01:28Z WARN  rustls::session] Sending fatal alert BadCertificate
[2021-05-15T14:01:28Z DEBUG jsonrpsee_ws_client::transport] Failed to connect to sockaddr: 127.0.0.1:443 with err: Io(Custom { kind: InvalidData, error: WebPKIError(CAUsedAsEndEntity) })
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Transport(NoAddressFound)'

Do you think there is something I am doing wrong on the server side? :)

niklasad1 commented 3 years ago

No, it turns out it that we don't support self-signed certificates.

I can open issue for, it turns out that async-tls requires us to manually to input the certificate for it to work, such as https://github.com/async-rs/async-tls/blob/master/examples/client/src/main.rs#L83-#L92.

It should be quite straight-forward to add:

  1. add another entry to WsClientBuilder such as custom_certificate: Path or something.
  2. Pass that to the WsTransportClientBuilder
  3. If a certificate_path then open that file and pass it AsyncTls::Connector.
  4. That should be it
usbalbin commented 3 years ago

Just starting experimenting down a quite similar path. I have some (not too great looking) code, I will try to improve it and open a PR.