extend shellchat net ws to utilize /lib/tls

This commit is contained in:
nworbnhoj 2023-12-18 18:35:39 +11:00
parent bcb6a8c3bf
commit de6b6baab9
3 changed files with 39 additions and 14 deletions

View file

@ -227,8 +227,8 @@ impl Tls {
let archive = let archive =
unsafe { rkyv::archived_value::<RustlsOwnedTrustAnchor>(&bytes, pos) }; unsafe { rkyv::archived_value::<RustlsOwnedTrustAnchor>(&bytes, pos) };
let ta = archive.deserialize(&mut AllocDeserializer {}).ok(); let ta = archive.deserialize(&mut AllocDeserializer {}).ok();
log::info!("get {}", key); log::info!("get trust anchor {}", key);
log::trace!("get '{}' = '{:?}'", key, &ta); log::trace!("get trust anchor'{}' = '{:?}'", key, &ta);
ta ta
} }
Err(e) => { Err(e) => {

View file

@ -82,7 +82,7 @@ url = {version = "2.2.2", optional = true}
ring = {version = "0.16.20", optional = true} ring = {version = "0.16.20", optional = true}
# for websocket testing # for websocket testing
tungstenite = {version = "0.20.0", optional = true, features = ["rustls-tls-webpki-roots"]} tungstenite = {version = "0.20.0", optional = true}
# for performance testing # for performance testing
perflib = {path="../../tools/perflib", optional = true} perflib = {path="../../tools/perflib", optional = true}

View file

@ -2,13 +2,13 @@ use crate::{CommonEnv, ShellCmdApi};
use com::api::NET_MTU; use com::api::NET_MTU;
use net::NetPingCallback; use net::NetPingCallback;
use num_traits::*; use num_traits::*;
use std::io::Read; use std::io::{Read, Write};
use std::io::Write;
use std::net::{IpAddr, TcpListener, TcpStream}; use std::net::{IpAddr, TcpListener, TcpStream};
use std::sync::mpsc; use std::sync::mpsc;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::thread; use std::thread;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use tls::Tls;
use xous::MessageEnvelope; use xous::MessageEnvelope;
use xous_ipc::String; use xous_ipc::String;
@ -415,16 +415,41 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
#[cfg(feature = "websocket")] #[cfg(feature = "websocket")]
"ws" => { "ws" => {
if self.ws.is_none() { if self.ws.is_none() {
let (socket, response) = let server = "wss://awake.noskills.club/ws";
tungstenite::connect(url::Url::parse("wss://awake.noskills.club/ws").unwrap()).expect("Can't connect"); let url = url::Url::parse(server).expect("Can't parse");
log::info!("attempting websocket connection to {}", url.as_str());
log::info!("Connected to the server"); let host = url.host_str().expect("failed to extract host from url");
log::info!("Response HTTP code: {}", response.status()); match TcpStream::connect((host, 443)) {
log::info!("Response contains the following headers:"); Ok(sock) => {
for (ref header, _value) in response.headers() { log::info!("tcp connected to {host}");
log::info!("* {}", header); let xtls = Tls::new();
match xtls.stream_owned(host, sock) {
Ok(tls_stream) => {
log::info!("tls configured");
match tungstenite::client(url, tls_stream) {
Ok((_socket, response)) => {
log::info!("Websocket connected to: {}", server);
log::info!(
"Response HTTP code: {}",
response.status()
);
log::info!(
"Response contains the following headers:"
);
for (ref header, _value) in response.headers() {
log::info!("* {}", header);
}
}
Err(e) => {
log::info!("failed to connect websocket: {}", e)
}
}
}
Err(e) => log::warn!("failed to configure tls: {e}"),
}
}
Err(e) => log::warn!("failed to connect tcp: {e}"),
} }
self.ws = Some(socket);
} }
let mut err = false; let mut err = false;
if let Some(socket) = &mut self.ws { if let Some(socket) = &mut self.ws {