Skip to main content

ControlHandler

Struct ControlHandler 

Source
pub struct ControlHandler { /* private fields */ }
Expand description

Control channel handler for a single tunnel connection

The ControlHandler manages the lifecycle of a tunnel connection, including:

  • WebSocket upgrade and authentication
  • Service registration and unregistration
  • Heartbeat monitoring
  • Message routing between server and client

§Example

use std::sync::Arc;
use zlayer_tunnel::{TunnelRegistry, TunnelServerConfig, ControlHandler, accept_all_tokens};

async fn handle_client(stream: tokio::net::TcpStream, addr: std::net::SocketAddr) {
    let registry = Arc::new(TunnelRegistry::default());
    let config = TunnelServerConfig::default();
    let validator = Arc::new(accept_all_tokens);

    let handler = ControlHandler::new(registry, config, validator);
    if let Err(e) = handler.handle_connection(stream, addr).await {
        tracing::error!("Connection error: {}", e);
    }
}

Implementations§

Source§

impl ControlHandler

Source

pub fn new( registry: Arc<TunnelRegistry>, config: TunnelServerConfig, token_validator: TokenValidator, ) -> Self

Create a new control handler

§Arguments
  • registry - The tunnel registry for managing tunnel state
  • config - Server configuration
  • token_validator - Function to validate authentication tokens
Source

pub fn with_dns_registrar(self, registrar: DynTunnelDnsRegistrar) -> Self

Set the DNS registrar for registering tunnel services in overlay DNS

Source

pub fn with_local_overlay_ip(self, ip: Ipv4Addr) -> Self

Set the local overlay IP for DNS registration

Source

pub async fn handle_connection( &self, stream: TcpStream, client_addr: SocketAddr, ) -> Result<()>

Handle a new WebSocket connection

This is the main entry point for each tunnel client connection. It performs the following steps:

  1. Upgrade TCP connection to WebSocket
  2. Wait for and validate AUTH message
  3. Register the tunnel in the registry
  4. Run the main message loop until disconnection
  5. Clean up the tunnel on disconnect
§Arguments
  • stream - The TCP stream to upgrade
  • client_addr - The client’s socket address
§Errors

Returns an error if:

  • WebSocket upgrade fails
  • Authentication times out or fails
  • Token is invalid or already in use
  • Connection is closed unexpectedly

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more