fix guest tls connection init

This commit is contained in:
yggverse 2024-12-01 10:37:05 +02:00
parent a63e05685c
commit 3791cbc4d0
2 changed files with 12 additions and 29 deletions

View file

@ -74,10 +74,6 @@ impl Client {
certificate: Option<TlsCertificate>, certificate: Option<TlsCertificate>,
callback: impl Fn(Result<connection::Response, Error>) + 'static, callback: impl Fn(Result<connection::Response, Error>) + 'static,
) { ) {
// Toggle socket mode
// * guest sessions will not work without!
self.socket.set_tls(certificate.is_none());
// Begin new connection // Begin new connection
// * [NetworkAddress](https://docs.gtk.org/gio/class.NetworkAddress.html) required for valid // * [NetworkAddress](https://docs.gtk.org/gio/class.NetworkAddress.html) required for valid
// [SNI](https://geminiprotocol.net/docs/protocol-specification.gmi#server-name-indication) // [SNI](https://geminiprotocol.net/docs/protocol-specification.gmi#server-name-indication)

View file

@ -14,8 +14,7 @@ use glib::{
}; };
pub struct Connection { pub struct Connection {
pub socket_connection: SocketConnection, pub tls_client_connection: TlsClientConnection,
pub tls_client_connection: Option<TlsClientConnection>, // is user certificate session
} }
impl Connection { impl Connection {
@ -28,19 +27,18 @@ impl Connection {
server_identity: Option<NetworkAddress>, server_identity: Option<NetworkAddress>,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
Ok(Self { Ok(Self {
tls_client_connection: match certificate { tls_client_connection: match new_tls_client_connection(
Some(ref certificate) => { &socket_connection,
match new_tls_client_connection(&socket_connection, server_identity.as_ref()) { server_identity.as_ref(),
) {
Ok(tls_client_connection) => { Ok(tls_client_connection) => {
if let Some(ref certificate) = certificate {
tls_client_connection.set_certificate(certificate); tls_client_connection.set_certificate(certificate);
Some(tls_client_connection) }
tls_client_connection
} }
Err(e) => return Err(e), Err(e) => return Err(e),
}
}
None => None,
}, },
socket_connection,
}) })
} }
@ -75,22 +73,11 @@ impl Connection {
// Getters // Getters
/// Cast [IOStream](https://docs.gtk.org/gio/class.IOStream.html) /// Get [IOStream](https://docs.gtk.org/gio/class.IOStream.html)
/// for [SocketConnection](https://docs.gtk.org/gio/class.SocketConnection.html)
/// or [TlsClientConnection](https://docs.gtk.org/gio/iface.TlsClientConnection.html) (if available)
/// * compatible with user (certificate) and guest (certificate-less) connection type /// * compatible with user (certificate) and guest (certificate-less) connection type
/// * useful to keep `Connection` reference active in async I/O context /// * useful to keep `Connection` reference active in async I/O context
pub fn stream(&self) -> impl IsA<IOStream> { pub fn stream(&self) -> impl IsA<IOStream> {
match self.tls_client_connection.is_some() { self.tls_client_connection.clone().upcast::<IOStream>()
// is user session
true => self
.tls_client_connection
.clone()
.unwrap()
.upcast::<IOStream>(),
// is guest session
false => self.socket_connection.clone().upcast::<IOStream>(),
}
} }
} }