sfackler / rust-native-tls

Apache License 2.0
468 stars 197 forks source link

TlsConnector throws an error: Failure(Error { code: -9836, message: "bad protocol version" }) #292

Closed khanhnt2 closed 4 months ago

khanhnt2 commented 4 months ago

I run the code in the examples folder with different domain

extern crate native_tls;

use native_tls::TlsConnector;
use std::io::{Read, Write};
use std::net::TcpStream;

fn main() {
    let connector = TlsConnector::new().unwrap();

    let stream = TcpStream::connect("token.safebrowsing.apple:443").expect("tcp connect");
    let mut stream = connector.connect("token.safebrowsing.apple", stream).expect("tls connect");

    stream.write_all(b"GET /api/v1/google.json HTTP/1.1\r\n\r\n").expect("write_all");
    let mut res = vec![];
    stream.read_to_end(&mut res).unwrap();
    println!("{}", String::from_utf8_lossy(&res));
}

Output:

$ cargo run
   Compiling native-tls-test v0.1.0 (/Users/khanhnt/Work/test/native-tls-test)
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/native-tls-test`
thread 'main' panicked at src/main.rs:11:76:
tls connect: Failure(Error { code: -9836, message: "bad protocol version" })
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Meanwhile, I can use a browser to open https://token.safebrowsing.apple/api/v1/google.json successfully

khanhnt2 commented 4 months ago

Hmm, this bug only occurs in MacOS