forked from ivmarkov/edge-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_client.rs
121 lines (89 loc) · 3.62 KB
/
ws_client.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use core::net::SocketAddr;
use anyhow::bail;
use edge_http::io::client::Connection;
use edge_http::ws::{MAX_BASE64_KEY_LEN, MAX_BASE64_KEY_RESPONSE_LEN, NONCE_LEN};
use edge_nal::{AddrType, Dns, TcpConnect};
use edge_ws::{FrameHeader, FrameType};
use rand::{thread_rng, RngCore};
use log::*;
// NOTE: HTTP-only echo WS servers seem to be hard to find, this one might or might not work...
const PUBLIC_ECHO_SERVER: (&str, u16, &str) = ("websockets.chilkat.io", 80, "/wsChilkatEcho.ashx");
const OUR_ECHO_SERVER: (&str, u16, &str) = ("127.0.0.1", 8881, "/");
fn main() {
env_logger::init_from_env(
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
);
let stack = edge_nal_std::Stack::new();
let mut buf = [0_u8; 8192];
futures_lite::future::block_on(work(&stack, &mut buf)).unwrap();
}
async fn work<T: TcpConnect + Dns>(stack: &T, buf: &mut [u8]) -> Result<(), anyhow::Error>
where
<T as Dns>::Error: Send + Sync + std::error::Error + 'static,
<T as TcpConnect>::Error: Send + Sync + std::error::Error + 'static,
{
let mut args = std::env::args();
args.next(); // Skip the executable name
let (fqdn, port, path) = if args.next().is_some() {
OUR_ECHO_SERVER
} else {
PUBLIC_ECHO_SERVER
};
info!("About to open an HTTP connection to {fqdn} port {port}");
let ip = stack.get_host_by_name(fqdn, AddrType::IPv4).await?;
let mut conn: Connection<_> = Connection::new(buf, stack, SocketAddr::new(ip, port));
let mut rng_source = thread_rng();
let mut nonce = [0_u8; NONCE_LEN];
rng_source.fill_bytes(&mut nonce);
let mut buf = [0_u8; MAX_BASE64_KEY_LEN];
conn.initiate_ws_upgrade_request(Some(fqdn), Some("foo.com"), path, None, &nonce, &mut buf)
.await?;
conn.initiate_response().await?;
let mut buf = [0_u8; MAX_BASE64_KEY_RESPONSE_LEN];
if !conn.is_ws_upgrade_accepted(&nonce, &mut buf)? {
bail!("WS upgrade failed");
}
conn.complete().await?;
// Now we have the TCP socket in a state where it can be operated as a WS connection
// Send some traffic to a WS echo server and read it back
let (mut socket, buf) = conn.release();
info!("Connection upgraded to WS, starting traffic now");
for payload in ["Hello world!", "How are you?", "I'm fine, thanks!"] {
let header = FrameHeader {
frame_type: FrameType::Text(false),
payload_len: payload.as_bytes().len() as _,
mask_key: rng_source.next_u32().into(),
};
info!("Sending {header}, with payload \"{payload}\"");
header.send(&mut socket).await?;
header.send_payload(&mut socket, payload.as_bytes()).await?;
let header = FrameHeader::recv(&mut socket).await?;
let payload = header.recv_payload(&mut socket, buf).await?;
match header.frame_type {
FrameType::Text(_) => {
info!(
"Got {header}, with payload \"{}\"",
core::str::from_utf8(payload).unwrap()
);
}
FrameType::Binary(_) => {
info!("Got {header}, with payload {payload:?}");
}
_ => {
bail!("Unexpected {}", header);
}
}
if !header.frame_type.is_final() {
bail!("Unexpected fragmented frame");
}
}
// Inform the server we are closing the connection
let header = FrameHeader {
frame_type: FrameType::Close,
payload_len: 0,
mask_key: rng_source.next_u32().into(),
};
info!("Closing");
header.send(&mut socket).await?;
Ok(())
}