forked from rustls/rustls-native-certs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle.rs
39 lines (34 loc) · 1.26 KB
/
google.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::sync::Arc;
use std::net::TcpStream;
use std::io::{Read, Write, stdout};
use rustls;
use rustls_native_certs;
use webpki;
use rustls::Session;
fn main() {
let mut config = rustls::ClientConfig::new();
config.root_store = rustls_native_certs::load_native_certs()
.expect("could not load platform certs");
let dns_name = webpki::DNSNameRef::try_from_ascii_str("google.com")
.unwrap();
let mut sess = rustls::ClientSession::new(&Arc::new(config), dns_name);
let mut sock = TcpStream::connect("google.com:443")
.expect("cannot connect");
let mut tls = rustls::Stream::new(&mut sess, &mut sock);
tls.write(concat!("GET / HTTP/1.1\r\n",
"Host: google.com\r\n",
"Connection: close\r\n",
"Accept-Encoding: identity\r\n",
"\r\n")
.as_bytes())
.expect("write failed");
let ciphersuite = tls.sess.get_negotiated_ciphersuite()
.expect("tls handshake failed");
writeln!(&mut std::io::stderr(), "Current ciphersuite: {:?}", ciphersuite.suite)
.unwrap();
let mut plaintext = Vec::new();
tls.read_to_end(&mut plaintext)
.unwrap();
stdout().write_all(&plaintext)
.unwrap();
}