cert work
This commit is contained in:
@@ -6,7 +6,7 @@ use openworm::net::{
|
||||
};
|
||||
use quinn::{
|
||||
ClientConfig, Connection, Endpoint, IdleTimeout, TransportConfig,
|
||||
crypto::rustls::QuicClientConfig,
|
||||
crypto::rustls::QuicClientConfig, rustls::pki_types::CertificateDer,
|
||||
};
|
||||
use std::{
|
||||
net::{Ipv6Addr, SocketAddr, SocketAddrV6, ToSocketAddrs},
|
||||
@@ -21,6 +21,7 @@ pub const CLIENT_SOCKET: SocketAddr =
|
||||
|
||||
pub struct ConnectInfo {
|
||||
pub url: String,
|
||||
pub cert: Vec<u8>,
|
||||
}
|
||||
|
||||
pub struct NetHandle {
|
||||
@@ -90,31 +91,20 @@ impl RequestMsg for CreateAccount {
|
||||
}
|
||||
}
|
||||
|
||||
// async fn connection_cert(addr: SocketAddr) -> NetResult<Connection> {
|
||||
// let dirs = directories_next::ProjectDirs::from("", "", "openworm").unwrap();
|
||||
// let mut roots = quinn::rustls::RootCertStore::empty();
|
||||
// match fs::read(dirs.data_local_dir().join("cert.der")) {
|
||||
// Ok(cert) => {
|
||||
// roots.add(CertificateDer::from(cert))?;
|
||||
// }
|
||||
// Err(ref e) if e.kind() == ErrorKind::NotFound => {
|
||||
// eprintln!("local server certificate not found");
|
||||
// }
|
||||
// Err(e) => {
|
||||
// eprintln!("failed to open local server certificate: {}", e);
|
||||
// }
|
||||
// }
|
||||
// let client_crypto = quinn::rustls::ClientConfig::builder()
|
||||
// .with_root_certificates(roots)
|
||||
// .with_no_client_auth();
|
||||
// let client_config = ClientConfig::new(Arc::new(QuicClientConfig::try_from(client_crypto)?));
|
||||
// let mut endpoint = quinn::Endpoint::client(SocketAddr::from_str("[::]:0").unwrap())?;
|
||||
// endpoint.set_default_client_config(client_config);
|
||||
// endpoint
|
||||
// .connect(addr, SERVER_NAME)?
|
||||
// .await
|
||||
// .map_err(|e| format!("failed to connect: {}", e))
|
||||
// }
|
||||
async fn connection_cert(addr: SocketAddr, cert: CertificateDer) -> NetResult<Connection> {
|
||||
let mut roots = quinn::rustls::RootCertStore::empty();
|
||||
roots.add(cert);
|
||||
let client_crypto = quinn::rustls::ClientConfig::builder()
|
||||
.with_root_certificates(roots)
|
||||
.with_no_client_auth();
|
||||
let client_config = ClientConfig::new(Arc::new(QuicClientConfig::try_from(client_crypto)?));
|
||||
let mut endpoint = quinn::Endpoint::client(SocketAddr::from_str("[::]:0").unwrap())?;
|
||||
endpoint.set_default_client_config(client_config);
|
||||
endpoint
|
||||
.connect(addr, SERVER_NAME)?
|
||||
.await
|
||||
.map_err(|e| format!("failed to connect: {}", e))
|
||||
}
|
||||
|
||||
async fn connection_no_cert(addr: SocketAddr) -> NetResult<(Endpoint, Connection)> {
|
||||
let mut endpoint = Endpoint::client(CLIENT_SOCKET).map_err(|e| e.to_string())?;
|
||||
@@ -147,59 +137,62 @@ async fn connection_no_cert(addr: SocketAddr) -> NetResult<(Endpoint, Connection
|
||||
Ok((endpoint, con))
|
||||
}
|
||||
|
||||
pub async fn connect(msg: impl MsgHandler, info: ConnectInfo) -> Result<NetHandle, String> {
|
||||
let (send, mut ui_recv) = tokio::sync::mpsc::unbounded_channel::<NetCtrlMsg>();
|
||||
impl NetHandle {
|
||||
pub async fn connect(msg: impl MsgHandler, info: ConnectInfo) -> Result<Self, String> {
|
||||
let (send, mut ui_recv) = tokio::sync::mpsc::unbounded_channel::<NetCtrlMsg>();
|
||||
|
||||
let addr = info
|
||||
.url
|
||||
.to_socket_addrs()
|
||||
.map_err(|e| e.to_string())?
|
||||
.next()
|
||||
.ok_or("no addresses found".to_string())?;
|
||||
let (endpoint, conn) = connection_no_cert(addr).await?;
|
||||
let conn_ = conn.clone();
|
||||
let cert = CertificateDer::from_slice(&info.cert);
|
||||
let addr = info
|
||||
.url
|
||||
.to_socket_addrs()
|
||||
.map_err(|e| e.to_string())?
|
||||
.next()
|
||||
.ok_or("no addresses found".to_string())?;
|
||||
let (endpoint, conn) = connection_cert(addr).await?;
|
||||
let conn_ = conn.clone();
|
||||
|
||||
let mut req_id = RequestId::first();
|
||||
let recv = Arc::new(ServerRecv {
|
||||
msg,
|
||||
requests: DashMap::default(),
|
||||
});
|
||||
tokio::spawn(recv_uni(conn_, recv.clone()));
|
||||
tokio::spawn(async move {
|
||||
while let Some(msg) = ui_recv.recv().await {
|
||||
let request_id = req_id.next();
|
||||
match msg {
|
||||
NetCtrlMsg::Send(msg) => {
|
||||
let msg = ClientRequestMsg {
|
||||
id: request_id,
|
||||
msg: msg.into(),
|
||||
};
|
||||
if send_uni(&conn, msg).await.is_err() {
|
||||
println!("disconnected from server");
|
||||
let mut req_id = RequestId::first();
|
||||
let recv = Arc::new(ServerRecv {
|
||||
msg,
|
||||
requests: DashMap::default(),
|
||||
});
|
||||
tokio::spawn(recv_uni(conn_, recv.clone()));
|
||||
tokio::spawn(async move {
|
||||
while let Some(msg) = ui_recv.recv().await {
|
||||
let request_id = req_id.next();
|
||||
match msg {
|
||||
NetCtrlMsg::Send(msg) => {
|
||||
let msg = ClientRequestMsg {
|
||||
id: request_id,
|
||||
msg: msg.into(),
|
||||
};
|
||||
if send_uni(&conn, msg).await.is_err() {
|
||||
println!("disconnected from server");
|
||||
break;
|
||||
}
|
||||
}
|
||||
NetCtrlMsg::Request(msg, send) => {
|
||||
let msg = ClientRequestMsg {
|
||||
id: request_id,
|
||||
msg: msg.into(),
|
||||
};
|
||||
recv.requests.insert(request_id, send);
|
||||
if send_uni(&conn, msg).await.is_err() {
|
||||
println!("disconnected from server");
|
||||
break;
|
||||
}
|
||||
}
|
||||
NetCtrlMsg::Exit => {
|
||||
conn.close(0u32.into(), &[]);
|
||||
endpoint.wait_idle().await;
|
||||
break;
|
||||
}
|
||||
}
|
||||
NetCtrlMsg::Request(msg, send) => {
|
||||
let msg = ClientRequestMsg {
|
||||
id: request_id,
|
||||
msg: msg.into(),
|
||||
};
|
||||
recv.requests.insert(request_id, send);
|
||||
if send_uni(&conn, msg).await.is_err() {
|
||||
println!("disconnected from server");
|
||||
break;
|
||||
}
|
||||
}
|
||||
NetCtrlMsg::Exit => {
|
||||
conn.close(0u32.into(), &[]);
|
||||
endpoint.wait_idle().await;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Ok(NetHandle { send })
|
||||
Ok(NetHandle { send })
|
||||
}
|
||||
}
|
||||
|
||||
pub trait MsgHandler: Sync + Send + 'static {
|
||||
|
||||
Reference in New Issue
Block a user