mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-03-31 17:15:31 +00:00
add tls_client_connection, rehandshake methods
This commit is contained in:
parent
c4c173f6cf
commit
b3e9bf239c
2 changed files with 37 additions and 6 deletions
|
|
@ -10,6 +10,7 @@ use glib::object::{Cast, IsA};
|
||||||
pub struct Connection {
|
pub struct Connection {
|
||||||
pub socket_connection: SocketConnection,
|
pub socket_connection: SocketConnection,
|
||||||
pub tls_client_connection: Option<TlsClientConnection>,
|
pub tls_client_connection: Option<TlsClientConnection>,
|
||||||
|
pub server_identity: Option<NetworkAddress>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Connection {
|
impl Connection {
|
||||||
|
|
@ -26,6 +27,7 @@ impl Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
server_identity: server_identity.clone(),
|
||||||
socket_connection: socket_connection.clone(),
|
socket_connection: socket_connection.clone(),
|
||||||
tls_client_connection: match certificate {
|
tls_client_connection: match certificate {
|
||||||
Some(certificate) => {
|
Some(certificate) => {
|
||||||
|
|
@ -75,6 +77,31 @@ impl Connection {
|
||||||
None => self.socket_connection.clone().upcast::<IOStream>(),
|
None => self.socket_connection.clone().upcast::<IOStream>(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn tls_client_connection(&self) -> Result<TlsClientConnection, Error> {
|
||||||
|
match self.tls_client_connection.clone() {
|
||||||
|
// User session
|
||||||
|
Some(tls_client_connection) => Ok(tls_client_connection),
|
||||||
|
// Guest session
|
||||||
|
None => {
|
||||||
|
// Create new wrapper to interact `TlsClientConnection` API
|
||||||
|
match TlsClientConnection::new(
|
||||||
|
self.stream().as_ref(),
|
||||||
|
self.server_identity.as_ref(),
|
||||||
|
) {
|
||||||
|
Ok(tls_client_connection) => Ok(tls_client_connection),
|
||||||
|
Err(reason) => Err(Error::TlsClientConnection(reason)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rehandshake(&self) -> Result<(), Error> {
|
||||||
|
match self.tls_client_connection()?.handshake(Cancellable::NONE) {
|
||||||
|
Ok(()) => Ok(()),
|
||||||
|
Err(reason) => Err(Error::Rehandshake(reason)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tools
|
// Tools
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,24 @@ use std::fmt::{Display, Formatter, Result};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
SocketConnectionClosed,
|
Rehandshake(glib::Error),
|
||||||
SocketConnection(glib::Error),
|
SocketConnection(glib::Error),
|
||||||
|
SocketConnectionClosed,
|
||||||
TlsClientConnection(glib::Error),
|
TlsClientConnection(glib::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Error {
|
impl Display for Error {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||||
match self {
|
match self {
|
||||||
Self::SocketConnectionClosed => write!(f, "Socket connection closed"),
|
Self::Rehandshake(e) => {
|
||||||
Self::SocketConnection(reason) => {
|
write!(f, "Rehandshake error: {e}")
|
||||||
write!(f, "Socket connection error: {reason}")
|
|
||||||
}
|
}
|
||||||
Self::TlsClientConnection(reason) => {
|
Self::SocketConnectionClosed => write!(f, "Socket connection closed"),
|
||||||
write!(f, "TLS client connection error: {reason}")
|
Self::SocketConnection(e) => {
|
||||||
|
write!(f, "Socket connection error: {e}")
|
||||||
|
}
|
||||||
|
Self::TlsClientConnection(e) => {
|
||||||
|
write!(f, "TLS client connection error: {e}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue