disable auth for now
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
use crate::client::{AppHandle, ClientEvent};
|
||||
use quinn::{crypto::rustls::QuicClientConfig, rustls::pki_types::CertificateDer};
|
||||
use crate::{
|
||||
client::{AppHandle, ClientEvent},
|
||||
net::{SERVER_NAME, no_cert::SkipServerVerification},
|
||||
};
|
||||
use quinn::{ClientConfig, Connection, Endpoint, crypto::rustls::QuicClientConfig};
|
||||
use std::{
|
||||
fs,
|
||||
io::ErrorKind,
|
||||
net::{IpAddr, Ipv6Addr, SocketAddr},
|
||||
net::{Ipv6Addr, SocketAddr, SocketAddrV6},
|
||||
str::FromStr,
|
||||
sync::Arc,
|
||||
};
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
|
||||
pub const CLIENT_SOCKET: SocketAddr =
|
||||
SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0));
|
||||
|
||||
pub fn connect(handle: AppHandle, ip: String) {
|
||||
std::thread::spawn(|| {
|
||||
connect_the(handle, ip).unwrap();
|
||||
@@ -21,39 +25,57 @@ pub enum NetCmd {
|
||||
|
||||
pub type NetSender = UnboundedSender<NetCmd>;
|
||||
|
||||
// async fn connection_cert(addr: SocketAddr) -> anyhow::Result<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| anyhow::anyhow!("failed to connect: {}", e))
|
||||
// }
|
||||
|
||||
async fn connection_no_cert(addr: SocketAddr) -> anyhow::Result<Connection> {
|
||||
let mut endpoint = Endpoint::client(CLIENT_SOCKET)?;
|
||||
|
||||
endpoint.set_default_client_config(ClientConfig::new(Arc::new(QuicClientConfig::try_from(
|
||||
quinn::rustls::ClientConfig::builder()
|
||||
.dangerous()
|
||||
.with_custom_certificate_verifier(SkipServerVerification::new())
|
||||
.with_no_client_auth(),
|
||||
)?)));
|
||||
|
||||
// connect to server
|
||||
endpoint
|
||||
.connect(addr, SERVER_NAME)
|
||||
.unwrap()
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("failed to connect: {e}"))
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn connect_the(handle: AppHandle, ip: String) -> anyhow::Result<()> {
|
||||
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 =
|
||||
quinn::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);
|
||||
let remote = SocketAddr::from_str(&ip).unwrap();
|
||||
// let remote = SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 4433);
|
||||
let host = "localhost";
|
||||
let conn = endpoint
|
||||
.connect(remote, host)?
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("failed to connect: {}", e))?;
|
||||
|
||||
let (client_send, mut client_recv) = tokio::sync::mpsc::unbounded_channel::<NetCmd>();
|
||||
|
||||
handle.send(ClientEvent::Connect(client_send));
|
||||
let addr = SocketAddr::from_str(&ip).unwrap();
|
||||
let conn = connection_no_cert(addr).await?;
|
||||
|
||||
while let Some(msg) = client_recv.recv().await {
|
||||
match msg {
|
||||
@@ -61,7 +83,7 @@ async fn connect_the(handle: AppHandle, ip: String) -> anyhow::Result<()> {
|
||||
let (mut send, recv) = conn
|
||||
.open_bi()
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("failed to open stream: {}", e))?;
|
||||
.map_err(|e| anyhow::anyhow!("failed to open stream: {e}"))?;
|
||||
|
||||
drop(recv);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user