This repository has been archived by the owner on Mar 12, 2023. It is now read-only.
/
unionable.rs
69 lines (55 loc) · 1.87 KB
/
unionable.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
cargo run -p async-stream-packed-demo-async-std --bin unionable httpbin.org 80 false /ip
cargo run -p async-stream-packed-demo-async-std --bin unionable httpbin.org 443 true /ip
*/
use std::env;
use std::io;
use std::str;
use async_std::net::TcpStream;
use async_tls::TlsConnector;
use futures_util::io::{AsyncReadExt, AsyncWriteExt};
use async_stream_packed::UnionableAsyncStream;
#[async_std::main]
async fn main() -> io::Result<()> {
run().await
}
async fn run() -> io::Result<()> {
let domain = env::args()
.nth(1)
.unwrap_or_else(|| env::var("DOMAIN").unwrap_or_else(|_| "httpbin.org".to_owned()));
let port: u16 = env::args()
.nth(2)
.unwrap_or_else(|| env::var("PORT").unwrap_or_else(|_| "80".to_owned()))
.parse()
.unwrap();
let is_tls: bool = env::args()
.nth(3)
.unwrap_or_else(|| env::var("IS_TLS").unwrap_or_else(|_| "false".to_owned()))
.parse()
.unwrap();
let uri = env::args()
.nth(4)
.unwrap_or_else(|| env::var("URI").unwrap_or_else(|_| "/".to_owned()));
println!("{} {} {} {}", domain, port, is_tls, uri);
//
let addr = format!("{}:{}", domain, port);
let mut stream = if is_tls {
let stream = TcpStream::connect(addr).await?;
let stream = TlsConnector::default().connect(&domain, stream).await?;
UnionableAsyncStream::one(stream)
} else {
let stream = TcpStream::connect(addr).await?;
UnionableAsyncStream::the_other(stream)
};
let req_string = format!(
"GET {} HTTP/1.1\r\nHost: {}\r\nUser-Agent: curl/7.71.1\r\nAccept: */*\r\n\r\n",
uri, domain
);
println!("{}", req_string);
stream.write(&req_string.as_bytes()).await?;
let mut buf = vec![0u8; 288];
stream.read(&mut buf).await?;
println!("{:?}", str::from_utf8(&buf));
println!("done");
Ok(())
}