| 39 | } |
| 40 | |
| 41 | fn get_client_cert(connection: &dyn Any, data: &mut Extensions) { |
| 42 | if let Some(tls_socket) = connection.downcast_ref::<TlsStream<TcpStream>>() { |
| 43 | info!("TLS on_connect"); |
| 44 | |
| 45 | let (socket, tls_session) = tls_socket.get_ref(); |
| 46 | |
| 47 | data.insert(ConnectionInfo { |
| 48 | bind: socket.local_addr().unwrap(), |
| 49 | peer: socket.peer_addr().unwrap(), |
| 50 | ttl: socket.ttl().ok(), |
| 51 | }); |
| 52 | |
| 53 | if let Some(certs) = tls_session.peer_certificates() { |
| 54 | info!("client certificate found"); |
| 55 | |
| 56 | // Insert the peer (end-entity) certificate into request data. |
| 57 | if let Some(cert) = certs.first() { |
| 58 | data.insert(cert.clone()); |
| 59 | } |
| 60 | } |
| 61 | } else if let Some(socket) = connection.downcast_ref::<TcpStream>() { |
| 62 | info!("plaintext on_connect"); |
| 63 | |
| 64 | data.insert(ConnectionInfo { |
| 65 | bind: socket.local_addr().unwrap(), |
| 66 | peer: socket.peer_addr().unwrap(), |
| 67 | ttl: socket.ttl().ok(), |
| 68 | }); |
| 69 | } else { |
| 70 | unreachable!("socket should be TLS or plaintext"); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | #[actix_web::main] |
| 75 | async fn main() -> std::io::Result<()> { |