MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / spawn_gateway_connection

Function spawn_gateway_connection

crates/openshell-server/src/lib.rs:620–692  ·  view source on GitHub ↗
(
    stream: TcpStream,
    addr: SocketAddr,
    listen_addr: SocketAddr,
    service: MultiplexService,
    tls_acceptor: Option<TlsAcceptor>,
    enable_loopback_service_http: bool,
)

Source from the content-addressed store, hash-verified

618}
619
620fn spawn_gateway_connection(
621 stream: TcpStream,
622 addr: SocketAddr,
623 listen_addr: SocketAddr,
624 service: MultiplexService,
625 tls_acceptor: Option<TlsAcceptor>,
626 enable_loopback_service_http: bool,
627) {
628 if let Some(acceptor) = tls_acceptor {
629 tokio::spawn(async move {
630 match classify_connection_protocol(&stream).await {
631 Ok(ConnectionProtocol::PlainHttp)
632 if allow_plaintext_service_http(
633 enable_loopback_service_http,
634 listen_addr,
635 addr,
636 ) =>
637 {
638 if let Err(e) = service.serve_service_http(stream).await {
639 if is_benign_connection_close(e.as_ref()) {
640 debug!(error = %e, client = %addr, listen = %listen_addr, "Plaintext service HTTP connection closed");
641 } else {
642 error!(error = %e, client = %addr, listen = %listen_addr, "Plaintext service HTTP connection error");
643 }
644 }
645 }
646 Ok(ConnectionProtocol::PlainHttp) => {
647 warn!(client = %addr, listen = %listen_addr, "Rejected plaintext HTTP on non-loopback gateway listener");
648 }
649 Ok(ConnectionProtocol::Tls | ConnectionProtocol::Unknown) => {
650 // acceptor.acceptor() snapshots the current TLS config;
651 // the returned acceptor owns an Arc that stays alive for
652 // the full duration of the handshake.
653 match acceptor.acceptor().accept(stream).await {
654 Ok(tls_stream) => {
655 let peer_identity = multiplex::extract_peer_identity(&tls_stream);
656 if let Err(e) = service
657 .serve_with_peer_identity(tls_stream, peer_identity)
658 .await
659 {
660 if is_benign_connection_close(e.as_ref()) {
661 debug!(error = %e, client = %addr, "Connection closed");
662 } else {
663 error!(error = %e, client = %addr, "Connection error");
664 }
665 }
666 }
667 Err(e) => {
668 if is_benign_tls_handshake_failure(&e) {
669 debug!(error = %e, client = %addr, "TLS handshake closed early");
670 } else {
671 error!(error = %e, client = %addr, "TLS handshake failed");
672 }
673 }
674 }
675 }
676 Err(e) => {
677 debug!(error = %e, client = %addr, "Failed to inspect connection preface");

Callers 1

serve_gateway_listenerFunction · 0.85

Calls 10

extract_peer_identityFunction · 0.85
serve_service_httpMethod · 0.80
acceptorMethod · 0.80
serveMethod · 0.80
spawnFunction · 0.50

Tested by

no test coverage detected