mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-04-01 01:25:32 +00:00
move session update method to struct implementation
This commit is contained in:
parent
2db7d77f43
commit
ab9c7f4400
4 changed files with 109 additions and 73 deletions
|
|
@ -1,8 +1,16 @@
|
|||
mod error;
|
||||
pub use error::Error;
|
||||
|
||||
use super::Connection;
|
||||
use gio::{
|
||||
prelude::{TlsCertificateExt, TlsConnectionExt},
|
||||
Cancellable, TlsCertificate,
|
||||
};
|
||||
use glib::Uri;
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||
|
||||
/// Request sessions holder for `Client` object
|
||||
/// * useful to keep connections open and / or validate TLS certificate updates in runtime
|
||||
/// Request sessions holder
|
||||
/// * useful to keep connections open in async context and / or validate TLS certificate updates in runtime
|
||||
pub struct Session {
|
||||
index: RefCell<HashMap<String, Rc<Connection>>>,
|
||||
}
|
||||
|
|
@ -24,7 +32,57 @@ impl Session {
|
|||
self.index.borrow().get(request).cloned()
|
||||
}
|
||||
|
||||
pub fn update(&self, request: String, connection: Rc<Connection>) -> Option<Rc<Connection>> {
|
||||
pub fn set(&self, request: String, connection: Rc<Connection>) -> Option<Rc<Connection>> {
|
||||
self.index.borrow_mut().insert(request, connection)
|
||||
}
|
||||
|
||||
/// Update existing session for given [Uri](https://docs.gtk.org/glib/struct.Uri.html)
|
||||
/// and [TlsCertificate](https://docs.gtk.org/gio/class.TlsCertificate.html)
|
||||
/// * force rehandshake on user certificate was changed in runtime (ignore default session resumption by Glib TLS backend implementation)
|
||||
/// * close previous connection match `Uri` if not closed yet
|
||||
pub fn update(&self, uri: &Uri, certificate: Option<&TlsCertificate>) -> Result<(), Error> {
|
||||
if let Some(connection) = self.get(&uri.to_string()) {
|
||||
match connection.tls_client_connection {
|
||||
// User certificate session
|
||||
Some(ref tls_client_connection) => {
|
||||
match certificate {
|
||||
Some(new) => {
|
||||
// Get previous certificate
|
||||
if let Some(ref old) = tls_client_connection.certificate() {
|
||||
// User -> User
|
||||
if !new.is_same(old) {
|
||||
rehandshake(connection.as_ref());
|
||||
}
|
||||
}
|
||||
}
|
||||
// User -> Guest
|
||||
None => rehandshake(connection.as_ref()),
|
||||
}
|
||||
}
|
||||
// Guest
|
||||
None => {
|
||||
// Guest -> User
|
||||
if certificate.is_some() {
|
||||
rehandshake(connection.as_ref())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close connection if active yet
|
||||
if let Err(reason) = connection.close(Cancellable::NONE) {
|
||||
return Err(Error::Connection(reason));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// Tools
|
||||
|
||||
// Applies re-handshake to connection to prevent default session resumption
|
||||
// on user certificate change in runtime
|
||||
pub fn rehandshake(connection: &Connection) {
|
||||
if let Err(e) = connection.rehandshake() {
|
||||
println!("warning: {e}"); // @TODO keep in mind until solution for TLS 1.3
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue